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