blob: f1c85a00ae9bb6586527a845c302cba47c4c8daf [file] [log] [blame]
Matthias Kestenholz64413632006-08-03 17:24:37 +02001#include "builtin.h"
Neeraj Singh425d2902022-04-04 22:20:13 -07002#include "bulk-checkin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00004#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00005#include "gettext.h"
Elijah Newrend88dbaa2023-04-11 00:41:51 -07006#include "git-zlib.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00007#include "hex.h"
Elijah Newrena034e912023-05-16 06:34:06 +00008#include "object-store-ll.h"
Linus Torvalds74536952005-06-25 15:59:31 -07009#include "object.h"
Junio C Hamano8ee378a2005-06-26 04:29:18 -070010#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -070011#include "pack.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020012#include "blob.h"
Elijah Newrencbeab742023-02-24 00:09:33 +000013#include "replace-object.h"
Calvin Wanfda5d952023-07-05 17:09:19 +000014#include "strbuf.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040015#include "progress.h"
Martin Koegler2add1e62008-02-25 22:46:10 +010016#include "decorate.h"
Martin Koeglerb41860b2008-02-25 22:46:11 +010017#include "fsck.h"
Linus Torvaldsbad50dc2005-06-25 15:27:14 -070018
Martin Koeglerb41860b2008-02-25 22:46:11 +010019static int dry_run, quiet, recover, has_errors, strict;
Junio C Hamano33e8fc82015-10-16 11:27:42 -070020static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]";
Linus Torvaldsbad50dc2005-06-25 15:27:14 -070021
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070022/* We always read in 4kB chunks. */
23static unsigned char buffer[4096];
Nicolas Pitred7dd0222007-04-09 01:06:30 -040024static unsigned int offset, len;
25static off_t consumed_bytes;
Christian Couder5ad21862016-08-24 20:41:56 +020026static off_t max_input_size;
brian m. carlson3206b6b2018-02-01 02:18:40 +000027static git_hash_ctx ctx;
Johannes Schindelin22410542015-06-22 17:25:00 +020028static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT;
SZEDER Gáborbae60ba2019-11-19 02:25:25 +010029static struct progress *progress;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070030
Junio C Hamanof2898cf2008-03-04 23:46:51 -080031/*
32 * When running under --strict mode, objects whose reachability are
33 * suspect are kept in core without getting written in the object
34 * store.
35 */
Martin Koegler2add1e62008-02-25 22:46:10 +010036struct obj_buffer {
37 char *buffer;
38 unsigned long size;
39};
40
41static struct decoration obj_decorate;
42
43static struct obj_buffer *lookup_object_buffer(struct object *base)
44{
45 return lookup_decoration(&obj_decorate, base);
46}
47
Martin Koeglerb41860b2008-02-25 22:46:11 +010048static void add_object_buffer(struct object *object, char *buffer, unsigned long size)
49{
50 struct obj_buffer *obj;
René Scharfeca56dad2021-03-13 17:17:22 +010051 CALLOC_ARRAY(obj, 1);
Martin Koeglerb41860b2008-02-25 22:46:11 +010052 obj->buffer = buffer;
53 obj->size = size;
54 if (add_decoration(&obj_decorate, object, obj))
brian m. carlsonf2fd0762015-11-10 02:22:28 +000055 die("object %s tried to add buffer twice!", oid_to_hex(&object->oid));
Martin Koeglerb41860b2008-02-25 22:46:11 +010056}
57
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070058/*
59 * Make sure at least "min" bytes are available in the buffer, and
60 * return the pointer to the buffer.
61 */
Edgar Toernig79a65692006-10-30 17:44:27 -080062static void *fill(int min)
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070063{
64 if (min <= len)
65 return buffer + offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070066 if (min > sizeof(buffer))
67 die("cannot fill %d bytes", min);
68 if (offset) {
brian m. carlson3206b6b2018-02-01 02:18:40 +000069 the_hash_algo->update_fn(&ctx, buffer, offset);
Edgar Toernig79a65692006-10-30 17:44:27 -080070 memmove(buffer, buffer + offset, len);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070071 offset = 0;
72 }
73 do {
Johan Herland8a912bc2007-05-15 14:49:22 +020074 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070075 if (ret <= 0) {
76 if (!ret)
77 die("early EOF");
Thomas Rastd824cbb2009-06-27 17:58:46 +020078 die_errno("read error on input");
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070079 }
80 len += ret;
81 } while (len < min);
82 return buffer;
83}
84
85static void use(int bytes)
86{
87 if (bytes > len)
88 die("used more bytes than were available");
89 len -= bytes;
90 offset += bytes;
Nicolas Pitred7dd0222007-04-09 01:06:30 -040091
92 /* make sure off_t is sufficiently large not to wrap */
Erik Faye-Lundc03c8312010-10-05 09:24:10 +020093 if (signed_add_overflows(consumed_bytes, bytes))
Nicolas Pitred7dd0222007-04-09 01:06:30 -040094 die("pack too large for current definition of off_t");
Nicolas Pitre209c5542006-09-21 00:07:39 -040095 consumed_bytes += bytes;
Christian Couder5ad21862016-08-24 20:41:56 +020096 if (max_input_size && consumed_bytes > max_input_size)
97 die(_("pack exceeds maximum allowed size"));
SZEDER Gáborbae60ba2019-11-19 02:25:25 +010098 display_throughput(progress, consumed_bytes);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070099}
100
Han Xina1bf5ca2022-06-11 10:44:16 +0800101/*
102 * Decompress zstream from the standard input into a newly
103 * allocated buffer of specified size and return the buffer.
104 * The caller is responsible to free the returned buffer.
105 *
106 * But for dry_run mode, "get_data()" is only used to check the
107 * integrity of data, and the returned buffer is not used at all.
108 * Therefore, in dry_run mode, "get_data()" will release the small
109 * allocated buffer which is reused to hold temporary zstream output
110 * and return NULL instead of returning garbage data.
111 */
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700112static void *get_data(unsigned long size)
113{
Junio C Hamanoef49a7a2011-06-10 11:52:15 -0700114 git_zstream stream;
Han Xina1bf5ca2022-06-11 10:44:16 +0800115 unsigned long bufsize = dry_run && size > 8192 ? 8192 : size;
116 void *buf = xmallocz(bufsize);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700117
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700118 memset(&stream, 0, sizeof(stream));
119
120 stream.next_out = buf;
Han Xina1bf5ca2022-06-11 10:44:16 +0800121 stream.avail_out = bufsize;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700122 stream.next_in = fill(1);
123 stream.avail_in = len;
Linus Torvalds39c68542009-01-07 19:54:47 -0800124 git_inflate_init(&stream);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700125
126 for (;;) {
Linus Torvalds39c68542009-01-07 19:54:47 -0800127 int ret = git_inflate(&stream, 0);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700128 use(len - stream.avail_in);
129 if (stream.total_out == size && ret == Z_STREAM_END)
130 break;
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700131 if (ret != Z_OK) {
Pete Wyckoff82247e92012-04-29 20:28:45 -0400132 error("inflate returned %d", ret);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000133 FREE_AND_NULL(buf);
Junio C Hamano3b67d292006-09-13 12:59:20 -0700134 if (!recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700135 exit(1);
136 has_errors = 1;
137 break;
138 }
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700139 stream.next_in = fill(1);
140 stream.avail_in = len;
Han Xina1bf5ca2022-06-11 10:44:16 +0800141 if (dry_run) {
142 /* reuse the buffer in dry_run mode */
143 stream.next_out = buf;
144 stream.avail_out = bufsize > size - stream.total_out ?
145 size - stream.total_out :
146 bufsize;
147 }
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700148 }
Linus Torvalds39c68542009-01-07 19:54:47 -0800149 git_inflate_end(&stream);
Han Xina1bf5ca2022-06-11 10:44:16 +0800150 if (dry_run)
151 FREE_AND_NULL(buf);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700152 return buf;
153}
154
155struct delta_info {
brian m. carlson834bc472017-05-06 22:10:12 +0000156 struct object_id base_oid;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400157 unsigned nr;
158 off_t base_offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700159 unsigned long size;
160 void *delta;
161 struct delta_info *next;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700162};
163
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700164static struct delta_info *delta_list;
Linus Torvalds74536952005-06-25 15:59:31 -0700165
brian m. carlson834bc472017-05-06 22:10:12 +0000166static void add_delta_to_list(unsigned nr, const struct object_id *base_oid,
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400167 off_t base_offset,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400168 void *delta, unsigned long size)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700169{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700170 struct delta_info *info = xmalloc(sizeof(*info));
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700171
brian m. carlson834bc472017-05-06 22:10:12 +0000172 oidcpy(&info->base_oid, base_oid);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400173 info->base_offset = base_offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700174 info->size = size;
175 info->delta = delta;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400176 info->nr = nr;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700177 info->next = delta_list;
178 delta_list = info;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700179}
180
Nicolas Pitre209c5542006-09-21 00:07:39 -0400181struct obj_info {
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400182 off_t offset;
brian m. carlson834bc472017-05-06 22:10:12 +0000183 struct object_id oid;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100184 struct object *obj;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400185};
186
Nguyễn Thái Ngọc Duy95308d62018-03-06 17:16:14 +0700187/* Remember to update object flag allocation in object.h */
Martin Koeglerb41860b2008-02-25 22:46:11 +0100188#define FLAG_OPEN (1u<<20)
189#define FLAG_WRITTEN (1u<<21)
190
Nicolas Pitre209c5542006-09-21 00:07:39 -0400191static struct obj_info *obj_list;
Linus Torvalds2af202b2009-06-18 10:28:43 -0700192static unsigned nr_objects;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100193
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800194/*
195 * Called only from check_object() after it verified this object
196 * is Ok.
197 */
Johannes Schindelin90a398b2014-09-10 15:52:51 +0200198static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf)
Martin Koeglerb41860b2008-02-25 22:46:11 +0100199{
brian m. carlson834bc472017-05-06 22:10:12 +0000200 struct object_id oid;
Johannes Schindelin90a398b2014-09-10 15:52:51 +0200201
Patryk Obaraa09c9852018-01-28 01:13:19 +0100202 if (write_object_file(obj_buf->buffer, obj_buf->size,
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100203 obj->type, &oid) < 0)
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000204 die("failed to write object %s", oid_to_hex(&obj->oid));
Martin Koeglerb41860b2008-02-25 22:46:11 +0100205 obj->flags |= FLAG_WRITTEN;
206}
207
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800208/*
209 * At the very end of the processing, write_rest() scans the objects
210 * that have reachability requirements and calls this function.
211 * Verify its reachability and validity recursively and write it out.
212 */
Ævar Arnfjörð Bjarmasona1aad712021-03-28 15:15:35 +0200213static int check_object(struct object *obj, enum object_type type,
Jeff King0b4e9012023-07-03 02:44:18 -0400214 void *data UNUSED,
215 struct fsck_options *options UNUSED)
Martin Koeglerb41860b2008-02-25 22:46:11 +0100216{
Johannes Schindelin90a398b2014-09-10 15:52:51 +0200217 struct obj_buffer *obj_buf;
218
Martin Koeglerb41860b2008-02-25 22:46:11 +0100219 if (!obj)
Junio C Hamano9a217392009-08-13 12:41:14 -0700220 return 1;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100221
222 if (obj->flags & FLAG_WRITTEN)
Junio C Hamano9a217392009-08-13 12:41:14 -0700223 return 0;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100224
225 if (type != OBJ_ANY && obj->type != type)
226 die("object type mismatch");
227
228 if (!(obj->flags & FLAG_OPEN)) {
229 unsigned long size;
Stefan Beller0df8e962018-04-25 11:20:59 -0700230 int type = oid_object_info(the_repository, &obj->oid, &size);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100231 if (type != obj->type || type <= 0)
232 die("object of unexpected type");
233 obj->flags |= FLAG_WRITTEN;
Junio C Hamano9a217392009-08-13 12:41:14 -0700234 return 0;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100235 }
236
Johannes Schindelin90a398b2014-09-10 15:52:51 +0200237 obj_buf = lookup_object_buffer(obj);
238 if (!obj_buf)
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000239 die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
Johannes Schindelin22410542015-06-22 17:25:00 +0200240 if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
Jeff Kingdb5a58c2018-05-02 16:37:09 -0400241 die("fsck error in packed object");
Johannes Schindelin22410542015-06-22 17:25:00 +0200242 fsck_options.walk = check_object;
243 if (fsck_walk(obj, NULL, &fsck_options))
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000244 die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
Johannes Schindelin90a398b2014-09-10 15:52:51 +0200245 write_cached_object(obj, obj_buf);
Junio C Hamano9a217392009-08-13 12:41:14 -0700246 return 0;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100247}
248
249static void write_rest(void)
250{
251 unsigned i;
Junio C Hamano9a217392009-08-13 12:41:14 -0700252 for (i = 0; i < nr_objects; i++) {
253 if (obj_list[i].obj)
Johannes Schindelin22410542015-06-22 17:25:00 +0200254 check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL);
Junio C Hamano9a217392009-08-13 12:41:14 -0700255 }
Martin Koeglerb41860b2008-02-25 22:46:11 +0100256}
Nicolas Pitre209c5542006-09-21 00:07:39 -0400257
Nicolas Pitre21666f12007-02-26 14:55:59 -0500258static void added_object(unsigned nr, enum object_type type,
259 void *data, unsigned long size);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400260
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800261/*
262 * Write out nr-th object from the list, now we know the contents
263 * of it. Under --strict, this buffers structured objects in-core,
264 * to be checked at the end.
265 */
Nicolas Pitre21666f12007-02-26 14:55:59 -0500266static void write_object(unsigned nr, enum object_type type,
267 void *buf, unsigned long size)
Nicolas Pitre209c5542006-09-21 00:07:39 -0400268{
Martin Koeglerb41860b2008-02-25 22:46:11 +0100269 if (!strict) {
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100270 if (write_object_file(buf, size, type,
Patryk Obaraa09c9852018-01-28 01:13:19 +0100271 &obj_list[nr].oid) < 0)
Martin Koeglerb41860b2008-02-25 22:46:11 +0100272 die("failed to write object");
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800273 added_object(nr, type, buf, size);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100274 free(buf);
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800275 obj_list[nr].obj = NULL;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100276 } else if (type == OBJ_BLOB) {
277 struct blob *blob;
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +0100278 if (write_object_file(buf, size, type,
Patryk Obaraa09c9852018-01-28 01:13:19 +0100279 &obj_list[nr].oid) < 0)
Martin Koeglerb41860b2008-02-25 22:46:11 +0100280 die("failed to write object");
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800281 added_object(nr, type, buf, size);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100282 free(buf);
283
Stefan Bellerda14a7f2018-06-28 18:21:55 -0700284 blob = lookup_blob(the_repository, &obj_list[nr].oid);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100285 if (blob)
286 blob->object.flags |= FLAG_WRITTEN;
287 else
288 die("invalid blob object");
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800289 obj_list[nr].obj = NULL;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100290 } else {
291 struct object *obj;
292 int eaten;
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +0100293 hash_object_file(the_hash_algo, buf, size, type,
Matheus Tavares2dcde202020-01-30 17:32:22 -0300294 &obj_list[nr].oid);
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800295 added_object(nr, type, buf, size);
Stefan Beller1ec5bfd2018-06-28 18:21:53 -0700296 obj = parse_object_buffer(the_repository, &obj_list[nr].oid,
297 type, size, buf,
brian m. carlsonc251c832017-05-06 22:10:38 +0000298 &eaten);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100299 if (!obj)
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800300 die("invalid %s", type_name(type));
Martin Koeglerb41860b2008-02-25 22:46:11 +0100301 add_object_buffer(obj, buf, size);
302 obj->flags |= FLAG_OPEN;
303 obj_list[nr].obj = obj;
304 }
Linus Torvaldscca70812005-06-29 09:38:02 -0700305}
306
Nicolas Pitre21666f12007-02-26 14:55:59 -0500307static void resolve_delta(unsigned nr, enum object_type type,
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700308 void *base, unsigned long base_size,
309 void *delta, unsigned long delta_size)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700310{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700311 void *result;
312 unsigned long result_size;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700313
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700314 result = patch_delta(base, base_size,
315 delta, delta_size,
316 &result_size);
317 if (!result)
318 die("failed to apply delta");
319 free(delta);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500320 write_object(nr, type, result, result_size);
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700321}
322
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800323/*
324 * We now know the contents of an object (which is nr-th in the pack);
325 * resolve all the deltified objects that are based on it.
326 */
Nicolas Pitre21666f12007-02-26 14:55:59 -0500327static void added_object(unsigned nr, enum object_type type,
328 void *data, unsigned long size)
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700329{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700330 struct delta_info **p = &delta_list;
331 struct delta_info *info;
332
333 while ((info = *p) != NULL) {
Jeff King4a7e27e2018-08-28 17:22:40 -0400334 if (oideq(&info->base_oid, &obj_list[nr].oid) ||
Nicolas Pitre209c5542006-09-21 00:07:39 -0400335 info->base_offset == obj_list[nr].offset) {
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700336 *p = info->next;
337 p = &delta_list;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400338 resolve_delta(info->nr, type, data, size,
339 info->delta, info->size);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700340 free(info);
341 continue;
342 }
343 p = &info->next;
344 }
345}
346
Nicolas Pitre21666f12007-02-26 14:55:59 -0500347static void unpack_non_delta_entry(enum object_type type, unsigned long size,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400348 unsigned nr)
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700349{
350 void *buf = get_data(size);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700351
Han Xina1bf5ca2022-06-11 10:44:16 +0800352 if (buf)
Nicolas Pitre21666f12007-02-26 14:55:59 -0500353 write_object(nr, type, buf, size);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700354}
355
Han Xinaaf81222022-06-11 10:44:21 +0800356struct input_zstream_data {
357 git_zstream *zstream;
358 unsigned char buf[8192];
359 int status;
360};
361
362static const void *feed_input_zstream(struct input_stream *in_stream,
363 unsigned long *readlen)
364{
365 struct input_zstream_data *data = in_stream->data;
366 git_zstream *zstream = data->zstream;
367 void *in = fill(1);
368
369 if (in_stream->is_finished) {
370 *readlen = 0;
371 return NULL;
372 }
373
374 zstream->next_out = data->buf;
375 zstream->avail_out = sizeof(data->buf);
376 zstream->next_in = in;
377 zstream->avail_in = len;
378
379 data->status = git_inflate(zstream, 0);
380
381 in_stream->is_finished = data->status != Z_OK;
382 use(len - zstream->avail_in);
383 *readlen = sizeof(data->buf) - zstream->avail_out;
384
385 return data->buf;
386}
387
388static void stream_blob(unsigned long size, unsigned nr)
389{
390 git_zstream zstream = { 0 };
391 struct input_zstream_data data = { 0 };
392 struct input_stream in_stream = {
393 .read = feed_input_zstream,
394 .data = &data,
395 };
396 struct obj_info *info = &obj_list[nr];
397
398 data.zstream = &zstream;
399 git_inflate_init(&zstream);
400
401 if (stream_loose_object(&in_stream, size, &info->oid))
402 die(_("failed to write object in stream"));
403
404 if (data.status != Z_STREAM_END)
405 die(_("inflate returned (%d)"), data.status);
406 git_inflate_end(&zstream);
407
408 if (strict) {
409 struct blob *blob = lookup_blob(the_repository, &info->oid);
410
411 if (!blob)
412 die(_("invalid blob object from stream"));
413 blob->object.flags |= FLAG_WRITTEN;
414 }
415 info->obj = NULL;
416}
417
brian m. carlson834bc472017-05-06 22:10:12 +0000418static int resolve_against_held(unsigned nr, const struct object_id *base,
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800419 void *delta_data, unsigned long delta_size)
420{
421 struct object *obj;
422 struct obj_buffer *obj_buffer;
Jeff Kingd0229ab2019-06-20 03:41:14 -0400423 obj = lookup_object(the_repository, base);
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800424 if (!obj)
425 return 0;
426 obj_buffer = lookup_object_buffer(obj);
427 if (!obj_buffer)
428 return 0;
429 resolve_delta(nr, obj->type, obj_buffer->buffer,
430 obj_buffer->size, delta_data, delta_size);
431 return 1;
432}
433
Nicolas Pitre21666f12007-02-26 14:55:59 -0500434static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400435 unsigned nr)
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700436{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700437 void *delta_data, *base;
438 unsigned long base_size;
brian m. carlson834bc472017-05-06 22:10:12 +0000439 struct object_id base_oid;
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700440
Nicolas Pitre21666f12007-02-26 14:55:59 -0500441 if (type == OBJ_REF_DELTA) {
brian m. carlson92e2cab2021-04-26 01:02:50 +0000442 oidread(&base_oid, fill(the_hash_algo->rawsz));
brian m. carlson3206b6b2018-02-01 02:18:40 +0000443 use(the_hash_algo->rawsz);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400444 delta_data = get_data(delta_size);
Han Xina1bf5ca2022-06-11 10:44:16 +0800445 if (!delta_data)
Nicolas Pitre209c5542006-09-21 00:07:39 -0400446 return;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200447 if (repo_has_object_file(the_repository, &base_oid))
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800448 ; /* Ok we have this one */
brian m. carlson834bc472017-05-06 22:10:12 +0000449 else if (resolve_against_held(nr, &base_oid,
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800450 delta_data, delta_size))
451 return; /* we are done */
452 else {
453 /* cannot resolve yet --- queue it */
brian m. carlson834bc472017-05-06 22:10:12 +0000454 oidclr(&obj_list[nr].oid);
455 add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400456 return;
457 }
458 } else {
459 unsigned base_found = 0;
460 unsigned char *pack, c;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400461 off_t base_offset;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400462 unsigned lo, mid, hi;
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700463
Nicolas Pitre209c5542006-09-21 00:07:39 -0400464 pack = fill(1);
465 c = *pack;
466 use(1);
467 base_offset = c & 127;
468 while (c & 128) {
469 base_offset += 1;
Nicolas Pitre8723f212007-04-09 01:06:29 -0400470 if (!base_offset || MSB(base_offset, 7))
Nicolas Pitre209c5542006-09-21 00:07:39 -0400471 die("offset value overflow for delta base object");
472 pack = fill(1);
473 c = *pack;
474 use(1);
475 base_offset = (base_offset << 7) + (c & 127);
476 }
477 base_offset = obj_list[nr].offset - base_offset;
Nicolas Pitred8f32552008-10-29 19:02:45 -0400478 if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
479 die("offset value out of bound for delta base object");
Nicolas Pitre209c5542006-09-21 00:07:39 -0400480
481 delta_data = get_data(delta_size);
Han Xina1bf5ca2022-06-11 10:44:16 +0800482 if (!delta_data)
Nicolas Pitre209c5542006-09-21 00:07:39 -0400483 return;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400484 lo = 0;
485 hi = nr;
486 while (lo < hi) {
Derrick Stolee19716b22017-10-08 14:29:37 -0400487 mid = lo + (hi - lo) / 2;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400488 if (base_offset < obj_list[mid].offset) {
489 hi = mid;
490 } else if (base_offset > obj_list[mid].offset) {
491 lo = mid + 1;
492 } else {
brian m. carlson834bc472017-05-06 22:10:12 +0000493 oidcpy(&base_oid, &obj_list[mid].oid);
494 base_found = !is_null_oid(&base_oid);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400495 break;
496 }
497 }
498 if (!base_found) {
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800499 /*
500 * The delta base object is itself a delta that
501 * has not been resolved yet.
502 */
brian m. carlson834bc472017-05-06 22:10:12 +0000503 oidclr(&obj_list[nr].oid);
brian m. carlson14228442021-04-26 01:02:56 +0000504 add_delta_to_list(nr, null_oid(), base_offset,
505 delta_data, delta_size);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400506 return;
507 }
Linus Torvaldsdddafff2005-06-29 09:53:20 -0700508 }
Linus Torvaldsc4fb06c2005-06-26 08:40:08 -0700509
brian m. carlson834bc472017-05-06 22:10:12 +0000510 if (resolve_against_held(nr, &base_oid, delta_data, delta_size))
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800511 return;
Martin Koegler2add1e62008-02-25 22:46:10 +0100512
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200513 base = repo_read_object_file(the_repository, &base_oid, &type,
514 &base_size);
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700515 if (!base) {
516 error("failed to read delta-pack base object %s",
brian m. carlson834bc472017-05-06 22:10:12 +0000517 oid_to_hex(&base_oid));
Junio C Hamano3b67d292006-09-13 12:59:20 -0700518 if (!recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700519 exit(1);
520 has_errors = 1;
521 return;
522 }
Nicolas Pitre209c5542006-09-21 00:07:39 -0400523 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
Sergey Vlasovee639142005-08-03 16:11:00 +0400524 free(base);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700525}
526
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400527static void unpack_one(unsigned nr)
Linus Torvalds74536952005-06-25 15:59:31 -0700528{
Linus Torvalds01247d82005-06-28 22:15:57 -0700529 unsigned shift;
Linus Torvalds48fb7de2009-06-17 17:22:27 -0700530 unsigned char *pack;
531 unsigned long size, c;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700532 enum object_type type;
Linus Torvalds74536952005-06-25 15:59:31 -0700533
Nicolas Pitre209c5542006-09-21 00:07:39 -0400534 obj_list[nr].offset = consumed_bytes;
535
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700536 pack = fill(1);
537 c = *pack;
538 use(1);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700539 type = (c >> 4) & 7;
540 size = (c & 15);
Linus Torvalds01247d82005-06-28 22:15:57 -0700541 shift = 4;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700542 while (c & 0x80) {
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700543 pack = fill(1);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400544 c = *pack;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700545 use(1);
Linus Torvalds01247d82005-06-28 22:15:57 -0700546 size += (c & 0x7f) << shift;
547 shift += 7;
Linus Torvalds74536952005-06-25 15:59:31 -0700548 }
Linus Torvaldsd36f7b82005-07-09 10:43:02 -0700549
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700550 switch (type) {
Han Xinaaf81222022-06-11 10:44:21 +0800551 case OBJ_BLOB:
552 if (!dry_run && size > big_file_threshold) {
553 stream_blob(size, nr);
554 return;
555 }
556 /* fallthrough */
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700557 case OBJ_COMMIT:
558 case OBJ_TREE:
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700559 case OBJ_TAG:
Nicolas Pitre209c5542006-09-21 00:07:39 -0400560 unpack_non_delta_entry(type, size, nr);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700561 return;
Nicolas Pitreeb32d232006-09-21 00:06:49 -0400562 case OBJ_REF_DELTA:
Nicolas Pitre209c5542006-09-21 00:07:39 -0400563 case OBJ_OFS_DELTA:
564 unpack_delta_entry(type, size, nr);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700565 return;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700566 default:
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700567 error("bad object type %d", type);
568 has_errors = 1;
Junio C Hamano3b67d292006-09-13 12:59:20 -0700569 if (recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700570 return;
571 exit(1);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700572 }
Linus Torvalds74536952005-06-25 15:59:31 -0700573}
574
Linus Torvalds74536952005-06-25 15:59:31 -0700575static void unpack_all(void)
576{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700577 int i;
578 struct pack_header *hdr = fill(sizeof(struct pack_header));
Martin Koeglerb41860b2008-02-25 22:46:11 +0100579
580 nr_objects = ntohl(hdr->hdr_entries);
Linus Torvalds74536952005-06-25 15:59:31 -0700581
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700582 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
583 die("bad pack file");
Nicolas Pitred60fc1c2006-02-09 17:50:04 -0500584 if (!pack_version_ok(hdr->hdr_version))
Ramsay Jones6e1c2342008-07-03 16:52:09 +0100585 die("unknown pack file version %"PRIu32,
586 ntohl(hdr->hdr_version));
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700587 use(sizeof(struct pack_header));
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400588
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400589 if (!quiet)
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700590 progress = start_progress(_("Unpacking objects"), nr_objects);
René Scharfeca56dad2021-03-13 17:17:22 +0100591 CALLOC_ARRAY(obj_list, nr_objects);
Neeraj Singh425d2902022-04-04 22:20:13 -0700592 begin_odb_transaction();
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400593 for (i = 0; i < nr_objects; i++) {
594 unpack_one(i);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400595 display_progress(progress, i + 1);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400596 }
Neeraj Singh425d2902022-04-04 22:20:13 -0700597 end_odb_transaction();
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400598 stop_progress(&progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400599
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700600 if (delta_list)
601 die("unresolved deltas left after unpacking");
Linus Torvalds74536952005-06-25 15:59:31 -0700602}
603
Jeff King5247b762023-03-28 16:56:55 -0400604int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700605{
606 int i;
brian m. carlson834bc472017-05-06 22:10:12 +0000607 struct object_id oid;
Eric Wonge0b8c842023-09-01 02:09:28 +0000608 git_hash_ctx tmp_ctx;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700609
Derrick Stoleed24eda42023-06-06 13:24:35 +0000610 disable_replace_refs();
Christian Couderdae556b2009-01-23 10:07:46 +0100611
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100612 git_config(git_default_config, NULL);
Junio C Hamano53228a52005-11-26 00:50:02 -0800613
Junio C Hamano476e8012006-01-06 18:53:16 -0800614 quiet = !isatty(2);
615
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700616 for (i = 1 ; i < argc; i++) {
617 const char *arg = argv[i];
618
619 if (*arg == '-') {
Linus Torvalds74536952005-06-25 15:59:31 -0700620 if (!strcmp(arg, "-n")) {
621 dry_run = 1;
622 continue;
623 }
Linus Torvaldsd36f7b82005-07-09 10:43:02 -0700624 if (!strcmp(arg, "-q")) {
625 quiet = 1;
626 continue;
627 }
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700628 if (!strcmp(arg, "-r")) {
Junio C Hamano3b67d292006-09-13 12:59:20 -0700629 recover = 1;
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700630 continue;
631 }
Martin Koeglerb41860b2008-02-25 22:46:11 +0100632 if (!strcmp(arg, "--strict")) {
633 strict = 1;
634 continue;
635 }
Johannes Schindelin5d477a32015-06-22 17:25:31 +0200636 if (skip_prefix(arg, "--strict=", &arg)) {
637 strict = 1;
638 fsck_set_msg_types(&fsck_options, arg);
639 continue;
640 }
Christian Couder59556542013-11-30 21:55:40 +0100641 if (starts_with(arg, "--pack_header=")) {
Nicolas Pitrebed006f2006-11-01 17:06:20 -0500642 struct pack_header *hdr;
643 char *c;
644
645 hdr = (struct pack_header *)buffer;
646 hdr->hdr_signature = htonl(PACK_SIGNATURE);
647 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
648 if (*c != ',')
649 die("bad %s", arg);
650 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
651 if (*c)
652 die("bad %s", arg);
653 len = sizeof(*hdr);
654 continue;
655 }
Christian Couder5ad21862016-08-24 20:41:56 +0200656 if (skip_prefix(arg, "--max-input-size=", &arg)) {
657 max_input_size = strtoumax(arg, NULL, 10);
658 continue;
659 }
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700660 usage(unpack_usage);
661 }
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700662
663 /* We don't take any non-flag arguments now.. Maybe some day */
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700664 usage(unpack_usage);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700665 }
brian m. carlson3206b6b2018-02-01 02:18:40 +0000666 the_hash_algo->init_fn(&ctx);
Linus Torvalds74536952005-06-25 15:59:31 -0700667 unpack_all();
brian m. carlson3206b6b2018-02-01 02:18:40 +0000668 the_hash_algo->update_fn(&ctx, buffer, offset);
Eric Wonge0b8c842023-09-01 02:09:28 +0000669 the_hash_algo->init_fn(&tmp_ctx);
670 the_hash_algo->clone_fn(&tmp_ctx, &ctx);
671 the_hash_algo->final_oid_fn(&oid, &tmp_ctx);
Jeff King6e328d62018-05-04 19:40:08 -0400672 if (strict) {
Martin Koeglerb41860b2008-02-25 22:46:11 +0100673 write_rest();
Jeff King6e328d62018-05-04 19:40:08 -0400674 if (fsck_finish(&fsck_options))
675 die(_("fsck error in pack objects"));
676 }
Jeff King67947c32018-08-28 17:22:52 -0400677 if (!hasheq(fill(the_hash_algo->rawsz), oid.hash))
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700678 die("final sha1 did not match");
brian m. carlson3206b6b2018-02-01 02:18:40 +0000679 use(the_hash_algo->rawsz);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700680
681 /* Write the last part of the buffer to stdout */
Junio C Hamanofa6c3832024-03-02 11:03:46 -0800682 write_in_full(1, buffer + offset, len);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700683
684 /* All done */
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700685 return has_errors;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700686}