]>
Commit | Line | Data |
---|---|---|
9cf6d335 SV |
1 | #include "cache.h" |
2 | #include "delta.h" | |
3 | #include "pack.h" | |
4 | #include "csum-file.h" | |
8e440259 PE |
5 | #include "blob.h" |
6 | #include "commit.h" | |
7 | #include "tag.h" | |
8 | #include "tree.h" | |
9cf6d335 SV |
9 | |
10 | static const char index_pack_usage[] = | |
e42797f5 | 11 | "git-index-pack [-o <index-file>] { <pack-file> | --stdin [<pack-file>] }"; |
9cf6d335 SV |
12 | |
13 | struct object_entry | |
14 | { | |
15 | unsigned long offset; | |
2d477051 NP |
16 | unsigned long size; |
17 | unsigned int hdr_size; | |
9cf6d335 SV |
18 | enum object_type type; |
19 | enum object_type real_type; | |
20 | unsigned char sha1[20]; | |
21 | }; | |
22 | ||
53dda6ff NP |
23 | union delta_base { |
24 | unsigned char sha1[20]; | |
25 | unsigned long offset; | |
26 | }; | |
27 | ||
3c552873 NP |
28 | /* |
29 | * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want | |
30 | * to memcmp() only the first 20 bytes. | |
31 | */ | |
32 | #define UNION_BASE_SZ 20 | |
33 | ||
9cf6d335 SV |
34 | struct delta_entry |
35 | { | |
36 | struct object_entry *obj; | |
53dda6ff | 37 | union delta_base base; |
9cf6d335 SV |
38 | }; |
39 | ||
9cf6d335 SV |
40 | static struct object_entry *objects; |
41 | static struct delta_entry *deltas; | |
42 | static int nr_objects; | |
43 | static int nr_deltas; | |
44 | ||
e42797f5 NP |
45 | static int from_stdin; |
46 | ||
2d477051 NP |
47 | /* We always read in 4kB chunks. */ |
48 | static unsigned char input_buffer[4096]; | |
49 | static unsigned long input_offset, input_len, consumed_bytes; | |
50 | static SHA_CTX input_ctx; | |
e42797f5 | 51 | static int input_fd, output_fd, mmap_fd; |
2d477051 NP |
52 | |
53 | /* | |
54 | * Make sure at least "min" bytes are available in the buffer, and | |
55 | * return the pointer to the buffer. | |
56 | */ | |
57 | static void * fill(int min) | |
9cf6d335 | 58 | { |
2d477051 NP |
59 | if (min <= input_len) |
60 | return input_buffer + input_offset; | |
61 | if (min > sizeof(input_buffer)) | |
62 | die("cannot fill %d bytes", min); | |
63 | if (input_offset) { | |
e42797f5 NP |
64 | if (output_fd >= 0) |
65 | write_or_die(output_fd, input_buffer, input_offset); | |
2d477051 NP |
66 | SHA1_Update(&input_ctx, input_buffer, input_offset); |
67 | memcpy(input_buffer, input_buffer + input_offset, input_len); | |
68 | input_offset = 0; | |
69 | } | |
70 | do { | |
71 | int ret = xread(input_fd, input_buffer + input_len, | |
72 | sizeof(input_buffer) - input_len); | |
73 | if (ret <= 0) { | |
74 | if (!ret) | |
75 | die("early EOF"); | |
76 | die("read error on input: %s", strerror(errno)); | |
77 | } | |
78 | input_len += ret; | |
79 | } while (input_len < min); | |
80 | return input_buffer; | |
81 | } | |
82 | ||
83 | static void use(int bytes) | |
84 | { | |
85 | if (bytes > input_len) | |
86 | die("used more bytes than were available"); | |
87 | input_len -= bytes; | |
88 | input_offset += bytes; | |
89 | consumed_bytes += bytes; | |
90 | } | |
9cf6d335 | 91 | |
e42797f5 | 92 | static const char * open_pack_file(const char *pack_name) |
2d477051 | 93 | { |
e42797f5 NP |
94 | if (from_stdin) { |
95 | input_fd = 0; | |
96 | if (!pack_name) { | |
97 | static char tmpfile[PATH_MAX]; | |
98 | snprintf(tmpfile, sizeof(tmpfile), | |
99 | "%s/pack_XXXXXX", get_object_directory()); | |
100 | output_fd = mkstemp(tmpfile); | |
101 | pack_name = xstrdup(tmpfile); | |
102 | } else | |
103 | output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); | |
104 | if (output_fd < 0) | |
105 | die("unable to create %s: %s\n", pack_name, strerror(errno)); | |
106 | mmap_fd = output_fd; | |
107 | } else { | |
108 | input_fd = open(pack_name, O_RDONLY); | |
109 | if (input_fd < 0) | |
110 | die("cannot open packfile '%s': %s", | |
111 | pack_name, strerror(errno)); | |
112 | output_fd = -1; | |
113 | mmap_fd = input_fd; | |
114 | } | |
2d477051 | 115 | SHA1_Init(&input_ctx); |
e42797f5 | 116 | return pack_name; |
9cf6d335 SV |
117 | } |
118 | ||
119 | static void parse_pack_header(void) | |
120 | { | |
2d477051 | 121 | struct pack_header *hdr = fill(sizeof(struct pack_header)); |
9cf6d335 SV |
122 | |
123 | /* Header consistency check */ | |
9cf6d335 | 124 | if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) |
e42797f5 | 125 | die("pack signature mismatch"); |
d60fc1c8 | 126 | if (!pack_version_ok(hdr->hdr_version)) |
e42797f5 | 127 | die("pack version %d unsupported", ntohl(hdr->hdr_version)); |
9cf6d335 SV |
128 | |
129 | nr_objects = ntohl(hdr->hdr_entries); | |
2d477051 NP |
130 | use(sizeof(struct pack_header)); |
131 | /*fprintf(stderr, "Indexing %d objects\n", nr_objects);*/ | |
9cf6d335 SV |
132 | } |
133 | ||
134 | static void bad_object(unsigned long offset, const char *format, | |
135 | ...) NORETURN __attribute__((format (printf, 2, 3))); | |
136 | ||
137 | static void bad_object(unsigned long offset, const char *format, ...) | |
138 | { | |
139 | va_list params; | |
140 | char buf[1024]; | |
141 | ||
142 | va_start(params, format); | |
143 | vsnprintf(buf, sizeof(buf), format, params); | |
144 | va_end(params); | |
e42797f5 | 145 | die("pack has bad object at offset %lu: %s", offset, buf); |
9cf6d335 SV |
146 | } |
147 | ||
2d477051 | 148 | static void *unpack_entry_data(unsigned long offset, unsigned long size) |
9cf6d335 | 149 | { |
9cf6d335 SV |
150 | z_stream stream; |
151 | void *buf = xmalloc(size); | |
152 | ||
153 | memset(&stream, 0, sizeof(stream)); | |
154 | stream.next_out = buf; | |
155 | stream.avail_out = size; | |
2d477051 NP |
156 | stream.next_in = fill(1); |
157 | stream.avail_in = input_len; | |
9cf6d335 SV |
158 | inflateInit(&stream); |
159 | ||
160 | for (;;) { | |
161 | int ret = inflate(&stream, 0); | |
2d477051 NP |
162 | use(input_len - stream.avail_in); |
163 | if (stream.total_out == size && ret == Z_STREAM_END) | |
9cf6d335 SV |
164 | break; |
165 | if (ret != Z_OK) | |
166 | bad_object(offset, "inflate returned %d", ret); | |
2d477051 NP |
167 | stream.next_in = fill(1); |
168 | stream.avail_in = input_len; | |
9cf6d335 SV |
169 | } |
170 | inflateEnd(&stream); | |
9cf6d335 SV |
171 | return buf; |
172 | } | |
173 | ||
2d477051 | 174 | static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base) |
9cf6d335 | 175 | { |
2d477051 | 176 | unsigned char *p, c; |
53dda6ff | 177 | unsigned long size, base_offset; |
9cf6d335 | 178 | unsigned shift; |
9cf6d335 | 179 | |
2d477051 NP |
180 | obj->offset = consumed_bytes; |
181 | ||
182 | p = fill(1); | |
183 | c = *p; | |
184 | use(1); | |
185 | obj->type = (c >> 4) & 7; | |
9cf6d335 SV |
186 | size = (c & 15); |
187 | shift = 4; | |
188 | while (c & 0x80) { | |
2d477051 NP |
189 | p = fill(1); |
190 | c = *p; | |
191 | use(1); | |
9cf6d335 SV |
192 | size += (c & 0x7fUL) << shift; |
193 | shift += 7; | |
194 | } | |
2d477051 | 195 | obj->size = size; |
9cf6d335 | 196 | |
2d477051 | 197 | switch (obj->type) { |
eb32d236 | 198 | case OBJ_REF_DELTA: |
2d477051 NP |
199 | hashcpy(delta_base->sha1, fill(20)); |
200 | use(20); | |
53dda6ff NP |
201 | break; |
202 | case OBJ_OFS_DELTA: | |
203 | memset(delta_base, 0, sizeof(*delta_base)); | |
2d477051 NP |
204 | p = fill(1); |
205 | c = *p; | |
206 | use(1); | |
53dda6ff NP |
207 | base_offset = c & 127; |
208 | while (c & 128) { | |
209 | base_offset += 1; | |
210 | if (!base_offset || base_offset & ~(~0UL >> 7)) | |
2d477051 NP |
211 | bad_object(obj->offset, "offset value overflow for delta base object"); |
212 | p = fill(1); | |
213 | c = *p; | |
214 | use(1); | |
53dda6ff NP |
215 | base_offset = (base_offset << 7) + (c & 127); |
216 | } | |
2d477051 NP |
217 | delta_base->offset = obj->offset - base_offset; |
218 | if (delta_base->offset >= obj->offset) | |
219 | bad_object(obj->offset, "delta base offset is out of bound"); | |
53dda6ff | 220 | break; |
9cf6d335 SV |
221 | case OBJ_COMMIT: |
222 | case OBJ_TREE: | |
223 | case OBJ_BLOB: | |
224 | case OBJ_TAG: | |
9cf6d335 SV |
225 | break; |
226 | default: | |
2d477051 | 227 | bad_object(obj->offset, "bad object type %d", obj->type); |
9cf6d335 | 228 | } |
2d477051 NP |
229 | obj->hdr_size = consumed_bytes - obj->offset; |
230 | ||
231 | return unpack_entry_data(obj->offset, obj->size); | |
232 | } | |
233 | ||
234 | static void * get_data_from_pack(struct object_entry *obj) | |
235 | { | |
236 | unsigned long from = obj[0].offset + obj[0].hdr_size; | |
237 | unsigned long len = obj[1].offset - from; | |
238 | unsigned pg_offset = from % getpagesize(); | |
239 | unsigned char *map, *data; | |
240 | z_stream stream; | |
241 | int st; | |
9cf6d335 | 242 | |
2d477051 | 243 | map = mmap(NULL, len + pg_offset, PROT_READ, MAP_PRIVATE, |
e42797f5 | 244 | mmap_fd, from - pg_offset); |
2d477051 | 245 | if (map == MAP_FAILED) |
e42797f5 | 246 | die("cannot mmap pack file: %s", strerror(errno)); |
2d477051 NP |
247 | data = xmalloc(obj->size); |
248 | memset(&stream, 0, sizeof(stream)); | |
249 | stream.next_out = data; | |
250 | stream.avail_out = obj->size; | |
251 | stream.next_in = map + pg_offset; | |
252 | stream.avail_in = len; | |
253 | inflateInit(&stream); | |
254 | while ((st = inflate(&stream, Z_FINISH)) == Z_OK); | |
255 | inflateEnd(&stream); | |
256 | if (st != Z_STREAM_END || stream.total_out != obj->size) | |
257 | die("serious inflate inconsistency"); | |
258 | munmap(map, len + pg_offset); | |
9cf6d335 SV |
259 | return data; |
260 | } | |
261 | ||
53dda6ff | 262 | static int find_delta(const union delta_base *base) |
9cf6d335 SV |
263 | { |
264 | int first = 0, last = nr_deltas; | |
265 | ||
266 | while (first < last) { | |
267 | int next = (first + last) / 2; | |
268 | struct delta_entry *delta = &deltas[next]; | |
269 | int cmp; | |
270 | ||
3c552873 | 271 | cmp = memcmp(base, &delta->base, UNION_BASE_SZ); |
9cf6d335 SV |
272 | if (!cmp) |
273 | return next; | |
274 | if (cmp < 0) { | |
275 | last = next; | |
276 | continue; | |
277 | } | |
278 | first = next+1; | |
279 | } | |
280 | return -first-1; | |
281 | } | |
282 | ||
53dda6ff NP |
283 | static int find_delta_childs(const union delta_base *base, |
284 | int *first_index, int *last_index) | |
9cf6d335 | 285 | { |
53dda6ff | 286 | int first = find_delta(base); |
9cf6d335 SV |
287 | int last = first; |
288 | int end = nr_deltas - 1; | |
289 | ||
290 | if (first < 0) | |
291 | return -1; | |
3c552873 | 292 | while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ)) |
9cf6d335 | 293 | --first; |
3c552873 | 294 | while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ)) |
9cf6d335 SV |
295 | ++last; |
296 | *first_index = first; | |
297 | *last_index = last; | |
298 | return 0; | |
299 | } | |
300 | ||
301 | static void sha1_object(const void *data, unsigned long size, | |
302 | enum object_type type, unsigned char *sha1) | |
303 | { | |
304 | SHA_CTX ctx; | |
305 | char header[50]; | |
306 | int header_size; | |
307 | const char *type_str; | |
308 | ||
309 | switch (type) { | |
8e440259 PE |
310 | case OBJ_COMMIT: type_str = commit_type; break; |
311 | case OBJ_TREE: type_str = tree_type; break; | |
312 | case OBJ_BLOB: type_str = blob_type; break; | |
313 | case OBJ_TAG: type_str = tag_type; break; | |
9cf6d335 SV |
314 | default: |
315 | die("bad type %d", type); | |
316 | } | |
317 | ||
318 | header_size = sprintf(header, "%s %lu", type_str, size) + 1; | |
319 | ||
320 | SHA1_Init(&ctx); | |
321 | SHA1_Update(&ctx, header, header_size); | |
322 | SHA1_Update(&ctx, data, size); | |
323 | SHA1_Final(sha1, &ctx); | |
324 | } | |
325 | ||
326 | static void resolve_delta(struct delta_entry *delta, void *base_data, | |
327 | unsigned long base_size, enum object_type type) | |
328 | { | |
329 | struct object_entry *obj = delta->obj; | |
330 | void *delta_data; | |
331 | unsigned long delta_size; | |
332 | void *result; | |
333 | unsigned long result_size; | |
53dda6ff | 334 | union delta_base delta_base; |
9cf6d335 SV |
335 | int j, first, last; |
336 | ||
337 | obj->real_type = type; | |
2d477051 NP |
338 | delta_data = get_data_from_pack(obj); |
339 | delta_size = obj->size; | |
9cf6d335 SV |
340 | result = patch_delta(base_data, base_size, delta_data, delta_size, |
341 | &result_size); | |
342 | free(delta_data); | |
343 | if (!result) | |
344 | bad_object(obj->offset, "failed to apply delta"); | |
345 | sha1_object(result, result_size, type, obj->sha1); | |
53dda6ff NP |
346 | |
347 | hashcpy(delta_base.sha1, obj->sha1); | |
348 | if (!find_delta_childs(&delta_base, &first, &last)) { | |
349 | for (j = first; j <= last; j++) | |
350 | if (deltas[j].obj->type == OBJ_REF_DELTA) | |
351 | resolve_delta(&deltas[j], result, result_size, type); | |
352 | } | |
353 | ||
354 | memset(&delta_base, 0, sizeof(delta_base)); | |
355 | delta_base.offset = obj->offset; | |
356 | if (!find_delta_childs(&delta_base, &first, &last)) { | |
9cf6d335 | 357 | for (j = first; j <= last; j++) |
53dda6ff NP |
358 | if (deltas[j].obj->type == OBJ_OFS_DELTA) |
359 | resolve_delta(&deltas[j], result, result_size, type); | |
9cf6d335 | 360 | } |
53dda6ff | 361 | |
9cf6d335 SV |
362 | free(result); |
363 | } | |
364 | ||
365 | static int compare_delta_entry(const void *a, const void *b) | |
366 | { | |
367 | const struct delta_entry *delta_a = a; | |
368 | const struct delta_entry *delta_b = b; | |
3c552873 | 369 | return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ); |
9cf6d335 SV |
370 | } |
371 | ||
2d477051 NP |
372 | /* Parse all objects and return the pack content SHA1 hash */ |
373 | static void parse_pack_objects(unsigned char *sha1) | |
9cf6d335 SV |
374 | { |
375 | int i; | |
53dda6ff | 376 | struct delta_entry *delta = deltas; |
9cf6d335 | 377 | void *data; |
2d477051 | 378 | struct stat st; |
9cf6d335 SV |
379 | |
380 | /* | |
381 | * First pass: | |
382 | * - find locations of all objects; | |
383 | * - calculate SHA1 of all non-delta objects; | |
384 | * - remember base SHA1 for all deltas. | |
385 | */ | |
386 | for (i = 0; i < nr_objects; i++) { | |
387 | struct object_entry *obj = &objects[i]; | |
2d477051 | 388 | data = unpack_raw_entry(obj, &delta->base); |
9cf6d335 | 389 | obj->real_type = obj->type; |
53dda6ff NP |
390 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) { |
391 | nr_deltas++; | |
9cf6d335 | 392 | delta->obj = obj; |
53dda6ff | 393 | delta++; |
9cf6d335 | 394 | } else |
2d477051 | 395 | sha1_object(data, obj->size, obj->type, obj->sha1); |
9cf6d335 SV |
396 | free(data); |
397 | } | |
2d477051 NP |
398 | objects[i].offset = consumed_bytes; |
399 | ||
400 | /* Check pack integrity */ | |
401 | SHA1_Update(&input_ctx, input_buffer, input_offset); | |
402 | SHA1_Final(sha1, &input_ctx); | |
403 | if (hashcmp(fill(20), sha1)) | |
e42797f5 | 404 | die("pack is corrupted (SHA1 mismatch)"); |
2d477051 | 405 | use(20); |
e42797f5 NP |
406 | if (output_fd >= 0) |
407 | write_or_die(output_fd, input_buffer, input_offset); | |
2d477051 NP |
408 | |
409 | /* If input_fd is a file, we should have reached its end now. */ | |
410 | if (fstat(input_fd, &st)) | |
e42797f5 | 411 | die("cannot fstat packfile: %s", strerror(errno)); |
2d477051 | 412 | if (S_ISREG(st.st_mode) && st.st_size != consumed_bytes) |
e42797f5 | 413 | die("pack has junk at the end"); |
9cf6d335 | 414 | |
53dda6ff | 415 | /* Sort deltas by base SHA1/offset for fast searching */ |
9cf6d335 SV |
416 | qsort(deltas, nr_deltas, sizeof(struct delta_entry), |
417 | compare_delta_entry); | |
418 | ||
419 | /* | |
420 | * Second pass: | |
421 | * - for all non-delta objects, look if it is used as a base for | |
422 | * deltas; | |
423 | * - if used as a base, uncompress the object and apply all deltas, | |
424 | * recursively checking if the resulting object is used as a base | |
425 | * for some more deltas. | |
426 | */ | |
427 | for (i = 0; i < nr_objects; i++) { | |
428 | struct object_entry *obj = &objects[i]; | |
53dda6ff NP |
429 | union delta_base base; |
430 | int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last; | |
9cf6d335 | 431 | |
53dda6ff | 432 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) |
9cf6d335 | 433 | continue; |
53dda6ff NP |
434 | hashcpy(base.sha1, obj->sha1); |
435 | ref = !find_delta_childs(&base, &ref_first, &ref_last); | |
436 | memset(&base, 0, sizeof(base)); | |
437 | base.offset = obj->offset; | |
438 | ofs = !find_delta_childs(&base, &ofs_first, &ofs_last); | |
439 | if (!ref && !ofs) | |
9cf6d335 | 440 | continue; |
2d477051 | 441 | data = get_data_from_pack(obj); |
53dda6ff NP |
442 | if (ref) |
443 | for (j = ref_first; j <= ref_last; j++) | |
444 | if (deltas[j].obj->type == OBJ_REF_DELTA) | |
445 | resolve_delta(&deltas[j], data, | |
2d477051 | 446 | obj->size, obj->type); |
53dda6ff NP |
447 | if (ofs) |
448 | for (j = ofs_first; j <= ofs_last; j++) | |
449 | if (deltas[j].obj->type == OBJ_OFS_DELTA) | |
450 | resolve_delta(&deltas[j], data, | |
2d477051 | 451 | obj->size, obj->type); |
9cf6d335 SV |
452 | free(data); |
453 | } | |
454 | ||
455 | /* Check for unresolved deltas */ | |
456 | for (i = 0; i < nr_deltas; i++) { | |
53dda6ff NP |
457 | if (deltas[i].obj->real_type == OBJ_REF_DELTA || |
458 | deltas[i].obj->real_type == OBJ_OFS_DELTA) | |
e42797f5 | 459 | die("pack has unresolved deltas"); |
9cf6d335 SV |
460 | } |
461 | } | |
462 | ||
463 | static int sha1_compare(const void *_a, const void *_b) | |
464 | { | |
465 | struct object_entry *a = *(struct object_entry **)_a; | |
466 | struct object_entry *b = *(struct object_entry **)_b; | |
a89fccd2 | 467 | return hashcmp(a->sha1, b->sha1); |
9cf6d335 SV |
468 | } |
469 | ||
2d477051 NP |
470 | /* |
471 | * On entry *sha1 contains the pack content SHA1 hash, on exit it is | |
472 | * the SHA1 hash of sorted object names. | |
473 | */ | |
e42797f5 | 474 | static const char * write_index_file(const char *index_name, unsigned char *sha1) |
9cf6d335 SV |
475 | { |
476 | struct sha1file *f; | |
7e4a2a84 | 477 | struct object_entry **sorted_by_sha, **list, **last; |
9cf6d335 | 478 | unsigned int array[256]; |
e42797f5 | 479 | int i, fd; |
84c8d8ae | 480 | SHA_CTX ctx; |
9cf6d335 | 481 | |
7e4a2a84 JH |
482 | if (nr_objects) { |
483 | sorted_by_sha = | |
484 | xcalloc(nr_objects, sizeof(struct object_entry *)); | |
485 | list = sorted_by_sha; | |
486 | last = sorted_by_sha + nr_objects; | |
487 | for (i = 0; i < nr_objects; ++i) | |
488 | sorted_by_sha[i] = &objects[i]; | |
489 | qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]), | |
490 | sha1_compare); | |
491 | ||
492 | } | |
493 | else | |
494 | sorted_by_sha = list = last = NULL; | |
9cf6d335 | 495 | |
e42797f5 NP |
496 | if (!index_name) { |
497 | static char tmpfile[PATH_MAX]; | |
498 | snprintf(tmpfile, sizeof(tmpfile), | |
499 | "%s/index_XXXXXX", get_object_directory()); | |
500 | fd = mkstemp(tmpfile); | |
501 | index_name = xstrdup(tmpfile); | |
502 | } else { | |
503 | unlink(index_name); | |
504 | fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600); | |
505 | } | |
506 | if (fd < 0) | |
507 | die("unable to create %s: %s", index_name, strerror(errno)); | |
508 | f = sha1fd(fd, index_name); | |
9cf6d335 SV |
509 | |
510 | /* | |
511 | * Write the first-level table (the list is sorted, | |
512 | * but we use a 256-entry lookup to be able to avoid | |
513 | * having to do eight extra binary search iterations). | |
514 | */ | |
515 | for (i = 0; i < 256; i++) { | |
516 | struct object_entry **next = list; | |
517 | while (next < last) { | |
518 | struct object_entry *obj = *next; | |
519 | if (obj->sha1[0] != i) | |
520 | break; | |
521 | next++; | |
522 | } | |
523 | array[i] = htonl(next - sorted_by_sha); | |
524 | list = next; | |
525 | } | |
526 | sha1write(f, array, 256 * sizeof(int)); | |
527 | ||
84c8d8ae JH |
528 | /* recompute the SHA1 hash of sorted object names. |
529 | * currently pack-objects does not do this, but that | |
530 | * can be fixed. | |
531 | */ | |
532 | SHA1_Init(&ctx); | |
9cf6d335 SV |
533 | /* |
534 | * Write the actual SHA1 entries.. | |
535 | */ | |
536 | list = sorted_by_sha; | |
537 | for (i = 0; i < nr_objects; i++) { | |
538 | struct object_entry *obj = *list++; | |
539 | unsigned int offset = htonl(obj->offset); | |
540 | sha1write(f, &offset, 4); | |
541 | sha1write(f, obj->sha1, 20); | |
84c8d8ae | 542 | SHA1_Update(&ctx, obj->sha1, 20); |
9cf6d335 | 543 | } |
2d477051 | 544 | sha1write(f, sha1, 20); |
9cf6d335 SV |
545 | sha1close(f, NULL, 1); |
546 | free(sorted_by_sha); | |
84c8d8ae | 547 | SHA1_Final(sha1, &ctx); |
e42797f5 NP |
548 | return index_name; |
549 | } | |
550 | ||
551 | static void final(const char *final_pack_name, const char *curr_pack_name, | |
552 | const char *final_index_name, const char *curr_index_name, | |
553 | unsigned char *sha1) | |
554 | { | |
555 | char name[PATH_MAX]; | |
556 | int err; | |
557 | ||
558 | if (!from_stdin) { | |
559 | close(input_fd); | |
560 | } else { | |
561 | err = close(output_fd); | |
562 | if (err) | |
563 | die("error while closing pack file: %s", strerror(errno)); | |
564 | chmod(curr_pack_name, 0444); | |
565 | } | |
566 | ||
567 | if (final_pack_name != curr_pack_name) { | |
568 | if (!final_pack_name) { | |
569 | snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", | |
570 | get_object_directory(), sha1_to_hex(sha1)); | |
571 | final_pack_name = name; | |
572 | } | |
573 | if (move_temp_to_file(curr_pack_name, final_pack_name)) | |
574 | die("cannot store pack file"); | |
575 | } | |
576 | ||
577 | chmod(curr_index_name, 0444); | |
578 | if (final_index_name != curr_index_name) { | |
579 | if (!final_index_name) { | |
580 | snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", | |
581 | get_object_directory(), sha1_to_hex(sha1)); | |
582 | final_index_name = name; | |
583 | } | |
584 | if (move_temp_to_file(curr_index_name, final_index_name)) | |
585 | die("cannot store index file"); | |
586 | } | |
9cf6d335 SV |
587 | } |
588 | ||
589 | int main(int argc, char **argv) | |
590 | { | |
591 | int i; | |
e42797f5 NP |
592 | const char *curr_pack, *pack_name = NULL; |
593 | const char *curr_index, *index_name = NULL; | |
9cf6d335 | 594 | char *index_name_buf = NULL; |
84c8d8ae | 595 | unsigned char sha1[20]; |
9cf6d335 SV |
596 | |
597 | for (i = 1; i < argc; i++) { | |
598 | const char *arg = argv[i]; | |
599 | ||
600 | if (*arg == '-') { | |
e42797f5 NP |
601 | if (!strcmp(arg, "--stdin")) { |
602 | from_stdin = 1; | |
603 | } else if (!strcmp(arg, "-o")) { | |
9cf6d335 SV |
604 | if (index_name || (i+1) >= argc) |
605 | usage(index_pack_usage); | |
606 | index_name = argv[++i]; | |
607 | } else | |
608 | usage(index_pack_usage); | |
609 | continue; | |
610 | } | |
611 | ||
612 | if (pack_name) | |
613 | usage(index_pack_usage); | |
614 | pack_name = arg; | |
615 | } | |
616 | ||
e42797f5 | 617 | if (!pack_name && !from_stdin) |
9cf6d335 | 618 | usage(index_pack_usage); |
e42797f5 | 619 | if (!index_name && pack_name) { |
9cf6d335 | 620 | int len = strlen(pack_name); |
5bb1cda5 | 621 | if (!has_extension(pack_name, ".pack")) |
9cf6d335 SV |
622 | die("packfile name '%s' does not end with '.pack'", |
623 | pack_name); | |
6689f087 | 624 | index_name_buf = xmalloc(len); |
9cf6d335 SV |
625 | memcpy(index_name_buf, pack_name, len - 5); |
626 | strcpy(index_name_buf + len - 5, ".idx"); | |
627 | index_name = index_name_buf; | |
628 | } | |
629 | ||
e42797f5 | 630 | curr_pack = open_pack_file(pack_name); |
9cf6d335 | 631 | parse_pack_header(); |
2d477051 | 632 | objects = xcalloc(nr_objects + 1, sizeof(struct object_entry)); |
9cf6d335 | 633 | deltas = xcalloc(nr_objects, sizeof(struct delta_entry)); |
2d477051 | 634 | parse_pack_objects(sha1); |
9cf6d335 | 635 | free(deltas); |
e42797f5 NP |
636 | curr_index = write_index_file(index_name, sha1); |
637 | final(pack_name, curr_pack, index_name, curr_index, sha1); | |
9cf6d335 SV |
638 | free(objects); |
639 | free(index_name_buf); | |
640 | ||
84c8d8ae JH |
641 | printf("%s\n", sha1_to_hex(sha1)); |
642 | ||
9cf6d335 SV |
643 | return 0; |
644 | } |