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