4 #include "object-store.h"
12 #include "tree-walk.h"
17 static int dry_run, quiet, recover, has_errors, strict;
18 static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
20 /* We always read in 4kB chunks. */
21 static unsigned char buffer[4096];
22 static unsigned int offset, len;
23 static off_t consumed_bytes;
24 static off_t max_input_size;
25 static git_hash_ctx ctx;
26 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
27 static struct progress *progress;
30 * When running under --strict mode, objects whose reachability are
31 * suspect are kept in core without getting written in the object
39 static struct decoration obj_decorate;
41 static struct obj_buffer *lookup_object_buffer(struct object *base)
43 return lookup_decoration(&obj_decorate, base);
46 static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
48 struct obj_buffer *obj;
49 obj = xcalloc(1, sizeof(struct obj_buffer));
52 if (add_decoration(&obj_decorate, object, obj))
53 die("object %s tried to add buffer twice!", oid_to_hex(&object->oid));
57 * Make sure at least "min" bytes are available in the buffer, and
58 * return the pointer to the buffer.
60 static void *fill(int min)
63 return buffer + offset;
64 if (min > sizeof(buffer))
65 die("cannot fill %d bytes", min);
67 the_hash_algo->update_fn(&ctx, buffer, offset);
68 memmove(buffer, buffer + offset, len);
72 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
76 die_errno("read error on input");
83 static void use(int bytes)
86 die("used more bytes than were available");
90 /* make sure off_t is sufficiently large not to wrap */
91 if (signed_add_overflows(consumed_bytes, bytes))
92 die("pack too large for current definition of off_t");
93 consumed_bytes += bytes;
94 if (max_input_size && consumed_bytes > max_input_size)
95 die(_("pack exceeds maximum allowed size"));
96 display_throughput(progress, consumed_bytes);
99 static void *get_data(unsigned long size)
102 void *buf = xmallocz(size);
104 memset(&stream, 0, sizeof(stream));
106 stream.next_out = buf;
107 stream.avail_out = size;
108 stream.next_in = fill(1);
109 stream.avail_in = len;
110 git_inflate_init(&stream);
113 int ret = git_inflate(&stream, 0);
114 use(len - stream.avail_in);
115 if (stream.total_out == size && ret == Z_STREAM_END)
118 error("inflate returned %d", ret);
125 stream.next_in = fill(1);
126 stream.avail_in = len;
128 git_inflate_end(&stream);
133 struct object_id base_oid;
138 struct delta_info *next;
141 static struct delta_info *delta_list;
143 static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
145 void *delta, unsigned long size)
147 struct delta_info *info = xmalloc(sizeof(*info));
149 oidcpy(&info->base_oid, base_oid);
150 info->base_offset = base_offset;
154 info->next = delta_list;
160 struct object_id oid;
164 /* Remember to update object flag allocation in object.h */
165 #define FLAG_OPEN (1u<<20)
166 #define FLAG_WRITTEN (1u<<21)
168 static struct obj_info *obj_list;
169 static unsigned nr_objects;
172 * Called only from check_object() after it verified this object
175 static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
177 struct object_id oid;
179 if (write_object_file(obj_buf->buffer, obj_buf->size,
180 type_name(obj->type), &oid) < 0)
181 die("failed to write object %s", oid_to_hex(&obj->oid));
182 obj->flags |= FLAG_WRITTEN;
186 * At the very end of the processing, write_rest() scans the objects
187 * that have reachability requirements and calls this function.
188 * Verify its reachability and validity recursively and write it out.
190 static int check_object(struct object *obj, int type, void *data, struct fsck_options *options)
192 struct obj_buffer *obj_buf;
197 if (obj->flags & FLAG_WRITTEN)
200 if (type != OBJ_ANY && obj->type != type)
201 die("object type mismatch");
203 if (!(obj->flags & FLAG_OPEN)) {
205 int type = oid_object_info(the_repository, &obj->oid, &size);
206 if (type != obj->type || type <= 0)
207 die("object of unexpected type");
208 obj->flags |= FLAG_WRITTEN;
212 obj_buf = lookup_object_buffer(obj);
214 die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
215 if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
216 die("fsck error in packed object");
217 fsck_options.walk = check_object;
218 if (fsck_walk(obj, NULL, &fsck_options))
219 die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
220 write_cached_object(obj, obj_buf);
224 static void write_rest(void)
227 for (i = 0; i < nr_objects; i++) {
229 check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL);
233 static void added_object(unsigned nr, enum object_type type,
234 void *data, unsigned long size);
237 * Write out nr-th object from the list, now we know the contents
238 * of it. Under --strict, this buffers structured objects in-core,
239 * to be checked at the end.
241 static void write_object(unsigned nr, enum object_type type,
242 void *buf, unsigned long size)
245 if (write_object_file(buf, size, type_name(type),
246 &obj_list[nr].oid) < 0)
247 die("failed to write object");
248 added_object(nr, type, buf, size);
250 obj_list[nr].obj = NULL;
251 } else if (type == OBJ_BLOB) {
253 if (write_object_file(buf, size, type_name(type),
254 &obj_list[nr].oid) < 0)
255 die("failed to write object");
256 added_object(nr, type, buf, size);
259 blob = lookup_blob(the_repository, &obj_list[nr].oid);
261 blob->object.flags |= FLAG_WRITTEN;
263 die("invalid blob object");
264 obj_list[nr].obj = NULL;
268 hash_object_file(buf, size, type_name(type), &obj_list[nr].oid);
269 added_object(nr, type, buf, size);
270 obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
274 die("invalid %s", type_name(type));
275 add_object_buffer(obj, buf, size);
276 obj->flags |= FLAG_OPEN;
277 obj_list[nr].obj = obj;
281 static void resolve_delta(unsigned nr, enum object_type type,
282 void *base, unsigned long base_size,
283 void *delta, unsigned long delta_size)
286 unsigned long result_size;
288 result = patch_delta(base, base_size,
292 die("failed to apply delta");
294 write_object(nr, type, result, result_size);
298 * We now know the contents of an object (which is nr-th in the pack);
299 * resolve all the deltified objects that are based on it.
301 static void added_object(unsigned nr, enum object_type type,
302 void *data, unsigned long size)
304 struct delta_info **p = &delta_list;
305 struct delta_info *info;
307 while ((info = *p) != NULL) {
308 if (oideq(&info->base_oid, &obj_list[nr].oid) ||
309 info->base_offset == obj_list[nr].offset) {
312 resolve_delta(info->nr, type, data, size,
313 info->delta, info->size);
321 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
324 void *buf = get_data(size);
327 write_object(nr, type, buf, size);
332 static int resolve_against_held(unsigned nr, const struct object_id *base,
333 void *delta_data, unsigned long delta_size)
336 struct obj_buffer *obj_buffer;
337 obj = lookup_object(the_repository, base);
340 obj_buffer = lookup_object_buffer(obj);
343 resolve_delta(nr, obj->type, obj_buffer->buffer,
344 obj_buffer->size, delta_data, delta_size);
348 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
351 void *delta_data, *base;
352 unsigned long base_size;
353 struct object_id base_oid;
355 if (type == OBJ_REF_DELTA) {
356 hashcpy(base_oid.hash, fill(the_hash_algo->rawsz));
357 use(the_hash_algo->rawsz);
358 delta_data = get_data(delta_size);
359 if (dry_run || !delta_data) {
363 if (has_object_file(&base_oid))
364 ; /* Ok we have this one */
365 else if (resolve_against_held(nr, &base_oid,
366 delta_data, delta_size))
367 return; /* we are done */
369 /* cannot resolve yet --- queue it */
370 oidclr(&obj_list[nr].oid);
371 add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
375 unsigned base_found = 0;
376 unsigned char *pack, c;
378 unsigned lo, mid, hi;
383 base_offset = c & 127;
386 if (!base_offset || MSB(base_offset, 7))
387 die("offset value overflow for delta base object");
391 base_offset = (base_offset << 7) + (c & 127);
393 base_offset = obj_list[nr].offset - base_offset;
394 if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
395 die("offset value out of bound for delta base object");
397 delta_data = get_data(delta_size);
398 if (dry_run || !delta_data) {
405 mid = lo + (hi - lo) / 2;
406 if (base_offset < obj_list[mid].offset) {
408 } else if (base_offset > obj_list[mid].offset) {
411 oidcpy(&base_oid, &obj_list[mid].oid);
412 base_found = !is_null_oid(&base_oid);
418 * The delta base object is itself a delta that
419 * has not been resolved yet.
421 oidclr(&obj_list[nr].oid);
422 add_delta_to_list(nr, &null_oid, base_offset, delta_data, delta_size);
427 if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
430 base = read_object_file(&base_oid, &type, &base_size);
432 error("failed to read delta-pack base object %s",
433 oid_to_hex(&base_oid));
439 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
443 static void unpack_one(unsigned nr)
447 unsigned long size, c;
448 enum object_type type;
450 obj_list[nr].offset = consumed_bytes;
462 size += (c & 0x7f) << shift;
471 unpack_non_delta_entry(type, size, nr);
475 unpack_delta_entry(type, size, nr);
478 error("bad object type %d", type);
486 static void unpack_all(void)
489 struct pack_header *hdr = fill(sizeof(struct pack_header));
491 nr_objects = ntohl(hdr->hdr_entries);
493 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
494 die("bad pack file");
495 if (!pack_version_ok(hdr->hdr_version))
496 die("unknown pack file version %"PRIu32,
497 ntohl(hdr->hdr_version));
498 use(sizeof(struct pack_header));
501 progress = start_progress(_("Unpacking objects"), nr_objects);
502 obj_list = xcalloc(nr_objects, sizeof(*obj_list));
503 for (i = 0; i < nr_objects; i++) {
505 display_progress(progress, i + 1);
507 stop_progress(&progress);
510 die("unresolved deltas left after unpacking");
513 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
516 struct object_id oid;
518 read_replace_refs = 0;
520 git_config(git_default_config, NULL);
524 for (i = 1 ; i < argc; i++) {
525 const char *arg = argv[i];
528 if (!strcmp(arg, "-n")) {
532 if (!strcmp(arg, "-q")) {
536 if (!strcmp(arg, "-r")) {
540 if (!strcmp(arg, "--strict")) {
544 if (skip_prefix(arg, "--strict=", &arg)) {
546 fsck_set_msg_types(&fsck_options, arg);
549 if (starts_with(arg, "--pack_header=")) {
550 struct pack_header *hdr;
553 hdr = (struct pack_header *)buffer;
554 hdr->hdr_signature = htonl(PACK_SIGNATURE);
555 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
558 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
564 if (skip_prefix(arg, "--max-input-size=", &arg)) {
565 max_input_size = strtoumax(arg, NULL, 10);
571 /* We don't take any non-flag arguments now.. Maybe some day */
574 the_hash_algo->init_fn(&ctx);
576 the_hash_algo->update_fn(&ctx, buffer, offset);
577 the_hash_algo->final_fn(oid.hash, &ctx);
580 if (fsck_finish(&fsck_options))
581 die(_("fsck error in pack objects"));
583 if (!hasheq(fill(the_hash_algo->rawsz), oid.hash))
584 die("final sha1 did not match");
585 use(the_hash_algo->rawsz);
587 /* Write the last part of the buffer to stdout */
589 int ret = xwrite(1, buffer + offset, len);