92e7eefb405c1df9a91dbe003a0b2fe1c29cfc69
1 #include "cache.h"
2 #include "pack.h"
3 #include "csum-file.h"
5 void reset_pack_idx_option(struct pack_idx_option *opts)
6 {
7 memset(opts, 0, sizeof(*opts));
8 opts->version = 2;
9 opts->off32_limit = 0x7fffffff;
10 }
12 static int sha1_compare(const void *_a, const void *_b)
13 {
14 struct pack_idx_entry *a = *(struct pack_idx_entry **)_a;
15 struct pack_idx_entry *b = *(struct pack_idx_entry **)_b;
16 return hashcmp(a->sha1, b->sha1);
17 }
19 static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
20 {
21 return (offset >> 31) || (opts->off32_limit < offset);
22 }
24 /*
25 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
26 * the SHA1 hash of sorted object names. The objects array passed in
27 * will be sorted by SHA1 on exit.
28 */
29 const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
30 int nr_objects, const struct pack_idx_option *opts,
31 unsigned char *sha1)
32 {
33 struct sha1file *f;
34 struct pack_idx_entry **sorted_by_sha, **list, **last;
35 off_t last_obj_offset = 0;
36 uint32_t array[256];
37 int i, fd;
38 git_SHA_CTX ctx;
39 uint32_t index_version;
41 if (nr_objects) {
42 sorted_by_sha = objects;
43 list = sorted_by_sha;
44 last = sorted_by_sha + nr_objects;
45 for (i = 0; i < nr_objects; ++i) {
46 if (objects[i]->offset > last_obj_offset)
47 last_obj_offset = objects[i]->offset;
48 }
49 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
50 sha1_compare);
51 }
52 else
53 sorted_by_sha = list = last = NULL;
55 if (opts->flags & WRITE_IDX_VERIFY) {
56 assert(index_name);
57 f = sha1fd_check(index_name);
58 } else {
59 if (!index_name) {
60 static char tmpfile[PATH_MAX];
61 fd = odb_mkstemp(tmpfile, sizeof(tmpfile), "pack/tmp_idx_XXXXXX");
62 index_name = xstrdup(tmpfile);
63 } else {
64 unlink(index_name);
65 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
66 }
67 if (fd < 0)
68 die_errno("unable to create '%s'", index_name);
69 f = sha1fd(fd, index_name);
70 }
72 /* if last object's offset is >= 2^31 we should use index V2 */
73 index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version;
75 /* index versions 2 and above need a header */
76 if (index_version >= 2) {
77 struct pack_idx_header hdr;
78 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
79 hdr.idx_version = htonl(index_version);
80 sha1write(f, &hdr, sizeof(hdr));
81 }
83 /*
84 * Write the first-level table (the list is sorted,
85 * but we use a 256-entry lookup to be able to avoid
86 * having to do eight extra binary search iterations).
87 */
88 for (i = 0; i < 256; i++) {
89 struct pack_idx_entry **next = list;
90 while (next < last) {
91 struct pack_idx_entry *obj = *next;
92 if (obj->sha1[0] != i)
93 break;
94 next++;
95 }
96 array[i] = htonl(next - sorted_by_sha);
97 list = next;
98 }
99 sha1write(f, array, 256 * 4);
101 /* compute the SHA1 hash of sorted object names. */
102 git_SHA1_Init(&ctx);
104 /*
105 * Write the actual SHA1 entries..
106 */
107 list = sorted_by_sha;
108 for (i = 0; i < nr_objects; i++) {
109 struct pack_idx_entry *obj = *list++;
110 if (index_version < 2) {
111 uint32_t offset = htonl(obj->offset);
112 sha1write(f, &offset, 4);
113 }
114 sha1write(f, obj->sha1, 20);
115 git_SHA1_Update(&ctx, obj->sha1, 20);
116 }
118 if (index_version >= 2) {
119 unsigned int nr_large_offset = 0;
121 /* write the crc32 table */
122 list = sorted_by_sha;
123 for (i = 0; i < nr_objects; i++) {
124 struct pack_idx_entry *obj = *list++;
125 uint32_t crc32_val = htonl(obj->crc32);
126 sha1write(f, &crc32_val, 4);
127 }
129 /* write the 32-bit offset table */
130 list = sorted_by_sha;
131 for (i = 0; i < nr_objects; i++) {
132 struct pack_idx_entry *obj = *list++;
133 uint32_t offset;
135 offset = (need_large_offset(obj->offset, opts)
136 ? (0x80000000 | nr_large_offset++)
137 : obj->offset);
138 offset = htonl(offset);
139 sha1write(f, &offset, 4);
140 }
142 /* write the large offset table */
143 list = sorted_by_sha;
144 while (nr_large_offset) {
145 struct pack_idx_entry *obj = *list++;
146 uint64_t offset = obj->offset;
147 uint32_t split[2];
149 if (!need_large_offset(offset, opts))
150 continue;
151 split[0] = htonl(offset >> 32);
152 split[1] = htonl(offset & 0xffffffff);
153 sha1write(f, split, 8);
154 nr_large_offset--;
155 }
156 }
158 sha1write(f, sha1, 20);
159 sha1close(f, NULL, ((opts->flags & WRITE_IDX_VERIFY)
160 ? CSUM_CLOSE : CSUM_FSYNC));
161 git_SHA1_Final(sha1, &ctx);
162 return index_name;
163 }
165 /*
166 * Update pack header with object_count and compute new SHA1 for pack data
167 * associated to pack_fd, and write that SHA1 at the end. That new SHA1
168 * is also returned in new_pack_sha1.
169 *
170 * If partial_pack_sha1 is non null, then the SHA1 of the existing pack
171 * (without the header update) is computed and validated against the
172 * one provided in partial_pack_sha1. The validation is performed at
173 * partial_pack_offset bytes in the pack file. The SHA1 of the remaining
174 * data (i.e. from partial_pack_offset to the end) is then computed and
175 * returned in partial_pack_sha1.
176 *
177 * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and
178 * partial_pack_sha1 can refer to the same buffer if the caller is not
179 * interested in the resulting SHA1 of pack data above partial_pack_offset.
180 */
181 void fixup_pack_header_footer(int pack_fd,
182 unsigned char *new_pack_sha1,
183 const char *pack_name,
184 uint32_t object_count,
185 unsigned char *partial_pack_sha1,
186 off_t partial_pack_offset)
187 {
188 int aligned_sz, buf_sz = 8 * 1024;
189 git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
190 struct pack_header hdr;
191 char *buf;
193 git_SHA1_Init(&old_sha1_ctx);
194 git_SHA1_Init(&new_sha1_ctx);
196 if (lseek(pack_fd, 0, SEEK_SET) != 0)
197 die_errno("Failed seeking to start of '%s'", pack_name);
198 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
199 die_errno("Unable to reread header of '%s'", pack_name);
200 if (lseek(pack_fd, 0, SEEK_SET) != 0)
201 die_errno("Failed seeking to start of '%s'", pack_name);
202 git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
203 hdr.hdr_entries = htonl(object_count);
204 git_SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr));
205 write_or_die(pack_fd, &hdr, sizeof(hdr));
206 partial_pack_offset -= sizeof(hdr);
208 buf = xmalloc(buf_sz);
209 aligned_sz = buf_sz - sizeof(hdr);
210 for (;;) {
211 ssize_t m, n;
212 m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ?
213 partial_pack_offset : aligned_sz;
214 n = xread(pack_fd, buf, m);
215 if (!n)
216 break;
217 if (n < 0)
218 die_errno("Failed to checksum '%s'", pack_name);
219 git_SHA1_Update(&new_sha1_ctx, buf, n);
221 aligned_sz -= n;
222 if (!aligned_sz)
223 aligned_sz = buf_sz;
225 if (!partial_pack_sha1)
226 continue;
228 git_SHA1_Update(&old_sha1_ctx, buf, n);
229 partial_pack_offset -= n;
230 if (partial_pack_offset == 0) {
231 unsigned char sha1[20];
232 git_SHA1_Final(sha1, &old_sha1_ctx);
233 if (hashcmp(sha1, partial_pack_sha1) != 0)
234 die("Unexpected checksum for %s "
235 "(disk corruption?)", pack_name);
236 /*
237 * Now let's compute the SHA1 of the remainder of the
238 * pack, which also means making partial_pack_offset
239 * big enough not to matter anymore.
240 */
241 git_SHA1_Init(&old_sha1_ctx);
242 partial_pack_offset = ~partial_pack_offset;
243 partial_pack_offset -= MSB(partial_pack_offset, 1);
244 }
245 }
246 free(buf);
248 if (partial_pack_sha1)
249 git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx);
250 git_SHA1_Final(new_pack_sha1, &new_sha1_ctx);
251 write_or_die(pack_fd, new_pack_sha1, 20);
252 fsync_or_die(pack_fd, pack_name);
253 }
255 char *index_pack_lockfile(int ip_out)
256 {
257 char packname[46];
259 /*
260 * The first thing we expect from index-pack's output
261 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
262 * %40s is the newly created pack SHA1 name. In the "keep"
263 * case, we need it to remove the corresponding .keep file
264 * later on. If we don't get that then tough luck with it.
265 */
266 if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' &&
267 memcmp(packname, "keep\t", 5) == 0) {
268 char path[PATH_MAX];
269 packname[45] = 0;
270 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
271 get_object_directory(), packname + 5);
272 return xstrdup(path);
273 }
274 return NULL;
275 }
277 /*
278 * The per-object header is a pretty dense thing, which is
279 * - first byte: low four bits are "size", then three bits of "type",
280 * and the high bit is "size continues".
281 * - each byte afterwards: low seven bits are size continuation,
282 * with the high bit being "size continues"
283 */
284 int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
285 {
286 int n = 1;
287 unsigned char c;
289 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
290 die("bad type %d", type);
292 c = (type << 4) | (size & 15);
293 size >>= 4;
294 while (size) {
295 *hdr++ = c | 0x80;
296 c = size & 0x7f;
297 size >>= 7;
298 n++;
299 }
300 *hdr = c;
301 return n;
302 }