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] < pack-file";
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;
25 * When running under --strict mode, objects whose reachability are
26 * suspect are kept in core without getting written in the object
34 static struct decoration obj_decorate;
36 static struct obj_buffer *lookup_object_buffer(struct object *base)
38 return lookup_decoration(&obj_decorate, base);
41 static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
43 struct obj_buffer *obj;
44 obj = xcalloc(1, sizeof(struct obj_buffer));
47 if (add_decoration(&obj_decorate, object, obj))
48 die("object %s tried to add buffer twice!", sha1_to_hex(object->sha1));
52 * Make sure at least "min" bytes are available in the buffer, and
53 * return the pointer to the buffer.
55 static void *fill(int min)
58 return buffer + offset;
59 if (min > sizeof(buffer))
60 die("cannot fill %d bytes", min);
62 git_SHA1_Update(&ctx, buffer, offset);
63 memmove(buffer, buffer + offset, len);
67 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
71 die("read error on input: %s", strerror(errno));
78 static void use(int bytes)
81 die("used more bytes than were available");
85 /* make sure off_t is sufficiently large not to wrap */
86 if (consumed_bytes > consumed_bytes + bytes)
87 die("pack too large for current definition of off_t");
88 consumed_bytes += bytes;
91 static void *get_data(unsigned long size)
94 void *buf = xmalloc(size);
96 memset(&stream, 0, sizeof(stream));
98 stream.next_out = buf;
99 stream.avail_out = size;
100 stream.next_in = fill(1);
101 stream.avail_in = len;
102 inflateInit(&stream);
105 int ret = inflate(&stream, 0);
106 use(len - stream.avail_in);
107 if (stream.total_out == size && ret == Z_STREAM_END)
110 error("inflate returned %d\n", ret);
118 stream.next_in = fill(1);
119 stream.avail_in = len;
126 unsigned char base_sha1[20];
131 struct delta_info *next;
134 static struct delta_info *delta_list;
136 static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
138 void *delta, unsigned long size)
140 struct delta_info *info = xmalloc(sizeof(*info));
142 hashcpy(info->base_sha1, base_sha1);
143 info->base_offset = base_offset;
147 info->next = delta_list;
153 unsigned char sha1[20];
157 #define FLAG_OPEN (1u<<20)
158 #define FLAG_WRITTEN (1u<<21)
160 static struct obj_info *obj_list;
164 * Called only from check_object() after it verified this object
167 static void write_cached_object(struct object *obj)
169 unsigned char sha1[20];
170 struct obj_buffer *obj_buf = lookup_object_buffer(obj);
171 if (write_sha1_file(obj_buf->buffer, obj_buf->size, typename(obj->type), sha1) < 0)
172 die("failed to write object %s", sha1_to_hex(obj->sha1));
173 obj->flags |= FLAG_WRITTEN;
177 * At the very end of the processing, write_rest() scans the objects
178 * that have reachability requirements and calls this function.
179 * Verify its reachability and validity recursively and write it out.
181 static int check_object(struct object *obj, int type, void *data)
186 if (obj->flags & FLAG_WRITTEN)
189 if (type != OBJ_ANY && obj->type != type)
190 die("object type mismatch");
192 if (!(obj->flags & FLAG_OPEN)) {
194 int type = sha1_object_info(obj->sha1, &size);
195 if (type != obj->type || type <= 0)
196 die("object of unexpected type");
197 obj->flags |= FLAG_WRITTEN;
201 if (fsck_object(obj, 1, fsck_error_function))
202 die("Error in object");
203 if (!fsck_walk(obj, check_object, 0))
204 die("Error on reachable objects of %s", sha1_to_hex(obj->sha1));
205 write_cached_object(obj);
209 static void write_rest(void)
212 for (i = 0; i < nr_objects; i++)
213 check_object(obj_list[i].obj, OBJ_ANY, 0);
216 static void added_object(unsigned nr, enum object_type type,
217 void *data, unsigned long size);
220 * Write out nr-th object from the list, now we know the contents
221 * of it. Under --strict, this buffers structured objects in-core,
222 * to be checked at the end.
224 static void write_object(unsigned nr, enum object_type type,
225 void *buf, unsigned long size)
228 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
229 die("failed to write object");
230 added_object(nr, type, buf, size);
232 obj_list[nr].obj = NULL;
233 } else if (type == OBJ_BLOB) {
235 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
236 die("failed to write object");
237 added_object(nr, type, buf, size);
240 blob = lookup_blob(obj_list[nr].sha1);
242 blob->object.flags |= FLAG_WRITTEN;
244 die("invalid blob object");
245 obj_list[nr].obj = NULL;
249 hash_sha1_file(buf, size, typename(type), obj_list[nr].sha1);
250 added_object(nr, type, buf, size);
251 obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten);
253 die("invalid %s", typename(type));
254 add_object_buffer(obj, buf, size);
255 obj->flags |= FLAG_OPEN;
256 obj_list[nr].obj = obj;
260 static void resolve_delta(unsigned nr, enum object_type type,
261 void *base, unsigned long base_size,
262 void *delta, unsigned long delta_size)
265 unsigned long result_size;
267 result = patch_delta(base, base_size,
271 die("failed to apply delta");
273 write_object(nr, type, result, result_size);
277 * We now know the contents of an object (which is nr-th in the pack);
278 * resolve all the deltified objects that are based on it.
280 static void added_object(unsigned nr, enum object_type type,
281 void *data, unsigned long size)
283 struct delta_info **p = &delta_list;
284 struct delta_info *info;
286 while ((info = *p) != NULL) {
287 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
288 info->base_offset == obj_list[nr].offset) {
291 resolve_delta(info->nr, type, data, size,
292 info->delta, info->size);
300 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
303 void *buf = get_data(size);
306 write_object(nr, type, buf, size);
311 static int resolve_against_held(unsigned nr, const unsigned char *base,
312 void *delta_data, unsigned long delta_size)
315 struct obj_buffer *obj_buffer;
316 obj = lookup_object(base);
319 obj_buffer = lookup_object_buffer(obj);
322 resolve_delta(nr, obj->type, obj_buffer->buffer,
323 obj_buffer->size, delta_data, delta_size);
327 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
330 void *delta_data, *base;
331 unsigned long base_size;
332 unsigned char base_sha1[20];
334 if (type == OBJ_REF_DELTA) {
335 hashcpy(base_sha1, fill(20));
337 delta_data = get_data(delta_size);
338 if (dry_run || !delta_data) {
342 if (has_sha1_file(base_sha1))
343 ; /* Ok we have this one */
344 else if (resolve_against_held(nr, base_sha1,
345 delta_data, delta_size))
346 return; /* we are done */
348 /* cannot resolve yet --- queue it */
349 hashcpy(obj_list[nr].sha1, null_sha1);
350 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
354 unsigned base_found = 0;
355 unsigned char *pack, c;
357 unsigned lo, mid, hi;
362 base_offset = c & 127;
365 if (!base_offset || MSB(base_offset, 7))
366 die("offset value overflow for delta base object");
370 base_offset = (base_offset << 7) + (c & 127);
372 base_offset = obj_list[nr].offset - base_offset;
374 delta_data = get_data(delta_size);
375 if (dry_run || !delta_data) {
383 if (base_offset < obj_list[mid].offset) {
385 } else if (base_offset > obj_list[mid].offset) {
388 hashcpy(base_sha1, obj_list[mid].sha1);
389 base_found = !is_null_sha1(base_sha1);
395 * The delta base object is itself a delta that
396 * has not been resolved yet.
398 hashcpy(obj_list[nr].sha1, null_sha1);
399 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
404 if (resolve_against_held(nr, base_sha1, delta_data, delta_size))
407 base = read_sha1_file(base_sha1, &type, &base_size);
409 error("failed to read delta-pack base object %s",
410 sha1_to_hex(base_sha1));
416 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
420 static void unpack_one(unsigned nr)
423 unsigned char *pack, c;
425 enum object_type type;
427 obj_list[nr].offset = consumed_bytes;
439 size += (c & 0x7f) << shift;
448 unpack_non_delta_entry(type, size, nr);
452 unpack_delta_entry(type, size, nr);
455 error("bad object type %d", type);
463 static void unpack_all(void)
466 struct progress *progress = NULL;
467 struct pack_header *hdr = fill(sizeof(struct pack_header));
469 nr_objects = ntohl(hdr->hdr_entries);
471 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
472 die("bad pack file");
473 if (!pack_version_ok(hdr->hdr_version))
474 die("unknown pack file version %"PRIu32,
475 ntohl(hdr->hdr_version));
476 use(sizeof(struct pack_header));
479 progress = start_progress("Unpacking objects", nr_objects);
480 obj_list = xcalloc(nr_objects, sizeof(*obj_list));
481 for (i = 0; i < nr_objects; i++) {
483 display_progress(progress, i + 1);
485 stop_progress(&progress);
488 die("unresolved deltas left after unpacking");
491 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
494 unsigned char sha1[20];
496 git_config(git_default_config, NULL);
500 for (i = 1 ; i < argc; i++) {
501 const char *arg = argv[i];
504 if (!strcmp(arg, "-n")) {
508 if (!strcmp(arg, "-q")) {
512 if (!strcmp(arg, "-r")) {
516 if (!strcmp(arg, "--strict")) {
520 if (!prefixcmp(arg, "--pack_header=")) {
521 struct pack_header *hdr;
524 hdr = (struct pack_header *)buffer;
525 hdr->hdr_signature = htonl(PACK_SIGNATURE);
526 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
529 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
538 /* We don't take any non-flag arguments now.. Maybe some day */
543 git_SHA1_Update(&ctx, buffer, offset);
544 git_SHA1_Final(sha1, &ctx);
547 if (hashcmp(fill(20), sha1))
548 die("final sha1 did not match");
551 /* Write the last part of the buffer to stdout */
553 int ret = xwrite(1, buffer + offset, len);