3 #include "bulk-checkin.h"
5 #include "environment.h"
9 #include "object-store.h"
15 #include "replace-object.h"
18 #include "tree-walk.h"
23 static int dry_run
, quiet
, recover
, has_errors
, strict
;
24 static const char unpack_usage
[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
26 /* We always read in 4kB chunks. */
27 static unsigned char buffer
[4096];
28 static unsigned int offset
, len
;
29 static off_t consumed_bytes
;
30 static off_t max_input_size
;
31 static git_hash_ctx ctx
;
32 static struct fsck_options fsck_options
= FSCK_OPTIONS_STRICT
;
33 static struct progress
*progress
;
36 * When running under --strict mode, objects whose reachability are
37 * suspect are kept in core without getting written in the object
45 static struct decoration obj_decorate
;
47 static struct obj_buffer
*lookup_object_buffer(struct object
*base
)
49 return lookup_decoration(&obj_decorate
, base
);
52 static void add_object_buffer(struct object
*object
, char *buffer
, unsigned long size
)
54 struct obj_buffer
*obj
;
58 if (add_decoration(&obj_decorate
, object
, obj
))
59 die("object %s tried to add buffer twice!", oid_to_hex(&object
->oid
));
63 * Make sure at least "min" bytes are available in the buffer, and
64 * return the pointer to the buffer.
66 static void *fill(int min
)
69 return buffer
+ offset
;
70 if (min
> sizeof(buffer
))
71 die("cannot fill %d bytes", min
);
73 the_hash_algo
->update_fn(&ctx
, buffer
, offset
);
74 memmove(buffer
, buffer
+ offset
, len
);
78 ssize_t ret
= xread(0, buffer
+ len
, sizeof(buffer
) - len
);
82 die_errno("read error on input");
89 static void use(int bytes
)
92 die("used more bytes than were available");
96 /* make sure off_t is sufficiently large not to wrap */
97 if (signed_add_overflows(consumed_bytes
, bytes
))
98 die("pack too large for current definition of off_t");
99 consumed_bytes
+= bytes
;
100 if (max_input_size
&& consumed_bytes
> max_input_size
)
101 die(_("pack exceeds maximum allowed size"));
102 display_throughput(progress
, consumed_bytes
);
106 * Decompress zstream from the standard input into a newly
107 * allocated buffer of specified size and return the buffer.
108 * The caller is responsible to free the returned buffer.
110 * But for dry_run mode, "get_data()" is only used to check the
111 * integrity of data, and the returned buffer is not used at all.
112 * Therefore, in dry_run mode, "get_data()" will release the small
113 * allocated buffer which is reused to hold temporary zstream output
114 * and return NULL instead of returning garbage data.
116 static void *get_data(unsigned long size
)
119 unsigned long bufsize
= dry_run
&& size
> 8192 ? 8192 : size
;
120 void *buf
= xmallocz(bufsize
);
122 memset(&stream
, 0, sizeof(stream
));
124 stream
.next_out
= buf
;
125 stream
.avail_out
= bufsize
;
126 stream
.next_in
= fill(1);
127 stream
.avail_in
= len
;
128 git_inflate_init(&stream
);
131 int ret
= git_inflate(&stream
, 0);
132 use(len
- stream
.avail_in
);
133 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
136 error("inflate returned %d", ret
);
143 stream
.next_in
= fill(1);
144 stream
.avail_in
= len
;
146 /* reuse the buffer in dry_run mode */
147 stream
.next_out
= buf
;
148 stream
.avail_out
= bufsize
> size
- stream
.total_out
?
149 size
- stream
.total_out
:
153 git_inflate_end(&stream
);
160 struct object_id base_oid
;
165 struct delta_info
*next
;
168 static struct delta_info
*delta_list
;
170 static void add_delta_to_list(unsigned nr
, const struct object_id
*base_oid
,
172 void *delta
, unsigned long size
)
174 struct delta_info
*info
= xmalloc(sizeof(*info
));
176 oidcpy(&info
->base_oid
, base_oid
);
177 info
->base_offset
= base_offset
;
181 info
->next
= delta_list
;
187 struct object_id oid
;
191 /* Remember to update object flag allocation in object.h */
192 #define FLAG_OPEN (1u<<20)
193 #define FLAG_WRITTEN (1u<<21)
195 static struct obj_info
*obj_list
;
196 static unsigned nr_objects
;
199 * Called only from check_object() after it verified this object
202 static void write_cached_object(struct object
*obj
, struct obj_buffer
*obj_buf
)
204 struct object_id oid
;
206 if (write_object_file(obj_buf
->buffer
, obj_buf
->size
,
207 obj
->type
, &oid
) < 0)
208 die("failed to write object %s", oid_to_hex(&obj
->oid
));
209 obj
->flags
|= FLAG_WRITTEN
;
213 * At the very end of the processing, write_rest() scans the objects
214 * that have reachability requirements and calls this function.
215 * Verify its reachability and validity recursively and write it out.
217 static int check_object(struct object
*obj
, enum object_type type
,
218 void *data
, struct fsck_options
*options
)
220 struct obj_buffer
*obj_buf
;
225 if (obj
->flags
& FLAG_WRITTEN
)
228 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
229 die("object type mismatch");
231 if (!(obj
->flags
& FLAG_OPEN
)) {
233 int type
= oid_object_info(the_repository
, &obj
->oid
, &size
);
234 if (type
!= obj
->type
|| type
<= 0)
235 die("object of unexpected type");
236 obj
->flags
|= FLAG_WRITTEN
;
240 obj_buf
= lookup_object_buffer(obj
);
242 die("Whoops! Cannot find object '%s'", oid_to_hex(&obj
->oid
));
243 if (fsck_object(obj
, obj_buf
->buffer
, obj_buf
->size
, &fsck_options
))
244 die("fsck error in packed object");
245 fsck_options
.walk
= check_object
;
246 if (fsck_walk(obj
, NULL
, &fsck_options
))
247 die("Error on reachable objects of %s", oid_to_hex(&obj
->oid
));
248 write_cached_object(obj
, obj_buf
);
252 static void write_rest(void)
255 for (i
= 0; i
< nr_objects
; i
++) {
257 check_object(obj_list
[i
].obj
, OBJ_ANY
, NULL
, NULL
);
261 static void added_object(unsigned nr
, enum object_type type
,
262 void *data
, unsigned long size
);
265 * Write out nr-th object from the list, now we know the contents
266 * of it. Under --strict, this buffers structured objects in-core,
267 * to be checked at the end.
269 static void write_object(unsigned nr
, enum object_type type
,
270 void *buf
, unsigned long size
)
273 if (write_object_file(buf
, size
, type
,
274 &obj_list
[nr
].oid
) < 0)
275 die("failed to write object");
276 added_object(nr
, type
, buf
, size
);
278 obj_list
[nr
].obj
= NULL
;
279 } else if (type
== OBJ_BLOB
) {
281 if (write_object_file(buf
, size
, type
,
282 &obj_list
[nr
].oid
) < 0)
283 die("failed to write object");
284 added_object(nr
, type
, buf
, size
);
287 blob
= lookup_blob(the_repository
, &obj_list
[nr
].oid
);
289 blob
->object
.flags
|= FLAG_WRITTEN
;
291 die("invalid blob object");
292 obj_list
[nr
].obj
= NULL
;
296 hash_object_file(the_hash_algo
, buf
, size
, type
,
298 added_object(nr
, type
, buf
, size
);
299 obj
= parse_object_buffer(the_repository
, &obj_list
[nr
].oid
,
303 die("invalid %s", type_name(type
));
304 add_object_buffer(obj
, buf
, size
);
305 obj
->flags
|= FLAG_OPEN
;
306 obj_list
[nr
].obj
= obj
;
310 static void resolve_delta(unsigned nr
, enum object_type type
,
311 void *base
, unsigned long base_size
,
312 void *delta
, unsigned long delta_size
)
315 unsigned long result_size
;
317 result
= patch_delta(base
, base_size
,
321 die("failed to apply delta");
323 write_object(nr
, type
, result
, result_size
);
327 * We now know the contents of an object (which is nr-th in the pack);
328 * resolve all the deltified objects that are based on it.
330 static void added_object(unsigned nr
, enum object_type type
,
331 void *data
, unsigned long size
)
333 struct delta_info
**p
= &delta_list
;
334 struct delta_info
*info
;
336 while ((info
= *p
) != NULL
) {
337 if (oideq(&info
->base_oid
, &obj_list
[nr
].oid
) ||
338 info
->base_offset
== obj_list
[nr
].offset
) {
341 resolve_delta(info
->nr
, type
, data
, size
,
342 info
->delta
, info
->size
);
350 static void unpack_non_delta_entry(enum object_type type
, unsigned long size
,
353 void *buf
= get_data(size
);
356 write_object(nr
, type
, buf
, size
);
359 struct input_zstream_data
{
360 git_zstream
*zstream
;
361 unsigned char buf
[8192];
365 static const void *feed_input_zstream(struct input_stream
*in_stream
,
366 unsigned long *readlen
)
368 struct input_zstream_data
*data
= in_stream
->data
;
369 git_zstream
*zstream
= data
->zstream
;
372 if (in_stream
->is_finished
) {
377 zstream
->next_out
= data
->buf
;
378 zstream
->avail_out
= sizeof(data
->buf
);
379 zstream
->next_in
= in
;
380 zstream
->avail_in
= len
;
382 data
->status
= git_inflate(zstream
, 0);
384 in_stream
->is_finished
= data
->status
!= Z_OK
;
385 use(len
- zstream
->avail_in
);
386 *readlen
= sizeof(data
->buf
) - zstream
->avail_out
;
391 static void stream_blob(unsigned long size
, unsigned nr
)
393 git_zstream zstream
= { 0 };
394 struct input_zstream_data data
= { 0 };
395 struct input_stream in_stream
= {
396 .read
= feed_input_zstream
,
399 struct obj_info
*info
= &obj_list
[nr
];
401 data
.zstream
= &zstream
;
402 git_inflate_init(&zstream
);
404 if (stream_loose_object(&in_stream
, size
, &info
->oid
))
405 die(_("failed to write object in stream"));
407 if (data
.status
!= Z_STREAM_END
)
408 die(_("inflate returned (%d)"), data
.status
);
409 git_inflate_end(&zstream
);
412 struct blob
*blob
= lookup_blob(the_repository
, &info
->oid
);
415 die(_("invalid blob object from stream"));
416 blob
->object
.flags
|= FLAG_WRITTEN
;
421 static int resolve_against_held(unsigned nr
, const struct object_id
*base
,
422 void *delta_data
, unsigned long delta_size
)
425 struct obj_buffer
*obj_buffer
;
426 obj
= lookup_object(the_repository
, base
);
429 obj_buffer
= lookup_object_buffer(obj
);
432 resolve_delta(nr
, obj
->type
, obj_buffer
->buffer
,
433 obj_buffer
->size
, delta_data
, delta_size
);
437 static void unpack_delta_entry(enum object_type type
, unsigned long delta_size
,
440 void *delta_data
, *base
;
441 unsigned long base_size
;
442 struct object_id base_oid
;
444 if (type
== OBJ_REF_DELTA
) {
445 oidread(&base_oid
, fill(the_hash_algo
->rawsz
));
446 use(the_hash_algo
->rawsz
);
447 delta_data
= get_data(delta_size
);
450 if (repo_has_object_file(the_repository
, &base_oid
))
451 ; /* Ok we have this one */
452 else if (resolve_against_held(nr
, &base_oid
,
453 delta_data
, delta_size
))
454 return; /* we are done */
456 /* cannot resolve yet --- queue it */
457 oidclr(&obj_list
[nr
].oid
);
458 add_delta_to_list(nr
, &base_oid
, 0, delta_data
, delta_size
);
462 unsigned base_found
= 0;
463 unsigned char *pack
, c
;
465 unsigned lo
, mid
, hi
;
470 base_offset
= c
& 127;
473 if (!base_offset
|| MSB(base_offset
, 7))
474 die("offset value overflow for delta base object");
478 base_offset
= (base_offset
<< 7) + (c
& 127);
480 base_offset
= obj_list
[nr
].offset
- base_offset
;
481 if (base_offset
<= 0 || base_offset
>= obj_list
[nr
].offset
)
482 die("offset value out of bound for delta base object");
484 delta_data
= get_data(delta_size
);
490 mid
= lo
+ (hi
- lo
) / 2;
491 if (base_offset
< obj_list
[mid
].offset
) {
493 } else if (base_offset
> obj_list
[mid
].offset
) {
496 oidcpy(&base_oid
, &obj_list
[mid
].oid
);
497 base_found
= !is_null_oid(&base_oid
);
503 * The delta base object is itself a delta that
504 * has not been resolved yet.
506 oidclr(&obj_list
[nr
].oid
);
507 add_delta_to_list(nr
, null_oid(), base_offset
,
508 delta_data
, delta_size
);
513 if (resolve_against_held(nr
, &base_oid
, delta_data
, delta_size
))
516 base
= repo_read_object_file(the_repository
, &base_oid
, &type
,
519 error("failed to read delta-pack base object %s",
520 oid_to_hex(&base_oid
));
526 resolve_delta(nr
, type
, base
, base_size
, delta_data
, delta_size
);
530 static void unpack_one(unsigned nr
)
534 unsigned long size
, c
;
535 enum object_type type
;
537 obj_list
[nr
].offset
= consumed_bytes
;
549 size
+= (c
& 0x7f) << shift
;
555 if (!dry_run
&& size
> big_file_threshold
) {
556 stream_blob(size
, nr
);
563 unpack_non_delta_entry(type
, size
, nr
);
567 unpack_delta_entry(type
, size
, nr
);
570 error("bad object type %d", type
);
578 static void unpack_all(void)
581 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
583 nr_objects
= ntohl(hdr
->hdr_entries
);
585 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
586 die("bad pack file");
587 if (!pack_version_ok(hdr
->hdr_version
))
588 die("unknown pack file version %"PRIu32
,
589 ntohl(hdr
->hdr_version
));
590 use(sizeof(struct pack_header
));
593 progress
= start_progress(_("Unpacking objects"), nr_objects
);
594 CALLOC_ARRAY(obj_list
, nr_objects
);
595 begin_odb_transaction();
596 for (i
= 0; i
< nr_objects
; i
++) {
598 display_progress(progress
, i
+ 1);
600 end_odb_transaction();
601 stop_progress(&progress
);
604 die("unresolved deltas left after unpacking");
607 int cmd_unpack_objects(int argc
, const char **argv
, const char *prefix UNUSED
)
610 struct object_id oid
;
612 disable_replace_refs();
614 git_config(git_default_config
, NULL
);
618 for (i
= 1 ; i
< argc
; i
++) {
619 const char *arg
= argv
[i
];
622 if (!strcmp(arg
, "-n")) {
626 if (!strcmp(arg
, "-q")) {
630 if (!strcmp(arg
, "-r")) {
634 if (!strcmp(arg
, "--strict")) {
638 if (skip_prefix(arg
, "--strict=", &arg
)) {
640 fsck_set_msg_types(&fsck_options
, arg
);
643 if (starts_with(arg
, "--pack_header=")) {
644 struct pack_header
*hdr
;
647 hdr
= (struct pack_header
*)buffer
;
648 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
649 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
652 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
658 if (skip_prefix(arg
, "--max-input-size=", &arg
)) {
659 max_input_size
= strtoumax(arg
, NULL
, 10);
665 /* We don't take any non-flag arguments now.. Maybe some day */
668 the_hash_algo
->init_fn(&ctx
);
670 the_hash_algo
->update_fn(&ctx
, buffer
, offset
);
671 the_hash_algo
->final_oid_fn(&oid
, &ctx
);
674 if (fsck_finish(&fsck_options
))
675 die(_("fsck error in pack objects"));
677 if (!hasheq(fill(the_hash_algo
->rawsz
), oid
.hash
))
678 die("final sha1 did not match");
679 use(the_hash_algo
->rawsz
);
681 /* Write the last part of the buffer to stdout */
683 int ret
= xwrite(1, buffer
+ offset
, len
);