Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
3
4 #include "builtin.h"
5 #include "config.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "git-zlib.h"
9 #include "hex.h"
10 #include "object-file.h"
11 #include "odb.h"
12 #include "odb/streaming.h"
13 #include "odb/transaction.h"
14 #include "object.h"
15 #include "delta.h"
16 #include "pack.h"
17 #include "blob.h"
18 #include "replace-object.h"
19 #include "strbuf.h"
20 #include "progress.h"
21 #include "decorate.h"
22 #include "fsck.h"
23 #include "packfile.h"
24
25 static int dry_run, quiet, recover, has_errors, strict;
26 static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
27
28 static unsigned char buffer[DEFAULT_IO_BUFFER_SIZE];
29 static unsigned int offset, len;
30 static off_t consumed_bytes;
31 static off_t max_input_size;
32 static struct git_hash_ctx ctx;
33 static struct fsck_options fsck_options;
34 static struct progress *progress;
35
36 /*
37 * When running under --strict mode, objects whose reachability are
38 * suspect are kept in core without getting written in the object
39 * store.
40 */
41 struct obj_buffer {
42 char *buffer;
43 unsigned long size;
44 };
45
46 static struct decoration obj_decorate;
47
48 static struct obj_buffer *lookup_object_buffer(struct object *base)
49 {
50 return lookup_decoration(&obj_decorate, base);
51 }
52
53 static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
54 {
55 struct obj_buffer *obj;
56 CALLOC_ARRAY(obj, 1);
57 obj->buffer = buffer;
58 obj->size = size;
59 if (add_decoration(&obj_decorate, object, obj))
60 die("object %s tried to add buffer twice!", oid_to_hex(&object->oid));
61 }
62
63 /*
64 * Make sure at least "min" bytes are available in the buffer, and
65 * return the pointer to the buffer.
66 */
67 static void *fill(int min)
68 {
69 if (min <= len)
70 return buffer + offset;
71 if (min > sizeof(buffer))
72 die("cannot fill %d bytes", min);
73 if (offset) {
74 git_hash_update(&ctx, buffer, offset);
75 memmove(buffer, buffer + offset, len);
76 offset = 0;
77 }
78 do {
79 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
80 if (ret <= 0) {
81 if (!ret)
82 die("early EOF");
83 die_errno("read error on input");
84 }
85 len += ret;
86 } while (len < min);
87 return buffer;
88 }
89
90 static void use(int bytes)
91 {
92 if (bytes > len)
93 die("used more bytes than were available");
94 len -= bytes;
95 offset += bytes;
96
97 /* make sure off_t is sufficiently large not to wrap */
98 if (signed_add_overflows(consumed_bytes, bytes))
99 die("pack too large for current definition of off_t");
100 consumed_bytes += bytes;
101 if (max_input_size && consumed_bytes > max_input_size)
102 die(_("pack exceeds maximum allowed size"));
103 display_throughput(progress, consumed_bytes);
104 }
105
106 /*
107 * Decompress zstream from the standard input into a newly
108 * allocated buffer of specified size and return the buffer.
109 * The caller is responsible to free the returned buffer.
110 *
111 * But for dry_run mode, "get_data()" is only used to check the
112 * integrity of data, and the returned buffer is not used at all.
113 * Therefore, in dry_run mode, "get_data()" will release the small
114 * allocated buffer which is reused to hold temporary zstream output
115 * and return NULL instead of returning garbage data.
116 */
117 static void *get_data(unsigned long size)
118 {
119 git_zstream stream;
120 unsigned long bufsize = dry_run && size > 8192 ? 8192 : size;
121 void *buf = xmallocz(bufsize);
122
123 memset(&stream, 0, sizeof(stream));
124
125 stream.next_out = buf;
126 stream.avail_out = bufsize;
127 stream.next_in = fill(1);
128 stream.avail_in = len;
129 git_inflate_init(&stream);
130
131 for (;;) {
132 int ret = git_inflate(&stream, 0);
133 use(len - stream.avail_in);
134 if (stream.total_out == size && ret == Z_STREAM_END)
135 break;
136 if (ret != Z_OK) {
137 error("inflate returned %d", ret);
138 FREE_AND_NULL(buf);
139 if (!recover)
140 exit(1);
141 has_errors = 1;
142 break;
143 }
144 stream.next_in = fill(1);
145 stream.avail_in = len;
146 if (dry_run) {
147 /* reuse the buffer in dry_run mode */
148 stream.next_out = buf;
149 stream.avail_out = bufsize > size - stream.total_out ?
150 size - stream.total_out :
151 bufsize;
152 }
153 }
154 git_inflate_end(&stream);
155 if (dry_run)
156 FREE_AND_NULL(buf);
157 return buf;
158 }
159
160 struct delta_info {
161 struct object_id base_oid;
162 unsigned nr;
163 off_t base_offset;
164 unsigned long size;
165 void *delta;
166 struct delta_info *next;
167 };
168
169 static struct delta_info *delta_list;
170
171 static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
172 off_t base_offset,
173 void *delta, unsigned long size)
174 {
175 struct delta_info *info = xmalloc(sizeof(*info));
176
177 oidcpy(&info->base_oid, base_oid);
178 info->base_offset = base_offset;
179 info->size = size;
180 info->delta = delta;
181 info->nr = nr;
182 info->next = delta_list;
183 delta_list = info;
184 }
185
186 struct obj_info {
187 off_t offset;
188 struct object_id oid;
189 struct object *obj;
190 };
191
192 /* Remember to update object flag allocation in object.h */
193 #define FLAG_OPEN (1u<<20)
194 #define FLAG_WRITTEN (1u<<21)
195
196 static struct obj_info *obj_list;
197 static unsigned nr_objects;
198
199 /*
200 * Called only from check_object() after it verified this object
201 * is Ok.
202 */
203 static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
204 {
205 struct object_id oid;
206
207 if (odb_write_object(the_repository->objects, obj_buf->buffer, obj_buf->size,
208 obj->type, &oid) < 0)
209 die("failed to write object %s", oid_to_hex(&obj->oid));
210 obj->flags |= FLAG_WRITTEN;
211 }
212
213 /*
214 * At the very end of the processing, write_rest() scans the objects
215 * that have reachability requirements and calls this function.
216 * Verify its reachability and validity recursively and write it out.
217 */
218 static int check_object(struct object *obj, enum object_type type,
219 void *data UNUSED,
220 struct fsck_options *options UNUSED)
221 {
222 struct obj_buffer *obj_buf;
223
224 if (!obj)
225 return 1;
226
227 if (obj->flags & FLAG_WRITTEN)
228 return 0;
229
230 if (type != OBJ_ANY && obj->type != type)
231 die("object type mismatch");
232
233 if (!(obj->flags & FLAG_OPEN)) {
234 size_t size;
235 int type = odb_read_object_info(the_repository->objects, &obj->oid, &size);
236 if (type != obj->type || type <= 0)
237 die("object of unexpected type");
238 obj->flags |= FLAG_WRITTEN;
239 return 0;
240 }
241
242 obj_buf = lookup_object_buffer(obj);
243 if (!obj_buf)
244 die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
245 if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
246 die("fsck error in packed object");
247 fsck_options.walk = check_object;
248 if (fsck_walk(obj, NULL, &fsck_options))
249 die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
250 write_cached_object(obj, obj_buf);
251 return 0;
252 }
253
254 static void write_rest(void)
255 {
256 unsigned i;
257 for (i = 0; i < nr_objects; i++) {
258 if (obj_list[i].obj)
259 check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL);
260 }
261 }
262
263 static void added_object(unsigned nr, enum object_type type,
264 void *data, unsigned long size);
265
266 /*
267 * Write out nr-th object from the list, now we know the contents
268 * of it. Under --strict, this buffers structured objects in-core,
269 * to be checked at the end.
270 */
271 static void write_object(unsigned nr, enum object_type type,
272 void *buf, unsigned long size)
273 {
274 if (!strict) {
275 if (odb_write_object(the_repository->objects, buf, size, type,
276 &obj_list[nr].oid) < 0)
277 die("failed to write object");
278 added_object(nr, type, buf, size);
279 free(buf);
280 obj_list[nr].obj = NULL;
281 } else if (type == OBJ_BLOB) {
282 struct blob *blob;
283 if (odb_write_object(the_repository->objects, buf, size, type,
284 &obj_list[nr].oid) < 0)
285 die("failed to write object");
286 added_object(nr, type, buf, size);
287 free(buf);
288
289 blob = lookup_blob(the_repository, &obj_list[nr].oid);
290 if (blob)
291 blob->object.flags |= FLAG_WRITTEN;
292 else
293 die("invalid blob object");
294 obj_list[nr].obj = NULL;
295 } else {
296 struct object *obj;
297 int eaten;
298 hash_object_file(the_hash_algo, buf, size, type,
299 &obj_list[nr].oid);
300 added_object(nr, type, buf, size);
301 obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
302 type, size, buf,
303 &eaten);
304 if (!obj)
305 die("invalid %s", type_name(type));
306 add_object_buffer(obj, buf, size);
307 obj->flags |= FLAG_OPEN;
308 obj_list[nr].obj = obj;
309 }
310 }
311
312 static void resolve_delta(unsigned nr, enum object_type type,
313 void *base, unsigned long base_size,
314 void *delta, unsigned long delta_size)
315 {
316 void *result;
317 size_t result_size;
318
319 result = patch_delta(base, base_size,
320 delta, delta_size,
321 &result_size);
322 if (!result)
323 die("failed to apply delta");
324 free(delta);
325 write_object(nr, type, result, result_size);
326 }
327
328 /*
329 * We now know the contents of an object (which is nr-th in the pack);
330 * resolve all the deltified objects that are based on it.
331 */
332 static void added_object(unsigned nr, enum object_type type,
333 void *data, unsigned long size)
334 {
335 struct delta_info **p = &delta_list;
336 struct delta_info *info;
337
338 while ((info = *p) != NULL) {
339 if (oideq(&info->base_oid, &obj_list[nr].oid) ||
340 info->base_offset == obj_list[nr].offset) {
341 *p = info->next;
342 p = &delta_list;
343 resolve_delta(info->nr, type, data, size,
344 info->delta, info->size);
345 free(info);
346 continue;
347 }
348 p = &info->next;
349 }
350 }
351
352 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
353 unsigned nr)
354 {
355 void *buf = get_data(size);
356
357 if (buf)
358 write_object(nr, type, buf, size);
359 }
360
361 struct input_zstream_data {
362 git_zstream *zstream;
363 int status;
364 };
365
366 static ssize_t feed_input_zstream(struct odb_write_stream *in_stream,
367 unsigned char *buf, size_t buf_len)
368 {
369 struct input_zstream_data *data = in_stream->data;
370 git_zstream *zstream = data->zstream;
371 void *in = fill(1);
372
373 if (in_stream->is_finished)
374 return 0;
375
376 zstream->next_out = buf;
377 zstream->avail_out = buf_len;
378 zstream->next_in = in;
379 zstream->avail_in = len;
380
381 data->status = git_inflate(zstream, 0);
382
383 in_stream->is_finished = data->status != Z_OK;
384 use(len - zstream->avail_in);
385 return buf_len - zstream->avail_out;
386 }
387
388 static void stream_blob(unsigned long size, unsigned nr)
389 {
390 git_zstream zstream = { 0 };
391 struct input_zstream_data data = { 0 };
392 struct odb_write_stream in_stream = {
393 .read = feed_input_zstream,
394 .data = &data,
395 };
396 struct obj_info *info = &obj_list[nr];
397
398 data.zstream = &zstream;
399 git_inflate_init(&zstream);
400
401 if (odb_write_object_stream(the_repository->objects, &in_stream, size, &info->oid))
402 die(_("failed to write object in stream"));
403
404 if (data.status != Z_STREAM_END)
405 die(_("inflate returned (%d)"), data.status);
406 git_inflate_end(&zstream);
407
408 if (strict) {
409 struct blob *blob = lookup_blob(the_repository, &info->oid);
410
411 if (!blob)
412 die(_("invalid blob object from stream"));
413 blob->object.flags |= FLAG_WRITTEN;
414 }
415 info->obj = NULL;
416 }
417
418 static int resolve_against_held(unsigned nr, const struct object_id *base,
419 void *delta_data, unsigned long delta_size)
420 {
421 struct object *obj;
422 struct obj_buffer *obj_buffer;
423 obj = lookup_object(the_repository, base);
424 if (!obj)
425 return 0;
426 obj_buffer = lookup_object_buffer(obj);
427 if (!obj_buffer)
428 return 0;
429 resolve_delta(nr, obj->type, obj_buffer->buffer,
430 obj_buffer->size, delta_data, delta_size);
431 return 1;
432 }
433
434 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
435 unsigned nr)
436 {
437 void *delta_data, *base;
438 unsigned long base_size;
439 size_t base_size_st = 0;
440 struct object_id base_oid;
441
442 if (type == OBJ_REF_DELTA) {
443 oidread(&base_oid, fill(the_hash_algo->rawsz), the_repository->hash_algo);
444 use(the_hash_algo->rawsz);
445 delta_data = get_data(delta_size);
446 if (!delta_data)
447 return;
448 if (odb_has_object(the_repository->objects, &base_oid,
449 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
450 ; /* Ok we have this one */
451 else if (resolve_against_held(nr, &base_oid,
452 delta_data, delta_size))
453 return; /* we are done */
454 else {
455 /* cannot resolve yet --- queue it */
456 oidclr(&obj_list[nr].oid, the_repository->hash_algo);
457 add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
458 return;
459 }
460 } else {
461 unsigned base_found = 0;
462 unsigned char *pack, c;
463 off_t base_offset;
464 unsigned lo, mid, hi;
465
466 pack = fill(1);
467 c = *pack;
468 use(1);
469 base_offset = c & 127;
470 while (c & 128) {
471 base_offset += 1;
472 if (!base_offset || MSB(base_offset, 7))
473 die("offset value overflow for delta base object");
474 pack = fill(1);
475 c = *pack;
476 use(1);
477 base_offset = (base_offset << 7) + (c & 127);
478 }
479 base_offset = obj_list[nr].offset - base_offset;
480 if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
481 die("offset value out of bound for delta base object");
482
483 delta_data = get_data(delta_size);
484 if (!delta_data)
485 return;
486 lo = 0;
487 hi = nr;
488 while (lo < hi) {
489 mid = lo + (hi - lo) / 2;
490 if (base_offset < obj_list[mid].offset) {
491 hi = mid;
492 } else if (base_offset > obj_list[mid].offset) {
493 lo = mid + 1;
494 } else {
495 oidcpy(&base_oid, &obj_list[mid].oid);
496 base_found = !is_null_oid(&base_oid);
497 break;
498 }
499 }
500 if (!base_found) {
501 /*
502 * The delta base object is itself a delta that
503 * has not been resolved yet.
504 */
505 oidclr(&obj_list[nr].oid, the_repository->hash_algo);
506 add_delta_to_list(nr, null_oid(the_hash_algo), base_offset,
507 delta_data, delta_size);
508 return;
509 }
510 }
511
512 if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
513 return;
514
515 base = odb_read_object(the_repository->objects, &base_oid,
516 &type, &base_size_st);
517 base_size = cast_size_t_to_ulong(base_size_st);
518 if (!base) {
519 error("failed to read delta-pack base object %s",
520 oid_to_hex(&base_oid));
521 if (!recover)
522 exit(1);
523 has_errors = 1;
524 return;
525 }
526 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
527 free(base);
528 }
529
530 static void unpack_one(unsigned nr)
531 {
532 unsigned shift;
533 unsigned char *pack;
534 size_t size, c;
535 enum object_type type;
536
537 obj_list[nr].offset = consumed_bytes;
538
539 pack = fill(1);
540 c = *pack;
541 use(1);
542 type = (c >> 4) & 7;
543 size = (c & 15);
544 shift = 4;
545 while (c & 0x80) {
546 if ((bitsizeof(size_t) - 7) < shift)
547 die(_("object size too large for this platform"));
548 pack = fill(1);
549 c = *pack;
550 use(1);
551 size += (c & 0x7f) << shift;
552 shift += 7;
553 }
554
555 switch (type) {
556 case OBJ_BLOB:
557 if (!dry_run &&
558 size > repo_settings_get_big_file_threshold(the_repository)) {
559 stream_blob(size, nr);
560 return;
561 }
562 /* fallthrough */
563 case OBJ_COMMIT:
564 case OBJ_TREE:
565 case OBJ_TAG:
566 unpack_non_delta_entry(type, size, nr);
567 return;
568 case OBJ_REF_DELTA:
569 case OBJ_OFS_DELTA:
570 unpack_delta_entry(type, size, nr);
571 return;
572 default:
573 error("bad object type %d", type);
574 has_errors = 1;
575 if (recover)
576 return;
577 exit(1);
578 }
579 }
580
581 static void unpack_all(void)
582 {
583 int i;
584 unsigned char *hdr = fill(sizeof(struct pack_header));
585 struct odb_transaction *transaction;
586
587 if (get_be32(hdr) != PACK_SIGNATURE)
588 die("bad pack file");
589 hdr += 4;
590 if (!pack_version_ok_native(get_be32(hdr)))
591 die("unknown pack file version %"PRIu32,
592 get_be32(hdr));
593 hdr += 4;
594 nr_objects = get_be32(hdr);
595 use(sizeof(struct pack_header));
596
597 if (!quiet)
598 progress = start_progress(the_repository,
599 _("Unpacking objects"), nr_objects);
600 CALLOC_ARRAY(obj_list, nr_objects);
601 transaction = odb_transaction_begin(the_repository->objects);
602 for (i = 0; i < nr_objects; i++) {
603 unpack_one(i);
604 display_progress(progress, i + 1);
605 }
606 odb_transaction_commit(transaction);
607 stop_progress(&progress);
608
609 if (delta_list)
610 die("unresolved deltas left after unpacking");
611 }
612
613 int cmd_unpack_objects(int argc,
614 const char **argv,
615 const char *prefix UNUSED,
616 struct repository *repo)
617 {
618 int i;
619 struct object_id oid;
620 struct git_hash_ctx tmp_ctx;
621
622 disable_replace_refs();
623
624 repo_config(the_repository, git_default_config, NULL);
625
626 quiet = !isatty(2);
627
628 show_usage_if_asked(argc, argv, unpack_usage);
629
630 fsck_options_init(&fsck_options, repo, FSCK_OPTIONS_STRICT);
631
632 for (i = 1 ; i < argc; i++) {
633 const char *arg = argv[i];
634
635 if (*arg == '-') {
636 if (!strcmp(arg, "-n")) {
637 dry_run = 1;
638 continue;
639 }
640 if (!strcmp(arg, "-q")) {
641 quiet = 1;
642 continue;
643 }
644 if (!strcmp(arg, "-r")) {
645 recover = 1;
646 continue;
647 }
648 if (!strcmp(arg, "--strict")) {
649 strict = 1;
650 continue;
651 }
652 if (skip_prefix(arg, "--strict=", &arg)) {
653 strict = 1;
654 fsck_set_msg_types(&fsck_options, arg);
655 continue;
656 }
657 if (skip_prefix(arg, "--pack_header=", &arg)) {
658 if (parse_pack_header_option(arg,
659 buffer, &len) < 0)
660 die(_("bad --pack_header: %s"), arg);
661 continue;
662 }
663 if (skip_prefix(arg, "--max-input-size=", &arg)) {
664 max_input_size = strtoumax(arg, NULL, 10);
665 continue;
666 }
667 usage(unpack_usage);
668 }
669
670 /* We don't take any non-flag arguments now.. Maybe some day */
671 usage(unpack_usage);
672 }
673 the_hash_algo->init_fn(&ctx);
674 unpack_all();
675 git_hash_update(&ctx, buffer, offset);
676 the_hash_algo->init_fn(&tmp_ctx);
677 git_hash_clone(&tmp_ctx, &ctx);
678 git_hash_final_oid(&oid, &tmp_ctx);
679 if (strict) {
680 write_rest();
681 if (fsck_finish(&fsck_options))
682 die(_("fsck error in pack objects"));
683 }
684 if (!hasheq(fill(the_hash_algo->rawsz), oid.hash,
685 the_repository->hash_algo))
686 die("final sha1 did not match");
687 use(the_hash_algo->rawsz);
688
689 /* Write the last part of the buffer to stdout */
690 write_in_full(1, buffer + offset, len);
691
692 /* All done */
693 return has_errors;
694 }