+static void start_packfile(void)
+{
+ static char tmpfile[PATH_MAX];
+ struct packed_git *p;
+ struct pack_header hdr;
+ int pack_fd;
+
+ snprintf(tmpfile, sizeof(tmpfile),
+ "%s/pack_XXXXXX", get_object_directory());
+ pack_fd = mkstemp(tmpfile);
+ if (pack_fd < 0)
+ die("Can't create %s: %s", tmpfile, strerror(errno));
+ p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
+ strcpy(p->pack_name, tmpfile);
+ p->pack_fd = pack_fd;
+
+ hdr.hdr_signature = htonl(PACK_SIGNATURE);
+ hdr.hdr_version = htonl(2);
+ hdr.hdr_entries = 0;
+ write_or_die(p->pack_fd, &hdr, sizeof(hdr));
+
+ pack_data = p;
+ pack_size = sizeof(hdr);
+ object_count = 0;
+
+ all_packs = xrealloc(all_packs, sizeof(*all_packs) * (pack_id + 1));
+ all_packs[pack_id] = p;
+}
+
+static void fixup_header_footer(void)
+{
+ static const int buf_sz = 128 * 1024;
+ int pack_fd = pack_data->pack_fd;
+ SHA_CTX c;
+ struct pack_header hdr;
+ char *buf;
+
+ if (lseek(pack_fd, 0, SEEK_SET) != 0)
+ die("Failed seeking to start: %s", strerror(errno));
+ if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
+ die("Unable to reread header of %s", pack_data->pack_name);
+ if (lseek(pack_fd, 0, SEEK_SET) != 0)
+ die("Failed seeking to start: %s", strerror(errno));
+ hdr.hdr_entries = htonl(object_count);
+ write_or_die(pack_fd, &hdr, sizeof(hdr));
+
+ SHA1_Init(&c);
+ SHA1_Update(&c, &hdr, sizeof(hdr));
+
+ buf = xmalloc(buf_sz);
+ for (;;) {
+ size_t n = xread(pack_fd, buf, buf_sz);
+ if (!n)
+ break;
+ if (n < 0)
+ die("Failed to checksum %s", pack_data->pack_name);
+ SHA1_Update(&c, buf, n);
+ }
+ free(buf);
+
+ SHA1_Final(pack_data->sha1, &c);
+ write_or_die(pack_fd, pack_data->sha1, sizeof(pack_data->sha1));
+ close(pack_fd);
+}
+
+static int oecmp (const void *a_, const void *b_)
+{
+ struct object_entry *a = *((struct object_entry**)a_);
+ struct object_entry *b = *((struct object_entry**)b_);
+ return hashcmp(a->sha1, b->sha1);
+}
+
+static char* create_index(void)
+{
+ static char tmpfile[PATH_MAX];
+ SHA_CTX ctx;
+ struct sha1file *f;
+ struct object_entry **idx, **c, **last, *e;
+ struct object_entry_pool *o;
+ uint32_t array[256];
+ int i, idx_fd;
+
+ /* Build the sorted table of object IDs. */
+ idx = xmalloc(object_count * sizeof(struct object_entry*));
+ c = idx;
+ for (o = blocks; o; o = o->next_pool)
+ for (e = o->next_free; e-- != o->entries;)
+ if (pack_id == e->pack_id)
+ *c++ = e;
+ last = idx + object_count;
+ if (c != last)
+ die("internal consistency error creating the index");
+ qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
+
+ /* Generate the fan-out array. */
+ c = idx;
+ for (i = 0; i < 256; i++) {
+ struct object_entry **next = c;;
+ while (next < last) {
+ if ((*next)->sha1[0] != i)
+ break;
+ next++;
+ }
+ array[i] = htonl(next - idx);
+ c = next;
+ }
+
+ snprintf(tmpfile, sizeof(tmpfile),
+ "%s/index_XXXXXX", get_object_directory());
+ idx_fd = mkstemp(tmpfile);
+ if (idx_fd < 0)
+ die("Can't create %s: %s", tmpfile, strerror(errno));
+ f = sha1fd(idx_fd, tmpfile);
+ sha1write(f, array, 256 * sizeof(int));
+ SHA1_Init(&ctx);
+ for (c = idx; c != last; c++) {
+ uint32_t offset = htonl((*c)->offset);
+ sha1write(f, &offset, 4);
+ sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
+ SHA1_Update(&ctx, (*c)->sha1, 20);
+ }
+ sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
+ sha1close(f, NULL, 1);
+ free(idx);
+ SHA1_Final(pack_data->sha1, &ctx);
+ return tmpfile;
+}
+
+static char* keep_pack(char *curr_index_name)
+{
+ static char name[PATH_MAX];
+ static char *keep_msg = "fast-import";
+ int keep_fd;
+
+ chmod(pack_data->pack_name, 0444);
+ chmod(curr_index_name, 0444);
+
+ snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
+ get_object_directory(), sha1_to_hex(pack_data->sha1));
+ keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
+ if (keep_fd < 0)
+ die("cannot create keep file");
+ write(keep_fd, keep_msg, strlen(keep_msg));
+ close(keep_fd);
+
+ snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
+ get_object_directory(), sha1_to_hex(pack_data->sha1));
+ if (move_temp_to_file(pack_data->pack_name, name))
+ die("cannot store pack file");
+
+ snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
+ get_object_directory(), sha1_to_hex(pack_data->sha1));
+ if (move_temp_to_file(curr_index_name, name))
+ die("cannot store index file");
+ return name;
+}
+
+static void unkeep_all_packs(void)