]> git.ipfire.org Git - thirdparty/git.git/blob - pack-write.c
treewide: be explicit about dependence on trace.h & trace2.h
[thirdparty/git.git] / pack-write.c
1 #include "cache.h"
2 #include "environment.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "pack.h"
6 #include "csum-file.h"
7 #include "remote.h"
8 #include "chunk-format.h"
9 #include "pack-mtimes.h"
10 #include "oidmap.h"
11 #include "pack-objects.h"
12
13 void reset_pack_idx_option(struct pack_idx_option *opts)
14 {
15 memset(opts, 0, sizeof(*opts));
16 opts->version = 2;
17 opts->off32_limit = 0x7fffffff;
18 }
19
20 static int sha1_compare(const void *_a, const void *_b)
21 {
22 struct pack_idx_entry *a = *(struct pack_idx_entry **)_a;
23 struct pack_idx_entry *b = *(struct pack_idx_entry **)_b;
24 return oidcmp(&a->oid, &b->oid);
25 }
26
27 static int cmp_uint32(const void *a_, const void *b_)
28 {
29 uint32_t a = *((uint32_t *)a_);
30 uint32_t b = *((uint32_t *)b_);
31
32 return (a < b) ? -1 : (a != b);
33 }
34
35 static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
36 {
37 uint32_t ofsval;
38
39 if ((offset >> 31) || (opts->off32_limit < offset))
40 return 1;
41 if (!opts->anomaly_nr)
42 return 0;
43 ofsval = offset;
44 return !!bsearch(&ofsval, opts->anomaly, opts->anomaly_nr,
45 sizeof(ofsval), cmp_uint32);
46 }
47
48 /*
49 * The *sha1 contains the pack content SHA1 hash.
50 * The objects array passed in will be sorted by SHA1 on exit.
51 */
52 const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
53 int nr_objects, const struct pack_idx_option *opts,
54 const unsigned char *sha1)
55 {
56 struct hashfile *f;
57 struct pack_idx_entry **sorted_by_sha, **list, **last;
58 off_t last_obj_offset = 0;
59 int i, fd;
60 uint32_t index_version;
61
62 if (nr_objects) {
63 sorted_by_sha = objects;
64 list = sorted_by_sha;
65 last = sorted_by_sha + nr_objects;
66 for (i = 0; i < nr_objects; ++i) {
67 if (objects[i]->offset > last_obj_offset)
68 last_obj_offset = objects[i]->offset;
69 }
70 QSORT(sorted_by_sha, nr_objects, sha1_compare);
71 }
72 else
73 sorted_by_sha = list = last = NULL;
74
75 if (opts->flags & WRITE_IDX_VERIFY) {
76 assert(index_name);
77 f = hashfd_check(index_name);
78 } else {
79 if (!index_name) {
80 struct strbuf tmp_file = STRBUF_INIT;
81 fd = odb_mkstemp(&tmp_file, "pack/tmp_idx_XXXXXX");
82 index_name = strbuf_detach(&tmp_file, NULL);
83 } else {
84 unlink(index_name);
85 fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
86 }
87 f = hashfd(fd, index_name);
88 }
89
90 /* if last object's offset is >= 2^31 we should use index V2 */
91 index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version;
92
93 /* index versions 2 and above need a header */
94 if (index_version >= 2) {
95 struct pack_idx_header hdr;
96 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
97 hdr.idx_version = htonl(index_version);
98 hashwrite(f, &hdr, sizeof(hdr));
99 }
100
101 /*
102 * Write the first-level table (the list is sorted,
103 * but we use a 256-entry lookup to be able to avoid
104 * having to do eight extra binary search iterations).
105 */
106 for (i = 0; i < 256; i++) {
107 struct pack_idx_entry **next = list;
108 while (next < last) {
109 struct pack_idx_entry *obj = *next;
110 if (obj->oid.hash[0] != i)
111 break;
112 next++;
113 }
114 hashwrite_be32(f, next - sorted_by_sha);
115 list = next;
116 }
117
118 /*
119 * Write the actual SHA1 entries..
120 */
121 list = sorted_by_sha;
122 for (i = 0; i < nr_objects; i++) {
123 struct pack_idx_entry *obj = *list++;
124 if (index_version < 2)
125 hashwrite_be32(f, obj->offset);
126 hashwrite(f, obj->oid.hash, the_hash_algo->rawsz);
127 if ((opts->flags & WRITE_IDX_STRICT) &&
128 (i && oideq(&list[-2]->oid, &obj->oid)))
129 die("The same object %s appears twice in the pack",
130 oid_to_hex(&obj->oid));
131 }
132
133 if (index_version >= 2) {
134 unsigned int nr_large_offset = 0;
135
136 /* write the crc32 table */
137 list = sorted_by_sha;
138 for (i = 0; i < nr_objects; i++) {
139 struct pack_idx_entry *obj = *list++;
140 hashwrite_be32(f, obj->crc32);
141 }
142
143 /* write the 32-bit offset table */
144 list = sorted_by_sha;
145 for (i = 0; i < nr_objects; i++) {
146 struct pack_idx_entry *obj = *list++;
147 uint32_t offset;
148
149 offset = (need_large_offset(obj->offset, opts)
150 ? (0x80000000 | nr_large_offset++)
151 : obj->offset);
152 hashwrite_be32(f, offset);
153 }
154
155 /* write the large offset table */
156 list = sorted_by_sha;
157 while (nr_large_offset) {
158 struct pack_idx_entry *obj = *list++;
159 uint64_t offset = obj->offset;
160
161 if (!need_large_offset(offset, opts))
162 continue;
163 hashwrite_be64(f, offset);
164 nr_large_offset--;
165 }
166 }
167
168 hashwrite(f, sha1, the_hash_algo->rawsz);
169 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
170 CSUM_HASH_IN_STREAM | CSUM_CLOSE |
171 ((opts->flags & WRITE_IDX_VERIFY) ? 0 : CSUM_FSYNC));
172 return index_name;
173 }
174
175 static int pack_order_cmp(const void *va, const void *vb, void *ctx)
176 {
177 struct pack_idx_entry **objects = ctx;
178
179 off_t oa = objects[*(uint32_t*)va]->offset;
180 off_t ob = objects[*(uint32_t*)vb]->offset;
181
182 if (oa < ob)
183 return -1;
184 if (oa > ob)
185 return 1;
186 return 0;
187 }
188
189 static void write_rev_header(struct hashfile *f)
190 {
191 hashwrite_be32(f, RIDX_SIGNATURE);
192 hashwrite_be32(f, RIDX_VERSION);
193 hashwrite_be32(f, oid_version(the_hash_algo));
194 }
195
196 static void write_rev_index_positions(struct hashfile *f,
197 uint32_t *pack_order,
198 uint32_t nr_objects)
199 {
200 uint32_t i;
201 for (i = 0; i < nr_objects; i++)
202 hashwrite_be32(f, pack_order[i]);
203 }
204
205 static void write_rev_trailer(struct hashfile *f, const unsigned char *hash)
206 {
207 hashwrite(f, hash, the_hash_algo->rawsz);
208 }
209
210 const char *write_rev_file(const char *rev_name,
211 struct pack_idx_entry **objects,
212 uint32_t nr_objects,
213 const unsigned char *hash,
214 unsigned flags)
215 {
216 uint32_t *pack_order;
217 uint32_t i;
218 const char *ret;
219
220 if (!(flags & WRITE_REV) && !(flags & WRITE_REV_VERIFY))
221 return NULL;
222
223 ALLOC_ARRAY(pack_order, nr_objects);
224 for (i = 0; i < nr_objects; i++)
225 pack_order[i] = i;
226 QSORT_S(pack_order, nr_objects, pack_order_cmp, objects);
227
228 ret = write_rev_file_order(rev_name, pack_order, nr_objects, hash,
229 flags);
230
231 free(pack_order);
232
233 return ret;
234 }
235
236 const char *write_rev_file_order(const char *rev_name,
237 uint32_t *pack_order,
238 uint32_t nr_objects,
239 const unsigned char *hash,
240 unsigned flags)
241 {
242 struct hashfile *f;
243 int fd;
244
245 if ((flags & WRITE_REV) && (flags & WRITE_REV_VERIFY))
246 die(_("cannot both write and verify reverse index"));
247
248 if (flags & WRITE_REV) {
249 if (!rev_name) {
250 struct strbuf tmp_file = STRBUF_INIT;
251 fd = odb_mkstemp(&tmp_file, "pack/tmp_rev_XXXXXX");
252 rev_name = strbuf_detach(&tmp_file, NULL);
253 } else {
254 unlink(rev_name);
255 fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
256 }
257 f = hashfd(fd, rev_name);
258 } else if (flags & WRITE_REV_VERIFY) {
259 struct stat statbuf;
260 if (stat(rev_name, &statbuf)) {
261 if (errno == ENOENT) {
262 /* .rev files are optional */
263 return NULL;
264 } else
265 die_errno(_("could not stat: %s"), rev_name);
266 }
267 f = hashfd_check(rev_name);
268 } else
269 return NULL;
270
271 write_rev_header(f);
272
273 write_rev_index_positions(f, pack_order, nr_objects);
274 write_rev_trailer(f, hash);
275
276 if (rev_name && adjust_shared_perm(rev_name) < 0)
277 die(_("failed to make %s readable"), rev_name);
278
279 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
280 CSUM_HASH_IN_STREAM | CSUM_CLOSE |
281 ((flags & WRITE_IDX_VERIFY) ? 0 : CSUM_FSYNC));
282
283 return rev_name;
284 }
285
286 static void write_mtimes_header(struct hashfile *f)
287 {
288 hashwrite_be32(f, MTIMES_SIGNATURE);
289 hashwrite_be32(f, MTIMES_VERSION);
290 hashwrite_be32(f, oid_version(the_hash_algo));
291 }
292
293 /*
294 * Writes the object mtimes of "objects" for use in a .mtimes file.
295 * Note that objects must be in lexicographic (index) order, which is
296 * the expected ordering of these values in the .mtimes file.
297 */
298 static void write_mtimes_objects(struct hashfile *f,
299 struct packing_data *to_pack,
300 struct pack_idx_entry **objects,
301 uint32_t nr_objects)
302 {
303 uint32_t i;
304 for (i = 0; i < nr_objects; i++) {
305 struct object_entry *e = (struct object_entry*)objects[i];
306 hashwrite_be32(f, oe_cruft_mtime(to_pack, e));
307 }
308 }
309
310 static void write_mtimes_trailer(struct hashfile *f, const unsigned char *hash)
311 {
312 hashwrite(f, hash, the_hash_algo->rawsz);
313 }
314
315 static const char *write_mtimes_file(struct packing_data *to_pack,
316 struct pack_idx_entry **objects,
317 uint32_t nr_objects,
318 const unsigned char *hash)
319 {
320 struct strbuf tmp_file = STRBUF_INIT;
321 const char *mtimes_name;
322 struct hashfile *f;
323 int fd;
324
325 if (!to_pack)
326 BUG("cannot call write_mtimes_file with NULL packing_data");
327
328 fd = odb_mkstemp(&tmp_file, "pack/tmp_mtimes_XXXXXX");
329 mtimes_name = strbuf_detach(&tmp_file, NULL);
330 f = hashfd(fd, mtimes_name);
331
332 write_mtimes_header(f);
333 write_mtimes_objects(f, to_pack, objects, nr_objects);
334 write_mtimes_trailer(f, hash);
335
336 if (adjust_shared_perm(mtimes_name) < 0)
337 die(_("failed to make %s readable"), mtimes_name);
338
339 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
340 CSUM_HASH_IN_STREAM | CSUM_CLOSE | CSUM_FSYNC);
341
342 return mtimes_name;
343 }
344
345 off_t write_pack_header(struct hashfile *f, uint32_t nr_entries)
346 {
347 struct pack_header hdr;
348
349 hdr.hdr_signature = htonl(PACK_SIGNATURE);
350 hdr.hdr_version = htonl(PACK_VERSION);
351 hdr.hdr_entries = htonl(nr_entries);
352 hashwrite(f, &hdr, sizeof(hdr));
353 return sizeof(hdr);
354 }
355
356 /*
357 * Update pack header with object_count and compute new SHA1 for pack data
358 * associated to pack_fd, and write that SHA1 at the end. That new SHA1
359 * is also returned in new_pack_sha1.
360 *
361 * If partial_pack_sha1 is non null, then the SHA1 of the existing pack
362 * (without the header update) is computed and validated against the
363 * one provided in partial_pack_sha1. The validation is performed at
364 * partial_pack_offset bytes in the pack file. The SHA1 of the remaining
365 * data (i.e. from partial_pack_offset to the end) is then computed and
366 * returned in partial_pack_sha1.
367 *
368 * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and
369 * partial_pack_sha1 can refer to the same buffer if the caller is not
370 * interested in the resulting SHA1 of pack data above partial_pack_offset.
371 */
372 void fixup_pack_header_footer(int pack_fd,
373 unsigned char *new_pack_hash,
374 const char *pack_name,
375 uint32_t object_count,
376 unsigned char *partial_pack_hash,
377 off_t partial_pack_offset)
378 {
379 int aligned_sz, buf_sz = 8 * 1024;
380 git_hash_ctx old_hash_ctx, new_hash_ctx;
381 struct pack_header hdr;
382 char *buf;
383 ssize_t read_result;
384
385 the_hash_algo->init_fn(&old_hash_ctx);
386 the_hash_algo->init_fn(&new_hash_ctx);
387
388 if (lseek(pack_fd, 0, SEEK_SET) != 0)
389 die_errno("Failed seeking to start of '%s'", pack_name);
390 read_result = read_in_full(pack_fd, &hdr, sizeof(hdr));
391 if (read_result < 0)
392 die_errno("Unable to reread header of '%s'", pack_name);
393 else if (read_result != sizeof(hdr))
394 die_errno("Unexpected short read for header of '%s'",
395 pack_name);
396 if (lseek(pack_fd, 0, SEEK_SET) != 0)
397 die_errno("Failed seeking to start of '%s'", pack_name);
398 the_hash_algo->update_fn(&old_hash_ctx, &hdr, sizeof(hdr));
399 hdr.hdr_entries = htonl(object_count);
400 the_hash_algo->update_fn(&new_hash_ctx, &hdr, sizeof(hdr));
401 write_or_die(pack_fd, &hdr, sizeof(hdr));
402 partial_pack_offset -= sizeof(hdr);
403
404 buf = xmalloc(buf_sz);
405 aligned_sz = buf_sz - sizeof(hdr);
406 for (;;) {
407 ssize_t m, n;
408 m = (partial_pack_hash && partial_pack_offset < aligned_sz) ?
409 partial_pack_offset : aligned_sz;
410 n = xread(pack_fd, buf, m);
411 if (!n)
412 break;
413 if (n < 0)
414 die_errno("Failed to checksum '%s'", pack_name);
415 the_hash_algo->update_fn(&new_hash_ctx, buf, n);
416
417 aligned_sz -= n;
418 if (!aligned_sz)
419 aligned_sz = buf_sz;
420
421 if (!partial_pack_hash)
422 continue;
423
424 the_hash_algo->update_fn(&old_hash_ctx, buf, n);
425 partial_pack_offset -= n;
426 if (partial_pack_offset == 0) {
427 unsigned char hash[GIT_MAX_RAWSZ];
428 the_hash_algo->final_fn(hash, &old_hash_ctx);
429 if (!hasheq(hash, partial_pack_hash))
430 die("Unexpected checksum for %s "
431 "(disk corruption?)", pack_name);
432 /*
433 * Now let's compute the SHA1 of the remainder of the
434 * pack, which also means making partial_pack_offset
435 * big enough not to matter anymore.
436 */
437 the_hash_algo->init_fn(&old_hash_ctx);
438 partial_pack_offset = ~partial_pack_offset;
439 partial_pack_offset -= MSB(partial_pack_offset, 1);
440 }
441 }
442 free(buf);
443
444 if (partial_pack_hash)
445 the_hash_algo->final_fn(partial_pack_hash, &old_hash_ctx);
446 the_hash_algo->final_fn(new_pack_hash, &new_hash_ctx);
447 write_or_die(pack_fd, new_pack_hash, the_hash_algo->rawsz);
448 fsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name);
449 }
450
451 char *index_pack_lockfile(int ip_out, int *is_well_formed)
452 {
453 char packname[GIT_MAX_HEXSZ + 6];
454 const int len = the_hash_algo->hexsz + 6;
455
456 /*
457 * The first thing we expect from index-pack's output
458 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
459 * %40s is the newly created pack SHA1 name. In the "keep"
460 * case, we need it to remove the corresponding .keep file
461 * later on. If we don't get that then tough luck with it.
462 */
463 if (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\n') {
464 const char *name;
465
466 if (is_well_formed)
467 *is_well_formed = 1;
468 packname[len-1] = 0;
469 if (skip_prefix(packname, "keep\t", &name))
470 return xstrfmt("%s/pack/pack-%s.keep",
471 get_object_directory(), name);
472 return NULL;
473 }
474 if (is_well_formed)
475 *is_well_formed = 0;
476 return NULL;
477 }
478
479 /*
480 * The per-object header is a pretty dense thing, which is
481 * - first byte: low four bits are "size", then three bits of "type",
482 * and the high bit is "size continues".
483 * - each byte afterwards: low seven bits are size continuation,
484 * with the high bit being "size continues"
485 */
486 int encode_in_pack_object_header(unsigned char *hdr, int hdr_len,
487 enum object_type type, uintmax_t size)
488 {
489 int n = 1;
490 unsigned char c;
491
492 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
493 die("bad type %d", type);
494
495 c = (type << 4) | (size & 15);
496 size >>= 4;
497 while (size) {
498 if (n == hdr_len)
499 die("object size is too enormous to format");
500 *hdr++ = c | 0x80;
501 c = size & 0x7f;
502 size >>= 7;
503 n++;
504 }
505 *hdr = c;
506 return n;
507 }
508
509 struct hashfile *create_tmp_packfile(char **pack_tmp_name)
510 {
511 struct strbuf tmpname = STRBUF_INIT;
512 int fd;
513
514 fd = odb_mkstemp(&tmpname, "pack/tmp_pack_XXXXXX");
515 *pack_tmp_name = strbuf_detach(&tmpname, NULL);
516 return hashfd(fd, *pack_tmp_name);
517 }
518
519 static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source,
520 const char *ext)
521 {
522 size_t name_prefix_len = name_prefix->len;
523
524 strbuf_addstr(name_prefix, ext);
525 if (rename(source, name_prefix->buf))
526 die_errno("unable to rename temporary file to '%s'",
527 name_prefix->buf);
528 strbuf_setlen(name_prefix, name_prefix_len);
529 }
530
531 void rename_tmp_packfile_idx(struct strbuf *name_buffer,
532 char **idx_tmp_name)
533 {
534 rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx");
535 }
536
537 void stage_tmp_packfiles(struct strbuf *name_buffer,
538 const char *pack_tmp_name,
539 struct pack_idx_entry **written_list,
540 uint32_t nr_written,
541 struct packing_data *to_pack,
542 struct pack_idx_option *pack_idx_opts,
543 unsigned char hash[],
544 char **idx_tmp_name)
545 {
546 const char *rev_tmp_name = NULL;
547 const char *mtimes_tmp_name = NULL;
548
549 if (adjust_shared_perm(pack_tmp_name))
550 die_errno("unable to make temporary pack file readable");
551
552 *idx_tmp_name = (char *)write_idx_file(NULL, written_list, nr_written,
553 pack_idx_opts, hash);
554 if (adjust_shared_perm(*idx_tmp_name))
555 die_errno("unable to make temporary index file readable");
556
557 rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash,
558 pack_idx_opts->flags);
559
560 if (pack_idx_opts->flags & WRITE_MTIMES) {
561 mtimes_tmp_name = write_mtimes_file(to_pack, written_list,
562 nr_written,
563 hash);
564 }
565
566 rename_tmp_packfile(name_buffer, pack_tmp_name, "pack");
567 if (rev_tmp_name)
568 rename_tmp_packfile(name_buffer, rev_tmp_name, "rev");
569 if (mtimes_tmp_name)
570 rename_tmp_packfile(name_buffer, mtimes_tmp_name, "mtimes");
571 }
572
573 void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought)
574 {
575 int i, err;
576 FILE *output = xfopen(promisor_name, "w");
577
578 for (i = 0; i < nr_sought; i++)
579 fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid),
580 sought[i]->name);
581
582 err = ferror(output);
583 err |= fclose(output);
584 if (err)
585 die(_("could not write '%s' promisor file"), promisor_name);
586 }