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 git_SHA_CTX ctx;
23 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
26 * When running under --strict mode, objects whose reachability are
27 * suspect are kept in core without getting written in the object
35 static struct decoration obj_decorate;
37 static struct obj_buffer *lookup_object_buffer(struct object *base)
39 return lookup_decoration(&obj_decorate, base);
42 static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
44 struct obj_buffer *obj;
45 obj = xcalloc(1, sizeof(struct obj_buffer));
48 if (add_decoration(&obj_decorate, object, obj))
49 die("object %s tried to add buffer twice!", oid_to_hex(&object->oid));
53 * Make sure at least "min" bytes are available in the buffer, and
54 * return the pointer to the buffer.
56 static void *fill(int min)
59 return buffer + offset;
60 if (min > sizeof(buffer))
61 die("cannot fill %d bytes", min);
63 git_SHA1_Update(&ctx, buffer, offset);
64 memmove(buffer, buffer + offset, len);
68 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
72 die_errno("read error on input");
79 static void use(int bytes)
82 die("used more bytes than were available");
86 /* make sure off_t is sufficiently large not to wrap */
87 if (signed_add_overflows(consumed_bytes, bytes))
88 die("pack too large for current definition of off_t");
89 consumed_bytes += bytes;
92 static void *get_data(unsigned long size)
95 void *buf = xmallocz(size);
97 memset(&stream, 0, sizeof(stream));
99 stream.next_out = buf;
100 stream.avail_out = size;
101 stream.next_in = fill(1);
102 stream.avail_in = len;
103 git_inflate_init(&stream);
106 int ret = git_inflate(&stream, 0);
107 use(len - stream.avail_in);
108 if (stream.total_out == size && ret == Z_STREAM_END)
111 error("inflate returned %d", ret);
119 stream.next_in = fill(1);
120 stream.avail_in = len;
122 git_inflate_end(&stream);
127 unsigned char base_sha1[20];
132 struct delta_info *next;
135 static struct delta_info *delta_list;
137 static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
139 void *delta, unsigned long size)
141 struct delta_info *info = xmalloc(sizeof(*info));
143 hashcpy(info->base_sha1, base_sha1);
144 info->base_offset = base_offset;
148 info->next = delta_list;
154 unsigned char sha1[20];
158 #define FLAG_OPEN (1u<<20)
159 #define FLAG_WRITTEN (1u<<21)
161 static struct obj_info *obj_list;
162 static unsigned nr_objects;
165 * Called only from check_object() after it verified this object
168 static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
170 unsigned char sha1[20];
172 if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), sha1) < 0)
173 die("failed to write object %s", oid_to_hex(&obj->oid));
174 obj->flags |= FLAG_WRITTEN;
178 * At the very end of the processing, write_rest() scans the objects
179 * that have reachability requirements and calls this function.
180 * Verify its reachability and validity recursively and write it out.
182 static int check_object(struct object *obj, int type, void *data, struct fsck_options *options)
184 struct obj_buffer *obj_buf;
189 if (obj->flags & FLAG_WRITTEN)
192 if (type != OBJ_ANY && obj->type != type)
193 die("object type mismatch");
195 if (!(obj->flags & FLAG_OPEN)) {
197 int type = sha1_object_info(obj->oid.hash, &size);
198 if (type != obj->type || type <= 0)
199 die("object of unexpected type");
200 obj->flags |= FLAG_WRITTEN;
204 obj_buf = lookup_object_buffer(obj);
206 die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
207 if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
208 die("Error in object");
209 fsck_options.walk = check_object;
210 if (fsck_walk(obj, NULL, &fsck_options))
211 die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
212 write_cached_object(obj, obj_buf);
216 static void write_rest(void)
219 for (i = 0; i < nr_objects; i++) {
221 check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL);
225 static void added_object(unsigned nr, enum object_type type,
226 void *data, unsigned long size);
229 * Write out nr-th object from the list, now we know the contents
230 * of it. Under --strict, this buffers structured objects in-core,
231 * to be checked at the end.
233 static void write_object(unsigned nr, enum object_type type,
234 void *buf, unsigned long size)
237 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
238 die("failed to write object");
239 added_object(nr, type, buf, size);
241 obj_list[nr].obj = NULL;
242 } else if (type == OBJ_BLOB) {
244 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
245 die("failed to write object");
246 added_object(nr, type, buf, size);
249 blob = lookup_blob(obj_list[nr].sha1);
251 blob->object.flags |= FLAG_WRITTEN;
253 die("invalid blob object");
254 obj_list[nr].obj = NULL;
258 hash_sha1_file(buf, size, typename(type), obj_list[nr].sha1);
259 added_object(nr, type, buf, size);
260 obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten);
262 die("invalid %s", typename(type));
263 add_object_buffer(obj, buf, size);
264 obj->flags |= FLAG_OPEN;
265 obj_list[nr].obj = obj;
269 static void resolve_delta(unsigned nr, enum object_type type,
270 void *base, unsigned long base_size,
271 void *delta, unsigned long delta_size)
274 unsigned long result_size;
276 result = patch_delta(base, base_size,
280 die("failed to apply delta");
282 write_object(nr, type, result, result_size);
286 * We now know the contents of an object (which is nr-th in the pack);
287 * resolve all the deltified objects that are based on it.
289 static void added_object(unsigned nr, enum object_type type,
290 void *data, unsigned long size)
292 struct delta_info **p = &delta_list;
293 struct delta_info *info;
295 while ((info = *p) != NULL) {
296 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
297 info->base_offset == obj_list[nr].offset) {
300 resolve_delta(info->nr, type, data, size,
301 info->delta, info->size);
309 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
312 void *buf = get_data(size);
315 write_object(nr, type, buf, size);
320 static int resolve_against_held(unsigned nr, const unsigned char *base,
321 void *delta_data, unsigned long delta_size)
324 struct obj_buffer *obj_buffer;
325 obj = lookup_object(base);
328 obj_buffer = lookup_object_buffer(obj);
331 resolve_delta(nr, obj->type, obj_buffer->buffer,
332 obj_buffer->size, delta_data, delta_size);
336 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
339 void *delta_data, *base;
340 unsigned long base_size;
341 unsigned char base_sha1[20];
343 if (type == OBJ_REF_DELTA) {
344 hashcpy(base_sha1, fill(20));
346 delta_data = get_data(delta_size);
347 if (dry_run || !delta_data) {
351 if (has_sha1_file(base_sha1))
352 ; /* Ok we have this one */
353 else if (resolve_against_held(nr, base_sha1,
354 delta_data, delta_size))
355 return; /* we are done */
357 /* cannot resolve yet --- queue it */
358 hashclr(obj_list[nr].sha1);
359 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
363 unsigned base_found = 0;
364 unsigned char *pack, c;
366 unsigned lo, mid, hi;
371 base_offset = c & 127;
374 if (!base_offset || MSB(base_offset, 7))
375 die("offset value overflow for delta base object");
379 base_offset = (base_offset << 7) + (c & 127);
381 base_offset = obj_list[nr].offset - base_offset;
382 if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
383 die("offset value out of bound for delta base object");
385 delta_data = get_data(delta_size);
386 if (dry_run || !delta_data) {
394 if (base_offset < obj_list[mid].offset) {
396 } else if (base_offset > obj_list[mid].offset) {
399 hashcpy(base_sha1, obj_list[mid].sha1);
400 base_found = !is_null_sha1(base_sha1);
406 * The delta base object is itself a delta that
407 * has not been resolved yet.
409 hashclr(obj_list[nr].sha1);
410 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
415 if (resolve_against_held(nr, base_sha1, delta_data, delta_size))
418 base = read_sha1_file(base_sha1, &type, &base_size);
420 error("failed to read delta-pack base object %s",
421 sha1_to_hex(base_sha1));
427 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
431 static void unpack_one(unsigned nr)
435 unsigned long size, c;
436 enum object_type type;
438 obj_list[nr].offset = consumed_bytes;
450 size += (c & 0x7f) << shift;
459 unpack_non_delta_entry(type, size, nr);
463 unpack_delta_entry(type, size, nr);
466 error("bad object type %d", type);
474 static void unpack_all(void)
477 struct progress *progress = NULL;
478 struct pack_header *hdr = fill(sizeof(struct pack_header));
480 nr_objects = ntohl(hdr->hdr_entries);
482 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
483 die("bad pack file");
484 if (!pack_version_ok(hdr->hdr_version))
485 die("unknown pack file version %"PRIu32,
486 ntohl(hdr->hdr_version));
487 use(sizeof(struct pack_header));
490 progress = start_progress(_("Unpacking objects"), nr_objects);
491 obj_list = xcalloc(nr_objects, sizeof(*obj_list));
492 for (i = 0; i < nr_objects; i++) {
494 display_progress(progress, i + 1);
496 stop_progress(&progress);
499 die("unresolved deltas left after unpacking");
502 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
505 unsigned char sha1[20];
507 check_replace_refs = 0;
509 git_config(git_default_config, NULL);
513 for (i = 1 ; i < argc; i++) {
514 const char *arg = argv[i];
517 if (!strcmp(arg, "-n")) {
521 if (!strcmp(arg, "-q")) {
525 if (!strcmp(arg, "-r")) {
529 if (!strcmp(arg, "--strict")) {
533 if (skip_prefix(arg, "--strict=", &arg)) {
535 fsck_set_msg_types(&fsck_options, arg);
538 if (starts_with(arg, "--pack_header=")) {
539 struct pack_header *hdr;
542 hdr = (struct pack_header *)buffer;
543 hdr->hdr_signature = htonl(PACK_SIGNATURE);
544 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
547 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
556 /* We don't take any non-flag arguments now.. Maybe some day */
561 git_SHA1_Update(&ctx, buffer, offset);
562 git_SHA1_Final(sha1, &ctx);
565 if (hashcmp(fill(20), sha1))
566 die("final sha1 did not match");
569 /* Write the last part of the buffer to stdout */
571 int ret = xwrite(1, buffer + offset, len);