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