1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "object-store.h"
5 #include "object.h"
6 #include "delta.h"
7 #include "pack.h"
8 #include "blob.h"
9 #include "commit.h"
10 #include "tag.h"
11 #include "tree.h"
12 #include "tree-walk.h"
13 #include "progress.h"
14 #include "decorate.h"
15 #include "fsck.h"
16 
17 static int dry_run, quiet, recover, has_errors, strict;
18 static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
19 
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;
28 
29 /*
30  * When running under --strict mode, objects whose reachability are
31  * suspect are kept in core without getting written in the object
32  * store.
33  */
34 struct obj_buffer {
35 	char *buffer;
36 	unsigned long size;
37 };
38 
39 static struct decoration obj_decorate;
40 
lookup_object_buffer(struct object * base)41 static struct obj_buffer *lookup_object_buffer(struct object *base)
42 {
43 	return lookup_decoration(&obj_decorate, base);
44 }
45 
add_object_buffer(struct object * object,char * buffer,unsigned long size)46 static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
47 {
48 	struct obj_buffer *obj;
49 	CALLOC_ARRAY(obj, 1);
50 	obj->buffer = buffer;
51 	obj->size = size;
52 	if (add_decoration(&obj_decorate, object, obj))
53 		die("object %s tried to add buffer twice!", oid_to_hex(&object->oid));
54 }
55 
56 /*
57  * Make sure at least "min" bytes are available in the buffer, and
58  * return the pointer to the buffer.
59  */
fill(int min)60 static void *fill(int min)
61 {
62 	if (min <= len)
63 		return buffer + offset;
64 	if (min > sizeof(buffer))
65 		die("cannot fill %d bytes", min);
66 	if (offset) {
67 		the_hash_algo->update_fn(&ctx, buffer, offset);
68 		memmove(buffer, buffer + offset, len);
69 		offset = 0;
70 	}
71 	do {
72 		ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
73 		if (ret <= 0) {
74 			if (!ret)
75 				die("early EOF");
76 			die_errno("read error on input");
77 		}
78 		len += ret;
79 	} while (len < min);
80 	return buffer;
81 }
82 
use(int bytes)83 static void use(int bytes)
84 {
85 	if (bytes > len)
86 		die("used more bytes than were available");
87 	len -= bytes;
88 	offset += bytes;
89 
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);
97 }
98 
get_data(unsigned long size)99 static void *get_data(unsigned long size)
100 {
101 	git_zstream stream;
102 	void *buf = xmallocz(size);
103 
104 	memset(&stream, 0, sizeof(stream));
105 
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);
111 
112 	for (;;) {
113 		int ret = git_inflate(&stream, 0);
114 		use(len - stream.avail_in);
115 		if (stream.total_out == size && ret == Z_STREAM_END)
116 			break;
117 		if (ret != Z_OK) {
118 			error("inflate returned %d", ret);
119 			FREE_AND_NULL(buf);
120 			if (!recover)
121 				exit(1);
122 			has_errors = 1;
123 			break;
124 		}
125 		stream.next_in = fill(1);
126 		stream.avail_in = len;
127 	}
128 	git_inflate_end(&stream);
129 	return buf;
130 }
131 
132 struct delta_info {
133 	struct object_id base_oid;
134 	unsigned nr;
135 	off_t base_offset;
136 	unsigned long size;
137 	void *delta;
138 	struct delta_info *next;
139 };
140 
141 static struct delta_info *delta_list;
142 
add_delta_to_list(unsigned nr,const struct object_id * base_oid,off_t base_offset,void * delta,unsigned long size)143 static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
144 			      off_t base_offset,
145 			      void *delta, unsigned long size)
146 {
147 	struct delta_info *info = xmalloc(sizeof(*info));
148 
149 	oidcpy(&info->base_oid, base_oid);
150 	info->base_offset = base_offset;
151 	info->size = size;
152 	info->delta = delta;
153 	info->nr = nr;
154 	info->next = delta_list;
155 	delta_list = info;
156 }
157 
158 struct obj_info {
159 	off_t offset;
160 	struct object_id oid;
161 	struct object *obj;
162 };
163 
164 /* Remember to update object flag allocation in object.h */
165 #define FLAG_OPEN (1u<<20)
166 #define FLAG_WRITTEN (1u<<21)
167 
168 static struct obj_info *obj_list;
169 static unsigned nr_objects;
170 
171 /*
172  * Called only from check_object() after it verified this object
173  * is Ok.
174  */
write_cached_object(struct object * obj,struct obj_buffer * obj_buf)175 static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
176 {
177 	struct object_id oid;
178 
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;
183 }
184 
185 /*
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.
189  */
check_object(struct object * obj,enum object_type type,void * data,struct fsck_options * options)190 static int check_object(struct object *obj, enum object_type type,
191 			void *data, struct fsck_options *options)
192 {
193 	struct obj_buffer *obj_buf;
194 
195 	if (!obj)
196 		return 1;
197 
198 	if (obj->flags & FLAG_WRITTEN)
199 		return 0;
200 
201 	if (type != OBJ_ANY && obj->type != type)
202 		die("object type mismatch");
203 
204 	if (!(obj->flags & FLAG_OPEN)) {
205 		unsigned long size;
206 		int type = oid_object_info(the_repository, &obj->oid, &size);
207 		if (type != obj->type || type <= 0)
208 			die("object of unexpected type");
209 		obj->flags |= FLAG_WRITTEN;
210 		return 0;
211 	}
212 
213 	obj_buf = lookup_object_buffer(obj);
214 	if (!obj_buf)
215 		die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
216 	if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
217 		die("fsck error in packed object");
218 	fsck_options.walk = check_object;
219 	if (fsck_walk(obj, NULL, &fsck_options))
220 		die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
221 	write_cached_object(obj, obj_buf);
222 	return 0;
223 }
224 
write_rest(void)225 static void write_rest(void)
226 {
227 	unsigned i;
228 	for (i = 0; i < nr_objects; i++) {
229 		if (obj_list[i].obj)
230 			check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL);
231 	}
232 }
233 
234 static void added_object(unsigned nr, enum object_type type,
235 			 void *data, unsigned long size);
236 
237 /*
238  * Write out nr-th object from the list, now we know the contents
239  * of it.  Under --strict, this buffers structured objects in-core,
240  * to be checked at the end.
241  */
write_object(unsigned nr,enum object_type type,void * buf,unsigned long size)242 static void write_object(unsigned nr, enum object_type type,
243 			 void *buf, unsigned long size)
244 {
245 	if (!strict) {
246 		if (write_object_file(buf, size, type_name(type),
247 				      &obj_list[nr].oid) < 0)
248 			die("failed to write object");
249 		added_object(nr, type, buf, size);
250 		free(buf);
251 		obj_list[nr].obj = NULL;
252 	} else if (type == OBJ_BLOB) {
253 		struct blob *blob;
254 		if (write_object_file(buf, size, type_name(type),
255 				      &obj_list[nr].oid) < 0)
256 			die("failed to write object");
257 		added_object(nr, type, buf, size);
258 		free(buf);
259 
260 		blob = lookup_blob(the_repository, &obj_list[nr].oid);
261 		if (blob)
262 			blob->object.flags |= FLAG_WRITTEN;
263 		else
264 			die("invalid blob object");
265 		obj_list[nr].obj = NULL;
266 	} else {
267 		struct object *obj;
268 		int eaten;
269 		hash_object_file(the_hash_algo, buf, size, type_name(type),
270 				 &obj_list[nr].oid);
271 		added_object(nr, type, buf, size);
272 		obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
273 					  type, size, buf,
274 					  &eaten);
275 		if (!obj)
276 			die("invalid %s", type_name(type));
277 		add_object_buffer(obj, buf, size);
278 		obj->flags |= FLAG_OPEN;
279 		obj_list[nr].obj = obj;
280 	}
281 }
282 
resolve_delta(unsigned nr,enum object_type type,void * base,unsigned long base_size,void * delta,unsigned long delta_size)283 static void resolve_delta(unsigned nr, enum object_type type,
284 			  void *base, unsigned long base_size,
285 			  void *delta, unsigned long delta_size)
286 {
287 	void *result;
288 	unsigned long result_size;
289 
290 	result = patch_delta(base, base_size,
291 			     delta, delta_size,
292 			     &result_size);
293 	if (!result)
294 		die("failed to apply delta");
295 	free(delta);
296 	write_object(nr, type, result, result_size);
297 }
298 
299 /*
300  * We now know the contents of an object (which is nr-th in the pack);
301  * resolve all the deltified objects that are based on it.
302  */
added_object(unsigned nr,enum object_type type,void * data,unsigned long size)303 static void added_object(unsigned nr, enum object_type type,
304 			 void *data, unsigned long size)
305 {
306 	struct delta_info **p = &delta_list;
307 	struct delta_info *info;
308 
309 	while ((info = *p) != NULL) {
310 		if (oideq(&info->base_oid, &obj_list[nr].oid) ||
311 		    info->base_offset == obj_list[nr].offset) {
312 			*p = info->next;
313 			p = &delta_list;
314 			resolve_delta(info->nr, type, data, size,
315 				      info->delta, info->size);
316 			free(info);
317 			continue;
318 		}
319 		p = &info->next;
320 	}
321 }
322 
unpack_non_delta_entry(enum object_type type,unsigned long size,unsigned nr)323 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
324 				   unsigned nr)
325 {
326 	void *buf = get_data(size);
327 
328 	if (!dry_run && buf)
329 		write_object(nr, type, buf, size);
330 	else
331 		free(buf);
332 }
333 
resolve_against_held(unsigned nr,const struct object_id * base,void * delta_data,unsigned long delta_size)334 static int resolve_against_held(unsigned nr, const struct object_id *base,
335 				void *delta_data, unsigned long delta_size)
336 {
337 	struct object *obj;
338 	struct obj_buffer *obj_buffer;
339 	obj = lookup_object(the_repository, base);
340 	if (!obj)
341 		return 0;
342 	obj_buffer = lookup_object_buffer(obj);
343 	if (!obj_buffer)
344 		return 0;
345 	resolve_delta(nr, obj->type, obj_buffer->buffer,
346 		      obj_buffer->size, delta_data, delta_size);
347 	return 1;
348 }
349 
unpack_delta_entry(enum object_type type,unsigned long delta_size,unsigned nr)350 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
351 			       unsigned nr)
352 {
353 	void *delta_data, *base;
354 	unsigned long base_size;
355 	struct object_id base_oid;
356 
357 	if (type == OBJ_REF_DELTA) {
358 		oidread(&base_oid, fill(the_hash_algo->rawsz));
359 		use(the_hash_algo->rawsz);
360 		delta_data = get_data(delta_size);
361 		if (dry_run || !delta_data) {
362 			free(delta_data);
363 			return;
364 		}
365 		if (has_object_file(&base_oid))
366 			; /* Ok we have this one */
367 		else if (resolve_against_held(nr, &base_oid,
368 					      delta_data, delta_size))
369 			return; /* we are done */
370 		else {
371 			/* cannot resolve yet --- queue it */
372 			oidclr(&obj_list[nr].oid);
373 			add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
374 			return;
375 		}
376 	} else {
377 		unsigned base_found = 0;
378 		unsigned char *pack, c;
379 		off_t base_offset;
380 		unsigned lo, mid, hi;
381 
382 		pack = fill(1);
383 		c = *pack;
384 		use(1);
385 		base_offset = c & 127;
386 		while (c & 128) {
387 			base_offset += 1;
388 			if (!base_offset || MSB(base_offset, 7))
389 				die("offset value overflow for delta base object");
390 			pack = fill(1);
391 			c = *pack;
392 			use(1);
393 			base_offset = (base_offset << 7) + (c & 127);
394 		}
395 		base_offset = obj_list[nr].offset - base_offset;
396 		if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
397 			die("offset value out of bound for delta base object");
398 
399 		delta_data = get_data(delta_size);
400 		if (dry_run || !delta_data) {
401 			free(delta_data);
402 			return;
403 		}
404 		lo = 0;
405 		hi = nr;
406 		while (lo < hi) {
407 			mid = lo + (hi - lo) / 2;
408 			if (base_offset < obj_list[mid].offset) {
409 				hi = mid;
410 			} else if (base_offset > obj_list[mid].offset) {
411 				lo = mid + 1;
412 			} else {
413 				oidcpy(&base_oid, &obj_list[mid].oid);
414 				base_found = !is_null_oid(&base_oid);
415 				break;
416 			}
417 		}
418 		if (!base_found) {
419 			/*
420 			 * The delta base object is itself a delta that
421 			 * has not been resolved yet.
422 			 */
423 			oidclr(&obj_list[nr].oid);
424 			add_delta_to_list(nr, null_oid(), base_offset,
425 					  delta_data, delta_size);
426 			return;
427 		}
428 	}
429 
430 	if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
431 		return;
432 
433 	base = read_object_file(&base_oid, &type, &base_size);
434 	if (!base) {
435 		error("failed to read delta-pack base object %s",
436 		      oid_to_hex(&base_oid));
437 		if (!recover)
438 			exit(1);
439 		has_errors = 1;
440 		return;
441 	}
442 	resolve_delta(nr, type, base, base_size, delta_data, delta_size);
443 	free(base);
444 }
445 
unpack_one(unsigned nr)446 static void unpack_one(unsigned nr)
447 {
448 	unsigned shift;
449 	unsigned char *pack;
450 	unsigned long size, c;
451 	enum object_type type;
452 
453 	obj_list[nr].offset = consumed_bytes;
454 
455 	pack = fill(1);
456 	c = *pack;
457 	use(1);
458 	type = (c >> 4) & 7;
459 	size = (c & 15);
460 	shift = 4;
461 	while (c & 0x80) {
462 		pack = fill(1);
463 		c = *pack;
464 		use(1);
465 		size += (c & 0x7f) << shift;
466 		shift += 7;
467 	}
468 
469 	switch (type) {
470 	case OBJ_COMMIT:
471 	case OBJ_TREE:
472 	case OBJ_BLOB:
473 	case OBJ_TAG:
474 		unpack_non_delta_entry(type, size, nr);
475 		return;
476 	case OBJ_REF_DELTA:
477 	case OBJ_OFS_DELTA:
478 		unpack_delta_entry(type, size, nr);
479 		return;
480 	default:
481 		error("bad object type %d", type);
482 		has_errors = 1;
483 		if (recover)
484 			return;
485 		exit(1);
486 	}
487 }
488 
unpack_all(void)489 static void unpack_all(void)
490 {
491 	int i;
492 	struct pack_header *hdr = fill(sizeof(struct pack_header));
493 
494 	nr_objects = ntohl(hdr->hdr_entries);
495 
496 	if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
497 		die("bad pack file");
498 	if (!pack_version_ok(hdr->hdr_version))
499 		die("unknown pack file version %"PRIu32,
500 			ntohl(hdr->hdr_version));
501 	use(sizeof(struct pack_header));
502 
503 	if (!quiet)
504 		progress = start_progress(_("Unpacking objects"), nr_objects);
505 	CALLOC_ARRAY(obj_list, nr_objects);
506 	for (i = 0; i < nr_objects; i++) {
507 		unpack_one(i);
508 		display_progress(progress, i + 1);
509 	}
510 	stop_progress(&progress);
511 
512 	if (delta_list)
513 		die("unresolved deltas left after unpacking");
514 }
515 
cmd_unpack_objects(int argc,const char ** argv,const char * prefix)516 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
517 {
518 	int i;
519 	struct object_id oid;
520 
521 	read_replace_refs = 0;
522 
523 	git_config(git_default_config, NULL);
524 
525 	quiet = !isatty(2);
526 
527 	for (i = 1 ; i < argc; i++) {
528 		const char *arg = argv[i];
529 
530 		if (*arg == '-') {
531 			if (!strcmp(arg, "-n")) {
532 				dry_run = 1;
533 				continue;
534 			}
535 			if (!strcmp(arg, "-q")) {
536 				quiet = 1;
537 				continue;
538 			}
539 			if (!strcmp(arg, "-r")) {
540 				recover = 1;
541 				continue;
542 			}
543 			if (!strcmp(arg, "--strict")) {
544 				strict = 1;
545 				continue;
546 			}
547 			if (skip_prefix(arg, "--strict=", &arg)) {
548 				strict = 1;
549 				fsck_set_msg_types(&fsck_options, arg);
550 				continue;
551 			}
552 			if (starts_with(arg, "--pack_header=")) {
553 				struct pack_header *hdr;
554 				char *c;
555 
556 				hdr = (struct pack_header *)buffer;
557 				hdr->hdr_signature = htonl(PACK_SIGNATURE);
558 				hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
559 				if (*c != ',')
560 					die("bad %s", arg);
561 				hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
562 				if (*c)
563 					die("bad %s", arg);
564 				len = sizeof(*hdr);
565 				continue;
566 			}
567 			if (skip_prefix(arg, "--max-input-size=", &arg)) {
568 				max_input_size = strtoumax(arg, NULL, 10);
569 				continue;
570 			}
571 			usage(unpack_usage);
572 		}
573 
574 		/* We don't take any non-flag arguments now.. Maybe some day */
575 		usage(unpack_usage);
576 	}
577 	the_hash_algo->init_fn(&ctx);
578 	unpack_all();
579 	the_hash_algo->update_fn(&ctx, buffer, offset);
580 	the_hash_algo->final_oid_fn(&oid, &ctx);
581 	if (strict) {
582 		write_rest();
583 		if (fsck_finish(&fsck_options))
584 			die(_("fsck error in pack objects"));
585 	}
586 	if (!hasheq(fill(the_hash_algo->rawsz), oid.hash))
587 		die("final sha1 did not match");
588 	use(the_hash_algo->rawsz);
589 
590 	/* Write the last part of the buffer to stdout */
591 	while (len) {
592 		int ret = xwrite(1, buffer + offset, len);
593 		if (ret <= 0)
594 			break;
595 		len -= ret;
596 		offset += ret;
597 	}
598 
599 	/* All done */
600 	return has_errors;
601 }
602