]>
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[] = | |
b8077709 | 11 | "git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] { <pack-file> | --stdin [--fix-thin] [<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 | { | |
53dda6ff | 36 | union delta_base base; |
636171cb | 37 | int obj_no; |
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; | |
636171cb | 44 | static int nr_resolved_deltas; |
9cf6d335 | 45 | |
e42797f5 | 46 | static int from_stdin; |
3c9af366 NP |
47 | static int verbose; |
48 | ||
49 | static volatile sig_atomic_t progress_update; | |
50 | ||
51 | static void progress_interval(int signum) | |
52 | { | |
53 | progress_update = 1; | |
54 | } | |
55 | ||
56 | static void setup_progress_signal(void) | |
57 | { | |
58 | struct sigaction sa; | |
59 | struct itimerval v; | |
60 | ||
61 | memset(&sa, 0, sizeof(sa)); | |
62 | sa.sa_handler = progress_interval; | |
63 | sigemptyset(&sa.sa_mask); | |
64 | sa.sa_flags = SA_RESTART; | |
65 | sigaction(SIGALRM, &sa, NULL); | |
66 | ||
67 | v.it_interval.tv_sec = 1; | |
68 | v.it_interval.tv_usec = 0; | |
69 | v.it_value = v.it_interval; | |
70 | setitimer(ITIMER_REAL, &v, NULL); | |
71 | ||
72 | } | |
73 | ||
74 | static unsigned display_progress(unsigned n, unsigned total, unsigned last_pc) | |
75 | { | |
76 | unsigned percent = n * 100 / total; | |
77 | if (percent != last_pc || progress_update) { | |
78 | fprintf(stderr, "%4u%% (%u/%u) done\r", percent, n, total); | |
79 | progress_update = 0; | |
80 | } | |
81 | return percent; | |
82 | } | |
e42797f5 | 83 | |
2d477051 NP |
84 | /* We always read in 4kB chunks. */ |
85 | static unsigned char input_buffer[4096]; | |
86 | static unsigned long input_offset, input_len, consumed_bytes; | |
87 | static SHA_CTX input_ctx; | |
6d2fa7f1 | 88 | static int input_fd, output_fd, pack_fd; |
2d477051 | 89 | |
636171cb | 90 | /* Discard current buffer used content. */ |
a6e8a767 | 91 | static void flush(void) |
636171cb NP |
92 | { |
93 | if (input_offset) { | |
94 | if (output_fd >= 0) | |
95 | write_or_die(output_fd, input_buffer, input_offset); | |
96 | SHA1_Update(&input_ctx, input_buffer, input_offset); | |
554a2636 | 97 | memmove(input_buffer, input_buffer + input_offset, input_len); |
636171cb NP |
98 | input_offset = 0; |
99 | } | |
100 | } | |
101 | ||
2d477051 NP |
102 | /* |
103 | * Make sure at least "min" bytes are available in the buffer, and | |
104 | * return the pointer to the buffer. | |
105 | */ | |
b89c4e93 | 106 | static void *fill(int min) |
9cf6d335 | 107 | { |
2d477051 NP |
108 | if (min <= input_len) |
109 | return input_buffer + input_offset; | |
110 | if (min > sizeof(input_buffer)) | |
111 | die("cannot fill %d bytes", min); | |
636171cb | 112 | flush(); |
2d477051 NP |
113 | do { |
114 | int ret = xread(input_fd, input_buffer + input_len, | |
115 | sizeof(input_buffer) - input_len); | |
116 | if (ret <= 0) { | |
117 | if (!ret) | |
118 | die("early EOF"); | |
119 | die("read error on input: %s", strerror(errno)); | |
120 | } | |
121 | input_len += ret; | |
122 | } while (input_len < min); | |
123 | return input_buffer; | |
124 | } | |
125 | ||
126 | static void use(int bytes) | |
127 | { | |
128 | if (bytes > input_len) | |
129 | die("used more bytes than were available"); | |
130 | input_len -= bytes; | |
131 | input_offset += bytes; | |
132 | consumed_bytes += bytes; | |
133 | } | |
9cf6d335 | 134 | |
b89c4e93 | 135 | static const char *open_pack_file(const char *pack_name) |
2d477051 | 136 | { |
e42797f5 NP |
137 | if (from_stdin) { |
138 | input_fd = 0; | |
139 | if (!pack_name) { | |
140 | static char tmpfile[PATH_MAX]; | |
141 | snprintf(tmpfile, sizeof(tmpfile), | |
0e55181f | 142 | "%s/tmp_pack_XXXXXX", get_object_directory()); |
e42797f5 NP |
143 | output_fd = mkstemp(tmpfile); |
144 | pack_name = xstrdup(tmpfile); | |
145 | } else | |
146 | output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); | |
147 | if (output_fd < 0) | |
148 | die("unable to create %s: %s\n", pack_name, strerror(errno)); | |
6d2fa7f1 | 149 | pack_fd = output_fd; |
e42797f5 NP |
150 | } else { |
151 | input_fd = open(pack_name, O_RDONLY); | |
152 | if (input_fd < 0) | |
153 | die("cannot open packfile '%s': %s", | |
154 | pack_name, strerror(errno)); | |
155 | output_fd = -1; | |
6d2fa7f1 | 156 | pack_fd = input_fd; |
e42797f5 | 157 | } |
2d477051 | 158 | SHA1_Init(&input_ctx); |
e42797f5 | 159 | return pack_name; |
9cf6d335 SV |
160 | } |
161 | ||
162 | static void parse_pack_header(void) | |
163 | { | |
2d477051 | 164 | struct pack_header *hdr = fill(sizeof(struct pack_header)); |
9cf6d335 SV |
165 | |
166 | /* Header consistency check */ | |
9cf6d335 | 167 | if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) |
e42797f5 | 168 | die("pack signature mismatch"); |
d60fc1c8 | 169 | if (!pack_version_ok(hdr->hdr_version)) |
e42797f5 | 170 | die("pack version %d unsupported", ntohl(hdr->hdr_version)); |
9cf6d335 SV |
171 | |
172 | nr_objects = ntohl(hdr->hdr_entries); | |
2d477051 | 173 | use(sizeof(struct pack_header)); |
9cf6d335 SV |
174 | } |
175 | ||
176 | static void bad_object(unsigned long offset, const char *format, | |
177 | ...) NORETURN __attribute__((format (printf, 2, 3))); | |
178 | ||
179 | static void bad_object(unsigned long offset, const char *format, ...) | |
180 | { | |
181 | va_list params; | |
182 | char buf[1024]; | |
183 | ||
184 | va_start(params, format); | |
185 | vsnprintf(buf, sizeof(buf), format, params); | |
186 | va_end(params); | |
e42797f5 | 187 | die("pack has bad object at offset %lu: %s", offset, buf); |
9cf6d335 SV |
188 | } |
189 | ||
2d477051 | 190 | static void *unpack_entry_data(unsigned long offset, unsigned long size) |
9cf6d335 | 191 | { |
9cf6d335 SV |
192 | z_stream stream; |
193 | void *buf = xmalloc(size); | |
194 | ||
195 | memset(&stream, 0, sizeof(stream)); | |
196 | stream.next_out = buf; | |
197 | stream.avail_out = size; | |
2d477051 NP |
198 | stream.next_in = fill(1); |
199 | stream.avail_in = input_len; | |
9cf6d335 SV |
200 | inflateInit(&stream); |
201 | ||
202 | for (;;) { | |
203 | int ret = inflate(&stream, 0); | |
2d477051 NP |
204 | use(input_len - stream.avail_in); |
205 | if (stream.total_out == size && ret == Z_STREAM_END) | |
9cf6d335 SV |
206 | break; |
207 | if (ret != Z_OK) | |
208 | bad_object(offset, "inflate returned %d", ret); | |
2d477051 NP |
209 | stream.next_in = fill(1); |
210 | stream.avail_in = input_len; | |
9cf6d335 SV |
211 | } |
212 | inflateEnd(&stream); | |
9cf6d335 SV |
213 | return buf; |
214 | } | |
215 | ||
2d477051 | 216 | static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base) |
9cf6d335 | 217 | { |
2d477051 | 218 | unsigned char *p, c; |
53dda6ff | 219 | unsigned long size, base_offset; |
9cf6d335 | 220 | unsigned shift; |
9cf6d335 | 221 | |
2d477051 NP |
222 | obj->offset = consumed_bytes; |
223 | ||
224 | p = fill(1); | |
225 | c = *p; | |
226 | use(1); | |
227 | obj->type = (c >> 4) & 7; | |
9cf6d335 SV |
228 | size = (c & 15); |
229 | shift = 4; | |
230 | while (c & 0x80) { | |
2d477051 NP |
231 | p = fill(1); |
232 | c = *p; | |
233 | use(1); | |
9cf6d335 SV |
234 | size += (c & 0x7fUL) << shift; |
235 | shift += 7; | |
236 | } | |
2d477051 | 237 | obj->size = size; |
9cf6d335 | 238 | |
2d477051 | 239 | switch (obj->type) { |
eb32d236 | 240 | case OBJ_REF_DELTA: |
2d477051 NP |
241 | hashcpy(delta_base->sha1, fill(20)); |
242 | use(20); | |
53dda6ff NP |
243 | break; |
244 | case OBJ_OFS_DELTA: | |
245 | memset(delta_base, 0, sizeof(*delta_base)); | |
2d477051 NP |
246 | p = fill(1); |
247 | c = *p; | |
248 | use(1); | |
53dda6ff NP |
249 | base_offset = c & 127; |
250 | while (c & 128) { | |
251 | base_offset += 1; | |
252 | if (!base_offset || base_offset & ~(~0UL >> 7)) | |
2d477051 NP |
253 | bad_object(obj->offset, "offset value overflow for delta base object"); |
254 | p = fill(1); | |
255 | c = *p; | |
256 | use(1); | |
53dda6ff NP |
257 | base_offset = (base_offset << 7) + (c & 127); |
258 | } | |
2d477051 NP |
259 | delta_base->offset = obj->offset - base_offset; |
260 | if (delta_base->offset >= obj->offset) | |
261 | bad_object(obj->offset, "delta base offset is out of bound"); | |
53dda6ff | 262 | break; |
9cf6d335 SV |
263 | case OBJ_COMMIT: |
264 | case OBJ_TREE: | |
265 | case OBJ_BLOB: | |
266 | case OBJ_TAG: | |
9cf6d335 SV |
267 | break; |
268 | default: | |
08a19d87 | 269 | bad_object(obj->offset, "unknown object type %d", obj->type); |
9cf6d335 | 270 | } |
2d477051 NP |
271 | obj->hdr_size = consumed_bytes - obj->offset; |
272 | ||
273 | return unpack_entry_data(obj->offset, obj->size); | |
274 | } | |
275 | ||
b89c4e93 | 276 | static void *get_data_from_pack(struct object_entry *obj) |
2d477051 NP |
277 | { |
278 | unsigned long from = obj[0].offset + obj[0].hdr_size; | |
279 | unsigned long len = obj[1].offset - from; | |
a91d49cd | 280 | unsigned long rdy = 0; |
6d2fa7f1 | 281 | unsigned char *src, *data; |
2d477051 NP |
282 | z_stream stream; |
283 | int st; | |
9cf6d335 | 284 | |
6d2fa7f1 | 285 | src = xmalloc(len); |
a91d49cd SP |
286 | data = src; |
287 | do { | |
288 | ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy); | |
289 | if (n <= 0) | |
290 | die("cannot pread pack file: %s", strerror(errno)); | |
291 | rdy += n; | |
292 | } while (rdy < len); | |
2d477051 NP |
293 | data = xmalloc(obj->size); |
294 | memset(&stream, 0, sizeof(stream)); | |
295 | stream.next_out = data; | |
296 | stream.avail_out = obj->size; | |
6d2fa7f1 | 297 | stream.next_in = src; |
2d477051 NP |
298 | stream.avail_in = len; |
299 | inflateInit(&stream); | |
300 | while ((st = inflate(&stream, Z_FINISH)) == Z_OK); | |
301 | inflateEnd(&stream); | |
302 | if (st != Z_STREAM_END || stream.total_out != obj->size) | |
303 | die("serious inflate inconsistency"); | |
6d2fa7f1 | 304 | free(src); |
9cf6d335 SV |
305 | return data; |
306 | } | |
307 | ||
53dda6ff | 308 | static int find_delta(const union delta_base *base) |
9cf6d335 SV |
309 | { |
310 | int first = 0, last = nr_deltas; | |
311 | ||
312 | while (first < last) { | |
313 | int next = (first + last) / 2; | |
314 | struct delta_entry *delta = &deltas[next]; | |
315 | int cmp; | |
316 | ||
3c552873 | 317 | cmp = memcmp(base, &delta->base, UNION_BASE_SZ); |
9cf6d335 SV |
318 | if (!cmp) |
319 | return next; | |
320 | if (cmp < 0) { | |
321 | last = next; | |
322 | continue; | |
323 | } | |
324 | first = next+1; | |
325 | } | |
326 | return -first-1; | |
327 | } | |
328 | ||
b89c4e93 NP |
329 | static int find_delta_children(const union delta_base *base, |
330 | int *first_index, int *last_index) | |
9cf6d335 | 331 | { |
53dda6ff | 332 | int first = find_delta(base); |
9cf6d335 SV |
333 | int last = first; |
334 | int end = nr_deltas - 1; | |
335 | ||
336 | if (first < 0) | |
337 | return -1; | |
3c552873 | 338 | while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ)) |
9cf6d335 | 339 | --first; |
3c552873 | 340 | while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ)) |
9cf6d335 SV |
341 | ++last; |
342 | *first_index = first; | |
343 | *last_index = last; | |
344 | return 0; | |
345 | } | |
346 | ||
347 | static void sha1_object(const void *data, unsigned long size, | |
9096c660 | 348 | enum object_type type, unsigned char *sha1) |
9cf6d335 | 349 | { |
ce9fbf16 | 350 | hash_sha1_file(data, size, typename(type), sha1); |
9096c660 | 351 | if (has_sha1_file(sha1)) { |
8685da42 NP |
352 | void *has_data; |
353 | enum object_type has_type; | |
354 | unsigned long has_size; | |
355 | has_data = read_sha1_file(sha1, &has_type, &has_size); | |
356 | if (!has_data) | |
357 | die("cannot read existing object %s", sha1_to_hex(sha1)); | |
358 | if (size != has_size || type != has_type || | |
359 | memcmp(data, has_data, size) != 0) | |
360 | die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1)); | |
361 | } | |
9cf6d335 SV |
362 | } |
363 | ||
636171cb | 364 | static void resolve_delta(struct object_entry *delta_obj, void *base_data, |
9cf6d335 SV |
365 | unsigned long base_size, enum object_type type) |
366 | { | |
9cf6d335 SV |
367 | void *delta_data; |
368 | unsigned long delta_size; | |
369 | void *result; | |
370 | unsigned long result_size; | |
53dda6ff | 371 | union delta_base delta_base; |
9cf6d335 SV |
372 | int j, first, last; |
373 | ||
636171cb NP |
374 | delta_obj->real_type = type; |
375 | delta_data = get_data_from_pack(delta_obj); | |
376 | delta_size = delta_obj->size; | |
9cf6d335 SV |
377 | result = patch_delta(base_data, base_size, delta_data, delta_size, |
378 | &result_size); | |
379 | free(delta_data); | |
380 | if (!result) | |
636171cb | 381 | bad_object(delta_obj->offset, "failed to apply delta"); |
9096c660 | 382 | sha1_object(result, result_size, type, delta_obj->sha1); |
636171cb | 383 | nr_resolved_deltas++; |
53dda6ff | 384 | |
636171cb | 385 | hashcpy(delta_base.sha1, delta_obj->sha1); |
b89c4e93 | 386 | if (!find_delta_children(&delta_base, &first, &last)) { |
636171cb NP |
387 | for (j = first; j <= last; j++) { |
388 | struct object_entry *child = objects + deltas[j].obj_no; | |
389 | if (child->real_type == OBJ_REF_DELTA) | |
390 | resolve_delta(child, result, result_size, type); | |
391 | } | |
53dda6ff NP |
392 | } |
393 | ||
394 | memset(&delta_base, 0, sizeof(delta_base)); | |
636171cb | 395 | delta_base.offset = delta_obj->offset; |
b89c4e93 | 396 | if (!find_delta_children(&delta_base, &first, &last)) { |
636171cb NP |
397 | for (j = first; j <= last; j++) { |
398 | struct object_entry *child = objects + deltas[j].obj_no; | |
399 | if (child->real_type == OBJ_OFS_DELTA) | |
400 | resolve_delta(child, result, result_size, type); | |
401 | } | |
9cf6d335 | 402 | } |
53dda6ff | 403 | |
9cf6d335 SV |
404 | free(result); |
405 | } | |
406 | ||
407 | static int compare_delta_entry(const void *a, const void *b) | |
408 | { | |
409 | const struct delta_entry *delta_a = a; | |
410 | const struct delta_entry *delta_b = b; | |
3c552873 | 411 | return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ); |
9cf6d335 SV |
412 | } |
413 | ||
2d477051 NP |
414 | /* Parse all objects and return the pack content SHA1 hash */ |
415 | static void parse_pack_objects(unsigned char *sha1) | |
9cf6d335 | 416 | { |
3c9af366 | 417 | int i, percent = -1; |
53dda6ff | 418 | struct delta_entry *delta = deltas; |
9cf6d335 | 419 | void *data; |
2d477051 | 420 | struct stat st; |
9cf6d335 SV |
421 | |
422 | /* | |
423 | * First pass: | |
424 | * - find locations of all objects; | |
425 | * - calculate SHA1 of all non-delta objects; | |
b89c4e93 | 426 | * - remember base (SHA1 or offset) for all deltas. |
9cf6d335 | 427 | */ |
3c9af366 NP |
428 | if (verbose) |
429 | fprintf(stderr, "Indexing %d objects.\n", nr_objects); | |
9cf6d335 SV |
430 | for (i = 0; i < nr_objects; i++) { |
431 | struct object_entry *obj = &objects[i]; | |
2d477051 | 432 | data = unpack_raw_entry(obj, &delta->base); |
9cf6d335 | 433 | obj->real_type = obj->type; |
53dda6ff NP |
434 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) { |
435 | nr_deltas++; | |
636171cb | 436 | delta->obj_no = i; |
53dda6ff | 437 | delta++; |
9cf6d335 | 438 | } else |
9096c660 | 439 | sha1_object(data, obj->size, obj->type, obj->sha1); |
9cf6d335 | 440 | free(data); |
3c9af366 NP |
441 | if (verbose) |
442 | percent = display_progress(i+1, nr_objects, percent); | |
9cf6d335 | 443 | } |
2d477051 | 444 | objects[i].offset = consumed_bytes; |
3c9af366 NP |
445 | if (verbose) |
446 | fputc('\n', stderr); | |
2d477051 NP |
447 | |
448 | /* Check pack integrity */ | |
636171cb | 449 | flush(); |
2d477051 NP |
450 | SHA1_Final(sha1, &input_ctx); |
451 | if (hashcmp(fill(20), sha1)) | |
e42797f5 | 452 | die("pack is corrupted (SHA1 mismatch)"); |
9bee2478 | 453 | use(20); |
2d477051 NP |
454 | |
455 | /* If input_fd is a file, we should have reached its end now. */ | |
456 | if (fstat(input_fd, &st)) | |
e42797f5 | 457 | die("cannot fstat packfile: %s", strerror(errno)); |
fa257b05 JS |
458 | if (S_ISREG(st.st_mode) && |
459 | lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size) | |
e42797f5 | 460 | die("pack has junk at the end"); |
9cf6d335 | 461 | |
3c9af366 NP |
462 | if (!nr_deltas) |
463 | return; | |
464 | ||
53dda6ff | 465 | /* Sort deltas by base SHA1/offset for fast searching */ |
9cf6d335 SV |
466 | qsort(deltas, nr_deltas, sizeof(struct delta_entry), |
467 | compare_delta_entry); | |
468 | ||
469 | /* | |
470 | * Second pass: | |
471 | * - for all non-delta objects, look if it is used as a base for | |
472 | * deltas; | |
473 | * - if used as a base, uncompress the object and apply all deltas, | |
474 | * recursively checking if the resulting object is used as a base | |
475 | * for some more deltas. | |
476 | */ | |
3c9af366 NP |
477 | if (verbose) |
478 | fprintf(stderr, "Resolving %d deltas.\n", nr_deltas); | |
9cf6d335 SV |
479 | for (i = 0; i < nr_objects; i++) { |
480 | struct object_entry *obj = &objects[i]; | |
53dda6ff NP |
481 | union delta_base base; |
482 | int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last; | |
9cf6d335 | 483 | |
53dda6ff | 484 | if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) |
9cf6d335 | 485 | continue; |
53dda6ff | 486 | hashcpy(base.sha1, obj->sha1); |
b89c4e93 | 487 | ref = !find_delta_children(&base, &ref_first, &ref_last); |
53dda6ff NP |
488 | memset(&base, 0, sizeof(base)); |
489 | base.offset = obj->offset; | |
b89c4e93 | 490 | ofs = !find_delta_children(&base, &ofs_first, &ofs_last); |
53dda6ff | 491 | if (!ref && !ofs) |
9cf6d335 | 492 | continue; |
2d477051 | 493 | data = get_data_from_pack(obj); |
53dda6ff | 494 | if (ref) |
636171cb NP |
495 | for (j = ref_first; j <= ref_last; j++) { |
496 | struct object_entry *child = objects + deltas[j].obj_no; | |
497 | if (child->real_type == OBJ_REF_DELTA) | |
498 | resolve_delta(child, data, | |
2d477051 | 499 | obj->size, obj->type); |
636171cb | 500 | } |
53dda6ff | 501 | if (ofs) |
636171cb NP |
502 | for (j = ofs_first; j <= ofs_last; j++) { |
503 | struct object_entry *child = objects + deltas[j].obj_no; | |
504 | if (child->real_type == OBJ_OFS_DELTA) | |
505 | resolve_delta(child, data, | |
2d477051 | 506 | obj->size, obj->type); |
636171cb | 507 | } |
9cf6d335 | 508 | free(data); |
3c9af366 NP |
509 | if (verbose) |
510 | percent = display_progress(nr_resolved_deltas, | |
511 | nr_deltas, percent); | |
9cf6d335 | 512 | } |
3c9af366 NP |
513 | if (verbose && nr_resolved_deltas == nr_deltas) |
514 | fputc('\n', stderr); | |
636171cb NP |
515 | } |
516 | ||
517 | static int write_compressed(int fd, void *in, unsigned int size) | |
518 | { | |
519 | z_stream stream; | |
520 | unsigned long maxsize; | |
521 | void *out; | |
522 | ||
523 | memset(&stream, 0, sizeof(stream)); | |
524 | deflateInit(&stream, zlib_compression_level); | |
525 | maxsize = deflateBound(&stream, size); | |
526 | out = xmalloc(maxsize); | |
527 | ||
528 | /* Compress it */ | |
529 | stream.next_in = in; | |
530 | stream.avail_in = size; | |
531 | stream.next_out = out; | |
532 | stream.avail_out = maxsize; | |
533 | while (deflate(&stream, Z_FINISH) == Z_OK); | |
534 | deflateEnd(&stream); | |
535 | ||
536 | size = stream.total_out; | |
537 | write_or_die(fd, out, size); | |
538 | free(out); | |
539 | return size; | |
540 | } | |
541 | ||
9096c660 | 542 | static void append_obj_to_pack(const unsigned char *sha1, void *buf, |
636171cb NP |
543 | unsigned long size, enum object_type type) |
544 | { | |
545 | struct object_entry *obj = &objects[nr_objects++]; | |
546 | unsigned char header[10]; | |
547 | unsigned long s = size; | |
548 | int n = 0; | |
549 | unsigned char c = (type << 4) | (s & 15); | |
550 | s >>= 4; | |
551 | while (s) { | |
552 | header[n++] = c | 0x80; | |
553 | c = s & 0x7f; | |
554 | s >>= 7; | |
555 | } | |
556 | header[n++] = c; | |
557 | write_or_die(output_fd, header, n); | |
558 | obj[1].offset = obj[0].offset + n; | |
559 | obj[1].offset += write_compressed(output_fd, buf, size); | |
9096c660 | 560 | hashcpy(obj->sha1, sha1); |
636171cb NP |
561 | } |
562 | ||
563 | static int delta_pos_compare(const void *_a, const void *_b) | |
564 | { | |
565 | struct delta_entry *a = *(struct delta_entry **)_a; | |
566 | struct delta_entry *b = *(struct delta_entry **)_b; | |
567 | return a->obj_no - b->obj_no; | |
568 | } | |
9cf6d335 | 569 | |
636171cb NP |
570 | static void fix_unresolved_deltas(int nr_unresolved) |
571 | { | |
572 | struct delta_entry **sorted_by_pos; | |
3c9af366 | 573 | int i, n = 0, percent = -1; |
636171cb NP |
574 | |
575 | /* | |
576 | * Since many unresolved deltas may well be themselves base objects | |
577 | * for more unresolved deltas, we really want to include the | |
578 | * smallest number of base objects that would cover as much delta | |
579 | * as possible by picking the | |
580 | * trunc deltas first, allowing for other deltas to resolve without | |
581 | * additional base objects. Since most base objects are to be found | |
582 | * before deltas depending on them, a good heuristic is to start | |
583 | * resolving deltas in the same order as their position in the pack. | |
584 | */ | |
585 | sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos)); | |
9cf6d335 | 586 | for (i = 0; i < nr_deltas; i++) { |
636171cb NP |
587 | if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA) |
588 | continue; | |
589 | sorted_by_pos[n++] = &deltas[i]; | |
9cf6d335 | 590 | } |
636171cb NP |
591 | qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare); |
592 | ||
593 | for (i = 0; i < n; i++) { | |
594 | struct delta_entry *d = sorted_by_pos[i]; | |
595 | void *data; | |
596 | unsigned long size; | |
21666f1a | 597 | enum object_type type; |
636171cb NP |
598 | int j, first, last; |
599 | ||
600 | if (objects[d->obj_no].real_type != OBJ_REF_DELTA) | |
601 | continue; | |
21666f1a | 602 | data = read_sha1_file(d->base.sha1, &type, &size); |
636171cb NP |
603 | if (!data) |
604 | continue; | |
636171cb | 605 | |
b89c4e93 | 606 | find_delta_children(&d->base, &first, &last); |
636171cb NP |
607 | for (j = first; j <= last; j++) { |
608 | struct object_entry *child = objects + deltas[j].obj_no; | |
609 | if (child->real_type == OBJ_REF_DELTA) | |
21666f1a | 610 | resolve_delta(child, data, size, type); |
636171cb NP |
611 | } |
612 | ||
9096c660 NP |
613 | if (check_sha1_signature(d->base.sha1, data, size, typename(type))) |
614 | die("local object %s is corrupt", sha1_to_hex(d->base.sha1)); | |
615 | append_obj_to_pack(d->base.sha1, data, size, type); | |
636171cb | 616 | free(data); |
3c9af366 NP |
617 | if (verbose) |
618 | percent = display_progress(nr_resolved_deltas, | |
619 | nr_deltas, percent); | |
636171cb NP |
620 | } |
621 | free(sorted_by_pos); | |
3c9af366 NP |
622 | if (verbose) |
623 | fputc('\n', stderr); | |
636171cb NP |
624 | } |
625 | ||
626 | static void readjust_pack_header_and_sha1(unsigned char *sha1) | |
627 | { | |
628 | struct pack_header hdr; | |
629 | SHA_CTX ctx; | |
630 | int size; | |
631 | ||
632 | /* Rewrite pack header with updated object number */ | |
633 | if (lseek(output_fd, 0, SEEK_SET) != 0) | |
634 | die("cannot seek back: %s", strerror(errno)); | |
93d26e4c | 635 | if (read_in_full(output_fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
636171cb NP |
636 | die("cannot read pack header back: %s", strerror(errno)); |
637 | hdr.hdr_entries = htonl(nr_objects); | |
638 | if (lseek(output_fd, 0, SEEK_SET) != 0) | |
639 | die("cannot seek back: %s", strerror(errno)); | |
640 | write_or_die(output_fd, &hdr, sizeof(hdr)); | |
641 | if (lseek(output_fd, 0, SEEK_SET) != 0) | |
642 | die("cannot seek back: %s", strerror(errno)); | |
643 | ||
644 | /* Recompute and store the new pack's SHA1 */ | |
645 | SHA1_Init(&ctx); | |
646 | do { | |
647 | unsigned char *buf[4096]; | |
648 | size = xread(output_fd, buf, sizeof(buf)); | |
649 | if (size < 0) | |
650 | die("cannot read pack data back: %s", strerror(errno)); | |
651 | SHA1_Update(&ctx, buf, size); | |
652 | } while (size > 0); | |
653 | SHA1_Final(sha1, &ctx); | |
654 | write_or_die(output_fd, sha1, 20); | |
9cf6d335 SV |
655 | } |
656 | ||
657 | static int sha1_compare(const void *_a, const void *_b) | |
658 | { | |
659 | struct object_entry *a = *(struct object_entry **)_a; | |
660 | struct object_entry *b = *(struct object_entry **)_b; | |
a89fccd2 | 661 | return hashcmp(a->sha1, b->sha1); |
9cf6d335 SV |
662 | } |
663 | ||
2d477051 NP |
664 | /* |
665 | * On entry *sha1 contains the pack content SHA1 hash, on exit it is | |
666 | * the SHA1 hash of sorted object names. | |
667 | */ | |
b89c4e93 | 668 | static const char *write_index_file(const char *index_name, unsigned char *sha1) |
9cf6d335 SV |
669 | { |
670 | struct sha1file *f; | |
7e4a2a84 | 671 | struct object_entry **sorted_by_sha, **list, **last; |
9cf6d335 | 672 | unsigned int array[256]; |
e42797f5 | 673 | int i, fd; |
84c8d8ae | 674 | SHA_CTX ctx; |
9cf6d335 | 675 | |
7e4a2a84 JH |
676 | if (nr_objects) { |
677 | sorted_by_sha = | |
678 | xcalloc(nr_objects, sizeof(struct object_entry *)); | |
679 | list = sorted_by_sha; | |
680 | last = sorted_by_sha + nr_objects; | |
681 | for (i = 0; i < nr_objects; ++i) | |
682 | sorted_by_sha[i] = &objects[i]; | |
683 | qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]), | |
684 | sha1_compare); | |
685 | ||
686 | } | |
687 | else | |
688 | sorted_by_sha = list = last = NULL; | |
9cf6d335 | 689 | |
e42797f5 NP |
690 | if (!index_name) { |
691 | static char tmpfile[PATH_MAX]; | |
692 | snprintf(tmpfile, sizeof(tmpfile), | |
0e55181f | 693 | "%s/tmp_idx_XXXXXX", get_object_directory()); |
e42797f5 NP |
694 | fd = mkstemp(tmpfile); |
695 | index_name = xstrdup(tmpfile); | |
696 | } else { | |
697 | unlink(index_name); | |
698 | fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600); | |
699 | } | |
700 | if (fd < 0) | |
701 | die("unable to create %s: %s", index_name, strerror(errno)); | |
702 | f = sha1fd(fd, index_name); | |
9cf6d335 SV |
703 | |
704 | /* | |
705 | * Write the first-level table (the list is sorted, | |
706 | * but we use a 256-entry lookup to be able to avoid | |
707 | * having to do eight extra binary search iterations). | |
708 | */ | |
709 | for (i = 0; i < 256; i++) { | |
710 | struct object_entry **next = list; | |
711 | while (next < last) { | |
712 | struct object_entry *obj = *next; | |
713 | if (obj->sha1[0] != i) | |
714 | break; | |
715 | next++; | |
716 | } | |
717 | array[i] = htonl(next - sorted_by_sha); | |
718 | list = next; | |
719 | } | |
720 | sha1write(f, array, 256 * sizeof(int)); | |
721 | ||
84c8d8ae JH |
722 | /* recompute the SHA1 hash of sorted object names. |
723 | * currently pack-objects does not do this, but that | |
724 | * can be fixed. | |
725 | */ | |
726 | SHA1_Init(&ctx); | |
9cf6d335 SV |
727 | /* |
728 | * Write the actual SHA1 entries.. | |
729 | */ | |
730 | list = sorted_by_sha; | |
731 | for (i = 0; i < nr_objects; i++) { | |
732 | struct object_entry *obj = *list++; | |
733 | unsigned int offset = htonl(obj->offset); | |
734 | sha1write(f, &offset, 4); | |
735 | sha1write(f, obj->sha1, 20); | |
84c8d8ae | 736 | SHA1_Update(&ctx, obj->sha1, 20); |
9cf6d335 | 737 | } |
2d477051 | 738 | sha1write(f, sha1, 20); |
9cf6d335 SV |
739 | sha1close(f, NULL, 1); |
740 | free(sorted_by_sha); | |
84c8d8ae | 741 | SHA1_Final(sha1, &ctx); |
e42797f5 NP |
742 | return index_name; |
743 | } | |
744 | ||
745 | static void final(const char *final_pack_name, const char *curr_pack_name, | |
746 | const char *final_index_name, const char *curr_index_name, | |
b8077709 | 747 | const char *keep_name, const char *keep_msg, |
e42797f5 NP |
748 | unsigned char *sha1) |
749 | { | |
3a55602e | 750 | const char *report = "pack"; |
e42797f5 NP |
751 | char name[PATH_MAX]; |
752 | int err; | |
753 | ||
754 | if (!from_stdin) { | |
755 | close(input_fd); | |
756 | } else { | |
757 | err = close(output_fd); | |
758 | if (err) | |
759 | die("error while closing pack file: %s", strerror(errno)); | |
760 | chmod(curr_pack_name, 0444); | |
761 | } | |
762 | ||
b8077709 SP |
763 | if (keep_msg) { |
764 | int keep_fd, keep_msg_len = strlen(keep_msg); | |
765 | if (!keep_name) { | |
766 | snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", | |
767 | get_object_directory(), sha1_to_hex(sha1)); | |
768 | keep_name = name; | |
769 | } | |
9ca4a201 NP |
770 | keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600); |
771 | if (keep_fd < 0) { | |
772 | if (errno != EEXIST) | |
773 | die("cannot write keep file"); | |
774 | } else { | |
775 | if (keep_msg_len > 0) { | |
776 | write_or_die(keep_fd, keep_msg, keep_msg_len); | |
777 | write_or_die(keep_fd, "\n", 1); | |
778 | } | |
779 | close(keep_fd); | |
576162a4 | 780 | report = "keep"; |
b8077709 | 781 | } |
b8077709 SP |
782 | } |
783 | ||
e42797f5 NP |
784 | if (final_pack_name != curr_pack_name) { |
785 | if (!final_pack_name) { | |
786 | snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", | |
787 | get_object_directory(), sha1_to_hex(sha1)); | |
788 | final_pack_name = name; | |
789 | } | |
790 | if (move_temp_to_file(curr_pack_name, final_pack_name)) | |
791 | die("cannot store pack file"); | |
792 | } | |
793 | ||
794 | chmod(curr_index_name, 0444); | |
795 | if (final_index_name != curr_index_name) { | |
796 | if (!final_index_name) { | |
797 | snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", | |
798 | get_object_directory(), sha1_to_hex(sha1)); | |
799 | final_index_name = name; | |
800 | } | |
801 | if (move_temp_to_file(curr_index_name, final_index_name)) | |
802 | die("cannot store index file"); | |
803 | } | |
576162a4 NP |
804 | |
805 | if (!from_stdin) { | |
806 | printf("%s\n", sha1_to_hex(sha1)); | |
807 | } else { | |
808 | char buf[48]; | |
809 | int len = snprintf(buf, sizeof(buf), "%s\t%s\n", | |
810 | report, sha1_to_hex(sha1)); | |
d1b2ddc8 | 811 | write_or_die(1, buf, len); |
576162a4 NP |
812 | |
813 | /* | |
814 | * Let's just mimic git-unpack-objects here and write | |
815 | * the last part of the input buffer to stdout. | |
816 | */ | |
817 | while (input_len) { | |
818 | err = xwrite(1, input_buffer + input_offset, input_len); | |
819 | if (err <= 0) | |
820 | break; | |
821 | input_len -= err; | |
822 | input_offset += err; | |
823 | } | |
824 | } | |
9cf6d335 SV |
825 | } |
826 | ||
827 | int main(int argc, char **argv) | |
828 | { | |
636171cb | 829 | int i, fix_thin_pack = 0; |
e42797f5 NP |
830 | const char *curr_pack, *pack_name = NULL; |
831 | const char *curr_index, *index_name = NULL; | |
b8077709 SP |
832 | const char *keep_name = NULL, *keep_msg = NULL; |
833 | char *index_name_buf = NULL, *keep_name_buf = NULL; | |
84c8d8ae | 834 | unsigned char sha1[20]; |
9cf6d335 SV |
835 | |
836 | for (i = 1; i < argc; i++) { | |
837 | const char *arg = argv[i]; | |
838 | ||
839 | if (*arg == '-') { | |
e42797f5 NP |
840 | if (!strcmp(arg, "--stdin")) { |
841 | from_stdin = 1; | |
636171cb NP |
842 | } else if (!strcmp(arg, "--fix-thin")) { |
843 | fix_thin_pack = 1; | |
b8077709 SP |
844 | } else if (!strcmp(arg, "--keep")) { |
845 | keep_msg = ""; | |
cc44c765 | 846 | } else if (!prefixcmp(arg, "--keep=")) { |
b8077709 | 847 | keep_msg = arg + 7; |
cc44c765 | 848 | } else if (!prefixcmp(arg, "--pack_header=")) { |
bed006fb NP |
849 | struct pack_header *hdr; |
850 | char *c; | |
851 | ||
852 | hdr = (struct pack_header *)input_buffer; | |
853 | hdr->hdr_signature = htonl(PACK_SIGNATURE); | |
854 | hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); | |
855 | if (*c != ',') | |
856 | die("bad %s", arg); | |
857 | hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); | |
858 | if (*c) | |
859 | die("bad %s", arg); | |
860 | input_len = sizeof(*hdr); | |
3c9af366 NP |
861 | } else if (!strcmp(arg, "-v")) { |
862 | verbose = 1; | |
e42797f5 | 863 | } else if (!strcmp(arg, "-o")) { |
9cf6d335 SV |
864 | if (index_name || (i+1) >= argc) |
865 | usage(index_pack_usage); | |
866 | index_name = argv[++i]; | |
867 | } else | |
868 | usage(index_pack_usage); | |
869 | continue; | |
870 | } | |
871 | ||
872 | if (pack_name) | |
873 | usage(index_pack_usage); | |
874 | pack_name = arg; | |
875 | } | |
876 | ||
e42797f5 | 877 | if (!pack_name && !from_stdin) |
9cf6d335 | 878 | usage(index_pack_usage); |
636171cb NP |
879 | if (fix_thin_pack && !from_stdin) |
880 | die("--fix-thin cannot be used without --stdin"); | |
e42797f5 | 881 | if (!index_name && pack_name) { |
9cf6d335 | 882 | int len = strlen(pack_name); |
5bb1cda5 | 883 | if (!has_extension(pack_name, ".pack")) |
9cf6d335 SV |
884 | die("packfile name '%s' does not end with '.pack'", |
885 | pack_name); | |
6689f087 | 886 | index_name_buf = xmalloc(len); |
9cf6d335 SV |
887 | memcpy(index_name_buf, pack_name, len - 5); |
888 | strcpy(index_name_buf + len - 5, ".idx"); | |
889 | index_name = index_name_buf; | |
890 | } | |
b8077709 SP |
891 | if (keep_msg && !keep_name && pack_name) { |
892 | int len = strlen(pack_name); | |
893 | if (!has_extension(pack_name, ".pack")) | |
894 | die("packfile name '%s' does not end with '.pack'", | |
895 | pack_name); | |
896 | keep_name_buf = xmalloc(len); | |
897 | memcpy(keep_name_buf, pack_name, len - 5); | |
898 | strcpy(keep_name_buf + len - 5, ".keep"); | |
899 | keep_name = keep_name_buf; | |
900 | } | |
9cf6d335 | 901 | |
e42797f5 | 902 | curr_pack = open_pack_file(pack_name); |
9cf6d335 | 903 | parse_pack_header(); |
636171cb NP |
904 | objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry)); |
905 | deltas = xmalloc(nr_objects * sizeof(struct delta_entry)); | |
3c9af366 NP |
906 | if (verbose) |
907 | setup_progress_signal(); | |
2d477051 | 908 | parse_pack_objects(sha1); |
636171cb NP |
909 | if (nr_deltas != nr_resolved_deltas) { |
910 | if (fix_thin_pack) { | |
911 | int nr_unresolved = nr_deltas - nr_resolved_deltas; | |
3c9af366 | 912 | int nr_objects_initial = nr_objects; |
636171cb NP |
913 | if (nr_unresolved <= 0) |
914 | die("confusion beyond insanity"); | |
915 | objects = xrealloc(objects, | |
916 | (nr_objects + nr_unresolved + 1) | |
917 | * sizeof(*objects)); | |
918 | fix_unresolved_deltas(nr_unresolved); | |
3c9af366 NP |
919 | if (verbose) |
920 | fprintf(stderr, "%d objects were added to complete this thin pack.\n", | |
921 | nr_objects - nr_objects_initial); | |
636171cb NP |
922 | readjust_pack_header_and_sha1(sha1); |
923 | } | |
924 | if (nr_deltas != nr_resolved_deltas) | |
925 | die("pack has %d unresolved deltas", | |
926 | nr_deltas - nr_resolved_deltas); | |
927 | } else { | |
928 | /* Flush remaining pack final 20-byte SHA1. */ | |
636171cb NP |
929 | flush(); |
930 | } | |
9cf6d335 | 931 | free(deltas); |
e42797f5 | 932 | curr_index = write_index_file(index_name, sha1); |
b8077709 SP |
933 | final(pack_name, curr_pack, |
934 | index_name, curr_index, | |
935 | keep_name, keep_msg, | |
936 | sha1); | |
9cf6d335 SV |
937 | free(objects); |
938 | free(index_name_buf); | |
b8077709 | 939 | free(keep_name_buf); |
9cf6d335 SV |
940 | |
941 | return 0; | |
942 | } |