11 struct object_entry *next;
13 unsigned char sha1[20];
16 struct object_entry_block
18 struct object_entry_block *next_block;
19 struct object_entry *next_free;
20 struct object_entry *end;
21 struct object_entry entries[FLEX_ARRAY]; /* more */
29 unsigned char sha1[20];
32 /* Stats and misc. counters. */
33 static int max_depth = 10;
34 static unsigned long alloc_count;
35 static unsigned long object_count;
36 static unsigned long duplicate_count;
37 static unsigned long object_count_by_type[9];
38 static unsigned long duplicate_count_by_type[9];
42 static unsigned long pack_offset;
43 static unsigned char pack_sha1[20];
45 /* Table of objects we've written. */
46 struct object_entry_block *blocks;
47 struct object_entry *object_table[1 << 16];
50 struct last_object last_blob;
52 static void alloc_objects(int cnt)
54 struct object_entry_block *b;
56 b = xmalloc(sizeof(struct object_entry_block)
57 + cnt * sizeof(struct object_entry));
58 b->next_block = blocks;
59 b->next_free = b->entries;
60 b->end = b->entries + cnt;
65 static struct object_entry* new_object(unsigned char *sha1)
67 struct object_entry *e;
69 if (blocks->next_free == blocks->end)
72 e = blocks->next_free++;
73 memcpy(e->sha1, sha1, sizeof(e->sha1));
77 static struct object_entry* insert_object(unsigned char *sha1)
79 unsigned int h = sha1[0] << 8 | sha1[1];
80 struct object_entry *e = object_table[h];
81 struct object_entry *p = 0;
84 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
100 static ssize_t yread(int fd, void *buffer, size_t length)
103 while (ret < length) {
104 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
116 static ssize_t ywrite(int fd, void *buffer, size_t length)
119 while (ret < length) {
120 ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret);
132 static unsigned long encode_header(
133 enum object_type type,
140 if (type < OBJ_COMMIT || type > OBJ_DELTA)
141 die("bad type %d", type);
143 c = (type << 4) | (size & 15);
155 static int store_object(
156 enum object_type type,
158 unsigned long datlen,
159 struct last_object *last)
162 struct object_entry *e;
163 unsigned char hdr[96];
164 unsigned char sha1[20];
165 unsigned long hdrlen, deltalen;
169 hdrlen = sprintf((char*)hdr,"%s %lu",type_names[type],datlen) + 1;
171 SHA1_Update(&c, hdr, hdrlen);
172 SHA1_Update(&c, dat, datlen);
173 SHA1_Final(sha1, &c);
175 e = insert_object(sha1);
178 duplicate_count_by_type[type]++;
181 e->offset = pack_offset;
183 object_count_by_type[type]++;
185 if (last->data && last->depth < max_depth)
186 delta = diff_delta(last->data, last->len,
192 memset(&s, 0, sizeof(s));
193 deflateInit(&s, zlib_compression_level);
198 s.avail_in = deltalen;
199 hdrlen = encode_header(OBJ_DELTA, deltalen, hdr);
200 if (ywrite(pack_fd, hdr, hdrlen) != hdrlen)
201 die("Can't write object header: %s", strerror(errno));
202 if (ywrite(pack_fd, last->sha1, sizeof(sha1)) != sizeof(sha1))
203 die("Can't write object base: %s", strerror(errno));
204 pack_offset += hdrlen + sizeof(sha1);
209 hdrlen = encode_header(type, datlen, hdr);
210 if (ywrite(pack_fd, hdr, hdrlen) != hdrlen)
211 die("Can't write object header: %s", strerror(errno));
212 pack_offset += hdrlen;
215 s.avail_out = deflateBound(&s, s.avail_in);
216 s.next_out = out = xmalloc(s.avail_out);
217 while (deflate(&s, Z_FINISH) == Z_OK)
221 if (ywrite(pack_fd, out, s.total_out) != s.total_out)
222 die("Failed writing compressed data %s", strerror(errno));
223 pack_offset += s.total_out;
232 memcpy(last->sha1, sha1, sizeof(sha1));
236 static void init_pack_header()
238 const char* magic = "PACK";
239 unsigned long version = 3;
240 unsigned long zero = 0;
242 version = htonl(version);
244 if (ywrite(pack_fd, (char*)magic, 4) != 4)
245 die("Can't write pack magic: %s", strerror(errno));
246 if (ywrite(pack_fd, &version, 4) != 4)
247 die("Can't write pack version: %s", strerror(errno));
248 if (ywrite(pack_fd, &zero, 4) != 4)
249 die("Can't write 0 object count: %s", strerror(errno));
253 static void fixup_header_footer()
261 if (lseek(pack_fd, 0, SEEK_SET) != 0)
262 die("Failed seeking to start: %s", strerror(errno));
265 if (yread(pack_fd, hdr, 8) != 8)
266 die("Failed reading header: %s", strerror(errno));
267 SHA1_Update(&c, hdr, 8);
269 cnt = htonl(object_count);
270 SHA1_Update(&c, &cnt, 4);
271 if (ywrite(pack_fd, &cnt, 4) != 4)
272 die("Failed writing object count: %s", strerror(errno));
274 buf = xmalloc(128 * 1024);
276 n = xread(pack_fd, buf, 128 * 1024);
279 SHA1_Update(&c, buf, n);
283 SHA1_Final(pack_sha1, &c);
284 if (ywrite(pack_fd, pack_sha1, sizeof(pack_sha1)) != sizeof(pack_sha1))
285 die("Failed writing pack checksum: %s", strerror(errno));
288 static int oecmp (const void *_a, const void *_b)
290 struct object_entry *a = *((struct object_entry**)_a);
291 struct object_entry *b = *((struct object_entry**)_b);
292 return memcmp(a->sha1, b->sha1, sizeof(a->sha1));
295 static void write_index(const char *idx_name)
298 struct object_entry **idx, **c, **last;
299 struct object_entry *e;
300 struct object_entry_block *o;
301 unsigned int array[256];
304 /* Build the sorted table of object IDs. */
305 idx = xmalloc(object_count * sizeof(struct object_entry*));
307 for (o = blocks; o; o = o->next_block)
308 for (e = o->entries; e != o->next_free; e++)
310 last = idx + object_count;
311 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
313 /* Generate the fan-out array. */
315 for (i = 0; i < 256; i++) {
316 struct object_entry **next = c;;
317 while (next < last) {
318 if ((*next)->sha1[0] != i)
322 array[i] = htonl(next - idx);
326 f = sha1create("%s", idx_name);
327 sha1write(f, array, 256 * sizeof(int));
328 for (c = idx; c != last; c++) {
329 unsigned int offset = htonl((*c)->offset);
330 sha1write(f, &offset, 4);
331 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
333 sha1write(f, pack_sha1, sizeof(pack_sha1));
334 sha1close(f, NULL, 1);
338 static void new_blob()
340 unsigned long datlen;
343 if (yread(0, &datlen, 4) != 4)
344 die("Can't obtain blob length");
346 dat = xmalloc(datlen);
347 if (yread(0, dat, datlen) != datlen)
348 die("Con't obtain %lu bytes of blob data", datlen);
350 if (!store_object(OBJ_BLOB, dat, datlen, &last_blob))
354 int main(int argc, const char **argv)
356 const char *base_name = argv[1];
357 int est_obj_cnt = atoi(argv[2]);
362 pack_name = xmalloc(strlen(base_name) + 6);
363 sprintf(pack_name, "%s.pack", base_name);
364 idx_name = xmalloc(strlen(base_name) + 5);
365 sprintf(idx_name, "%s.idx", base_name);
367 pack_fd = open(pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
369 die("Can't create %s: %s", pack_name, strerror(errno));
371 alloc_objects(est_obj_cnt);
375 if (yread(0, &cmd, 4) != 4)
379 case 'blob': new_blob(); break;
381 die("Invalid command %lu", cmd);
384 fixup_header_footer();
386 write_index(idx_name);
388 fprintf(stderr, "%s statistics:\n", argv[0]);
389 fprintf(stderr, "---------------------------------------------------\n");
390 fprintf(stderr, "Alloc'd objects: %10lu (%10lu overflow )\n", alloc_count, alloc_count - est_obj_cnt);
391 fprintf(stderr, "Total objects: %10lu (%10lu duplicates)\n", object_count, duplicate_count);
392 fprintf(stderr, " blobs : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_BLOB], duplicate_count_by_type[OBJ_BLOB]);
393 fprintf(stderr, " trees : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TREE], duplicate_count_by_type[OBJ_TREE]);
394 fprintf(stderr, " commits: %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_COMMIT], duplicate_count_by_type[OBJ_COMMIT]);
395 fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
396 fprintf(stderr, "---------------------------------------------------\n");
398 stat(pack_name, &sb);
399 fprintf(stderr, "Pack size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
401 fprintf(stderr, "Index size: %10lu KiB\n", (unsigned long)(sb.st_size/1024));
403 fprintf(stderr, "\n");