pack.h 5.52 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 "git2/oid.h"

15
#include "array.h"
16 17
#include "map.h"
#include "mwindow.h"
18
#include "odb.h"
19
#include "offmap.h"
20
#include "oidmap.h"
21
#include "zstream.h"
22
#include "oid.h"
23

24 25 26
/**
 * Function type for callbacks from git_pack_foreach_entry_offset.
 */
lhchavez committed
27
typedef int git_pack_foreach_entry_offset_cb(
28 29 30 31
		const git_oid *id,
		off64_t offset,
		void *payload);

32 33
#define GIT_PACK_FILE_MODE 0444

34 35
#define PACK_SIGNATURE 0x5041434b	/* "PACK" */
#define PACK_VERSION 2
36
#define pack_version_ok(v) ((v) == htonl(2))
37
struct git_pack_header {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	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
57
 * byte word. This would be true in the proposed future index
58 59 60 61 62
 * format as idx_signature would be greater than idx_version.
 */

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

63
struct git_pack_idx_header {
64 65 66 67
	uint32_t idx_signature;
	uint32_t idx_version;
};

68
typedef struct git_pack_cache_entry {
69
	size_t last_usage; /* enough? */
70
	git_atomic32 refcount;
71 72 73
	git_rawobj raw;
} git_pack_cache_entry;

74
struct pack_chain_elem {
75 76
	off64_t base_key;
	off64_t offset;
77
	size_t size;
78
	git_object_t type;
79 80 81 82
};

typedef git_array_t(struct pack_chain_elem) git_dependency_chain;

83 84
#define GIT_PACK_CACHE_MEMORY_LIMIT 16 * 1024 * 1024
#define GIT_PACK_CACHE_SIZE_LIMIT 1024 * 1024 /* don't bother caching anything over 1MB */
85 86 87 88

typedef struct {
	size_t memory_used;
	size_t memory_limit;
89
	size_t use_ctr;
90 91 92 93
	git_mutex lock;
	git_offmap *entries;
} git_pack_cache;

94
struct git_pack_file {
95 96
	git_mwindow_file mwf;
	git_map index_map;
97
	git_mutex lock; /* protect updates to index_map */
98
	git_atomic32 refcount;
99 100 101

	uint32_t num_objects;
	uint32_t num_bad_objects;
102 103 104 105 106 107 108 109
	git_oid *bad_object_ids; /* array of git_oid */

	git_oid_t oid_type;
	unsigned oid_hexsize:7,
	         oid_size:6,
	         pack_local:1,
	         pack_keep:1,
		 has_cache:1;
110 111 112

	int index_version;
	git_time_t mtime;
113
	git_oidmap *idx_cache;
114
	unsigned char **ids;
115

116
	git_pack_cache bases; /* delta base cache */
117

118 119
	time_t last_freshen; /* last time the packfile was freshened */

120 121 122 123
	/* something like ".git/objects/pack/xxxxx.pack" */
	char pack_name[GIT_FLEX_ARRAY]; /* more */
};

124
/**
125 126 127
 * Return the position where an OID (or a prefix) would be inserted within
 * the OID Lookup Table of an .idx file. This performs binary search
 * between the lo and hi indices.
128
 *
129 130 131 132
 * The stride parameter is provided because .idx files version 1 store the
 * OIDs interleaved with the 4-byte file offsets of the objects within the
 * .pack file (stride = oid_size + 4), whereas files with version 2 store
 * them in a contiguous flat array (stride = oid_size).
133
 */
134 135 136 137 138 139 140
int git_pack__lookup_id(
	const void *id_lookup_table,
	size_t stride,
	unsigned lo,
	unsigned hi,
	const unsigned char *id_prefix,
	const git_oid_t oid_type);
141

142
struct git_pack_entry {
143
	off64_t offset;
144
	git_oid id;
145
	struct git_pack_file *p;
146 147
};

148
typedef struct git_packfile_stream {
149
	off64_t curpos;
150
	int done;
151
	git_zstream zstream;
152 153 154 155
	struct git_pack_file *p;
	git_mwindow *mw;
} git_packfile_stream;

Edward Thomson committed
156
int git_packfile__object_header(size_t *out, unsigned char *hdr, size_t size, git_object_t type);
157

158 159
int git_packfile__name(char **out, const char *path);

160 161
int git_packfile_unpack_header(
		size_t *size_p,
162
		git_object_t *type_p,
163
		struct git_pack_file *p,
164
		git_mwindow **w_curs,
165
		off64_t *curpos);
166

167 168
int git_packfile_resolve_header(
		size_t *size_p,
169
		git_object_t *type_p,
170
		struct git_pack_file *p,
171
		off64_t offset);
172

173
int git_packfile_unpack(git_rawobj *obj, struct git_pack_file *p, off64_t *obj_offset);
174

175
int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p, off64_t curpos);
176
ssize_t git_packfile_stream_read(git_packfile_stream *obj, void *buffer, size_t len);
177
void git_packfile_stream_dispose(git_packfile_stream *obj);
178

179 180 181 182 183 184
int get_delta_base(
		off64_t *delta_base_out,
		struct git_pack_file *p,
		git_mwindow **w_curs,
		off64_t *curpos,
		git_object_t type,
185
		off64_t delta_obj_offset);
186

187
void git_packfile_free(struct git_pack_file *p, bool unlink_packfile);
188 189 190 191
int git_packfile_alloc(
	struct git_pack_file **pack_out,
	const char *path,
	git_oid_t oid_type);
192

193 194 195
int git_pack_entry_find(
		struct git_pack_entry *e,
		struct git_pack_file *p,
196
		const git_oid *short_id,
197
		size_t len);
198 199
int git_pack_foreach_entry(
		struct git_pack_file *p,
200
		git_odb_foreach_cb cb,
201
		void *data);
202
/**
lhchavez committed
203 204 205 206 207
 * Similar to git_pack_foreach_entry, but:
 * - It also provides the offset of the object within the
 *   packfile.
 * - It does not sort the objects in any order.
 * - It retains the lock while invoking the callback.
208 209 210 211 212
 */
int git_pack_foreach_entry_offset(
		struct git_pack_file *p,
		git_pack_foreach_entry_offset_cb cb,
		void *data);
213

214
#endif