9 static int max_depth = 10;
10 static unsigned long object_count;
11 static unsigned long duplicate_count;
12 static unsigned long packoff;
13 static unsigned long overflow_count;
15 static int current_depth;
17 static unsigned long lastdatlen;
18 static unsigned char lastsha1[20];
19 static unsigned char packsha1[20];
23 struct object_entry *next;
25 unsigned char sha1[20];
28 struct overflow_object_entry
30 struct overflow_object_entry *next;
31 struct object_entry oe;
34 struct object_entry *pool_start;
35 struct object_entry *pool_next;
36 struct object_entry *pool_end;
37 struct overflow_object_entry *overflow;
38 struct object_entry *table[1 << 16];
40 static struct object_entry* new_object(unsigned char *sha1)
42 if (pool_next != pool_end) {
43 struct object_entry *e = pool_next++;
44 memcpy(e->sha1, sha1, sizeof(e->sha1));
47 struct overflow_object_entry *e;
49 e = xmalloc(sizeof(struct overflow_object_entry));
51 memcpy(e->oe.sha1, sha1, sizeof(e->oe.sha1));
58 static struct object_entry* insert_object(unsigned char *sha1)
60 unsigned int h = sha1[0] << 8 | sha1[1];
61 struct object_entry *e = table[h];
62 struct object_entry *p = 0;
65 if (!memcmp(sha1, e->sha1, sizeof(e->sha1)))
81 static ssize_t yread(int fd, void *buffer, size_t length)
84 while (ret < length) {
85 ssize_t size = xread(fd, (char *) buffer + ret, length - ret);
97 static ssize_t ywrite(int fd, void *buffer, size_t length)
100 while (ret < length) {
101 ssize_t size = xwrite(fd, (char *) buffer + ret, length - ret);
113 static unsigned long encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
118 if (type < OBJ_COMMIT || type > OBJ_DELTA)
119 die("bad type %d", type);
121 c = (type << 4) | (size & 15);
133 static void write_blob(void *dat, unsigned long datlen)
137 unsigned char hdr[64];
138 unsigned long hdrlen, deltalen;
140 if (lastdat && current_depth < max_depth) {
141 delta = diff_delta(lastdat, lastdatlen,
147 memset(&s, 0, sizeof(s));
148 deflateInit(&s, zlib_compression_level);
153 s.avail_in = deltalen;
154 hdrlen = encode_header(OBJ_DELTA, deltalen, hdr);
155 if (ywrite(packfd, hdr, hdrlen) != hdrlen)
156 die("Can't write object header: %s", strerror(errno));
157 if (ywrite(packfd, lastsha1, sizeof(lastsha1)) != sizeof(lastsha1))
158 die("Can't write object base: %s", strerror(errno));
159 packoff += hdrlen + sizeof(lastsha1);
164 hdrlen = encode_header(OBJ_BLOB, datlen, hdr);
165 if (ywrite(packfd, hdr, hdrlen) != hdrlen)
166 die("Can't write object header: %s", strerror(errno));
170 s.avail_out = deflateBound(&s, s.avail_in);
171 s.next_out = out = xmalloc(s.avail_out);
172 while (deflate(&s, Z_FINISH) == Z_OK)
176 if (ywrite(packfd, out, s.total_out) != s.total_out)
177 die("Failed writing compressed data %s", strerror(errno));
178 packoff += s.total_out;
185 static void init_pack_header()
187 const char* magic = "PACK";
188 unsigned long version = 2;
189 unsigned long zero = 0;
191 version = htonl(version);
193 if (ywrite(packfd, (char*)magic, 4) != 4)
194 die("Can't write pack magic: %s", strerror(errno));
195 if (ywrite(packfd, &version, 4) != 4)
196 die("Can't write pack version: %s", strerror(errno));
197 if (ywrite(packfd, &zero, 4) != 4)
198 die("Can't write 0 object count: %s", strerror(errno));
202 static void fixup_header_footer()
210 if (lseek(packfd, 0, SEEK_SET) != 0)
211 die("Failed seeking to start: %s", strerror(errno));
214 if (yread(packfd, hdr, 8) != 8)
215 die("Failed reading header: %s", strerror(errno));
216 SHA1_Update(&c, hdr, 8);
218 cnt = htonl(object_count);
219 SHA1_Update(&c, &cnt, 4);
220 if (ywrite(packfd, &cnt, 4) != 4)
221 die("Failed writing object count: %s", strerror(errno));
223 buf = xmalloc(128 * 1024);
225 n = xread(packfd, buf, 128 * 1024);
228 SHA1_Update(&c, buf, n);
232 SHA1_Final(packsha1, &c);
233 if (ywrite(packfd, packsha1, sizeof(packsha1)) != sizeof(packsha1))
234 die("Failed writing pack checksum: %s", strerror(errno));
237 static int oecmp (const void *_a, const void *_b)
239 struct object_entry *a = *((struct object_entry**)_a);
240 struct object_entry *b = *((struct object_entry**)_b);
241 return memcmp(a->sha1, b->sha1, sizeof(a->sha1));
244 static void write_index(const char *idx_name)
247 struct object_entry **idx, **c, **last;
248 struct object_entry *e;
249 struct overflow_object_entry *o;
250 unsigned int array[256];
253 /* Build the sorted table of object IDs. */
254 idx = xmalloc(object_count * sizeof(struct object_entry*));
256 for (e = pool_start; e != pool_next; e++)
258 for (o = overflow; o; o = o->next)
260 last = idx + object_count;
261 qsort(idx, object_count, sizeof(struct object_entry*), oecmp);
263 /* Generate the fan-out array. */
265 for (i = 0; i < 256; i++) {
266 struct object_entry **next = c;;
267 while (next < last) {
268 if ((*next)->sha1[0] != i)
272 array[i] = htonl(next - idx);
276 f = sha1create("%s", idx_name);
277 sha1write(f, array, 256 * sizeof(int));
278 for (c = idx; c != last; c++) {
279 unsigned int offset = htonl((*c)->offset);
280 sha1write(f, &offset, 4);
281 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
283 sha1write(f, packsha1, sizeof(packsha1));
284 sha1close(f, NULL, 1);
288 int main(int argc, const char **argv)
290 const char *base_name = argv[1];
291 int est_obj_cnt = atoi(argv[2]);
295 pack_name = xmalloc(strlen(base_name) + 6);
296 sprintf(pack_name, "%s.pack", base_name);
297 idx_name = xmalloc(strlen(base_name) + 5);
298 sprintf(idx_name, "%s.idx", base_name);
300 packfd = open(pack_name, O_RDWR|O_CREAT|O_TRUNC, 0666);
302 die("Can't create pack file %s: %s", pack_name, strerror(errno));
304 pool_start = xmalloc(est_obj_cnt * sizeof(struct object_entry));
305 pool_next = pool_start;
306 pool_end = pool_start + est_obj_cnt;
310 unsigned long datlen;
314 unsigned char sha1[20];
316 struct object_entry *e;
318 if (yread(0, &datlen, 4) != 4)
322 dat = xmalloc(datlen);
323 if (yread(0, dat, datlen) != datlen)
326 hdrlen = sprintf(hdr, "blob %lu", datlen) + 1;
328 SHA1_Update(&c, hdr, hdrlen);
329 SHA1_Update(&c, dat, datlen);
330 SHA1_Final(sha1, &c);
332 e = insert_object(sha1);
335 write_blob(dat, datlen);
337 printf("%s\n", sha1_to_hex(sha1));
344 memcpy(lastsha1, sha1, sizeof(sha1));
350 fixup_header_footer();
352 write_index(idx_name);
354 fprintf(stderr, "%lu objects, %lu duplicates, %lu pool overflow\n",
355 object_count, duplicate_count, overflow_count);