1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "bulk-checkin.h"
5 #include "environment.h"
9 #include "object-store-ll.h"
14 #include "replace-object.h"
20 static int dry_run
, quiet
, recover
, has_errors
, strict
;
21 static const char unpack_usage
[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
23 /* We always read in 4kB chunks. */
24 static unsigned char buffer
[4096];
25 static unsigned int offset
, len
;
26 static off_t consumed_bytes
;
27 static off_t max_input_size
;
28 static git_hash_ctx ctx
;
29 static struct fsck_options fsck_options
= FSCK_OPTIONS_STRICT
;
30 static struct progress
*progress
;
33 * When running under --strict mode, objects whose reachability are
34 * suspect are kept in core without getting written in the object
42 static struct decoration obj_decorate
;
44 static struct obj_buffer
*lookup_object_buffer(struct object
*base
)
46 return lookup_decoration(&obj_decorate
, base
);
49 static void add_object_buffer(struct object
*object
, char *buffer
, unsigned long size
)
51 struct obj_buffer
*obj
;
55 if (add_decoration(&obj_decorate
, object
, obj
))
56 die("object %s tried to add buffer twice!", oid_to_hex(&object
->oid
));
60 * Make sure at least "min" bytes are available in the buffer, and
61 * return the pointer to the buffer.
63 static void *fill(int min
)
66 return buffer
+ offset
;
67 if (min
> sizeof(buffer
))
68 die("cannot fill %d bytes", min
);
70 the_hash_algo
->update_fn(&ctx
, buffer
, offset
);
71 memmove(buffer
, buffer
+ offset
, len
);
75 ssize_t ret
= xread(0, buffer
+ len
, sizeof(buffer
) - len
);
79 die_errno("read error on input");
86 static void use(int bytes
)
89 die("used more bytes than were available");
93 /* make sure off_t is sufficiently large not to wrap */
94 if (signed_add_overflows(consumed_bytes
, bytes
))
95 die("pack too large for current definition of off_t");
96 consumed_bytes
+= bytes
;
97 if (max_input_size
&& consumed_bytes
> max_input_size
)
98 die(_("pack exceeds maximum allowed size"));
99 display_throughput(progress
, consumed_bytes
);
103 * Decompress zstream from the standard input into a newly
104 * allocated buffer of specified size and return the buffer.
105 * The caller is responsible to free the returned buffer.
107 * But for dry_run mode, "get_data()" is only used to check the
108 * integrity of data, and the returned buffer is not used at all.
109 * Therefore, in dry_run mode, "get_data()" will release the small
110 * allocated buffer which is reused to hold temporary zstream output
111 * and return NULL instead of returning garbage data.
113 static void *get_data(unsigned long size
)
116 unsigned long bufsize
= dry_run
&& size
> 8192 ? 8192 : size
;
117 void *buf
= xmallocz(bufsize
);
119 memset(&stream
, 0, sizeof(stream
));
121 stream
.next_out
= buf
;
122 stream
.avail_out
= bufsize
;
123 stream
.next_in
= fill(1);
124 stream
.avail_in
= len
;
125 git_inflate_init(&stream
);
128 int ret
= git_inflate(&stream
, 0);
129 use(len
- stream
.avail_in
);
130 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
133 error("inflate returned %d", ret
);
140 stream
.next_in
= fill(1);
141 stream
.avail_in
= len
;
143 /* reuse the buffer in dry_run mode */
144 stream
.next_out
= buf
;
145 stream
.avail_out
= bufsize
> size
- stream
.total_out
?
146 size
- stream
.total_out
:
150 git_inflate_end(&stream
);
157 struct object_id base_oid
;
162 struct delta_info
*next
;
165 static struct delta_info
*delta_list
;
167 static void add_delta_to_list(unsigned nr
, const struct object_id
*base_oid
,
169 void *delta
, unsigned long size
)
171 struct delta_info
*info
= xmalloc(sizeof(*info
));
173 oidcpy(&info
->base_oid
, base_oid
);
174 info
->base_offset
= base_offset
;
178 info
->next
= delta_list
;
184 struct object_id oid
;
188 /* Remember to update object flag allocation in object.h */
189 #define FLAG_OPEN (1u<<20)
190 #define FLAG_WRITTEN (1u<<21)
192 static struct obj_info
*obj_list
;
193 static unsigned nr_objects
;
196 * Called only from check_object() after it verified this object
199 static void write_cached_object(struct object
*obj
, struct obj_buffer
*obj_buf
)
201 struct object_id oid
;
203 if (write_object_file(obj_buf
->buffer
, obj_buf
->size
,
204 obj
->type
, &oid
) < 0)
205 die("failed to write object %s", oid_to_hex(&obj
->oid
));
206 obj
->flags
|= FLAG_WRITTEN
;
210 * At the very end of the processing, write_rest() scans the objects
211 * that have reachability requirements and calls this function.
212 * Verify its reachability and validity recursively and write it out.
214 static int check_object(struct object
*obj
, enum object_type type
,
216 struct fsck_options
*options UNUSED
)
218 struct obj_buffer
*obj_buf
;
223 if (obj
->flags
& FLAG_WRITTEN
)
226 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
227 die("object type mismatch");
229 if (!(obj
->flags
& FLAG_OPEN
)) {
231 int type
= oid_object_info(the_repository
, &obj
->oid
, &size
);
232 if (type
!= obj
->type
|| type
<= 0)
233 die("object of unexpected type");
234 obj
->flags
|= FLAG_WRITTEN
;
238 obj_buf
= lookup_object_buffer(obj
);
240 die("Whoops! Cannot find object '%s'", oid_to_hex(&obj
->oid
));
241 if (fsck_object(obj
, obj_buf
->buffer
, obj_buf
->size
, &fsck_options
))
242 die("fsck error in packed object");
243 fsck_options
.walk
= check_object
;
244 if (fsck_walk(obj
, NULL
, &fsck_options
))
245 die("Error on reachable objects of %s", oid_to_hex(&obj
->oid
));
246 write_cached_object(obj
, obj_buf
);
250 static void write_rest(void)
253 for (i
= 0; i
< nr_objects
; i
++) {
255 check_object(obj_list
[i
].obj
, OBJ_ANY
, NULL
, NULL
);
259 static void added_object(unsigned nr
, enum object_type type
,
260 void *data
, unsigned long size
);
263 * Write out nr-th object from the list, now we know the contents
264 * of it. Under --strict, this buffers structured objects in-core,
265 * to be checked at the end.
267 static void write_object(unsigned nr
, enum object_type type
,
268 void *buf
, unsigned long size
)
271 if (write_object_file(buf
, size
, type
,
272 &obj_list
[nr
].oid
) < 0)
273 die("failed to write object");
274 added_object(nr
, type
, buf
, size
);
276 obj_list
[nr
].obj
= NULL
;
277 } else if (type
== OBJ_BLOB
) {
279 if (write_object_file(buf
, size
, type
,
280 &obj_list
[nr
].oid
) < 0)
281 die("failed to write object");
282 added_object(nr
, type
, buf
, size
);
285 blob
= lookup_blob(the_repository
, &obj_list
[nr
].oid
);
287 blob
->object
.flags
|= FLAG_WRITTEN
;
289 die("invalid blob object");
290 obj_list
[nr
].obj
= NULL
;
294 hash_object_file(the_hash_algo
, buf
, size
, type
,
296 added_object(nr
, type
, buf
, size
);
297 obj
= parse_object_buffer(the_repository
, &obj_list
[nr
].oid
,
301 die("invalid %s", type_name(type
));
302 add_object_buffer(obj
, buf
, size
);
303 obj
->flags
|= FLAG_OPEN
;
304 obj_list
[nr
].obj
= obj
;
308 static void resolve_delta(unsigned nr
, enum object_type type
,
309 void *base
, unsigned long base_size
,
310 void *delta
, unsigned long delta_size
)
313 unsigned long result_size
;
315 result
= patch_delta(base
, base_size
,
319 die("failed to apply delta");
321 write_object(nr
, type
, result
, result_size
);
325 * We now know the contents of an object (which is nr-th in the pack);
326 * resolve all the deltified objects that are based on it.
328 static void added_object(unsigned nr
, enum object_type type
,
329 void *data
, unsigned long size
)
331 struct delta_info
**p
= &delta_list
;
332 struct delta_info
*info
;
334 while ((info
= *p
) != NULL
) {
335 if (oideq(&info
->base_oid
, &obj_list
[nr
].oid
) ||
336 info
->base_offset
== obj_list
[nr
].offset
) {
339 resolve_delta(info
->nr
, type
, data
, size
,
340 info
->delta
, info
->size
);
348 static void unpack_non_delta_entry(enum object_type type
, unsigned long size
,
351 void *buf
= get_data(size
);
354 write_object(nr
, type
, buf
, size
);
357 struct input_zstream_data
{
358 git_zstream
*zstream
;
359 unsigned char buf
[8192];
363 static const void *feed_input_zstream(struct input_stream
*in_stream
,
364 unsigned long *readlen
)
366 struct input_zstream_data
*data
= in_stream
->data
;
367 git_zstream
*zstream
= data
->zstream
;
370 if (in_stream
->is_finished
) {
375 zstream
->next_out
= data
->buf
;
376 zstream
->avail_out
= sizeof(data
->buf
);
377 zstream
->next_in
= in
;
378 zstream
->avail_in
= len
;
380 data
->status
= git_inflate(zstream
, 0);
382 in_stream
->is_finished
= data
->status
!= Z_OK
;
383 use(len
- zstream
->avail_in
);
384 *readlen
= sizeof(data
->buf
) - zstream
->avail_out
;
389 static void stream_blob(unsigned long size
, unsigned nr
)
391 git_zstream zstream
= { 0 };
392 struct input_zstream_data data
= { 0 };
393 struct input_stream in_stream
= {
394 .read
= feed_input_zstream
,
397 struct obj_info
*info
= &obj_list
[nr
];
399 data
.zstream
= &zstream
;
400 git_inflate_init(&zstream
);
402 if (stream_loose_object(&in_stream
, size
, &info
->oid
))
403 die(_("failed to write object in stream"));
405 if (data
.status
!= Z_STREAM_END
)
406 die(_("inflate returned (%d)"), data
.status
);
407 git_inflate_end(&zstream
);
410 struct blob
*blob
= lookup_blob(the_repository
, &info
->oid
);
413 die(_("invalid blob object from stream"));
414 blob
->object
.flags
|= FLAG_WRITTEN
;
419 static int resolve_against_held(unsigned nr
, const struct object_id
*base
,
420 void *delta_data
, unsigned long delta_size
)
423 struct obj_buffer
*obj_buffer
;
424 obj
= lookup_object(the_repository
, base
);
427 obj_buffer
= lookup_object_buffer(obj
);
430 resolve_delta(nr
, obj
->type
, obj_buffer
->buffer
,
431 obj_buffer
->size
, delta_data
, delta_size
);
435 static void unpack_delta_entry(enum object_type type
, unsigned long delta_size
,
438 void *delta_data
, *base
;
439 unsigned long base_size
;
440 struct object_id base_oid
;
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
);
448 if (repo_has_object_file(the_repository
, &base_oid
))
449 ; /* Ok we have this one */
450 else if (resolve_against_held(nr
, &base_oid
,
451 delta_data
, delta_size
))
452 return; /* we are done */
454 /* cannot resolve yet --- queue it */
455 oidclr(&obj_list
[nr
].oid
, the_repository
->hash_algo
);
456 add_delta_to_list(nr
, &base_oid
, 0, delta_data
, delta_size
);
460 unsigned base_found
= 0;
461 unsigned char *pack
, c
;
463 unsigned lo
, mid
, hi
;
468 base_offset
= c
& 127;
471 if (!base_offset
|| MSB(base_offset
, 7))
472 die("offset value overflow for delta base object");
476 base_offset
= (base_offset
<< 7) + (c
& 127);
478 base_offset
= obj_list
[nr
].offset
- base_offset
;
479 if (base_offset
<= 0 || base_offset
>= obj_list
[nr
].offset
)
480 die("offset value out of bound for delta base object");
482 delta_data
= get_data(delta_size
);
488 mid
= lo
+ (hi
- lo
) / 2;
489 if (base_offset
< obj_list
[mid
].offset
) {
491 } else if (base_offset
> obj_list
[mid
].offset
) {
494 oidcpy(&base_oid
, &obj_list
[mid
].oid
);
495 base_found
= !is_null_oid(&base_oid
);
501 * The delta base object is itself a delta that
502 * has not been resolved yet.
504 oidclr(&obj_list
[nr
].oid
, the_repository
->hash_algo
);
505 add_delta_to_list(nr
, null_oid(), base_offset
,
506 delta_data
, delta_size
);
511 if (resolve_against_held(nr
, &base_oid
, delta_data
, delta_size
))
514 base
= repo_read_object_file(the_repository
, &base_oid
, &type
,
517 error("failed to read delta-pack base object %s",
518 oid_to_hex(&base_oid
));
524 resolve_delta(nr
, type
, base
, base_size
, delta_data
, delta_size
);
528 static void unpack_one(unsigned nr
)
532 unsigned long size
, c
;
533 enum object_type type
;
535 obj_list
[nr
].offset
= consumed_bytes
;
547 size
+= (c
& 0x7f) << shift
;
553 if (!dry_run
&& size
> big_file_threshold
) {
554 stream_blob(size
, nr
);
561 unpack_non_delta_entry(type
, size
, nr
);
565 unpack_delta_entry(type
, size
, nr
);
568 error("bad object type %d", type
);
576 static void unpack_all(void)
579 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
581 nr_objects
= ntohl(hdr
->hdr_entries
);
583 if (ntohl(hdr
->hdr_signature
) != PACK_SIGNATURE
)
584 die("bad pack file");
585 if (!pack_version_ok(hdr
->hdr_version
))
586 die("unknown pack file version %"PRIu32
,
587 ntohl(hdr
->hdr_version
));
588 use(sizeof(struct pack_header
));
591 progress
= start_progress(_("Unpacking objects"), nr_objects
);
592 CALLOC_ARRAY(obj_list
, nr_objects
);
593 begin_odb_transaction();
594 for (i
= 0; i
< nr_objects
; i
++) {
596 display_progress(progress
, i
+ 1);
598 end_odb_transaction();
599 stop_progress(&progress
);
602 die("unresolved deltas left after unpacking");
605 int cmd_unpack_objects(int argc
,
607 const char *prefix UNUSED
,
608 struct repository
*repo UNUSED
)
611 struct object_id oid
;
612 git_hash_ctx tmp_ctx
;
614 disable_replace_refs();
616 git_config(git_default_config
, NULL
);
620 for (i
= 1 ; i
< argc
; i
++) {
621 const char *arg
= argv
[i
];
624 if (!strcmp(arg
, "-n")) {
628 if (!strcmp(arg
, "-q")) {
632 if (!strcmp(arg
, "-r")) {
636 if (!strcmp(arg
, "--strict")) {
640 if (skip_prefix(arg
, "--strict=", &arg
)) {
642 fsck_set_msg_types(&fsck_options
, arg
);
645 if (starts_with(arg
, "--pack_header=")) {
646 struct pack_header
*hdr
;
649 hdr
= (struct pack_header
*)buffer
;
650 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
651 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
654 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
660 if (skip_prefix(arg
, "--max-input-size=", &arg
)) {
661 max_input_size
= strtoumax(arg
, NULL
, 10);
667 /* We don't take any non-flag arguments now.. Maybe some day */
670 the_hash_algo
->init_fn(&ctx
);
672 the_hash_algo
->update_fn(&ctx
, buffer
, offset
);
673 the_hash_algo
->init_fn(&tmp_ctx
);
674 the_hash_algo
->clone_fn(&tmp_ctx
, &ctx
);
675 the_hash_algo
->final_oid_fn(&oid
, &tmp_ctx
);
678 if (fsck_finish(&fsck_options
))
679 die(_("fsck error in pack objects"));
681 if (!hasheq(fill(the_hash_algo
->rawsz
), oid
.hash
,
682 the_repository
->hash_algo
))
683 die("final sha1 did not match");
684 use(the_hash_algo
->rawsz
);
686 /* Write the last part of the buffer to stdout */
687 write_in_full(1, buffer
+ offset
, len
);