pack.h 4.33 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9 10
 */

#ifndef INCLUDE_pack_h__
#define INCLUDE_pack_h__

11 12
#include "common.h"

13 14
#include <zlib.h>

15 16 17 18
#include "git2/oid.h"

#include "map.h"
#include "mwindow.h"
19
#include "odb.h"
20
#include "oidmap.h"
21
#include "array.h"
22

23 24
#define GIT_PACK_FILE_MODE 0444

25 26 27
#define PACK_SIGNATURE 0x5041434b	/* "PACK" */
#define PACK_VERSION 2
#define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3))
28
struct git_pack_header {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	uint32_t hdr_signature;
	uint32_t hdr_version;
	uint32_t hdr_entries;
};

/*
 * The first four bytes of index formats later than version 1 should
 * start with this signature, as all older git binaries would find this
 * value illegal and abort reading the file.
 *
 * This is the case because the number of objects in a packfile
 * cannot exceed 1,431,660,000 as every object would need at least
 * 3 bytes of data and the overall packfile cannot exceed 4 GiB with
 * version 1 of the index file due to the offsets limited to 32 bits.
 * Clearly the signature exceeds this maximum.
 *
 * Very old git binaries will also compare the first 4 bytes to the
 * next 4 bytes in the index and abort with a "non-monotonic index"
 * error if the second 4 byte word is smaller than the first 4
Vicent Marti committed
48
 * byte word. This would be true in the proposed future index
49 50 51 52 53
 * format as idx_signature would be greater than idx_version.
 */

#define PACK_IDX_SIGNATURE 0xff744f63	/* "\377tOc" */

54
struct git_pack_idx_header {
55 56 57 58
	uint32_t idx_signature;
	uint32_t idx_version;
};

59
typedef struct git_pack_cache_entry {
60
	size_t last_usage; /* enough? */
61
	git_atomic refcount;
62 63 64
	git_rawobj raw;
} git_pack_cache_entry;

65
struct pack_chain_elem {
66
	git_off_t base_key;
67 68 69 70 71 72 73
	git_off_t offset;
	size_t size;
	git_otype type;
};

typedef git_array_t(struct pack_chain_elem) git_dependency_chain;

74
#include "offmap.h"
75
#include "oidmap.h"
76

77 78
#define GIT_PACK_CACHE_MEMORY_LIMIT 16 * 1024 * 1024
#define GIT_PACK_CACHE_SIZE_LIMIT 1024 * 1024 /* don't bother caching anything over 1MB */
79 80 81 82

typedef struct {
	size_t memory_used;
	size_t memory_limit;
83
	size_t use_ctr;
84 85 86 87
	git_mutex lock;
	git_offmap *entries;
} git_pack_cache;

88
struct git_pack_file {
89 90
	git_mwindow_file mwf;
	git_map index_map;
Russell Belfer committed
91
	git_mutex lock; /* protect updates to mwf and index_map */
92
	git_atomic refcount;
93 94 95 96 97 98 99

	uint32_t num_objects;
	uint32_t num_bad_objects;
	git_oid *bad_object_sha1; /* array of git_oid */

	int index_version;
	git_time_t mtime;
100
	unsigned pack_local:1, pack_keep:1, has_cache:1;
101
	git_oidmap *idx_cache;
102
	git_oid **oids;
103

104
	git_pack_cache bases; /* delta base cache */
105

106 107
	time_t last_freshen; /* last time the packfile was freshened */

108 109 110 111
	/* something like ".git/objects/pack/xxxxx.pack" */
	char pack_name[GIT_FLEX_ARRAY]; /* more */
};

112
struct git_pack_entry {
113
	git_off_t offset;
114
	git_oid sha1;
115
	struct git_pack_file *p;
116 117
};

118 119 120 121 122 123 124 125
typedef struct git_packfile_stream {
	git_off_t curpos;
	int done;
	z_stream zstream;
	struct git_pack_file *p;
	git_mwindow *mw;
} git_packfile_stream;

126
size_t git_packfile__object_header(unsigned char *hdr, size_t size, git_otype type);
127

128 129
int git_packfile__name(char **out, const char *path);

130 131 132 133 134
int git_packfile_unpack_header(
		size_t *size_p,
		git_otype *type_p,
		git_mwindow_file *mwf,
		git_mwindow **w_curs,
135
		git_off_t *curpos);
136

137 138 139 140 141 142
int git_packfile_resolve_header(
		size_t *size_p,
		git_otype *type_p,
		struct git_pack_file *p,
		git_off_t offset);

143
int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, git_off_t *obj_offset);
144

145 146
int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, git_off_t curpos);
ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len);
147
void git_packfile_stream_dispose(git_packfile_stream *obj);
148

149 150 151
git_off_t get_delta_base(struct git_pack_file *p, git_mwindow **w_curs,
		git_off_t *curpos, git_otype type,
		git_off_t delta_obj_offset);
152

153
void git_packfile_close(struct git_pack_file *p, bool unlink_packfile);
154
void git_packfile_free(struct git_pack_file *p);
155 156
int git_packfile_alloc(struct git_pack_file **pack_out, const char *path);

157 158 159 160
int git_pack_entry_find(
		struct git_pack_entry *e,
		struct git_pack_file *p,
		const git_oid *short_oid,
161
		size_t len);
162 163
int git_pack_foreach_entry(
		struct git_pack_file *p,
164
		git_odb_foreach_cb cb,
165
		void *data);
166

167
#endif