]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/unpack-objects.c
Merge branch 'ab/remove-implicit-use-of-the-repository' into en/header-split-cache-h
[thirdparty/git.git] / builtin / unpack-objects.c
CommitLineData
64413630 1#include "builtin.h"
bad50dc8 2#include "cache.h"
425d290c 3#include "bulk-checkin.h"
b2141fc1 4#include "config.h"
32a8f510 5#include "environment.h"
f394e093 6#include "gettext.h"
41771fa4 7#include "hex.h"
cbd53a21 8#include "object-store.h"
74536958 9#include "object.h"
8ee378a0 10#include "delta.h"
a733cb60 11#include "pack.h"
8e440259
PE
12#include "blob.h"
13#include "commit.h"
cbeab747 14#include "replace-object.h"
8e440259
PE
15#include "tag.h"
16#include "tree.h"
b41860bf 17#include "tree-walk.h"
96a02f8f 18#include "progress.h"
2add1e6d 19#include "decorate.h"
b41860bf 20#include "fsck.h"
bad50dc8 21
b41860bf 22static int dry_run, quiet, recover, has_errors, strict;
33e8fc87 23static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
bad50dc8 24
67e5a5ec
LT
25/* We always read in 4kB chunks. */
26static unsigned char buffer[4096];
d7dd0223
NP
27static unsigned int offset, len;
28static off_t consumed_bytes;
5ad21867 29static off_t max_input_size;
3206b6bd 30static git_hash_ctx ctx;
22410549 31static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
bae60ba7 32static struct progress *progress;
bad50dc8 33
f2898cfa
JH
34/*
35 * When running under --strict mode, objects whose reachability are
36 * suspect are kept in core without getting written in the object
37 * store.
38 */
2add1e6d
MK
39struct obj_buffer {
40 char *buffer;
41 unsigned long size;
42};
43
44static struct decoration obj_decorate;
45
46static struct obj_buffer *lookup_object_buffer(struct object *base)
47{
48 return lookup_decoration(&obj_decorate, base);
49}
50
b41860bf
MK
51static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
52{
53 struct obj_buffer *obj;
ca56dadb 54 CALLOC_ARRAY(obj, 1);
b41860bf
MK
55 obj->buffer = buffer;
56 obj->size = size;
57 if (add_decoration(&obj_decorate, object, obj))
f2fd0760 58 die("object %s tried to add buffer twice!", oid_to_hex(&object->oid));
b41860bf
MK
59}
60
67e5a5ec
LT
61/*
62 * Make sure at least "min" bytes are available in the buffer, and
63 * return the pointer to the buffer.
64 */
79a65697 65static void *fill(int min)
67e5a5ec
LT
66{
67 if (min <= len)
68 return buffer + offset;
67e5a5ec
LT
69 if (min > sizeof(buffer))
70 die("cannot fill %d bytes", min);
71 if (offset) {
3206b6bd 72 the_hash_algo->update_fn(&ctx, buffer, offset);
79a65697 73 memmove(buffer, buffer + offset, len);
67e5a5ec
LT
74 offset = 0;
75 }
76 do {
8a912bcb 77 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
67e5a5ec
LT
78 if (ret <= 0) {
79 if (!ret)
80 die("early EOF");
d824cbba 81 die_errno("read error on input");
67e5a5ec
LT
82 }
83 len += ret;
84 } while (len < min);
85 return buffer;
86}
74536958 87
67e5a5ec
LT
88static void use(int bytes)
89{
90 if (bytes > len)
91 die("used more bytes than were available");
92 len -= bytes;
93 offset += bytes;
d7dd0223
NP
94
95 /* make sure off_t is sufficiently large not to wrap */
c03c8315 96 if (signed_add_overflows(consumed_bytes, bytes))
d7dd0223 97 die("pack too large for current definition of off_t");
209c554a 98 consumed_bytes += bytes;
5ad21867
CC
99 if (max_input_size && consumed_bytes > max_input_size)
100 die(_("pack exceeds maximum allowed size"));
bae60ba7 101 display_throughput(progress, consumed_bytes);
67e5a5ec 102}
bad50dc8 103
a1bf5ca2
HX
104/*
105 * Decompress zstream from the standard input into a newly
106 * allocated buffer of specified size and return the buffer.
107 * The caller is responsible to free the returned buffer.
108 *
109 * But for dry_run mode, "get_data()" is only used to check the
110 * integrity of data, and the returned buffer is not used at all.
111 * Therefore, in dry_run mode, "get_data()" will release the small
112 * allocated buffer which is reused to hold temporary zstream output
113 * and return NULL instead of returning garbage data.
114 */
67e5a5ec 115static void *get_data(unsigned long size)
bad50dc8 116{
ef49a7a0 117 git_zstream stream;
a1bf5ca2
HX
118 unsigned long bufsize = dry_run && size > 8192 ? 8192 : size;
119 void *buf = xmallocz(bufsize);
67e5a5ec 120
67e5a5ec
LT
121 memset(&stream, 0, sizeof(stream));
122
123 stream.next_out = buf;
a1bf5ca2 124 stream.avail_out = bufsize;
67e5a5ec
LT
125 stream.next_in = fill(1);
126 stream.avail_in = len;
39c68542 127 git_inflate_init(&stream);
67e5a5ec
LT
128
129 for (;;) {
39c68542 130 int ret = git_inflate(&stream, 0);
67e5a5ec
LT
131 use(len - stream.avail_in);
132 if (stream.total_out == size && ret == Z_STREAM_END)
133 break;
f986f2c8 134 if (ret != Z_OK) {
82247e9b 135 error("inflate returned %d", ret);
6a83d902 136 FREE_AND_NULL(buf);
3b67d291 137 if (!recover)
f986f2c8
JH
138 exit(1);
139 has_errors = 1;
140 break;
141 }
67e5a5ec
LT
142 stream.next_in = fill(1);
143 stream.avail_in = len;
a1bf5ca2
HX
144 if (dry_run) {
145 /* reuse the buffer in dry_run mode */
146 stream.next_out = buf;
147 stream.avail_out = bufsize > size - stream.total_out ?
148 size - stream.total_out :
149 bufsize;
150 }
67e5a5ec 151 }
39c68542 152 git_inflate_end(&stream);
a1bf5ca2
HX
153 if (dry_run)
154 FREE_AND_NULL(buf);
67e5a5ec 155 return buf;
bad50dc8
LT
156}
157
67e5a5ec 158struct delta_info {
834bc47b 159 struct object_id base_oid;
d7dd0223
NP
160 unsigned nr;
161 off_t base_offset;
67e5a5ec
LT
162 unsigned long size;
163 void *delta;
164 struct delta_info *next;
165};
166
167static struct delta_info *delta_list;
168
834bc47b 169static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
d7dd0223 170 off_t base_offset,
209c554a 171 void *delta, unsigned long size)
bad50dc8 172{
67e5a5ec 173 struct delta_info *info = xmalloc(sizeof(*info));
bad50dc8 174
834bc47b 175 oidcpy(&info->base_oid, base_oid);
209c554a 176 info->base_offset = base_offset;
67e5a5ec
LT
177 info->size = size;
178 info->delta = delta;
209c554a 179 info->nr = nr;
67e5a5ec
LT
180 info->next = delta_list;
181 delta_list = info;
bad50dc8
LT
182}
183
209c554a 184struct obj_info {
d7dd0223 185 off_t offset;
834bc47b 186 struct object_id oid;
b41860bf 187 struct object *obj;
209c554a
NP
188};
189
95308d64 190/* Remember to update object flag allocation in object.h */
b41860bf
MK
191#define FLAG_OPEN (1u<<20)
192#define FLAG_WRITTEN (1u<<21)
193
209c554a 194static struct obj_info *obj_list;
2af202be 195static unsigned nr_objects;
b41860bf 196
f2898cfa
JH
197/*
198 * Called only from check_object() after it verified this object
199 * is Ok.
200 */
90a398bb 201static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
b41860bf 202{
834bc47b 203 struct object_id oid;
90a398bb 204
a09c985e 205 if (write_object_file(obj_buf->buffer, obj_buf->size,
c80d226a 206 obj->type, &oid) < 0)
f2fd0760 207 die("failed to write object %s", oid_to_hex(&obj->oid));
b41860bf
MK
208 obj->flags |= FLAG_WRITTEN;
209}
210
f2898cfa
JH
211/*
212 * At the very end of the processing, write_rest() scans the objects
213 * that have reachability requirements and calls this function.
214 * Verify its reachability and validity recursively and write it out.
215 */
a1aad716
ÆAB
216static int check_object(struct object *obj, enum object_type type,
217 void *data, struct fsck_options *options)
b41860bf 218{
90a398bb
JS
219 struct obj_buffer *obj_buf;
220
b41860bf 221 if (!obj)
9a217391 222 return 1;
b41860bf
MK
223
224 if (obj->flags & FLAG_WRITTEN)
9a217391 225 return 0;
b41860bf
MK
226
227 if (type != OBJ_ANY && obj->type != type)
228 die("object type mismatch");
229
230 if (!(obj->flags & FLAG_OPEN)) {
231 unsigned long size;
0df8e965 232 int type = oid_object_info(the_repository, &obj->oid, &size);
b41860bf
MK
233 if (type != obj->type || type <= 0)
234 die("object of unexpected type");
235 obj->flags |= FLAG_WRITTEN;
9a217391 236 return 0;
b41860bf
MK
237 }
238
90a398bb
JS
239 obj_buf = lookup_object_buffer(obj);
240 if (!obj_buf)
f2fd0760 241 die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
22410549 242 if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
db5a58c1 243 die("fsck error in packed object");
22410549
JS
244 fsck_options.walk = check_object;
245 if (fsck_walk(obj, NULL, &fsck_options))
f2fd0760 246 die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
90a398bb 247 write_cached_object(obj, obj_buf);
9a217391 248 return 0;
b41860bf
MK
249}
250
251static void write_rest(void)
252{
253 unsigned i;
9a217391
JH
254 for (i = 0; i < nr_objects; i++) {
255 if (obj_list[i].obj)
22410549 256 check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL);
9a217391 257 }
b41860bf 258}
67e5a5ec 259
21666f1a
NP
260static void added_object(unsigned nr, enum object_type type,
261 void *data, unsigned long size);
209c554a 262
f2898cfa
JH
263/*
264 * Write out nr-th object from the list, now we know the contents
265 * of it. Under --strict, this buffers structured objects in-core,
266 * to be checked at the end.
267 */
21666f1a
NP
268static void write_object(unsigned nr, enum object_type type,
269 void *buf, unsigned long size)
cca7081a 270{
b41860bf 271 if (!strict) {
c80d226a 272 if (write_object_file(buf, size, type,
a09c985e 273 &obj_list[nr].oid) < 0)
b41860bf 274 die("failed to write object");
f2898cfa 275 added_object(nr, type, buf, size);
b41860bf 276 free(buf);
f2898cfa 277 obj_list[nr].obj = NULL;
b41860bf
MK
278 } else if (type == OBJ_BLOB) {
279 struct blob *blob;
c80d226a 280 if (write_object_file(buf, size, type,
a09c985e 281 &obj_list[nr].oid) < 0)
b41860bf 282 die("failed to write object");
f2898cfa 283 added_object(nr, type, buf, size);
b41860bf
MK
284 free(buf);
285
da14a7ff 286 blob = lookup_blob(the_repository, &obj_list[nr].oid);
b41860bf
MK
287 if (blob)
288 blob->object.flags |= FLAG_WRITTEN;
289 else
290 die("invalid blob object");
f2898cfa 291 obj_list[nr].obj = NULL;
b41860bf
MK
292 } else {
293 struct object *obj;
294 int eaten;
44439c1c 295 hash_object_file(the_hash_algo, buf, size, type,
2dcde20e 296 &obj_list[nr].oid);
f2898cfa 297 added_object(nr, type, buf, size);
1ec5bfd2
SB
298 obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
299 type, size, buf,
c251c83d 300 &eaten);
b41860bf 301 if (!obj)
debca9d2 302 die("invalid %s", type_name(type));
b41860bf
MK
303 add_object_buffer(obj, buf, size);
304 obj->flags |= FLAG_OPEN;
305 obj_list[nr].obj = obj;
306 }
cca7081a
LT
307}
308
21666f1a 309static void resolve_delta(unsigned nr, enum object_type type,
f986f2c8
JH
310 void *base, unsigned long base_size,
311 void *delta, unsigned long delta_size)
bad50dc8 312{
67e5a5ec
LT
313 void *result;
314 unsigned long result_size;
bad50dc8 315
67e5a5ec
LT
316 result = patch_delta(base, base_size,
317 delta, delta_size,
318 &result_size);
319 if (!result)
320 die("failed to apply delta");
321 free(delta);
21666f1a 322 write_object(nr, type, result, result_size);
bad50dc8
LT
323}
324
f2898cfa
JH
325/*
326 * We now know the contents of an object (which is nr-th in the pack);
327 * resolve all the deltified objects that are based on it.
328 */
21666f1a
NP
329static void added_object(unsigned nr, enum object_type type,
330 void *data, unsigned long size)
74536958 331{
67e5a5ec
LT
332 struct delta_info **p = &delta_list;
333 struct delta_info *info;
334
335 while ((info = *p) != NULL) {
4a7e27e9 336 if (oideq(&info->base_oid, &obj_list[nr].oid) ||
209c554a 337 info->base_offset == obj_list[nr].offset) {
67e5a5ec
LT
338 *p = info->next;
339 p = &delta_list;
209c554a
NP
340 resolve_delta(info->nr, type, data, size,
341 info->delta, info->size);
67e5a5ec
LT
342 free(info);
343 continue;
344 }
345 p = &info->next;
346 }
347}
348
21666f1a 349static void unpack_non_delta_entry(enum object_type type, unsigned long size,
209c554a 350 unsigned nr)
67e5a5ec
LT
351{
352 void *buf = get_data(size);
8ee378a0 353
a1bf5ca2 354 if (buf)
21666f1a 355 write_object(nr, type, buf, size);
8ee378a0
JH
356}
357
aaf81223
HX
358struct input_zstream_data {
359 git_zstream *zstream;
360 unsigned char buf[8192];
361 int status;
362};
363
364static const void *feed_input_zstream(struct input_stream *in_stream,
365 unsigned long *readlen)
366{
367 struct input_zstream_data *data = in_stream->data;
368 git_zstream *zstream = data->zstream;
369 void *in = fill(1);
370
371 if (in_stream->is_finished) {
372 *readlen = 0;
373 return NULL;
374 }
375
376 zstream->next_out = data->buf;
377 zstream->avail_out = sizeof(data->buf);
378 zstream->next_in = in;
379 zstream->avail_in = len;
380
381 data->status = git_inflate(zstream, 0);
382
383 in_stream->is_finished = data->status != Z_OK;
384 use(len - zstream->avail_in);
385 *readlen = sizeof(data->buf) - zstream->avail_out;
386
387 return data->buf;
388}
389
390static void stream_blob(unsigned long size, unsigned nr)
391{
392 git_zstream zstream = { 0 };
393 struct input_zstream_data data = { 0 };
394 struct input_stream in_stream = {
395 .read = feed_input_zstream,
396 .data = &data,
397 };
398 struct obj_info *info = &obj_list[nr];
399
400 data.zstream = &zstream;
401 git_inflate_init(&zstream);
402
403 if (stream_loose_object(&in_stream, size, &info->oid))
404 die(_("failed to write object in stream"));
405
406 if (data.status != Z_STREAM_END)
407 die(_("inflate returned (%d)"), data.status);
408 git_inflate_end(&zstream);
409
410 if (strict) {
411 struct blob *blob = lookup_blob(the_repository, &info->oid);
412
413 if (!blob)
414 die(_("invalid blob object from stream"));
415 blob->object.flags |= FLAG_WRITTEN;
416 }
417 info->obj = NULL;
418}
419
834bc47b 420static int resolve_against_held(unsigned nr, const struct object_id *base,
f2898cfa
JH
421 void *delta_data, unsigned long delta_size)
422{
423 struct object *obj;
424 struct obj_buffer *obj_buffer;
d0229abd 425 obj = lookup_object(the_repository, base);
f2898cfa
JH
426 if (!obj)
427 return 0;
428 obj_buffer = lookup_object_buffer(obj);
429 if (!obj_buffer)
430 return 0;
431 resolve_delta(nr, obj->type, obj_buffer->buffer,
432 obj_buffer->size, delta_data, delta_size);
433 return 1;
434}
435
21666f1a 436static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
209c554a 437 unsigned nr)
8ee378a0 438{
67e5a5ec
LT
439 void *delta_data, *base;
440 unsigned long base_size;
834bc47b 441 struct object_id base_oid;
8ee378a0 442
21666f1a 443 if (type == OBJ_REF_DELTA) {
92e2cab9 444 oidread(&base_oid, fill(the_hash_algo->rawsz));
3206b6bd 445 use(the_hash_algo->rawsz);
209c554a 446 delta_data = get_data(delta_size);
a1bf5ca2 447 if (!delta_data)
209c554a 448 return;
bc726bd0 449 if (repo_has_object_file(the_repository, &base_oid))
f2898cfa 450 ; /* Ok we have this one */
834bc47b 451 else if (resolve_against_held(nr, &base_oid,
f2898cfa
JH
452 delta_data, delta_size))
453 return; /* we are done */
454 else {
455 /* cannot resolve yet --- queue it */
834bc47b 456 oidclr(&obj_list[nr].oid);
457 add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
209c554a
NP
458 return;
459 }
460 } else {
461 unsigned base_found = 0;
462 unsigned char *pack, c;
d7dd0223 463 off_t base_offset;
209c554a 464 unsigned lo, mid, hi;
c4fb06c0 465
209c554a
NP
466 pack = fill(1);
467 c = *pack;
468 use(1);
469 base_offset = c & 127;
470 while (c & 128) {
471 base_offset += 1;
8723f216 472 if (!base_offset || MSB(base_offset, 7))
209c554a
NP
473 die("offset value overflow for delta base object");
474 pack = fill(1);
475 c = *pack;
476 use(1);
477 base_offset = (base_offset << 7) + (c & 127);
478 }
479 base_offset = obj_list[nr].offset - base_offset;
d8f32556
NP
480 if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
481 die("offset value out of bound for delta base object");
8ee378a0 482
209c554a 483 delta_data = get_data(delta_size);
a1bf5ca2 484 if (!delta_data)
209c554a 485 return;
209c554a
NP
486 lo = 0;
487 hi = nr;
488 while (lo < hi) {
19716b21 489 mid = lo + (hi - lo) / 2;
209c554a
NP
490 if (base_offset < obj_list[mid].offset) {
491 hi = mid;
492 } else if (base_offset > obj_list[mid].offset) {
493 lo = mid + 1;
494 } else {
834bc47b 495 oidcpy(&base_oid, &obj_list[mid].oid);
496 base_found = !is_null_oid(&base_oid);
209c554a
NP
497 break;
498 }
499 }
500 if (!base_found) {
f2898cfa
JH
501 /*
502 * The delta base object is itself a delta that
503 * has not been resolved yet.
504 */
834bc47b 505 oidclr(&obj_list[nr].oid);
14228447 506 add_delta_to_list(nr, null_oid(), base_offset,
507 delta_data, delta_size);
209c554a 508 return;
2add1e6d
MK
509 }
510 }
511
834bc47b 512 if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
f2898cfa 513 return;
209c554a 514
bc726bd0
ÆAB
515 base = repo_read_object_file(the_repository, &base_oid, &type,
516 &base_size);
f986f2c8
JH
517 if (!base) {
518 error("failed to read delta-pack base object %s",
834bc47b 519 oid_to_hex(&base_oid));
3b67d291 520 if (!recover)
f986f2c8
JH
521 exit(1);
522 has_errors = 1;
523 return;
524 }
209c554a 525 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
ee639140 526 free(base);
8ee378a0
JH
527}
528
96a02f8f 529static void unpack_one(unsigned nr)
8ee378a0 530{
01247d87 531 unsigned shift;
48fb7deb
LT
532 unsigned char *pack;
533 unsigned long size, c;
67e5a5ec 534 enum object_type type;
74536958 535
209c554a
NP
536 obj_list[nr].offset = consumed_bytes;
537
67e5a5ec
LT
538 pack = fill(1);
539 c = *pack;
540 use(1);
a733cb60
LT
541 type = (c >> 4) & 7;
542 size = (c & 15);
01247d87 543 shift = 4;
a733cb60 544 while (c & 0x80) {
67e5a5ec 545 pack = fill(1);
209c554a 546 c = *pack;
67e5a5ec 547 use(1);
01247d87
LT
548 size += (c & 0x7f) << shift;
549 shift += 7;
a733cb60 550 }
96a02f8f 551
a733cb60 552 switch (type) {
aaf81223
HX
553 case OBJ_BLOB:
554 if (!dry_run && size > big_file_threshold) {
555 stream_blob(size, nr);
556 return;
557 }
558 /* fallthrough */
a733cb60
LT
559 case OBJ_COMMIT:
560 case OBJ_TREE:
a733cb60 561 case OBJ_TAG:
209c554a 562 unpack_non_delta_entry(type, size, nr);
a733cb60 563 return;
eb32d236 564 case OBJ_REF_DELTA:
209c554a
NP
565 case OBJ_OFS_DELTA:
566 unpack_delta_entry(type, size, nr);
a733cb60 567 return;
67e5a5ec 568 default:
f986f2c8
JH
569 error("bad object type %d", type);
570 has_errors = 1;
3b67d291 571 if (recover)
f986f2c8
JH
572 return;
573 exit(1);
74536958
LT
574 }
575}
576
74536958
LT
577static void unpack_all(void)
578{
67e5a5ec
LT
579 int i;
580 struct pack_header *hdr = fill(sizeof(struct pack_header));
b41860bf
MK
581
582 nr_objects = ntohl(hdr->hdr_entries);
67e5a5ec
LT
583
584 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
585 die("bad pack file");
d60fc1c8 586 if (!pack_version_ok(hdr->hdr_version))
6e1c2344
RJ
587 die("unknown pack file version %"PRIu32,
588 ntohl(hdr->hdr_version));
96a02f8f 589 use(sizeof(struct pack_header));
67e5a5ec 590
13aaf148 591 if (!quiet)
754dbc43 592 progress = start_progress(_("Unpacking objects"), nr_objects);
ca56dadb 593 CALLOC_ARRAY(obj_list, nr_objects);
425d290c 594 begin_odb_transaction();
96a02f8f
NP
595 for (i = 0; i < nr_objects; i++) {
596 unpack_one(i);
4d4fcc54 597 display_progress(progress, i + 1);
96a02f8f 598 }
425d290c 599 end_odb_transaction();
4d4fcc54 600 stop_progress(&progress);
96a02f8f 601
67e5a5ec
LT
602 if (delta_list)
603 die("unresolved deltas left after unpacking");
74536958
LT
604}
605
64413630 606int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
bad50dc8
LT
607{
608 int i;
834bc47b 609 struct object_id oid;
bad50dc8 610
6ebd1caf 611 read_replace_refs = 0;
dae556bd 612
ef90d6d4 613 git_config(git_default_config, NULL);
53228a5f 614
476e8011
JH
615 quiet = !isatty(2);
616
bad50dc8
LT
617 for (i = 1 ; i < argc; i++) {
618 const char *arg = argv[i];
619
620 if (*arg == '-') {
74536958
LT
621 if (!strcmp(arg, "-n")) {
622 dry_run = 1;
623 continue;
624 }
d36f7b80
LT
625 if (!strcmp(arg, "-q")) {
626 quiet = 1;
627 continue;
628 }
f986f2c8 629 if (!strcmp(arg, "-r")) {
3b67d291 630 recover = 1;
f986f2c8
JH
631 continue;
632 }
b41860bf
MK
633 if (!strcmp(arg, "--strict")) {
634 strict = 1;
635 continue;
636 }
5d477a33
JS
637 if (skip_prefix(arg, "--strict=", &arg)) {
638 strict = 1;
639 fsck_set_msg_types(&fsck_options, arg);
640 continue;
641 }
59556548 642 if (starts_with(arg, "--pack_header=")) {
bed006fb
NP
643 struct pack_header *hdr;
644 char *c;
645
646 hdr = (struct pack_header *)buffer;
647 hdr->hdr_signature = htonl(PACK_SIGNATURE);
648 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
649 if (*c != ',')
650 die("bad %s", arg);
651 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
652 if (*c)
653 die("bad %s", arg);
654 len = sizeof(*hdr);
655 continue;
656 }
5ad21867
CC
657 if (skip_prefix(arg, "--max-input-size=", &arg)) {
658 max_input_size = strtoumax(arg, NULL, 10);
659 continue;
660 }
bad50dc8
LT
661 usage(unpack_usage);
662 }
67e5a5ec
LT
663
664 /* We don't take any non-flag arguments now.. Maybe some day */
bad50dc8 665 usage(unpack_usage);
67e5a5ec 666 }
3206b6bd 667 the_hash_algo->init_fn(&ctx);
74536958 668 unpack_all();
3206b6bd 669 the_hash_algo->update_fn(&ctx, buffer, offset);
5951bf46 670 the_hash_algo->final_oid_fn(&oid, &ctx);
6e328d6c 671 if (strict) {
b41860bf 672 write_rest();
6e328d6c
JK
673 if (fsck_finish(&fsck_options))
674 die(_("fsck error in pack objects"));
675 }
67947c34 676 if (!hasheq(fill(the_hash_algo->rawsz), oid.hash))
67e5a5ec 677 die("final sha1 did not match");
3206b6bd 678 use(the_hash_algo->rawsz);
67e5a5ec
LT
679
680 /* Write the last part of the buffer to stdout */
681 while (len) {
1c15afb9
JH
682 int ret = xwrite(1, buffer + offset, len);
683 if (ret <= 0)
67e5a5ec 684 break;
67e5a5ec
LT
685 len -= ret;
686 offset += ret;
687 }
688
689 /* All done */
f986f2c8 690 return has_errors;
bad50dc8 691}