10 #include "tree-walk.h"
15 static int dry_run, quiet, recover, has_errors, strict;
16 static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
18 /* We always read in 4kB chunks. */
19 static unsigned char buffer[4096];
20 static unsigned int offset, len;
21 static off_t consumed_bytes;
22 static off_t max_input_size;
23 static git_SHA_CTX ctx;
24 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
27 * When running under --strict mode, objects whose reachability are
28 * suspect are kept in core without getting written in the object
36 static struct decoration obj_decorate;
38 static struct obj_buffer *lookup_object_buffer(struct object *base)
40 return lookup_decoration(&obj_decorate, base);
43 static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
45 struct obj_buffer *obj;
46 obj = xcalloc(1, sizeof(struct obj_buffer));
49 if (add_decoration(&obj_decorate, object, obj))
50 die("object %s tried to add buffer twice!", oid_to_hex(&object->oid));
54 * Make sure at least "min" bytes are available in the buffer, and
55 * return the pointer to the buffer.
57 static void *fill(int min)
60 return buffer + offset;
61 if (min > sizeof(buffer))
62 die("cannot fill %d bytes", min);
64 git_SHA1_Update(&ctx, buffer, offset);
65 memmove(buffer, buffer + offset, len);
69 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
73 die_errno("read error on input");
80 static void use(int bytes)
83 die("used more bytes than were available");
87 /* make sure off_t is sufficiently large not to wrap */
88 if (signed_add_overflows(consumed_bytes, bytes))
89 die("pack too large for current definition of off_t");
90 consumed_bytes += bytes;
91 if (max_input_size && consumed_bytes > max_input_size)
92 die(_("pack exceeds maximum allowed size"));
95 static void *get_data(unsigned long size)
98 void *buf = xmallocz(size);
100 memset(&stream, 0, sizeof(stream));
102 stream.next_out = buf;
103 stream.avail_out = size;
104 stream.next_in = fill(1);
105 stream.avail_in = len;
106 git_inflate_init(&stream);
109 int ret = git_inflate(&stream, 0);
110 use(len - stream.avail_in);
111 if (stream.total_out == size && ret == Z_STREAM_END)
114 error("inflate returned %d", ret);
121 stream.next_in = fill(1);
122 stream.avail_in = len;
124 git_inflate_end(&stream);
129 struct object_id base_oid;
134 struct delta_info *next;
137 static struct delta_info *delta_list;
139 static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
141 void *delta, unsigned long size)
143 struct delta_info *info = xmalloc(sizeof(*info));
145 oidcpy(&info->base_oid, base_oid);
146 info->base_offset = base_offset;
150 info->next = delta_list;
156 struct object_id oid;
160 #define FLAG_OPEN (1u<<20)
161 #define FLAG_WRITTEN (1u<<21)
163 static struct obj_info *obj_list;
164 static unsigned nr_objects;
167 * Called only from check_object() after it verified this object
170 static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
172 struct object_id oid;
174 if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), oid.hash) < 0)
175 die("failed to write object %s", oid_to_hex(&obj->oid));
176 obj->flags |= FLAG_WRITTEN;
180 * At the very end of the processing, write_rest() scans the objects
181 * that have reachability requirements and calls this function.
182 * Verify its reachability and validity recursively and write it out.
184 static int check_object(struct object *obj, int type, void *data, struct fsck_options *options)
186 struct obj_buffer *obj_buf;
191 if (obj->flags & FLAG_WRITTEN)
194 if (type != OBJ_ANY && obj->type != type)
195 die("object type mismatch");
197 if (!(obj->flags & FLAG_OPEN)) {
199 int type = sha1_object_info(obj->oid.hash, &size);
200 if (type != obj->type || type <= 0)
201 die("object of unexpected type");
202 obj->flags |= FLAG_WRITTEN;
206 obj_buf = lookup_object_buffer(obj);
208 die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
209 if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
210 die("Error in object");
211 fsck_options.walk = check_object;
212 if (fsck_walk(obj, NULL, &fsck_options))
213 die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
214 write_cached_object(obj, obj_buf);
218 static void write_rest(void)
221 for (i = 0; i < nr_objects; i++) {
223 check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL);
227 static void added_object(unsigned nr, enum object_type type,
228 void *data, unsigned long size);
231 * Write out nr-th object from the list, now we know the contents
232 * of it. Under --strict, this buffers structured objects in-core,
233 * to be checked at the end.
235 static void write_object(unsigned nr, enum object_type type,
236 void *buf, unsigned long size)
239 if (write_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash) < 0)
240 die("failed to write object");
241 added_object(nr, type, buf, size);
243 obj_list[nr].obj = NULL;
244 } else if (type == OBJ_BLOB) {
246 if (write_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash) < 0)
247 die("failed to write object");
248 added_object(nr, type, buf, size);
251 blob = lookup_blob(&obj_list[nr].oid);
253 blob->object.flags |= FLAG_WRITTEN;
255 die("invalid blob object");
256 obj_list[nr].obj = NULL;
260 hash_sha1_file(buf, size, typename(type), obj_list[nr].oid.hash);
261 added_object(nr, type, buf, size);
262 obj = parse_object_buffer(&obj_list[nr].oid, type, size, buf,
265 die("invalid %s", typename(type));
266 add_object_buffer(obj, buf, size);
267 obj->flags |= FLAG_OPEN;
268 obj_list[nr].obj = obj;
272 static void resolve_delta(unsigned nr, enum object_type type,
273 void *base, unsigned long base_size,
274 void *delta, unsigned long delta_size)
277 unsigned long result_size;
279 result = patch_delta(base, base_size,
283 die("failed to apply delta");
285 write_object(nr, type, result, result_size);
289 * We now know the contents of an object (which is nr-th in the pack);
290 * resolve all the deltified objects that are based on it.
292 static void added_object(unsigned nr, enum object_type type,
293 void *data, unsigned long size)
295 struct delta_info **p = &delta_list;
296 struct delta_info *info;
298 while ((info = *p) != NULL) {
299 if (!oidcmp(&info->base_oid, &obj_list[nr].oid) ||
300 info->base_offset == obj_list[nr].offset) {
303 resolve_delta(info->nr, type, data, size,
304 info->delta, info->size);
312 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
315 void *buf = get_data(size);
318 write_object(nr, type, buf, size);
323 static int resolve_against_held(unsigned nr, const struct object_id *base,
324 void *delta_data, unsigned long delta_size)
327 struct obj_buffer *obj_buffer;
328 obj = lookup_object(base->hash);
331 obj_buffer = lookup_object_buffer(obj);
334 resolve_delta(nr, obj->type, obj_buffer->buffer,
335 obj_buffer->size, delta_data, delta_size);
339 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
342 void *delta_data, *base;
343 unsigned long base_size;
344 struct object_id base_oid;
346 if (type == OBJ_REF_DELTA) {
347 hashcpy(base_oid.hash, fill(GIT_SHA1_RAWSZ));
349 delta_data = get_data(delta_size);
350 if (dry_run || !delta_data) {
354 if (has_object_file(&base_oid))
355 ; /* Ok we have this one */
356 else if (resolve_against_held(nr, &base_oid,
357 delta_data, delta_size))
358 return; /* we are done */
360 /* cannot resolve yet --- queue it */
361 oidclr(&obj_list[nr].oid);
362 add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
366 unsigned base_found = 0;
367 unsigned char *pack, c;
369 unsigned lo, mid, hi;
374 base_offset = c & 127;
377 if (!base_offset || MSB(base_offset, 7))
378 die("offset value overflow for delta base object");
382 base_offset = (base_offset << 7) + (c & 127);
384 base_offset = obj_list[nr].offset - base_offset;
385 if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
386 die("offset value out of bound for delta base object");
388 delta_data = get_data(delta_size);
389 if (dry_run || !delta_data) {
397 if (base_offset < obj_list[mid].offset) {
399 } else if (base_offset > obj_list[mid].offset) {
402 oidcpy(&base_oid, &obj_list[mid].oid);
403 base_found = !is_null_oid(&base_oid);
409 * The delta base object is itself a delta that
410 * has not been resolved yet.
412 oidclr(&obj_list[nr].oid);
413 add_delta_to_list(nr, &null_oid, base_offset, delta_data, delta_size);
418 if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
421 base = read_sha1_file(base_oid.hash, &type, &base_size);
423 error("failed to read delta-pack base object %s",
424 oid_to_hex(&base_oid));
430 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
434 static void unpack_one(unsigned nr)
438 unsigned long size, c;
439 enum object_type type;
441 obj_list[nr].offset = consumed_bytes;
453 size += (c & 0x7f) << shift;
462 unpack_non_delta_entry(type, size, nr);
466 unpack_delta_entry(type, size, nr);
469 error("bad object type %d", type);
477 static void unpack_all(void)
480 struct progress *progress = NULL;
481 struct pack_header *hdr = fill(sizeof(struct pack_header));
483 nr_objects = ntohl(hdr->hdr_entries);
485 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
486 die("bad pack file");
487 if (!pack_version_ok(hdr->hdr_version))
488 die("unknown pack file version %"PRIu32,
489 ntohl(hdr->hdr_version));
490 use(sizeof(struct pack_header));
493 progress = start_progress(_("Unpacking objects"), nr_objects);
494 obj_list = xcalloc(nr_objects, sizeof(*obj_list));
495 for (i = 0; i < nr_objects; i++) {
497 display_progress(progress, i + 1);
499 stop_progress(&progress);
502 die("unresolved deltas left after unpacking");
505 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
508 struct object_id oid;
510 check_replace_refs = 0;
512 git_config(git_default_config, NULL);
516 for (i = 1 ; i < argc; i++) {
517 const char *arg = argv[i];
520 if (!strcmp(arg, "-n")) {
524 if (!strcmp(arg, "-q")) {
528 if (!strcmp(arg, "-r")) {
532 if (!strcmp(arg, "--strict")) {
536 if (skip_prefix(arg, "--strict=", &arg)) {
538 fsck_set_msg_types(&fsck_options, arg);
541 if (starts_with(arg, "--pack_header=")) {
542 struct pack_header *hdr;
545 hdr = (struct pack_header *)buffer;
546 hdr->hdr_signature = htonl(PACK_SIGNATURE);
547 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
550 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
556 if (skip_prefix(arg, "--max-input-size=", &arg)) {
557 max_input_size = strtoumax(arg, NULL, 10);
563 /* We don't take any non-flag arguments now.. Maybe some day */
568 git_SHA1_Update(&ctx, buffer, offset);
569 git_SHA1_Final(oid.hash, &ctx);
572 if (hashcmp(fill(GIT_SHA1_RAWSZ), oid.hash))
573 die("final sha1 did not match");
576 /* Write the last part of the buffer to stdout */
578 int ret = xwrite(1, buffer + offset, len);