interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / builtin / unpack-objects.c

698 lines · 17364 bytes Raw Blame History
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
25static int dry_run, quiet, recover, has_errors, strict;
26static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
27
28static unsigned char buffer[DEFAULT_IO_BUFFER_SIZE];
29static unsigned int offset, len;
30static off_t consumed_bytes;
31static off_t max_input_size;
32static struct git_hash_ctx ctx;
33static struct fsck_options fsck_options;
34static 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 */
41struct obj_buffer {
42 char *buffer;
43 unsigned long size;
44};
45
46static struct decoration obj_decorate;
47
48static struct obj_buffer *lookup_object_buffer(struct object *base)
49{
50 return lookup_decoration(&obj_decorate, base);
51}
52
53static 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 */
67static 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
90static 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 */
117static 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
160struct 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
169static struct delta_info *delta_list;
170
171static 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
186struct 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
196static struct obj_info *obj_list;
197static unsigned nr_objects;
198
199/*
200 * Called only from check_object() after it verified this object
201 * is Ok.
202 */
203static 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 */
218static 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
254static 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
263static 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 */
271static 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
312static 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 */
332static 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
352static 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
361struct zlib_stream {
362 struct odb_stream base;
363 git_zstream *zstream;
364 int status;
365};
366
367static ssize_t zlib_stream_read(struct odb_stream *in_stream,
368 char *buf, size_t buf_len)
369{
370 struct zlib_stream *data = container_of(in_stream, struct zlib_stream, base);
371 git_zstream *zstream = data->zstream;
372
373 if (data->status != Z_OK)
374 return 0;
375
376 zstream->next_out = (unsigned char *) buf;
377 zstream->avail_out = buf_len;
378
379 while (data->status == Z_OK && zstream->avail_out == buf_len) {
380 zstream->next_in = fill(1);
381 zstream->avail_in = len;
382 data->status = git_inflate(zstream, 0);
383 use(len - zstream->avail_in);
384 }
385
386 return buf_len - zstream->avail_out;
387}
388
389static void stream_blob(unsigned long size, unsigned nr)
390{
391 git_zstream zstream = { 0 };
392 struct zlib_stream in_stream = {
393 .base = {
394 .read = zlib_stream_read,
395 .size = size,
396 .type = OBJ_BLOB,
397 },
398 .zstream = &zstream,
399 .status = Z_OK,
400 };
401 struct obj_info *info = &obj_list[nr];
402
403 git_inflate_init(&zstream);
404
405 if (odb_write_object_stream(the_repository->objects, &in_stream.base, &info->oid))
406 die(_("failed to write object in stream"));
407
408 if (in_stream.status != Z_STREAM_END)
409 die(_("inflate returned (%d)"), in_stream.status);
410 git_inflate_end(&zstream);
411
412 if (strict) {
413 struct blob *blob = lookup_blob(the_repository, &info->oid);
414
415 if (!blob)
416 die(_("invalid blob object from stream"));
417 blob->object.flags |= FLAG_WRITTEN;
418 }
419 info->obj = NULL;
420}
421
422static int resolve_against_held(unsigned nr, const struct object_id *base,
423 void *delta_data, unsigned long delta_size)
424{
425 struct object *obj;
426 struct obj_buffer *obj_buffer;
427 obj = lookup_object(the_repository, base);
428 if (!obj)
429 return 0;
430 obj_buffer = lookup_object_buffer(obj);
431 if (!obj_buffer)
432 return 0;
433 resolve_delta(nr, obj->type, obj_buffer->buffer,
434 obj_buffer->size, delta_data, delta_size);
435 return 1;
436}
437
438static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
439 unsigned nr)
440{
441 void *delta_data, *base;
442 unsigned long base_size;
443 size_t base_size_st = 0;
444 struct object_id base_oid;
445
446 if (type == OBJ_REF_DELTA) {
447 oidread(&base_oid, fill(the_hash_algo->rawsz), the_repository->hash_algo);
448 use(the_hash_algo->rawsz);
449 delta_data = get_data(delta_size);
450 if (!delta_data)
451 return;
452 if (odb_has_object(the_repository->objects, &base_oid,
453 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
454 ; /* Ok we have this one */
455 else if (resolve_against_held(nr, &base_oid,
456 delta_data, delta_size))
457 return; /* we are done */
458 else {
459 /* cannot resolve yet --- queue it */
460 oidclr(&obj_list[nr].oid, the_repository->hash_algo);
461 add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
462 return;
463 }
464 } else {
465 unsigned base_found = 0;
466 unsigned char *pack, c;
467 off_t base_offset;
468 unsigned lo, mid, hi;
469
470 pack = fill(1);
471 c = *pack;
472 use(1);
473 base_offset = c & 127;
474 while (c & 128) {
475 base_offset += 1;
476 if (!base_offset || MSB(base_offset, 7))
477 die("offset value overflow for delta base object");
478 pack = fill(1);
479 c = *pack;
480 use(1);
481 base_offset = (base_offset << 7) + (c & 127);
482 }
483 base_offset = obj_list[nr].offset - base_offset;
484 if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
485 die("offset value out of bound for delta base object");
486
487 delta_data = get_data(delta_size);
488 if (!delta_data)
489 return;
490 lo = 0;
491 hi = nr;
492 while (lo < hi) {
493 mid = lo + (hi - lo) / 2;
494 if (base_offset < obj_list[mid].offset) {
495 hi = mid;
496 } else if (base_offset > obj_list[mid].offset) {
497 lo = mid + 1;
498 } else {
499 oidcpy(&base_oid, &obj_list[mid].oid);
500 base_found = !is_null_oid(&base_oid);
501 break;
502 }
503 }
504 if (!base_found) {
505 /*
506 * The delta base object is itself a delta that
507 * has not been resolved yet.
508 */
509 oidclr(&obj_list[nr].oid, the_repository->hash_algo);
510 add_delta_to_list(nr, null_oid(the_hash_algo), base_offset,
511 delta_data, delta_size);
512 return;
513 }
514 }
515
516 if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
517 return;
518
519 base = odb_read_object(the_repository->objects, &base_oid,
520 &type, &base_size_st);
521 base_size = cast_size_t_to_ulong(base_size_st);
522 if (!base) {
523 error("failed to read delta-pack base object %s",
524 oid_to_hex(&base_oid));
525 if (!recover)
526 exit(1);
527 has_errors = 1;
528 return;
529 }
530 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
531 free(base);
532}
533
534static void unpack_one(unsigned nr)
535{
536 unsigned shift;
537 unsigned char *pack;
538 size_t size, c;
539 enum object_type type;
540
541 obj_list[nr].offset = consumed_bytes;
542
543 pack = fill(1);
544 c = *pack;
545 use(1);
546 type = (c >> 4) & 7;
547 size = (c & 15);
548 shift = 4;
549 while (c & 0x80) {
550 if ((bitsizeof(size_t) - 7) < shift)
551 die(_("object size too large for this platform"));
552 pack = fill(1);
553 c = *pack;
554 use(1);
555 size += (c & 0x7f) << shift;
556 shift += 7;
557 }
558
559 switch (type) {
560 case OBJ_BLOB:
561 if (!dry_run &&
562 size > repo_settings_get_big_file_threshold(the_repository)) {
563 stream_blob(size, nr);
564 return;
565 }
566 /* fallthrough */
567 case OBJ_COMMIT:
568 case OBJ_TREE:
569 case OBJ_TAG:
570 unpack_non_delta_entry(type, size, nr);
571 return;
572 case OBJ_REF_DELTA:
573 case OBJ_OFS_DELTA:
574 unpack_delta_entry(type, size, nr);
575 return;
576 default:
577 error("bad object type %d", type);
578 has_errors = 1;
579 if (recover)
580 return;
581 exit(1);
582 }
583}
584
585static void unpack_all(void)
586{
587 int i;
588 unsigned char *hdr = fill(sizeof(struct pack_header));
589 struct odb_transaction *transaction;
590
591 if (get_be32(hdr) != PACK_SIGNATURE)
592 die("bad pack file");
593 hdr += 4;
594 if (!pack_version_ok_native(get_be32(hdr)))
595 die("unknown pack file version %"PRIu32,
596 get_be32(hdr));
597 hdr += 4;
598 nr_objects = get_be32(hdr);
599 use(sizeof(struct pack_header));
600
601 if (!quiet)
602 progress = start_progress(the_repository,
603 _("Unpacking objects"), nr_objects);
604 CALLOC_ARRAY(obj_list, nr_objects);
605 odb_transaction_begin_or_die(the_repository->objects, &transaction, 0);
606 for (i = 0; i < nr_objects; i++) {
607 unpack_one(i);
608 display_progress(progress, i + 1);
609 }
610 odb_transaction_commit_and_finalize_or_die(transaction);
611 stop_progress(&progress);
612
613 if (delta_list)
614 die("unresolved deltas left after unpacking");
615}
616
617int cmd_unpack_objects(int argc,
618 const char **argv,
619 const char *prefix UNUSED,
620 struct repository *repo)
621{
622 int i;
623 struct object_id oid;
624 struct git_hash_ctx tmp_ctx;
625
626 disable_replace_refs();
627
628 repo_config(the_repository, git_default_config, NULL);
629
630 quiet = !isatty(2);
631
632 show_usage_if_asked(argc, argv, unpack_usage);
633
634 fsck_options_init(&fsck_options, repo, FSCK_OPTIONS_STRICT);
635
636 for (i = 1 ; i < argc; i++) {
637 const char *arg = argv[i];
638
639 if (*arg == '-') {
640 if (!strcmp(arg, "-n")) {
641 dry_run = 1;
642 continue;
643 }
644 if (!strcmp(arg, "-q")) {
645 quiet = 1;
646 continue;
647 }
648 if (!strcmp(arg, "-r")) {
649 recover = 1;
650 continue;
651 }
652 if (!strcmp(arg, "--strict")) {
653 strict = 1;
654 continue;
655 }
656 if (skip_prefix(arg, "--strict=", &arg)) {
657 strict = 1;
658 fsck_set_msg_types(&fsck_options, arg);
659 continue;
660 }
661 if (skip_prefix(arg, "--pack_header=", &arg)) {
662 if (parse_pack_header_option(arg,
663 buffer, &len) < 0)
664 die(_("bad --pack_header: %s"), arg);
665 continue;
666 }
667 if (skip_prefix(arg, "--max-input-size=", &arg)) {
668 max_input_size = strtoumax(arg, NULL, 10);
669 continue;
670 }
671 usage(unpack_usage);
672 }
673
674 /* We don't take any non-flag arguments now.. Maybe some day */
675 usage(unpack_usage);
676 }
677 git_hash_init(&ctx, the_hash_algo);
678 unpack_all();
679 git_hash_update(&ctx, buffer, offset);
680 git_hash_init(&tmp_ctx, the_hash_algo);
681 git_hash_clone(&tmp_ctx, &ctx);
682 git_hash_final_oid(&oid, &tmp_ctx);
683 if (strict) {
684 write_rest();
685 if (fsck_finish(&fsck_options))
686 die(_("fsck error in pack objects"));
687 }
688 if (!hasheq(fill(the_hash_algo->rawsz), oid.hash,
689 the_repository->hash_algo))
690 die("final sha1 did not match");
691 use(the_hash_algo->rawsz);
692
693 /* Write the last part of the buffer to stdout */
694 write_in_full(1, buffer + offset, len);
695
696 /* All done */
697 return has_errors;
698}