Code

index-pack: a miniscule refactor
[git.git] / builtin / index-pack.c
1 #include "cache.h"
2 #include "delta.h"
3 #include "pack.h"
4 #include "csum-file.h"
5 #include "blob.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "tree.h"
9 #include "progress.h"
10 #include "fsck.h"
11 #include "exec_cmd.h"
13 static const char index_pack_usage[] =
14 "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
16 struct object_entry
17 {
18         struct pack_idx_entry idx;
19         unsigned long size;
20         unsigned int hdr_size;
21         enum object_type type;
22         enum object_type real_type;
23 };
25 union delta_base {
26         unsigned char sha1[20];
27         off_t offset;
28 };
30 struct base_data {
31         struct base_data *base;
32         struct base_data *child;
33         struct object_entry *obj;
34         void *data;
35         unsigned long size;
36 };
38 /*
39  * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
40  * to memcmp() only the first 20 bytes.
41  */
42 #define UNION_BASE_SZ   20
44 #define FLAG_LINK (1u<<20)
45 #define FLAG_CHECKED (1u<<21)
47 struct delta_entry
48 {
49         union delta_base base;
50         int obj_no;
51 };
53 static struct object_entry *objects;
54 static struct delta_entry *deltas;
55 static struct base_data *base_cache;
56 static size_t base_cache_used;
57 static int nr_objects;
58 static int nr_deltas;
59 static int nr_resolved_deltas;
61 static int from_stdin;
62 static int strict;
63 static int verbose;
65 static struct progress *progress;
67 /* We always read in 4kB chunks. */
68 static unsigned char input_buffer[4096];
69 static unsigned int input_offset, input_len;
70 static off_t consumed_bytes;
71 static git_SHA_CTX input_ctx;
72 static uint32_t input_crc32;
73 static int input_fd, output_fd, pack_fd;
75 static int mark_link(struct object *obj, int type, void *data)
76 {
77         if (!obj)
78                 return -1;
80         if (type != OBJ_ANY && obj->type != type)
81                 die("object type mismatch at %s", sha1_to_hex(obj->sha1));
83         obj->flags |= FLAG_LINK;
84         return 0;
85 }
87 /* The content of each linked object must have been checked
88    or it must be already present in the object database */
89 static void check_object(struct object *obj)
90 {
91         if (!obj)
92                 return;
94         if (!(obj->flags & FLAG_LINK))
95                 return;
97         if (!(obj->flags & FLAG_CHECKED)) {
98                 unsigned long size;
99                 int type = sha1_object_info(obj->sha1, &size);
100                 if (type != obj->type || type <= 0)
101                         die("object of unexpected type");
102                 obj->flags |= FLAG_CHECKED;
103                 return;
104         }
107 static void check_objects(void)
109         unsigned i, max;
111         max = get_max_object_index();
112         for (i = 0; i < max; i++)
113                 check_object(get_indexed_object(i));
117 /* Discard current buffer used content. */
118 static void flush(void)
120         if (input_offset) {
121                 if (output_fd >= 0)
122                         write_or_die(output_fd, input_buffer, input_offset);
123                 git_SHA1_Update(&input_ctx, input_buffer, input_offset);
124                 memmove(input_buffer, input_buffer + input_offset, input_len);
125                 input_offset = 0;
126         }
129 /*
130  * Make sure at least "min" bytes are available in the buffer, and
131  * return the pointer to the buffer.
132  */
133 static void *fill(int min)
135         if (min <= input_len)
136                 return input_buffer + input_offset;
137         if (min > sizeof(input_buffer))
138                 die("cannot fill %d bytes", min);
139         flush();
140         do {
141                 ssize_t ret = xread(input_fd, input_buffer + input_len,
142                                 sizeof(input_buffer) - input_len);
143                 if (ret <= 0) {
144                         if (!ret)
145                                 die("early EOF");
146                         die_errno("read error on input");
147                 }
148                 input_len += ret;
149                 if (from_stdin)
150                         display_throughput(progress, consumed_bytes + input_len);
151         } while (input_len < min);
152         return input_buffer;
155 static void use(int bytes)
157         if (bytes > input_len)
158                 die("used more bytes than were available");
159         input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
160         input_len -= bytes;
161         input_offset += bytes;
163         /* make sure off_t is sufficiently large not to wrap */
164         if (signed_add_overflows(consumed_bytes, bytes))
165                 die("pack too large for current definition of off_t");
166         consumed_bytes += bytes;
169 static const char *open_pack_file(const char *pack_name)
171         if (from_stdin) {
172                 input_fd = 0;
173                 if (!pack_name) {
174                         static char tmpfile[PATH_MAX];
175                         output_fd = odb_mkstemp(tmpfile, sizeof(tmpfile),
176                                                 "pack/tmp_pack_XXXXXX");
177                         pack_name = xstrdup(tmpfile);
178                 } else
179                         output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
180                 if (output_fd < 0)
181                         die_errno("unable to create '%s'", pack_name);
182                 pack_fd = output_fd;
183         } else {
184                 input_fd = open(pack_name, O_RDONLY);
185                 if (input_fd < 0)
186                         die_errno("cannot open packfile '%s'", pack_name);
187                 output_fd = -1;
188                 pack_fd = input_fd;
189         }
190         git_SHA1_Init(&input_ctx);
191         return pack_name;
194 static void parse_pack_header(void)
196         struct pack_header *hdr = fill(sizeof(struct pack_header));
198         /* Header consistency check */
199         if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
200                 die("pack signature mismatch");
201         if (!pack_version_ok(hdr->hdr_version))
202                 die("pack version %"PRIu32" unsupported",
203                         ntohl(hdr->hdr_version));
205         nr_objects = ntohl(hdr->hdr_entries);
206         use(sizeof(struct pack_header));
209 static NORETURN void bad_object(unsigned long offset, const char *format,
210                        ...) __attribute__((format (printf, 2, 3)));
212 static void bad_object(unsigned long offset, const char *format, ...)
214         va_list params;
215         char buf[1024];
217         va_start(params, format);
218         vsnprintf(buf, sizeof(buf), format, params);
219         va_end(params);
220         die("pack has bad object at offset %lu: %s", offset, buf);
223 static void free_base_data(struct base_data *c)
225         if (c->data) {
226                 free(c->data);
227                 c->data = NULL;
228                 base_cache_used -= c->size;
229         }
232 static void prune_base_data(struct base_data *retain)
234         struct base_data *b;
235         for (b = base_cache;
236              base_cache_used > delta_base_cache_limit && b;
237              b = b->child) {
238                 if (b->data && b != retain)
239                         free_base_data(b);
240         }
243 static void link_base_data(struct base_data *base, struct base_data *c)
245         if (base)
246                 base->child = c;
247         else
248                 base_cache = c;
250         c->base = base;
251         c->child = NULL;
252         if (c->data)
253                 base_cache_used += c->size;
254         prune_base_data(c);
257 static void unlink_base_data(struct base_data *c)
259         struct base_data *base = c->base;
260         if (base)
261                 base->child = NULL;
262         else
263                 base_cache = NULL;
264         free_base_data(c);
267 static void *unpack_entry_data(unsigned long offset, unsigned long size)
269         int status;
270         z_stream stream;
271         void *buf = xmalloc(size);
273         memset(&stream, 0, sizeof(stream));
274         git_inflate_init(&stream);
275         stream.next_out = buf;
276         stream.avail_out = size;
278         do {
279                 stream.next_in = fill(1);
280                 stream.avail_in = input_len;
281                 status = git_inflate(&stream, 0);
282                 use(input_len - stream.avail_in);
283         } while (status == Z_OK);
284         if (stream.total_out != size || status != Z_STREAM_END)
285                 bad_object(offset, "inflate returned %d", status);
286         git_inflate_end(&stream);
287         return buf;
290 static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
292         unsigned char *p;
293         unsigned long size, c;
294         off_t base_offset;
295         unsigned shift;
296         void *data;
298         obj->idx.offset = consumed_bytes;
299         input_crc32 = crc32(0, Z_NULL, 0);
301         p = fill(1);
302         c = *p;
303         use(1);
304         obj->type = (c >> 4) & 7;
305         size = (c & 15);
306         shift = 4;
307         while (c & 0x80) {
308                 p = fill(1);
309                 c = *p;
310                 use(1);
311                 size += (c & 0x7f) << shift;
312                 shift += 7;
313         }
314         obj->size = size;
316         switch (obj->type) {
317         case OBJ_REF_DELTA:
318                 hashcpy(delta_base->sha1, fill(20));
319                 use(20);
320                 break;
321         case OBJ_OFS_DELTA:
322                 memset(delta_base, 0, sizeof(*delta_base));
323                 p = fill(1);
324                 c = *p;
325                 use(1);
326                 base_offset = c & 127;
327                 while (c & 128) {
328                         base_offset += 1;
329                         if (!base_offset || MSB(base_offset, 7))
330                                 bad_object(obj->idx.offset, "offset value overflow for delta base object");
331                         p = fill(1);
332                         c = *p;
333                         use(1);
334                         base_offset = (base_offset << 7) + (c & 127);
335                 }
336                 delta_base->offset = obj->idx.offset - base_offset;
337                 if (delta_base->offset <= 0 || delta_base->offset >= obj->idx.offset)
338                         bad_object(obj->idx.offset, "delta base offset is out of bound");
339                 break;
340         case OBJ_COMMIT:
341         case OBJ_TREE:
342         case OBJ_BLOB:
343         case OBJ_TAG:
344                 break;
345         default:
346                 bad_object(obj->idx.offset, "unknown object type %d", obj->type);
347         }
348         obj->hdr_size = consumed_bytes - obj->idx.offset;
350         data = unpack_entry_data(obj->idx.offset, obj->size);
351         obj->idx.crc32 = input_crc32;
352         return data;
355 static void *get_data_from_pack(struct object_entry *obj)
357         off_t from = obj[0].idx.offset + obj[0].hdr_size;
358         unsigned long len = obj[1].idx.offset - from;
359         unsigned char *data, *inbuf;
360         z_stream stream;
361         int status;
363         data = xmalloc(obj->size);
364         inbuf = xmalloc((len < 64*1024) ? len : 64*1024);
366         memset(&stream, 0, sizeof(stream));
367         git_inflate_init(&stream);
368         stream.next_out = data;
369         stream.avail_out = obj->size;
371         do {
372                 ssize_t n = (len < 64*1024) ? len : 64*1024;
373                 n = pread(pack_fd, inbuf, n, from);
374                 if (n < 0)
375                         die_errno("cannot pread pack file");
376                 if (!n)
377                         die("premature end of pack file, %lu bytes missing", len);
378                 from += n;
379                 len -= n;
380                 stream.next_in = inbuf;
381                 stream.avail_in = n;
382                 status = git_inflate(&stream, 0);
383         } while (len && status == Z_OK && !stream.avail_in);
385         /* This has been inflated OK when first encountered, so... */
386         if (status != Z_STREAM_END || stream.total_out != obj->size)
387                 die("serious inflate inconsistency");
389         git_inflate_end(&stream);
390         free(inbuf);
391         return data;
394 static int compare_delta_bases(const union delta_base *base1,
395                                const union delta_base *base2,
396                                enum object_type type1,
397                                enum object_type type2)
399         int cmp = type1 - type2;
400         if (cmp)
401                 return cmp;
402         return memcmp(base1, base2, UNION_BASE_SZ);
405 static int find_delta(const union delta_base *base, enum object_type type)
407         int first = 0, last = nr_deltas;
409         while (first < last) {
410                 int next = (first + last) / 2;
411                 struct delta_entry *delta = &deltas[next];
412                 int cmp;
414                 cmp = compare_delta_bases(base, &delta->base,
415                                           type, objects[delta->obj_no].type);
416                 if (!cmp)
417                         return next;
418                 if (cmp < 0) {
419                         last = next;
420                         continue;
421                 }
422                 first = next+1;
423         }
424         return -first-1;
427 static void find_delta_children(const union delta_base *base,
428                                 int *first_index, int *last_index,
429                                 enum object_type type)
431         int first = find_delta(base, type);
432         int last = first;
433         int end = nr_deltas - 1;
435         if (first < 0) {
436                 *first_index = 0;
437                 *last_index = -1;
438                 return;
439         }
440         while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
441                 --first;
442         while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
443                 ++last;
444         *first_index = first;
445         *last_index = last;
448 static void sha1_object(const void *data, unsigned long size,
449                         enum object_type type, unsigned char *sha1)
451         hash_sha1_file(data, size, typename(type), sha1);
452         if (has_sha1_file(sha1)) {
453                 void *has_data;
454                 enum object_type has_type;
455                 unsigned long has_size;
456                 has_data = read_sha1_file(sha1, &has_type, &has_size);
457                 if (!has_data)
458                         die("cannot read existing object %s", sha1_to_hex(sha1));
459                 if (size != has_size || type != has_type ||
460                     memcmp(data, has_data, size) != 0)
461                         die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1));
462                 free(has_data);
463         }
464         if (strict) {
465                 if (type == OBJ_BLOB) {
466                         struct blob *blob = lookup_blob(sha1);
467                         if (blob)
468                                 blob->object.flags |= FLAG_CHECKED;
469                         else
470                                 die("invalid blob object %s", sha1_to_hex(sha1));
471                 } else {
472                         struct object *obj;
473                         int eaten;
474                         void *buf = (void *) data;
476                         /*
477                          * we do not need to free the memory here, as the
478                          * buf is deleted by the caller.
479                          */
480                         obj = parse_object_buffer(sha1, type, size, buf, &eaten);
481                         if (!obj)
482                                 die("invalid %s", typename(type));
483                         if (fsck_object(obj, 1, fsck_error_function))
484                                 die("Error in object");
485                         if (fsck_walk(obj, mark_link, NULL))
486                                 die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1));
488                         if (obj->type == OBJ_TREE) {
489                                 struct tree *item = (struct tree *) obj;
490                                 item->buffer = NULL;
491                         }
492                         if (obj->type == OBJ_COMMIT) {
493                                 struct commit *commit = (struct commit *) obj;
494                                 commit->buffer = NULL;
495                         }
496                         obj->flags |= FLAG_CHECKED;
497                 }
498         }
501 static int is_delta_type(enum object_type type)
503         return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA);
506 static void *get_base_data(struct base_data *c)
508         if (!c->data) {
509                 struct object_entry *obj = c->obj;
511                 if (is_delta_type(obj->type)) {
512                         void *base = get_base_data(c->base);
513                         void *raw = get_data_from_pack(obj);
514                         c->data = patch_delta(
515                                 base, c->base->size,
516                                 raw, obj->size,
517                                 &c->size);
518                         free(raw);
519                         if (!c->data)
520                                 bad_object(obj->idx.offset, "failed to apply delta");
521                 } else {
522                         c->data = get_data_from_pack(obj);
523                         c->size = obj->size;
524                 }
526                 base_cache_used += c->size;
527                 prune_base_data(c);
528         }
529         return c->data;
532 static void resolve_delta(struct object_entry *delta_obj,
533                           struct base_data *base, struct base_data *result)
535         void *base_data, *delta_data;
537         delta_obj->real_type = base->obj->real_type;
538         delta_data = get_data_from_pack(delta_obj);
539         base_data = get_base_data(base);
540         result->obj = delta_obj;
541         result->data = patch_delta(base_data, base->size,
542                                    delta_data, delta_obj->size, &result->size);
543         free(delta_data);
544         if (!result->data)
545                 bad_object(delta_obj->idx.offset, "failed to apply delta");
546         sha1_object(result->data, result->size, delta_obj->real_type,
547                     delta_obj->idx.sha1);
548         nr_resolved_deltas++;
551 static void find_unresolved_deltas(struct base_data *base,
552                                    struct base_data *prev_base)
554         int i, ref_first, ref_last, ofs_first, ofs_last;
556         /*
557          * This is a recursive function. Those brackets should help reducing
558          * stack usage by limiting the scope of the delta_base union.
559          */
560         {
561                 union delta_base base_spec;
563                 hashcpy(base_spec.sha1, base->obj->idx.sha1);
564                 find_delta_children(&base_spec,
565                                     &ref_first, &ref_last, OBJ_REF_DELTA);
567                 memset(&base_spec, 0, sizeof(base_spec));
568                 base_spec.offset = base->obj->idx.offset;
569                 find_delta_children(&base_spec,
570                                     &ofs_first, &ofs_last, OBJ_OFS_DELTA);
571         }
573         if (ref_last == -1 && ofs_last == -1) {
574                 free(base->data);
575                 return;
576         }
578         link_base_data(prev_base, base);
580         for (i = ref_first; i <= ref_last; i++) {
581                 struct object_entry *child = objects + deltas[i].obj_no;
582                 struct base_data result;
584                 assert(child->real_type == OBJ_REF_DELTA);
585                 resolve_delta(child, base, &result);
586                 if (i == ref_last && ofs_last == -1)
587                         free_base_data(base);
588                 find_unresolved_deltas(&result, base);
589         }
591         for (i = ofs_first; i <= ofs_last; i++) {
592                 struct object_entry *child = objects + deltas[i].obj_no;
593                 struct base_data result;
595                 assert(child->real_type == OBJ_OFS_DELTA);
596                 resolve_delta(child, base, &result);
597                 if (i == ofs_last)
598                         free_base_data(base);
599                 find_unresolved_deltas(&result, base);
600         }
602         unlink_base_data(base);
605 static int compare_delta_entry(const void *a, const void *b)
607         const struct delta_entry *delta_a = a;
608         const struct delta_entry *delta_b = b;
610         /* group by type (ref vs ofs) and then by value (sha-1 or offset) */
611         return compare_delta_bases(&delta_a->base, &delta_b->base,
612                                    objects[delta_a->obj_no].type,
613                                    objects[delta_b->obj_no].type);
616 /* Parse all objects and return the pack content SHA1 hash */
617 static void parse_pack_objects(unsigned char *sha1)
619         int i;
620         struct delta_entry *delta = deltas;
621         struct stat st;
623         /*
624          * First pass:
625          * - find locations of all objects;
626          * - calculate SHA1 of all non-delta objects;
627          * - remember base (SHA1 or offset) for all deltas.
628          */
629         if (verbose)
630                 progress = start_progress(
631                                 from_stdin ? "Receiving objects" : "Indexing objects",
632                                 nr_objects);
633         for (i = 0; i < nr_objects; i++) {
634                 struct object_entry *obj = &objects[i];
635                 void *data = unpack_raw_entry(obj, &delta->base);
636                 obj->real_type = obj->type;
637                 if (is_delta_type(obj->type)) {
638                         nr_deltas++;
639                         delta->obj_no = i;
640                         delta++;
641                 } else
642                         sha1_object(data, obj->size, obj->type, obj->idx.sha1);
643                 free(data);
644                 display_progress(progress, i+1);
645         }
646         objects[i].idx.offset = consumed_bytes;
647         stop_progress(&progress);
649         /* Check pack integrity */
650         flush();
651         git_SHA1_Final(sha1, &input_ctx);
652         if (hashcmp(fill(20), sha1))
653                 die("pack is corrupted (SHA1 mismatch)");
654         use(20);
656         /* If input_fd is a file, we should have reached its end now. */
657         if (fstat(input_fd, &st))
658                 die_errno("cannot fstat packfile");
659         if (S_ISREG(st.st_mode) &&
660                         lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
661                 die("pack has junk at the end");
663         if (!nr_deltas)
664                 return;
666         /* Sort deltas by base SHA1/offset for fast searching */
667         qsort(deltas, nr_deltas, sizeof(struct delta_entry),
668               compare_delta_entry);
670         /*
671          * Second pass:
672          * - for all non-delta objects, look if it is used as a base for
673          *   deltas;
674          * - if used as a base, uncompress the object and apply all deltas,
675          *   recursively checking if the resulting object is used as a base
676          *   for some more deltas.
677          */
678         if (verbose)
679                 progress = start_progress("Resolving deltas", nr_deltas);
680         for (i = 0; i < nr_objects; i++) {
681                 struct object_entry *obj = &objects[i];
682                 struct base_data base_obj;
684                 if (is_delta_type(obj->type))
685                         continue;
686                 base_obj.obj = obj;
687                 base_obj.data = NULL;
688                 find_unresolved_deltas(&base_obj, NULL);
689                 display_progress(progress, nr_resolved_deltas);
690         }
693 static int write_compressed(struct sha1file *f, void *in, unsigned int size)
695         z_stream stream;
696         int status;
697         unsigned char outbuf[4096];
699         memset(&stream, 0, sizeof(stream));
700         deflateInit(&stream, zlib_compression_level);
701         stream.next_in = in;
702         stream.avail_in = size;
704         do {
705                 stream.next_out = outbuf;
706                 stream.avail_out = sizeof(outbuf);
707                 status = deflate(&stream, Z_FINISH);
708                 sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out);
709         } while (status == Z_OK);
711         if (status != Z_STREAM_END)
712                 die("unable to deflate appended object (%d)", status);
713         size = stream.total_out;
714         deflateEnd(&stream);
715         return size;
718 static struct object_entry *append_obj_to_pack(struct sha1file *f,
719                                const unsigned char *sha1, void *buf,
720                                unsigned long size, enum object_type type)
722         struct object_entry *obj = &objects[nr_objects++];
723         unsigned char header[10];
724         unsigned long s = size;
725         int n = 0;
726         unsigned char c = (type << 4) | (s & 15);
727         s >>= 4;
728         while (s) {
729                 header[n++] = c | 0x80;
730                 c = s & 0x7f;
731                 s >>= 7;
732         }
733         header[n++] = c;
734         crc32_begin(f);
735         sha1write(f, header, n);
736         obj[0].size = size;
737         obj[0].hdr_size = n;
738         obj[0].type = type;
739         obj[0].real_type = type;
740         obj[1].idx.offset = obj[0].idx.offset + n;
741         obj[1].idx.offset += write_compressed(f, buf, size);
742         obj[0].idx.crc32 = crc32_end(f);
743         sha1flush(f);
744         hashcpy(obj->idx.sha1, sha1);
745         return obj;
748 static int delta_pos_compare(const void *_a, const void *_b)
750         struct delta_entry *a = *(struct delta_entry **)_a;
751         struct delta_entry *b = *(struct delta_entry **)_b;
752         return a->obj_no - b->obj_no;
755 static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved)
757         struct delta_entry **sorted_by_pos;
758         int i, n = 0;
760         /*
761          * Since many unresolved deltas may well be themselves base objects
762          * for more unresolved deltas, we really want to include the
763          * smallest number of base objects that would cover as much delta
764          * as possible by picking the
765          * trunc deltas first, allowing for other deltas to resolve without
766          * additional base objects.  Since most base objects are to be found
767          * before deltas depending on them, a good heuristic is to start
768          * resolving deltas in the same order as their position in the pack.
769          */
770         sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
771         for (i = 0; i < nr_deltas; i++) {
772                 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
773                         continue;
774                 sorted_by_pos[n++] = &deltas[i];
775         }
776         qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
778         for (i = 0; i < n; i++) {
779                 struct delta_entry *d = sorted_by_pos[i];
780                 enum object_type type;
781                 struct base_data base_obj;
783                 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
784                         continue;
785                 base_obj.data = read_sha1_file(d->base.sha1, &type, &base_obj.size);
786                 if (!base_obj.data)
787                         continue;
789                 if (check_sha1_signature(d->base.sha1, base_obj.data,
790                                 base_obj.size, typename(type)))
791                         die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
792                 base_obj.obj = append_obj_to_pack(f, d->base.sha1,
793                                         base_obj.data, base_obj.size, type);
794                 find_unresolved_deltas(&base_obj, NULL);
795                 display_progress(progress, nr_resolved_deltas);
796         }
797         free(sorted_by_pos);
800 static void final(const char *final_pack_name, const char *curr_pack_name,
801                   const char *final_index_name, const char *curr_index_name,
802                   const char *keep_name, const char *keep_msg,
803                   unsigned char *sha1)
805         const char *report = "pack";
806         char name[PATH_MAX];
807         int err;
809         if (!from_stdin) {
810                 close(input_fd);
811         } else {
812                 fsync_or_die(output_fd, curr_pack_name);
813                 err = close(output_fd);
814                 if (err)
815                         die_errno("error while closing pack file");
816         }
818         if (keep_msg) {
819                 int keep_fd, keep_msg_len = strlen(keep_msg);
821                 if (!keep_name)
822                         keep_fd = odb_pack_keep(name, sizeof(name), sha1);
823                 else
824                         keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
826                 if (keep_fd < 0) {
827                         if (errno != EEXIST)
828                                 die_errno("cannot write keep file '%s'",
829                                           keep_name);
830                 } else {
831                         if (keep_msg_len > 0) {
832                                 write_or_die(keep_fd, keep_msg, keep_msg_len);
833                                 write_or_die(keep_fd, "\n", 1);
834                         }
835                         if (close(keep_fd) != 0)
836                                 die_errno("cannot close written keep file '%s'",
837                                     keep_name);
838                         report = "keep";
839                 }
840         }
842         if (final_pack_name != curr_pack_name) {
843                 if (!final_pack_name) {
844                         snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
845                                  get_object_directory(), sha1_to_hex(sha1));
846                         final_pack_name = name;
847                 }
848                 if (move_temp_to_file(curr_pack_name, final_pack_name))
849                         die("cannot store pack file");
850         } else if (from_stdin)
851                 chmod(final_pack_name, 0444);
853         if (final_index_name != curr_index_name) {
854                 if (!final_index_name) {
855                         snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
856                                  get_object_directory(), sha1_to_hex(sha1));
857                         final_index_name = name;
858                 }
859                 if (move_temp_to_file(curr_index_name, final_index_name))
860                         die("cannot store index file");
861         } else
862                 chmod(final_index_name, 0444);
864         if (!from_stdin) {
865                 printf("%s\n", sha1_to_hex(sha1));
866         } else {
867                 char buf[48];
868                 int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
869                                    report, sha1_to_hex(sha1));
870                 write_or_die(1, buf, len);
872                 /*
873                  * Let's just mimic git-unpack-objects here and write
874                  * the last part of the input buffer to stdout.
875                  */
876                 while (input_len) {
877                         err = xwrite(1, input_buffer + input_offset, input_len);
878                         if (err <= 0)
879                                 break;
880                         input_len -= err;
881                         input_offset += err;
882                 }
883         }
886 static int git_index_pack_config(const char *k, const char *v, void *cb)
888         struct pack_idx_option *opts = cb;
890         if (!strcmp(k, "pack.indexversion")) {
891                 opts->version = git_config_int(k, v);
892                 if (opts->version > 2)
893                         die("bad pack.indexversion=%"PRIu32, opts->version);
894                 return 0;
895         }
896         return git_default_config(k, v, cb);
899 static int cmp_uint32(const void *a_, const void *b_)
901         uint32_t a = *((uint32_t *)a_);
902         uint32_t b = *((uint32_t *)b_);
904         return (a < b) ? -1 : (a != b);
907 static void read_v2_anomalous_offsets(struct packed_git *p,
908                                       struct pack_idx_option *opts)
910         const uint32_t *idx1, *idx2;
911         uint32_t i;
913         /* The address of the 4-byte offset table */
914         idx1 = (((const uint32_t *)p->index_data)
915                 + 2 /* 8-byte header */
916                 + 256 /* fan out */
917                 + 5 * p->num_objects /* 20-byte SHA-1 table */
918                 + p->num_objects /* CRC32 table */
919                 );
921         /* The address of the 8-byte offset table */
922         idx2 = idx1 + p->num_objects;
924         for (i = 0; i < p->num_objects; i++) {
925                 uint32_t off = ntohl(idx1[i]);
926                 if (!(off & 0x80000000))
927                         continue;
928                 off = off & 0x7fffffff;
929                 if (idx2[off * 2])
930                         continue;
931                 /*
932                  * The real offset is ntohl(idx2[off * 2]) in high 4
933                  * octets, and ntohl(idx2[off * 2 + 1]) in low 4
934                  * octets.  But idx2[off * 2] is Zero!!!
935                  */
936                 ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc);
937                 opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
938         }
940         if (1 < opts->anomaly_nr)
941                 qsort(opts->anomaly, opts->anomaly_nr, sizeof(uint32_t), cmp_uint32);
944 static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)
946         struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1);
948         if (!p)
949                 die("Cannot open existing pack file '%s'", pack_name);
950         if (open_pack_index(p))
951                 die("Cannot open existing pack idx file for '%s'", pack_name);
953         /* Read the attributes from the existing idx file */
954         opts->version = p->index_version;
956         if (opts->version == 2)
957                 read_v2_anomalous_offsets(p, opts);
959         /*
960          * Get rid of the idx file as we do not need it anymore.
961          * NEEDSWORK: extract this bit from free_pack_by_name() in
962          * sha1_file.c, perhaps?  It shouldn't matter very much as we
963          * know we haven't installed this pack (hence we never have
964          * read anything from it).
965          */
966         close_pack_index(p);
967         free(p);
970 int cmd_index_pack(int argc, const char **argv, const char *prefix)
972         int i, fix_thin_pack = 0, verify = 0;
973         const char *curr_pack, *curr_index;
974         const char *index_name = NULL, *pack_name = NULL;
975         const char *keep_name = NULL, *keep_msg = NULL;
976         char *index_name_buf = NULL, *keep_name_buf = NULL;
977         struct pack_idx_entry **idx_objects;
978         struct pack_idx_option opts;
979         unsigned char pack_sha1[20];
981         if (argc == 2 && !strcmp(argv[1], "-h"))
982                 usage(index_pack_usage);
984         read_replace_refs = 0;
986         reset_pack_idx_option(&opts);
987         git_config(git_index_pack_config, &opts);
988         if (prefix && chdir(prefix))
989                 die("Cannot come back to cwd");
991         for (i = 1; i < argc; i++) {
992                 const char *arg = argv[i];
994                 if (*arg == '-') {
995                         if (!strcmp(arg, "--stdin")) {
996                                 from_stdin = 1;
997                         } else if (!strcmp(arg, "--fix-thin")) {
998                                 fix_thin_pack = 1;
999                         } else if (!strcmp(arg, "--strict")) {
1000                                 strict = 1;
1001                         } else if (!strcmp(arg, "--verify")) {
1002                                 verify = 1;
1003                         } else if (!strcmp(arg, "--keep")) {
1004                                 keep_msg = "";
1005                         } else if (!prefixcmp(arg, "--keep=")) {
1006                                 keep_msg = arg + 7;
1007                         } else if (!prefixcmp(arg, "--pack_header=")) {
1008                                 struct pack_header *hdr;
1009                                 char *c;
1011                                 hdr = (struct pack_header *)input_buffer;
1012                                 hdr->hdr_signature = htonl(PACK_SIGNATURE);
1013                                 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
1014                                 if (*c != ',')
1015                                         die("bad %s", arg);
1016                                 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
1017                                 if (*c)
1018                                         die("bad %s", arg);
1019                                 input_len = sizeof(*hdr);
1020                         } else if (!strcmp(arg, "-v")) {
1021                                 verbose = 1;
1022                         } else if (!strcmp(arg, "-o")) {
1023                                 if (index_name || (i+1) >= argc)
1024                                         usage(index_pack_usage);
1025                                 index_name = argv[++i];
1026                         } else if (!prefixcmp(arg, "--index-version=")) {
1027                                 char *c;
1028                                 opts.version = strtoul(arg + 16, &c, 10);
1029                                 if (opts.version > 2)
1030                                         die("bad %s", arg);
1031                                 if (*c == ',')
1032                                         opts.off32_limit = strtoul(c+1, &c, 0);
1033                                 if (*c || opts.off32_limit & 0x80000000)
1034                                         die("bad %s", arg);
1035                         } else
1036                                 usage(index_pack_usage);
1037                         continue;
1038                 }
1040                 if (pack_name)
1041                         usage(index_pack_usage);
1042                 pack_name = arg;
1043         }
1045         if (!pack_name && !from_stdin)
1046                 usage(index_pack_usage);
1047         if (fix_thin_pack && !from_stdin)
1048                 die("--fix-thin cannot be used without --stdin");
1049         if (!index_name && pack_name) {
1050                 int len = strlen(pack_name);
1051                 if (!has_extension(pack_name, ".pack"))
1052                         die("packfile name '%s' does not end with '.pack'",
1053                             pack_name);
1054                 index_name_buf = xmalloc(len);
1055                 memcpy(index_name_buf, pack_name, len - 5);
1056                 strcpy(index_name_buf + len - 5, ".idx");
1057                 index_name = index_name_buf;
1058         }
1059         if (keep_msg && !keep_name && pack_name) {
1060                 int len = strlen(pack_name);
1061                 if (!has_extension(pack_name, ".pack"))
1062                         die("packfile name '%s' does not end with '.pack'",
1063                             pack_name);
1064                 keep_name_buf = xmalloc(len);
1065                 memcpy(keep_name_buf, pack_name, len - 5);
1066                 strcpy(keep_name_buf + len - 5, ".keep");
1067                 keep_name = keep_name_buf;
1068         }
1069         if (verify) {
1070                 if (!index_name)
1071                         die("--verify with no packfile name given");
1072                 read_idx_option(&opts, index_name);
1073                 opts.flags |= WRITE_IDX_VERIFY;
1074         }
1076         curr_pack = open_pack_file(pack_name);
1077         parse_pack_header();
1078         objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
1079         deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
1080         parse_pack_objects(pack_sha1);
1081         if (nr_deltas == nr_resolved_deltas) {
1082                 stop_progress(&progress);
1083                 /* Flush remaining pack final 20-byte SHA1. */
1084                 flush();
1085         } else {
1086                 if (fix_thin_pack) {
1087                         struct sha1file *f;
1088                         unsigned char read_sha1[20], tail_sha1[20];
1089                         char msg[48];
1090                         int nr_unresolved = nr_deltas - nr_resolved_deltas;
1091                         int nr_objects_initial = nr_objects;
1092                         if (nr_unresolved <= 0)
1093                                 die("confusion beyond insanity");
1094                         objects = xrealloc(objects,
1095                                            (nr_objects + nr_unresolved + 1)
1096                                            * sizeof(*objects));
1097                         f = sha1fd(output_fd, curr_pack);
1098                         fix_unresolved_deltas(f, nr_unresolved);
1099                         sprintf(msg, "completed with %d local objects",
1100                                 nr_objects - nr_objects_initial);
1101                         stop_progress_msg(&progress, msg);
1102                         sha1close(f, tail_sha1, 0);
1103                         hashcpy(read_sha1, pack_sha1);
1104                         fixup_pack_header_footer(output_fd, pack_sha1,
1105                                                  curr_pack, nr_objects,
1106                                                  read_sha1, consumed_bytes-20);
1107                         if (hashcmp(read_sha1, tail_sha1) != 0)
1108                                 die("Unexpected tail checksum for %s "
1109                                     "(disk corruption?)", curr_pack);
1110                 }
1111                 if (nr_deltas != nr_resolved_deltas)
1112                         die("pack has %d unresolved deltas",
1113                             nr_deltas - nr_resolved_deltas);
1114         }
1115         free(deltas);
1116         if (strict)
1117                 check_objects();
1119         idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
1120         for (i = 0; i < nr_objects; i++)
1121                 idx_objects[i] = &objects[i].idx;
1122         curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1);
1123         free(idx_objects);
1125         if (!verify)
1126                 final(pack_name, curr_pack,
1127                       index_name, curr_index,
1128                       keep_name, keep_msg,
1129                       pack_sha1);
1130         else
1131                 close(input_fd);
1132         free(objects);
1133         free(index_name_buf);
1134         free(keep_name_buf);
1135         if (pack_name == NULL)
1136                 free((void *) curr_pack);
1137         if (index_name == NULL)
1138                 free((void *) curr_index);
1140         return 0;