1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
5 #include "bulk-checkin.h"
7 #include "environment.h"
11 #include "object-file.h"
12 #include "object-store.h"
17 #include "replace-object.h"
24 static int dry_run
, quiet
, recover
, has_errors
, strict
;
25 static const char unpack_usage
[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
27 /* We always read in 4kB chunks. */
28 static unsigned char buffer
[4096];
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
= FSCK_OPTIONS_STRICT
;
34 static struct progress
*progress
;
37 * When running under --strict mode, objects whose reachability are
38 * suspect are kept in core without getting written in the object
46 static struct decoration obj_decorate
;
48 static struct obj_buffer
*lookup_object_buffer(struct object
*base
)
50 return lookup_decoration(&obj_decorate
, base
);
53 static void add_object_buffer(struct object
*object
, char *buffer
, unsigned long size
)
55 struct obj_buffer
*obj
;
59 if (add_decoration(&obj_decorate
, object
, obj
))
60 die("object %s tried to add buffer twice!", oid_to_hex(&object
->oid
));
64 * Make sure at least "min" bytes are available in the buffer, and
65 * return the pointer to the buffer.
67 static void *fill(int min
)
70 return buffer
+ offset
;
71 if (min
> sizeof(buffer
))
72 die("cannot fill %d bytes", min
);
74 git_hash_update(&ctx
, buffer
, offset
);
75 memmove(buffer
, buffer
+ offset
, len
);
79 ssize_t ret
= xread(0, buffer
+ len
, sizeof(buffer
) - len
);
83 die_errno("read error on input");
90 static void use(int bytes
)
93 die("used more bytes than were available");
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
);
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.
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.
117 static void *get_data(unsigned long size
)
120 unsigned long bufsize
= dry_run
&& size
> 8192 ? 8192 : size
;
121 void *buf
= xmallocz(bufsize
);
123 memset(&stream
, 0, sizeof(stream
));
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
);
132 int ret
= git_inflate(&stream
, 0);
133 use(len
- stream
.avail_in
);
134 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
137 error("inflate returned %d", ret
);
144 stream
.next_in
= fill(1);
145 stream
.avail_in
= len
;
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
:
154 git_inflate_end(&stream
);
161 struct object_id base_oid
;
166 struct delta_info
*next
;
169 static struct delta_info
*delta_list
;
171 static void add_delta_to_list(unsigned nr
, const struct object_id
*base_oid
,
173 void *delta
, unsigned long size
)
175 struct delta_info
*info
= xmalloc(sizeof(*info
));
177 oidcpy(&info
->base_oid
, base_oid
);
178 info
->base_offset
= base_offset
;
182 info
->next
= delta_list
;
188 struct object_id oid
;
192 /* Remember to update object flag allocation in object.h */
193 #define FLAG_OPEN (1u<<20)
194 #define FLAG_WRITTEN (1u<<21)
196 static struct obj_info
*obj_list
;
197 static unsigned nr_objects
;
200 * Called only from check_object() after it verified this object
203 static void write_cached_object(struct object
*obj
, struct obj_buffer
*obj_buf
)
205 struct object_id oid
;
207 if (write_object_file(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
;
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.
218 static int check_object(struct object
*obj
, enum object_type type
,
220 struct fsck_options
*options UNUSED
)
222 struct obj_buffer
*obj_buf
;
227 if (obj
->flags
& FLAG_WRITTEN
)
230 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
231 die("object type mismatch");
233 if (!(obj
->flags
& FLAG_OPEN
)) {
235 int type
= oid_object_info(the_repository
, &obj
->oid
, &size
);
236 if (type
!= obj
->type
|| type
<= 0)
237 die("object of unexpected type");
238 obj
->flags
|= FLAG_WRITTEN
;
242 obj_buf
= lookup_object_buffer(obj
);
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
);
254 static void write_rest(void)
257 for (i
= 0; i
< nr_objects
; i
++) {
259 check_object(obj_list
[i
].obj
, OBJ_ANY
, NULL
, NULL
);
263 static void added_object(unsigned nr
, enum object_type type
,
264 void *data
, unsigned long size
);
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.
271 static void write_object(unsigned nr
, enum object_type type
,
272 void *buf
, unsigned long size
)
275 if (write_object_file(buf
, size
, type
,
276 &obj_list
[nr
].oid
) < 0)
277 die("failed to write object");
278 added_object(nr
, type
, buf
, size
);
280 obj_list
[nr
].obj
= NULL
;
281 } else if (type
== OBJ_BLOB
) {
283 if (write_object_file(buf
, size
, type
,
284 &obj_list
[nr
].oid
) < 0)
285 die("failed to write object");
286 added_object(nr
, type
, buf
, size
);
289 blob
= lookup_blob(the_repository
, &obj_list
[nr
].oid
);
291 blob
->object
.flags
|= FLAG_WRITTEN
;
293 die("invalid blob object");
294 obj_list
[nr
].obj
= NULL
;
298 hash_object_file(the_hash_algo
, buf
, size
, type
,
300 added_object(nr
, type
, buf
, size
);
301 obj
= parse_object_buffer(the_repository
, &obj_list
[nr
].oid
,
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
;
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
)
317 unsigned long result_size
;
319 result
= patch_delta(base
, base_size
,
323 die("failed to apply delta");
325 write_object(nr
, type
, result
, result_size
);
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.
332 static void added_object(unsigned nr
, enum object_type type
,
333 void *data
, unsigned long size
)
335 struct delta_info
**p
= &delta_list
;
336 struct delta_info
*info
;
338 while ((info
= *p
) != NULL
) {
339 if (oideq(&info
->base_oid
, &obj_list
[nr
].oid
) ||
340 info
->base_offset
== obj_list
[nr
].offset
) {
343 resolve_delta(info
->nr
, type
, data
, size
,
344 info
->delta
, info
->size
);
352 static void unpack_non_delta_entry(enum object_type type
, unsigned long size
,
355 void *buf
= get_data(size
);
358 write_object(nr
, type
, buf
, size
);
361 struct input_zstream_data
{
362 git_zstream
*zstream
;
363 unsigned char buf
[8192];
367 static const void *feed_input_zstream(struct input_stream
*in_stream
,
368 unsigned long *readlen
)
370 struct input_zstream_data
*data
= in_stream
->data
;
371 git_zstream
*zstream
= data
->zstream
;
374 if (in_stream
->is_finished
) {
379 zstream
->next_out
= data
->buf
;
380 zstream
->avail_out
= sizeof(data
->buf
);
381 zstream
->next_in
= in
;
382 zstream
->avail_in
= len
;
384 data
->status
= git_inflate(zstream
, 0);
386 in_stream
->is_finished
= data
->status
!= Z_OK
;
387 use(len
- zstream
->avail_in
);
388 *readlen
= sizeof(data
->buf
) - zstream
->avail_out
;
393 static void stream_blob(unsigned long size
, unsigned nr
)
395 git_zstream zstream
= { 0 };
396 struct input_zstream_data data
= { 0 };
397 struct input_stream in_stream
= {
398 .read
= feed_input_zstream
,
401 struct obj_info
*info
= &obj_list
[nr
];
403 data
.zstream
= &zstream
;
404 git_inflate_init(&zstream
);
406 if (stream_loose_object(&in_stream
, size
, &info
->oid
))
407 die(_("failed to write object in stream"));
409 if (data
.status
!= Z_STREAM_END
)
410 die(_("inflate returned (%d)"), data
.status
);
411 git_inflate_end(&zstream
);
414 struct blob
*blob
= lookup_blob(the_repository
, &info
->oid
);
417 die(_("invalid blob object from stream"));
418 blob
->object
.flags
|= FLAG_WRITTEN
;
423 static int resolve_against_held(unsigned nr
, const struct object_id
*base
,
424 void *delta_data
, unsigned long delta_size
)
427 struct obj_buffer
*obj_buffer
;
428 obj
= lookup_object(the_repository
, base
);
431 obj_buffer
= lookup_object_buffer(obj
);
434 resolve_delta(nr
, obj
->type
, obj_buffer
->buffer
,
435 obj_buffer
->size
, delta_data
, delta_size
);
439 static void unpack_delta_entry(enum object_type type
, unsigned long delta_size
,
442 void *delta_data
, *base
;
443 unsigned long base_size
;
444 struct object_id base_oid
;
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
);
452 if (has_object(the_repository
, &base_oid
,
453 HAS_OBJECT_RECHECK_PACKED
| 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 */
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
);
465 unsigned base_found
= 0;
466 unsigned char *pack
, c
;
468 unsigned lo
, mid
, hi
;
473 base_offset
= c
& 127;
476 if (!base_offset
|| MSB(base_offset
, 7))
477 die("offset value overflow for delta base object");
481 base_offset
= (base_offset
<< 7) + (c
& 127);
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");
487 delta_data
= get_data(delta_size
);
493 mid
= lo
+ (hi
- lo
) / 2;
494 if (base_offset
< obj_list
[mid
].offset
) {
496 } else if (base_offset
> obj_list
[mid
].offset
) {
499 oidcpy(&base_oid
, &obj_list
[mid
].oid
);
500 base_found
= !is_null_oid(&base_oid
);
506 * The delta base object is itself a delta that
507 * has not been resolved yet.
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
);
516 if (resolve_against_held(nr
, &base_oid
, delta_data
, delta_size
))
519 base
= repo_read_object_file(the_repository
, &base_oid
, &type
,
522 error("failed to read delta-pack base object %s",
523 oid_to_hex(&base_oid
));
529 resolve_delta(nr
, type
, base
, base_size
, delta_data
, delta_size
);
533 static void unpack_one(unsigned nr
)
537 unsigned long size
, c
;
538 enum object_type type
;
540 obj_list
[nr
].offset
= consumed_bytes
;
552 size
+= (c
& 0x7f) << shift
;
559 size
> repo_settings_get_big_file_threshold(the_repository
)) {
560 stream_blob(size
, nr
);
567 unpack_non_delta_entry(type
, size
, nr
);
571 unpack_delta_entry(type
, size
, nr
);
574 error("bad object type %d", type
);
582 static void unpack_all(void)
585 unsigned char *hdr
= fill(sizeof(struct pack_header
));
587 if (get_be32(hdr
) != PACK_SIGNATURE
)
588 die("bad pack file");
590 if (!pack_version_ok_native(get_be32(hdr
)))
591 die("unknown pack file version %"PRIu32
,
594 nr_objects
= get_be32(hdr
);
595 use(sizeof(struct pack_header
));
598 progress
= start_progress(the_repository
,
599 _("Unpacking objects"), nr_objects
);
600 CALLOC_ARRAY(obj_list
, nr_objects
);
601 begin_odb_transaction();
602 for (i
= 0; i
< nr_objects
; i
++) {
604 display_progress(progress
, i
+ 1);
606 end_odb_transaction();
607 stop_progress(&progress
);
610 die("unresolved deltas left after unpacking");
613 int cmd_unpack_objects(int argc
,
615 const char *prefix UNUSED
,
616 struct repository
*repo UNUSED
)
619 struct object_id oid
;
620 struct git_hash_ctx tmp_ctx
;
622 disable_replace_refs();
624 git_config(git_default_config
, NULL
);
628 show_usage_if_asked(argc
, argv
, unpack_usage
);
630 for (i
= 1 ; i
< argc
; i
++) {
631 const char *arg
= argv
[i
];
634 if (!strcmp(arg
, "-n")) {
638 if (!strcmp(arg
, "-q")) {
642 if (!strcmp(arg
, "-r")) {
646 if (!strcmp(arg
, "--strict")) {
650 if (skip_prefix(arg
, "--strict=", &arg
)) {
652 fsck_set_msg_types(&fsck_options
, arg
);
655 if (skip_prefix(arg
, "--pack_header=", &arg
)) {
656 if (parse_pack_header_option(arg
,
658 die(_("bad --pack_header: %s"), arg
);
661 if (skip_prefix(arg
, "--max-input-size=", &arg
)) {
662 max_input_size
= strtoumax(arg
, NULL
, 10);
668 /* We don't take any non-flag arguments now.. Maybe some day */
671 the_hash_algo
->init_fn(&ctx
);
673 git_hash_update(&ctx
, buffer
, offset
);
674 the_hash_algo
->init_fn(&tmp_ctx
);
675 git_hash_clone(&tmp_ctx
, &ctx
);
676 git_hash_final_oid(&oid
, &tmp_ctx
);
679 if (fsck_finish(&fsck_options
))
680 die(_("fsck error in pack objects"));
682 if (!hasheq(fill(the_hash_algo
->rawsz
), oid
.hash
,
683 the_repository
->hash_algo
))
684 die("final sha1 did not match");
685 use(the_hash_algo
->rawsz
);
687 /* Write the last part of the buffer to stdout */
688 write_in_full(1, buffer
+ offset
, len
);