]>
Commit | Line | Data |
---|---|---|
2ad9d4cb | 1 | #include "builtin.h" |
b2141fc1 | 2 | #include "config.h" |
9cf6d335 SV |
3 | #include "delta.h" |
4 | #include "pack.h" | |
5 | #include "csum-file.h" | |
8e440259 PE |
6 | #include "blob.h" |
7 | #include "commit.h" | |
8 | #include "tag.h" | |
9 | #include "tree.h" | |
96a02f8f | 10 | #include "progress.h" |
0153be05 | 11 | #include "fsck.h" |
d807c4a0 | 12 | #include "exec-cmd.h" |
4614043c | 13 | #include "streaming.h" |
b8a2486f | 14 | #include "thread-utils.h" |
4f39cd82 | 15 | #include "packfile.h" |
a80d72db | 16 | #include "object-store.h" |
b14ed5ad | 17 | #include "promisor-remote.h" |
9cf6d335 SV |
18 | |
19 | static const char index_pack_usage[] = | |
e337a04d | 20 | "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])"; |
9cf6d335 | 21 | |
9cba13ca | 22 | struct object_entry { |
aa7e44bf | 23 | struct pack_idx_entry idx; |
2d477051 | 24 | unsigned long size; |
41730576 NTND |
25 | unsigned char hdr_size; |
26 | signed char type; | |
27 | signed char real_type; | |
9cf6d335 SV |
28 | }; |
29 | ||
41730576 | 30 | struct object_stat { |
38a45561 JH |
31 | unsigned delta_depth; |
32 | int base_object_no; | |
53dda6ff NP |
33 | }; |
34 | ||
f41aebd4 | 35 | struct base_data { |
b4718cae | 36 | /* Initialized by make_base(). */ |
4a438cab | 37 | struct base_data *base; |
03993e13 | 38 | struct object_entry *obj; |
2baad220 NTND |
39 | int ref_first, ref_last; |
40 | int ofs_first, ofs_last; | |
f08cbf60 JT |
41 | /* |
42 | * Threads should increment retain_data if they are about to call | |
43 | * patch_delta() using this struct's data as a base, and decrement this | |
44 | * when they are done. While retain_data is nonzero, this struct's data | |
45 | * will not be freed even if the delta base cache limit is exceeded. | |
46 | */ | |
47 | int retain_data; | |
48 | /* | |
49 | * The number of direct children that have not been fully processed | |
50 | * (entered work_head, entered done_head, left done_head). When this | |
51 | * number reaches zero, this struct base_data can be freed. | |
52 | */ | |
53 | int children_remaining; | |
b4718cae JT |
54 | |
55 | /* Not initialized by make_base(). */ | |
f08cbf60 | 56 | struct list_head list; |
b4718cae JT |
57 | void *data; |
58 | unsigned long size; | |
f41aebd4 SP |
59 | }; |
60 | ||
f08cbf60 JT |
61 | /* |
62 | * Stack of struct base_data that have unprocessed children. | |
63 | * threaded_second_pass() uses this as a source of work (the other being the | |
64 | * objects array). | |
65 | * | |
66 | * Guarded by work_mutex. | |
67 | */ | |
68 | static LIST_HEAD(work_head); | |
69 | ||
70 | /* | |
71 | * Stack of struct base_data that have children, all of whom have been | |
72 | * processed or are being processed, and at least one child is being processed. | |
73 | * These struct base_data must be kept around until the last child is | |
74 | * processed. | |
75 | * | |
76 | * Guarded by work_mutex. | |
77 | */ | |
78 | static LIST_HEAD(done_head); | |
79 | ||
80 | /* | |
81 | * All threads share one delta base cache. | |
82 | * | |
83 | * base_cache_used is guarded by work_mutex, and base_cache_limit is read-only | |
84 | * in a thread. | |
85 | */ | |
86 | static size_t base_cache_used; | |
87 | static size_t base_cache_limit; | |
88 | ||
b8a2486f | 89 | struct thread_local { |
b8a2486f | 90 | pthread_t thread; |
39539495 | 91 | int pack_fd; |
b8a2486f NTND |
92 | }; |
93 | ||
95308d64 | 94 | /* Remember to update object flag allocation in object.h */ |
0153be05 MK |
95 | #define FLAG_LINK (1u<<20) |
96 | #define FLAG_CHECKED (1u<<21) | |
97 | ||
c6458e60 NTND |
98 | struct ofs_delta_entry { |
99 | off_t offset; | |
100 | int obj_no; | |
101 | }; | |
102 | ||
103 | struct ref_delta_entry { | |
af8caf33 | 104 | struct object_id oid; |
636171cb | 105 | int obj_no; |
9cf6d335 SV |
106 | }; |
107 | ||
9cf6d335 | 108 | static struct object_entry *objects; |
41730576 | 109 | static struct object_stat *obj_stat; |
c6458e60 NTND |
110 | static struct ofs_delta_entry *ofs_deltas; |
111 | static struct ref_delta_entry *ref_deltas; | |
b8a2486f | 112 | static struct thread_local nothread_data; |
9cf6d335 | 113 | static int nr_objects; |
c6458e60 NTND |
114 | static int nr_ofs_deltas; |
115 | static int nr_ref_deltas; | |
116 | static int ref_deltas_alloc; | |
636171cb | 117 | static int nr_resolved_deltas; |
b8a2486f | 118 | static int nr_threads; |
9cf6d335 | 119 | |
e42797f5 | 120 | static int from_stdin; |
0153be05 | 121 | static int strict; |
c6807a40 | 122 | static int do_fsck_object; |
22410549 | 123 | static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; |
3c9af366 | 124 | static int verbose; |
e376f17f | 125 | static int show_resolving_progress; |
3aba2fdd | 126 | static int show_stat; |
c6807a40 | 127 | static int check_self_contained_and_connected; |
3c9af366 | 128 | |
dc6a0757 | 129 | static struct progress *progress; |
e42797f5 | 130 | |
2d477051 NP |
131 | /* We always read in 4kB chunks. */ |
132 | static unsigned char input_buffer[4096]; | |
d7dd0223 NP |
133 | static unsigned int input_offset, input_len; |
134 | static off_t consumed_bytes; | |
411481be | 135 | static off_t max_input_size; |
d1a0ed18 | 136 | static unsigned deepest_delta; |
454253f0 | 137 | static git_hash_ctx input_ctx; |
ee5743ce | 138 | static uint32_t input_crc32; |
39539495 NTND |
139 | static int input_fd, output_fd; |
140 | static const char *curr_pack; | |
2d477051 | 141 | |
b8a2486f NTND |
142 | static struct thread_local *thread_data; |
143 | static int nr_dispatched; | |
144 | static int threads_active; | |
145 | ||
146 | static pthread_mutex_t read_mutex; | |
147 | #define read_lock() lock_mutex(&read_mutex) | |
148 | #define read_unlock() unlock_mutex(&read_mutex) | |
149 | ||
150 | static pthread_mutex_t counter_mutex; | |
151 | #define counter_lock() lock_mutex(&counter_mutex) | |
152 | #define counter_unlock() unlock_mutex(&counter_mutex) | |
153 | ||
154 | static pthread_mutex_t work_mutex; | |
155 | #define work_lock() lock_mutex(&work_mutex) | |
156 | #define work_unlock() unlock_mutex(&work_mutex) | |
157 | ||
3aba2fdd NTND |
158 | static pthread_mutex_t deepest_delta_mutex; |
159 | #define deepest_delta_lock() lock_mutex(&deepest_delta_mutex) | |
160 | #define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex) | |
161 | ||
ab791dd1 JK |
162 | static pthread_mutex_t type_cas_mutex; |
163 | #define type_cas_lock() lock_mutex(&type_cas_mutex) | |
164 | #define type_cas_unlock() unlock_mutex(&type_cas_mutex) | |
165 | ||
b8a2486f NTND |
166 | static pthread_key_t key; |
167 | ||
168 | static inline void lock_mutex(pthread_mutex_t *mutex) | |
169 | { | |
170 | if (threads_active) | |
171 | pthread_mutex_lock(mutex); | |
172 | } | |
173 | ||
174 | static inline void unlock_mutex(pthread_mutex_t *mutex) | |
175 | { | |
176 | if (threads_active) | |
177 | pthread_mutex_unlock(mutex); | |
178 | } | |
179 | ||
180 | /* | |
181 | * Mutex and conditional variable can't be statically-initialized on Windows. | |
182 | */ | |
183 | static void init_thread(void) | |
184 | { | |
39539495 | 185 | int i; |
b8a2486f NTND |
186 | init_recursive_mutex(&read_mutex); |
187 | pthread_mutex_init(&counter_mutex, NULL); | |
188 | pthread_mutex_init(&work_mutex, NULL); | |
ab791dd1 | 189 | pthread_mutex_init(&type_cas_mutex, NULL); |
3aba2fdd NTND |
190 | if (show_stat) |
191 | pthread_mutex_init(&deepest_delta_mutex, NULL); | |
b8a2486f NTND |
192 | pthread_key_create(&key, NULL); |
193 | thread_data = xcalloc(nr_threads, sizeof(*thread_data)); | |
39539495 NTND |
194 | for (i = 0; i < nr_threads; i++) { |
195 | thread_data[i].pack_fd = open(curr_pack, O_RDONLY); | |
196 | if (thread_data[i].pack_fd == -1) | |
197 | die_errno(_("unable to open %s"), curr_pack); | |
198 | } | |
199 | ||
b8a2486f NTND |
200 | threads_active = 1; |
201 | } | |
202 | ||
203 | static void cleanup_thread(void) | |
204 | { | |
39539495 | 205 | int i; |
b8a2486f NTND |
206 | if (!threads_active) |
207 | return; | |
208 | threads_active = 0; | |
209 | pthread_mutex_destroy(&read_mutex); | |
210 | pthread_mutex_destroy(&counter_mutex); | |
211 | pthread_mutex_destroy(&work_mutex); | |
ab791dd1 | 212 | pthread_mutex_destroy(&type_cas_mutex); |
3aba2fdd NTND |
213 | if (show_stat) |
214 | pthread_mutex_destroy(&deepest_delta_mutex); | |
39539495 NTND |
215 | for (i = 0; i < nr_threads; i++) |
216 | close(thread_data[i].pack_fd); | |
b8a2486f NTND |
217 | pthread_key_delete(key); |
218 | free(thread_data); | |
219 | } | |
220 | ||
22410549 | 221 | static int mark_link(struct object *obj, int type, void *data, struct fsck_options *options) |
0153be05 MK |
222 | { |
223 | if (!obj) | |
224 | return -1; | |
225 | ||
226 | if (type != OBJ_ANY && obj->type != type) | |
f2fd0760 | 227 | die(_("object type mismatch at %s"), oid_to_hex(&obj->oid)); |
0153be05 MK |
228 | |
229 | obj->flags |= FLAG_LINK; | |
230 | return 0; | |
231 | } | |
232 | ||
233 | /* The content of each linked object must have been checked | |
234 | or it must be already present in the object database */ | |
c6807a40 | 235 | static unsigned check_object(struct object *obj) |
0153be05 MK |
236 | { |
237 | if (!obj) | |
c6807a40 | 238 | return 0; |
0153be05 MK |
239 | |
240 | if (!(obj->flags & FLAG_LINK)) | |
c6807a40 | 241 | return 0; |
0153be05 MK |
242 | |
243 | if (!(obj->flags & FLAG_CHECKED)) { | |
244 | unsigned long size; | |
0df8e965 | 245 | int type = oid_object_info(the_repository, &obj->oid, &size); |
77583e77 JK |
246 | if (type <= 0) |
247 | die(_("did not receive expected object %s"), | |
f2fd0760 | 248 | oid_to_hex(&obj->oid)); |
77583e77 JK |
249 | if (type != obj->type) |
250 | die(_("object %s: expected type %s, found %s"), | |
f2fd0760 | 251 | oid_to_hex(&obj->oid), |
debca9d2 | 252 | type_name(obj->type), type_name(type)); |
0153be05 | 253 | obj->flags |= FLAG_CHECKED; |
c6807a40 | 254 | return 1; |
0153be05 | 255 | } |
c6807a40 NTND |
256 | |
257 | return 0; | |
0153be05 MK |
258 | } |
259 | ||
c6807a40 | 260 | static unsigned check_objects(void) |
0153be05 | 261 | { |
c6807a40 | 262 | unsigned i, max, foreign_nr = 0; |
0153be05 MK |
263 | |
264 | max = get_max_object_index(); | |
79e3aa66 SG |
265 | |
266 | if (verbose) | |
267 | progress = start_delayed_progress(_("Checking objects"), max); | |
268 | ||
269 | for (i = 0; i < max; i++) { | |
c6807a40 | 270 | foreign_nr += check_object(get_indexed_object(i)); |
79e3aa66 SG |
271 | display_progress(progress, i + 1); |
272 | } | |
273 | ||
274 | stop_progress(&progress); | |
c6807a40 | 275 | return foreign_nr; |
0153be05 MK |
276 | } |
277 | ||
278 | ||
636171cb | 279 | /* Discard current buffer used content. */ |
a6e8a767 | 280 | static void flush(void) |
636171cb NP |
281 | { |
282 | if (input_offset) { | |
283 | if (output_fd >= 0) | |
284 | write_or_die(output_fd, input_buffer, input_offset); | |
454253f0 | 285 | the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset); |
554a2636 | 286 | memmove(input_buffer, input_buffer + input_offset, input_len); |
636171cb NP |
287 | input_offset = 0; |
288 | } | |
289 | } | |
290 | ||
2d477051 NP |
291 | /* |
292 | * Make sure at least "min" bytes are available in the buffer, and | |
293 | * return the pointer to the buffer. | |
294 | */ | |
b89c4e93 | 295 | static void *fill(int min) |
9cf6d335 | 296 | { |
2d477051 NP |
297 | if (min <= input_len) |
298 | return input_buffer + input_offset; | |
299 | if (min > sizeof(input_buffer)) | |
c2b97ecf NTND |
300 | die(Q_("cannot fill %d byte", |
301 | "cannot fill %d bytes", | |
302 | min), | |
303 | min); | |
636171cb | 304 | flush(); |
2d477051 | 305 | do { |
8a912bcb | 306 | ssize_t ret = xread(input_fd, input_buffer + input_len, |
2d477051 NP |
307 | sizeof(input_buffer) - input_len); |
308 | if (ret <= 0) { | |
309 | if (!ret) | |
c2b97ecf NTND |
310 | die(_("early EOF")); |
311 | die_errno(_("read error on input")); | |
2d477051 NP |
312 | } |
313 | input_len += ret; | |
218558af NP |
314 | if (from_stdin) |
315 | display_throughput(progress, consumed_bytes + input_len); | |
2d477051 NP |
316 | } while (input_len < min); |
317 | return input_buffer; | |
318 | } | |
319 | ||
320 | static void use(int bytes) | |
321 | { | |
322 | if (bytes > input_len) | |
c2b97ecf | 323 | die(_("used more bytes than were available")); |
ee5743ce | 324 | input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes); |
2d477051 NP |
325 | input_len -= bytes; |
326 | input_offset += bytes; | |
d7dd0223 NP |
327 | |
328 | /* make sure off_t is sufficiently large not to wrap */ | |
c03c8315 | 329 | if (signed_add_overflows(consumed_bytes, bytes)) |
c2b97ecf | 330 | die(_("pack too large for current definition of off_t")); |
2d477051 | 331 | consumed_bytes += bytes; |
411481be JK |
332 | if (max_input_size && consumed_bytes > max_input_size) |
333 | die(_("pack exceeds maximum allowed size")); | |
2d477051 | 334 | } |
9cf6d335 | 335 | |
3bb72562 | 336 | static const char *open_pack_file(const char *pack_name) |
2d477051 | 337 | { |
e42797f5 NP |
338 | if (from_stdin) { |
339 | input_fd = 0; | |
340 | if (!pack_name) { | |
594fa999 JK |
341 | struct strbuf tmp_file = STRBUF_INIT; |
342 | output_fd = odb_mkstemp(&tmp_file, | |
6e180cdc | 343 | "pack/tmp_pack_XXXXXX"); |
594fa999 | 344 | pack_name = strbuf_detach(&tmp_file, NULL); |
892e723a | 345 | } else { |
e42797f5 | 346 | output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); |
892e723a JK |
347 | if (output_fd < 0) |
348 | die_errno(_("unable to create '%s'"), pack_name); | |
349 | } | |
39539495 | 350 | nothread_data.pack_fd = output_fd; |
e42797f5 NP |
351 | } else { |
352 | input_fd = open(pack_name, O_RDONLY); | |
353 | if (input_fd < 0) | |
c2b97ecf | 354 | die_errno(_("cannot open packfile '%s'"), pack_name); |
e42797f5 | 355 | output_fd = -1; |
39539495 | 356 | nothread_data.pack_fd = input_fd; |
e42797f5 | 357 | } |
454253f0 | 358 | the_hash_algo->init_fn(&input_ctx); |
e42797f5 | 359 | return pack_name; |
9cf6d335 SV |
360 | } |
361 | ||
362 | static void parse_pack_header(void) | |
363 | { | |
2d477051 | 364 | struct pack_header *hdr = fill(sizeof(struct pack_header)); |
9cf6d335 SV |
365 | |
366 | /* Header consistency check */ | |
9cf6d335 | 367 | if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) |
c2b97ecf | 368 | die(_("pack signature mismatch")); |
d60fc1c8 | 369 | if (!pack_version_ok(hdr->hdr_version)) |
f350df42 | 370 | die(_("pack version %"PRIu32" unsupported"), |
6e1c2344 | 371 | ntohl(hdr->hdr_version)); |
9cf6d335 SV |
372 | |
373 | nr_objects = ntohl(hdr->hdr_entries); | |
2d477051 | 374 | use(sizeof(struct pack_header)); |
9cf6d335 SV |
375 | } |
376 | ||
fd3e6747 | 377 | static NORETURN void bad_object(off_t offset, const char *format, |
a4f3131c | 378 | ...) __attribute__((format (printf, 2, 3))); |
9cf6d335 | 379 | |
fd3e6747 | 380 | static NORETURN void bad_object(off_t offset, const char *format, ...) |
9cf6d335 SV |
381 | { |
382 | va_list params; | |
383 | char buf[1024]; | |
384 | ||
385 | va_start(params, format); | |
386 | vsnprintf(buf, sizeof(buf), format, params); | |
387 | va_end(params); | |
fd3e6747 NTND |
388 | die(_("pack has bad object at offset %"PRIuMAX": %s"), |
389 | (uintmax_t)offset, buf); | |
9cf6d335 SV |
390 | } |
391 | ||
b8a2486f NTND |
392 | static inline struct thread_local *get_thread_data(void) |
393 | { | |
2094c5e5 NTND |
394 | if (HAVE_THREADS) { |
395 | if (threads_active) | |
396 | return pthread_getspecific(key); | |
397 | assert(!threads_active && | |
398 | "This should only be reached when all threads are gone"); | |
399 | } | |
b8a2486f NTND |
400 | return ¬hread_data; |
401 | } | |
402 | ||
b8a2486f NTND |
403 | static void set_thread_data(struct thread_local *data) |
404 | { | |
405 | if (threads_active) | |
406 | pthread_setspecific(key, data); | |
407 | } | |
b8a2486f | 408 | |
6a87ed97 NP |
409 | static void free_base_data(struct base_data *c) |
410 | { | |
411 | if (c->data) { | |
6a83d902 | 412 | FREE_AND_NULL(c->data); |
f08cbf60 | 413 | base_cache_used -= c->size; |
6a87ed97 NP |
414 | } |
415 | } | |
416 | ||
f08cbf60 | 417 | static void prune_base_data(struct base_data *retain) |
92392b4a | 418 | { |
f08cbf60 | 419 | struct list_head *pos; |
a7f7e84a | 420 | |
f08cbf60 | 421 | if (base_cache_used <= base_cache_limit) |
a7f7e84a JT |
422 | return; |
423 | ||
f08cbf60 JT |
424 | list_for_each_prev(pos, &done_head) { |
425 | struct base_data *b = list_entry(pos, struct base_data, list); | |
426 | if (b->retain_data || b == retain) | |
427 | continue; | |
428 | if (b->data) { | |
429 | free_base_data(b); | |
430 | if (base_cache_used <= base_cache_limit) | |
431 | return; | |
432 | } | |
92392b4a | 433 | } |
f08cbf60 JT |
434 | |
435 | list_for_each_prev(pos, &work_head) { | |
436 | struct base_data *b = list_entry(pos, struct base_data, list); | |
437 | if (b->retain_data || b == retain) | |
438 | continue; | |
439 | if (b->data) { | |
440 | free_base_data(b); | |
441 | if (base_cache_used <= base_cache_limit) | |
442 | return; | |
443 | } | |
a7f7e84a | 444 | } |
92392b4a SP |
445 | } |
446 | ||
681b07de NTND |
447 | static int is_delta_type(enum object_type type) |
448 | { | |
449 | return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA); | |
450 | } | |
451 | ||
da49a7da | 452 | static void *unpack_entry_data(off_t offset, unsigned long size, |
454253f0 | 453 | enum object_type type, struct object_id *oid) |
9cf6d335 | 454 | { |
9ec2dde9 | 455 | static char fixed_buf[8192]; |
7ce4721a | 456 | int status; |
ef49a7a0 | 457 | git_zstream stream; |
9ec2dde9 | 458 | void *buf; |
454253f0 | 459 | git_hash_ctx c; |
681b07de NTND |
460 | char hdr[32]; |
461 | int hdrlen; | |
462 | ||
463 | if (!is_delta_type(type)) { | |
ca473cef TB |
464 | hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX, |
465 | type_name(type),(uintmax_t)size) + 1; | |
454253f0 | 466 | the_hash_algo->init_fn(&c); |
467 | the_hash_algo->update_fn(&c, hdr, hdrlen); | |
681b07de | 468 | } else |
454253f0 | 469 | oid = NULL; |
9ec2dde9 NTND |
470 | if (type == OBJ_BLOB && size > big_file_threshold) |
471 | buf = fixed_buf; | |
472 | else | |
a1e920a0 | 473 | buf = xmallocz(size); |
9cf6d335 SV |
474 | |
475 | memset(&stream, 0, sizeof(stream)); | |
7ce4721a | 476 | git_inflate_init(&stream); |
9cf6d335 | 477 | stream.next_out = buf; |
9ec2dde9 | 478 | stream.avail_out = buf == fixed_buf ? sizeof(fixed_buf) : size; |
9cf6d335 | 479 | |
7ce4721a | 480 | do { |
681b07de | 481 | unsigned char *last_out = stream.next_out; |
2d477051 NP |
482 | stream.next_in = fill(1); |
483 | stream.avail_in = input_len; | |
7ce4721a NP |
484 | status = git_inflate(&stream, 0); |
485 | use(input_len - stream.avail_in); | |
454253f0 | 486 | if (oid) |
487 | the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out); | |
9ec2dde9 NTND |
488 | if (buf == fixed_buf) { |
489 | stream.next_out = buf; | |
490 | stream.avail_out = sizeof(fixed_buf); | |
491 | } | |
7ce4721a NP |
492 | } while (status == Z_OK); |
493 | if (stream.total_out != size || status != Z_STREAM_END) | |
c2b97ecf | 494 | bad_object(offset, _("inflate returned %d"), status); |
39c68542 | 495 | git_inflate_end(&stream); |
454253f0 | 496 | if (oid) |
497 | the_hash_algo->final_fn(oid->hash, &c); | |
9ec2dde9 | 498 | return buf == fixed_buf ? NULL : buf; |
9cf6d335 SV |
499 | } |
500 | ||
681b07de | 501 | static void *unpack_raw_entry(struct object_entry *obj, |
c6458e60 | 502 | off_t *ofs_offset, |
454253f0 | 503 | struct object_id *ref_oid, |
504 | struct object_id *oid) | |
9cf6d335 | 505 | { |
48fb7deb LT |
506 | unsigned char *p; |
507 | unsigned long size, c; | |
d7dd0223 | 508 | off_t base_offset; |
9cf6d335 | 509 | unsigned shift; |
ee5743ce | 510 | void *data; |
9cf6d335 | 511 | |
aa7e44bf | 512 | obj->idx.offset = consumed_bytes; |
1e4cd68c | 513 | input_crc32 = crc32(0, NULL, 0); |
2d477051 NP |
514 | |
515 | p = fill(1); | |
516 | c = *p; | |
517 | use(1); | |
518 | obj->type = (c >> 4) & 7; | |
9cf6d335 SV |
519 | size = (c & 15); |
520 | shift = 4; | |
521 | while (c & 0x80) { | |
2d477051 NP |
522 | p = fill(1); |
523 | c = *p; | |
524 | use(1); | |
48fb7deb | 525 | size += (c & 0x7f) << shift; |
9cf6d335 SV |
526 | shift += 7; |
527 | } | |
2d477051 | 528 | obj->size = size; |
9cf6d335 | 529 | |
2d477051 | 530 | switch (obj->type) { |
eb32d236 | 531 | case OBJ_REF_DELTA: |
454253f0 | 532 | hashcpy(ref_oid->hash, fill(the_hash_algo->rawsz)); |
533 | use(the_hash_algo->rawsz); | |
53dda6ff NP |
534 | break; |
535 | case OBJ_OFS_DELTA: | |
2d477051 NP |
536 | p = fill(1); |
537 | c = *p; | |
538 | use(1); | |
53dda6ff NP |
539 | base_offset = c & 127; |
540 | while (c & 128) { | |
541 | base_offset += 1; | |
8723f216 | 542 | if (!base_offset || MSB(base_offset, 7)) |
c2b97ecf | 543 | bad_object(obj->idx.offset, _("offset value overflow for delta base object")); |
2d477051 NP |
544 | p = fill(1); |
545 | c = *p; | |
546 | use(1); | |
53dda6ff NP |
547 | base_offset = (base_offset << 7) + (c & 127); |
548 | } | |
c6458e60 NTND |
549 | *ofs_offset = obj->idx.offset - base_offset; |
550 | if (*ofs_offset <= 0 || *ofs_offset >= obj->idx.offset) | |
c2b97ecf | 551 | bad_object(obj->idx.offset, _("delta base offset is out of bound")); |
53dda6ff | 552 | break; |
9cf6d335 SV |
553 | case OBJ_COMMIT: |
554 | case OBJ_TREE: | |
555 | case OBJ_BLOB: | |
556 | case OBJ_TAG: | |
9cf6d335 SV |
557 | break; |
558 | default: | |
c2b97ecf | 559 | bad_object(obj->idx.offset, _("unknown object type %d"), obj->type); |
9cf6d335 | 560 | } |
aa7e44bf | 561 | obj->hdr_size = consumed_bytes - obj->idx.offset; |
2d477051 | 562 | |
454253f0 | 563 | data = unpack_entry_data(obj->idx.offset, obj->size, obj->type, oid); |
aa7e44bf | 564 | obj->idx.crc32 = input_crc32; |
ee5743ce | 565 | return data; |
2d477051 NP |
566 | } |
567 | ||
8a2e163c NTND |
568 | static void *unpack_data(struct object_entry *obj, |
569 | int (*consume)(const unsigned char *, unsigned long, void *), | |
570 | void *cb_data) | |
2d477051 | 571 | { |
a91ef6e7 | 572 | off_t from = obj[0].idx.offset + obj[0].hdr_size; |
7171a0b0 | 573 | off_t len = obj[1].idx.offset - from; |
776ea370 | 574 | unsigned char *data, *inbuf; |
ef49a7a0 | 575 | git_zstream stream; |
776ea370 | 576 | int status; |
9cf6d335 | 577 | |
a1e920a0 | 578 | data = xmallocz(consume ? 64*1024 : obj->size); |
7171a0b0 | 579 | inbuf = xmalloc((len < 64*1024) ? (int)len : 64*1024); |
776ea370 | 580 | |
2d477051 | 581 | memset(&stream, 0, sizeof(stream)); |
776ea370 | 582 | git_inflate_init(&stream); |
2d477051 | 583 | stream.next_out = data; |
8a2e163c | 584 | stream.avail_out = consume ? 64*1024 : obj->size; |
776ea370 NP |
585 | |
586 | do { | |
7171a0b0 | 587 | ssize_t n = (len < 64*1024) ? (ssize_t)len : 64*1024; |
53f52cd9 | 588 | n = xpread(get_thread_data()->pack_fd, inbuf, n, from); |
776ea370 | 589 | if (n < 0) |
c2b97ecf | 590 | die_errno(_("cannot pread pack file")); |
776ea370 | 591 | if (!n) |
7171a0b0 NTND |
592 | die(Q_("premature end of pack file, %"PRIuMAX" byte missing", |
593 | "premature end of pack file, %"PRIuMAX" bytes missing", | |
594 | (unsigned int)len), | |
595 | (uintmax_t)len); | |
776ea370 NP |
596 | from += n; |
597 | len -= n; | |
598 | stream.next_in = inbuf; | |
599 | stream.avail_in = n; | |
f8b09038 JK |
600 | if (!consume) |
601 | status = git_inflate(&stream, 0); | |
602 | else { | |
603 | do { | |
604 | status = git_inflate(&stream, 0); | |
605 | if (consume(data, stream.next_out - data, cb_data)) { | |
606 | free(inbuf); | |
607 | free(data); | |
608 | return NULL; | |
609 | } | |
610 | stream.next_out = data; | |
611 | stream.avail_out = 64*1024; | |
612 | } while (status == Z_OK && stream.avail_in); | |
8a2e163c | 613 | } |
776ea370 NP |
614 | } while (len && status == Z_OK && !stream.avail_in); |
615 | ||
616 | /* This has been inflated OK when first encountered, so... */ | |
617 | if (status != Z_STREAM_END || stream.total_out != obj->size) | |
c2b97ecf | 618 | die(_("serious inflate inconsistency")); |
776ea370 NP |
619 | |
620 | git_inflate_end(&stream); | |
621 | free(inbuf); | |
8a2e163c | 622 | if (consume) { |
6a83d902 | 623 | FREE_AND_NULL(data); |
8a2e163c | 624 | } |
9cf6d335 SV |
625 | return data; |
626 | } | |
627 | ||
8a2e163c NTND |
628 | static void *get_data_from_pack(struct object_entry *obj) |
629 | { | |
630 | return unpack_data(obj, NULL, NULL); | |
631 | } | |
632 | ||
c6458e60 NTND |
633 | static int compare_ofs_delta_bases(off_t offset1, off_t offset2, |
634 | enum object_type type1, | |
635 | enum object_type type2) | |
7218a215 JH |
636 | { |
637 | int cmp = type1 - type2; | |
638 | if (cmp) | |
639 | return cmp; | |
f0e7f11d JK |
640 | return offset1 < offset2 ? -1 : |
641 | offset1 > offset2 ? 1 : | |
642 | 0; | |
7218a215 JH |
643 | } |
644 | ||
fc968e26 | 645 | static int find_ofs_delta(const off_t offset) |
9cf6d335 | 646 | { |
c6458e60 NTND |
647 | int first = 0, last = nr_ofs_deltas; |
648 | ||
649 | while (first < last) { | |
19716b21 | 650 | int next = first + (last - first) / 2; |
c6458e60 NTND |
651 | struct ofs_delta_entry *delta = &ofs_deltas[next]; |
652 | int cmp; | |
653 | ||
654 | cmp = compare_ofs_delta_bases(offset, delta->offset, | |
fc968e26 JT |
655 | OBJ_OFS_DELTA, |
656 | objects[delta->obj_no].type); | |
c6458e60 NTND |
657 | if (!cmp) |
658 | return next; | |
659 | if (cmp < 0) { | |
660 | last = next; | |
661 | continue; | |
662 | } | |
663 | first = next+1; | |
664 | } | |
665 | return -first-1; | |
9cf6d335 SV |
666 | } |
667 | ||
c6458e60 | 668 | static void find_ofs_delta_children(off_t offset, |
fc968e26 | 669 | int *first_index, int *last_index) |
9cf6d335 | 670 | { |
fc968e26 | 671 | int first = find_ofs_delta(offset); |
9cf6d335 | 672 | int last = first; |
c6458e60 | 673 | int end = nr_ofs_deltas - 1; |
9cf6d335 | 674 | |
6a87ed97 NP |
675 | if (first < 0) { |
676 | *first_index = 0; | |
677 | *last_index = -1; | |
678 | return; | |
679 | } | |
c6458e60 | 680 | while (first > 0 && ofs_deltas[first - 1].offset == offset) |
9cf6d335 | 681 | --first; |
c6458e60 NTND |
682 | while (last < end && ofs_deltas[last + 1].offset == offset) |
683 | ++last; | |
684 | *first_index = first; | |
685 | *last_index = last; | |
686 | } | |
687 | ||
af8caf33 | 688 | static int compare_ref_delta_bases(const struct object_id *oid1, |
689 | const struct object_id *oid2, | |
c6458e60 NTND |
690 | enum object_type type1, |
691 | enum object_type type2) | |
692 | { | |
693 | int cmp = type1 - type2; | |
694 | if (cmp) | |
695 | return cmp; | |
af8caf33 | 696 | return oidcmp(oid1, oid2); |
c6458e60 NTND |
697 | } |
698 | ||
fc968e26 | 699 | static int find_ref_delta(const struct object_id *oid) |
c6458e60 NTND |
700 | { |
701 | int first = 0, last = nr_ref_deltas; | |
702 | ||
703 | while (first < last) { | |
19716b21 | 704 | int next = first + (last - first) / 2; |
c6458e60 NTND |
705 | struct ref_delta_entry *delta = &ref_deltas[next]; |
706 | int cmp; | |
707 | ||
af8caf33 | 708 | cmp = compare_ref_delta_bases(oid, &delta->oid, |
fc968e26 JT |
709 | OBJ_REF_DELTA, |
710 | objects[delta->obj_no].type); | |
c6458e60 NTND |
711 | if (!cmp) |
712 | return next; | |
713 | if (cmp < 0) { | |
714 | last = next; | |
715 | continue; | |
716 | } | |
717 | first = next+1; | |
718 | } | |
719 | return -first-1; | |
720 | } | |
721 | ||
af8caf33 | 722 | static void find_ref_delta_children(const struct object_id *oid, |
fc968e26 | 723 | int *first_index, int *last_index) |
c6458e60 | 724 | { |
fc968e26 | 725 | int first = find_ref_delta(oid); |
c6458e60 NTND |
726 | int last = first; |
727 | int end = nr_ref_deltas - 1; | |
728 | ||
729 | if (first < 0) { | |
730 | *first_index = 0; | |
731 | *last_index = -1; | |
732 | return; | |
733 | } | |
4a7e27e9 | 734 | while (first > 0 && oideq(&ref_deltas[first - 1].oid, oid)) |
c6458e60 | 735 | --first; |
4a7e27e9 | 736 | while (last < end && oideq(&ref_deltas[last + 1].oid, oid)) |
9cf6d335 SV |
737 | ++last; |
738 | *first_index = first; | |
739 | *last_index = last; | |
9cf6d335 SV |
740 | } |
741 | ||
4614043c NTND |
742 | struct compare_data { |
743 | struct object_entry *entry; | |
744 | struct git_istream *st; | |
745 | unsigned char *buf; | |
746 | unsigned long buf_size; | |
747 | }; | |
748 | ||
749 | static int compare_objects(const unsigned char *buf, unsigned long size, | |
750 | void *cb_data) | |
751 | { | |
752 | struct compare_data *data = cb_data; | |
753 | ||
754 | if (data->buf_size < size) { | |
755 | free(data->buf); | |
756 | data->buf = xmalloc(size); | |
757 | data->buf_size = size; | |
758 | } | |
759 | ||
760 | while (size) { | |
761 | ssize_t len = read_istream(data->st, data->buf, size); | |
762 | if (len == 0) | |
763 | die(_("SHA1 COLLISION FOUND WITH %s !"), | |
e6a492b7 | 764 | oid_to_hex(&data->entry->idx.oid)); |
4614043c NTND |
765 | if (len < 0) |
766 | die(_("unable to read %s"), | |
e6a492b7 | 767 | oid_to_hex(&data->entry->idx.oid)); |
4614043c NTND |
768 | if (memcmp(buf, data->buf, len)) |
769 | die(_("SHA1 COLLISION FOUND WITH %s !"), | |
e6a492b7 | 770 | oid_to_hex(&data->entry->idx.oid)); |
4614043c NTND |
771 | size -= len; |
772 | buf += len; | |
773 | } | |
774 | return 0; | |
775 | } | |
776 | ||
777 | static int check_collison(struct object_entry *entry) | |
778 | { | |
779 | struct compare_data data; | |
780 | enum object_type type; | |
781 | unsigned long size; | |
782 | ||
783 | if (entry->size <= big_file_threshold || entry->type != OBJ_BLOB) | |
784 | return -1; | |
785 | ||
786 | memset(&data, 0, sizeof(data)); | |
787 | data.entry = entry; | |
c8123e72 MT |
788 | data.st = open_istream(the_repository, &entry->idx.oid, &type, &size, |
789 | NULL); | |
4614043c NTND |
790 | if (!data.st) |
791 | return -1; | |
792 | if (size != entry->size || type != entry->type) | |
793 | die(_("SHA1 COLLISION FOUND WITH %s !"), | |
e6a492b7 | 794 | oid_to_hex(&entry->idx.oid)); |
4614043c NTND |
795 | unpack_data(entry, compare_objects, &data); |
796 | close_istream(data.st); | |
797 | free(data.buf); | |
798 | return 0; | |
799 | } | |
800 | ||
9ec2dde9 NTND |
801 | static void sha1_object(const void *data, struct object_entry *obj_entry, |
802 | unsigned long size, enum object_type type, | |
3e930981 | 803 | const struct object_id *oid) |
9cf6d335 | 804 | { |
9ec2dde9 | 805 | void *new_data = NULL; |
29401e15 | 806 | int collision_test_needed = 0; |
9ec2dde9 NTND |
807 | |
808 | assert(data || obj_entry); | |
809 | ||
29401e15 JK |
810 | if (startup_info->have_repository) { |
811 | read_lock(); | |
e83e71c5 | 812 | collision_test_needed = |
98374a07 | 813 | has_object_file_with_flags(oid, OBJECT_INFO_QUICK); |
29401e15 JK |
814 | read_unlock(); |
815 | } | |
4614043c NTND |
816 | |
817 | if (collision_test_needed && !data) { | |
818 | read_lock(); | |
819 | if (!check_collison(obj_entry)) | |
820 | collision_test_needed = 0; | |
821 | read_unlock(); | |
822 | } | |
823 | if (collision_test_needed) { | |
8685da42 NP |
824 | void *has_data; |
825 | enum object_type has_type; | |
826 | unsigned long has_size; | |
4614043c | 827 | read_lock(); |
0df8e965 | 828 | has_type = oid_object_info(the_repository, oid, &has_size); |
51054177 | 829 | if (has_type < 0) |
3e930981 | 830 | die(_("cannot read existing object info %s"), oid_to_hex(oid)); |
4614043c | 831 | if (has_type != type || has_size != size) |
3e930981 | 832 | die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid)); |
b4f5aca4 | 833 | has_data = read_object_file(oid, &has_type, &has_size); |
b8a2486f | 834 | read_unlock(); |
4614043c NTND |
835 | if (!data) |
836 | data = new_data = get_data_from_pack(obj_entry); | |
8685da42 | 837 | if (!has_data) |
3e930981 | 838 | die(_("cannot read existing object %s"), oid_to_hex(oid)); |
8685da42 NP |
839 | if (size != has_size || type != has_type || |
840 | memcmp(data, has_data, size) != 0) | |
3e930981 | 841 | die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid)); |
bbf4b41b | 842 | free(has_data); |
4614043c | 843 | } |
b8a2486f | 844 | |
ffb2c0fe | 845 | if (strict || do_fsck_object) { |
b8a2486f | 846 | read_lock(); |
0153be05 | 847 | if (type == OBJ_BLOB) { |
da14a7ff | 848 | struct blob *blob = lookup_blob(the_repository, oid); |
0153be05 MK |
849 | if (blob) |
850 | blob->object.flags |= FLAG_CHECKED; | |
851 | else | |
3e930981 | 852 | die(_("invalid blob object %s"), oid_to_hex(oid)); |
73c3f0f7 JK |
853 | if (do_fsck_object && |
854 | fsck_object(&blob->object, (void *)data, size, &fsck_options)) | |
855 | die(_("fsck error in packed object")); | |
0153be05 MK |
856 | } else { |
857 | struct object *obj; | |
858 | int eaten; | |
859 | void *buf = (void *) data; | |
860 | ||
920734b0 | 861 | assert(data && "data can only be NULL for large _blobs_"); |
9ec2dde9 | 862 | |
0153be05 MK |
863 | /* |
864 | * we do not need to free the memory here, as the | |
865 | * buf is deleted by the caller. | |
866 | */ | |
1ec5bfd2 SB |
867 | obj = parse_object_buffer(the_repository, oid, type, |
868 | size, buf, | |
c251c83d | 869 | &eaten); |
0153be05 | 870 | if (!obj) |
debca9d2 | 871 | die(_("invalid %s"), type_name(type)); |
c6807a40 | 872 | if (do_fsck_object && |
22410549 | 873 | fsck_object(obj, buf, size, &fsck_options)) |
db5a58c1 | 874 | die(_("fsck error in packed object")); |
ffb2c0fe | 875 | if (strict && fsck_walk(obj, NULL, &fsck_options)) |
f2fd0760 | 876 | die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid)); |
0153be05 MK |
877 | |
878 | if (obj->type == OBJ_TREE) { | |
879 | struct tree *item = (struct tree *) obj; | |
880 | item->buffer = NULL; | |
6e454b9a | 881 | obj->parsed = 0; |
0153be05 MK |
882 | } |
883 | if (obj->type == OBJ_COMMIT) { | |
884 | struct commit *commit = (struct commit *) obj; | |
8597ea3a | 885 | if (detach_commit_buffer(commit, NULL) != data) |
033abf97 | 886 | BUG("parse_object_buffer transmogrified our buffer"); |
0153be05 MK |
887 | } |
888 | obj->flags |= FLAG_CHECKED; | |
889 | } | |
b8a2486f | 890 | read_unlock(); |
0153be05 | 891 | } |
9ec2dde9 NTND |
892 | |
893 | free(new_data); | |
9cf6d335 SV |
894 | } |
895 | ||
20e95d0a | 896 | /* |
f08cbf60 | 897 | * Walk from current node up |
20e95d0a NTND |
898 | * to top parent if necessary to deflate the node. In normal |
899 | * situation, its parent node would be already deflated, so it just | |
900 | * needs to apply delta. | |
901 | * | |
902 | * In the worst case scenario, parent node is no longer deflated because | |
903 | * we're running out of delta_base_cache_limit; we need to re-deflate | |
904 | * parents, possibly up to the top base. | |
905 | * | |
906 | * All deflated objects here are subject to be freed if we exceed | |
907 | * delta_base_cache_limit, just like in find_unresolved_deltas(), we | |
908 | * just need to make sure the last node is not freed. | |
909 | */ | |
92392b4a SP |
910 | static void *get_base_data(struct base_data *c) |
911 | { | |
912 | if (!c->data) { | |
913 | struct object_entry *obj = c->obj; | |
20e95d0a NTND |
914 | struct base_data **delta = NULL; |
915 | int delta_nr = 0, delta_alloc = 0; | |
92392b4a | 916 | |
20e95d0a NTND |
917 | while (is_delta_type(c->obj->type) && !c->data) { |
918 | ALLOC_GROW(delta, delta_nr + 1, delta_alloc); | |
919 | delta[delta_nr++] = c; | |
920 | c = c->base; | |
921 | } | |
922 | if (!delta_nr) { | |
923 | c->data = get_data_from_pack(obj); | |
924 | c->size = obj->size; | |
f08cbf60 | 925 | base_cache_used += c->size; |
20e95d0a NTND |
926 | prune_base_data(c); |
927 | } | |
928 | for (; delta_nr > 0; delta_nr--) { | |
929 | void *base, *raw; | |
930 | c = delta[delta_nr - 1]; | |
931 | obj = c->obj; | |
932 | base = get_base_data(c->base); | |
933 | raw = get_data_from_pack(obj); | |
92392b4a SP |
934 | c->data = patch_delta( |
935 | base, c->base->size, | |
936 | raw, obj->size, | |
937 | &c->size); | |
938 | free(raw); | |
939 | if (!c->data) | |
c2b97ecf | 940 | bad_object(obj->idx.offset, _("failed to apply delta")); |
f08cbf60 | 941 | base_cache_used += c->size; |
20e95d0a | 942 | prune_base_data(c); |
9441b61d | 943 | } |
20e95d0a | 944 | free(delta); |
92392b4a SP |
945 | } |
946 | return c->data; | |
947 | } | |
948 | ||
b4718cae JT |
949 | static struct base_data *make_base(struct object_entry *obj, |
950 | struct base_data *parent) | |
9cf6d335 | 951 | { |
b4718cae JT |
952 | struct base_data *base = xcalloc(1, sizeof(struct base_data)); |
953 | base->base = parent; | |
954 | base->obj = obj; | |
955 | find_ref_delta_children(&obj->idx.oid, | |
956 | &base->ref_first, &base->ref_last); | |
957 | find_ofs_delta_children(obj->idx.offset, | |
958 | &base->ofs_first, &base->ofs_last); | |
f08cbf60 JT |
959 | base->children_remaining = base->ref_last - base->ref_first + |
960 | base->ofs_last - base->ofs_first + 2; | |
b4718cae JT |
961 | return base; |
962 | } | |
963 | ||
964 | static struct base_data *resolve_delta(struct object_entry *delta_obj, | |
965 | struct base_data *base) | |
966 | { | |
ee6f0583 | 967 | void *delta_data, *result_data; |
b4718cae JT |
968 | struct base_data *result; |
969 | unsigned long result_size; | |
9cf6d335 | 970 | |
3aba2fdd | 971 | if (show_stat) { |
41730576 NTND |
972 | int i = delta_obj - objects; |
973 | int j = base->obj - objects; | |
974 | obj_stat[i].delta_depth = obj_stat[j].delta_depth + 1; | |
3aba2fdd | 975 | deepest_delta_lock(); |
41730576 NTND |
976 | if (deepest_delta < obj_stat[i].delta_depth) |
977 | deepest_delta = obj_stat[i].delta_depth; | |
3aba2fdd | 978 | deepest_delta_unlock(); |
41730576 | 979 | obj_stat[i].base_object_no = j; |
3aba2fdd | 980 | } |
636171cb | 981 | delta_data = get_data_from_pack(delta_obj); |
ee6f0583 JT |
982 | assert(base->data); |
983 | result_data = patch_delta(base->data, base->size, | |
b4718cae | 984 | delta_data, delta_obj->size, &result_size); |
9cf6d335 | 985 | free(delta_data); |
b4718cae | 986 | if (!result_data) |
c2b97ecf | 987 | bad_object(delta_obj->idx.offset, _("failed to apply delta")); |
b4718cae | 988 | hash_object_file(the_hash_algo, result_data, result_size, |
169c9c01 | 989 | type_name(delta_obj->real_type), &delta_obj->idx.oid); |
b4718cae | 990 | sha1_object(result_data, NULL, result_size, delta_obj->real_type, |
3e930981 | 991 | &delta_obj->idx.oid); |
b4718cae JT |
992 | |
993 | result = make_base(delta_obj, base); | |
f08cbf60 JT |
994 | result->data = result_data; |
995 | result->size = result_size; | |
b4718cae | 996 | |
b8a2486f | 997 | counter_lock(); |
636171cb | 998 | nr_resolved_deltas++; |
b8a2486f | 999 | counter_unlock(); |
b4718cae JT |
1000 | |
1001 | return result; | |
9441b61d NP |
1002 | } |
1003 | ||
c6458e60 | 1004 | static int compare_ofs_delta_entry(const void *a, const void *b) |
9cf6d335 | 1005 | { |
c6458e60 NTND |
1006 | const struct ofs_delta_entry *delta_a = a; |
1007 | const struct ofs_delta_entry *delta_b = b; | |
7218a215 | 1008 | |
f0e7f11d JK |
1009 | return delta_a->offset < delta_b->offset ? -1 : |
1010 | delta_a->offset > delta_b->offset ? 1 : | |
1011 | 0; | |
c6458e60 NTND |
1012 | } |
1013 | ||
1014 | static int compare_ref_delta_entry(const void *a, const void *b) | |
9cf6d335 | 1015 | { |
c6458e60 NTND |
1016 | const struct ref_delta_entry *delta_a = a; |
1017 | const struct ref_delta_entry *delta_b = b; | |
7218a215 | 1018 | |
af8caf33 | 1019 | return oidcmp(&delta_a->oid, &delta_b->oid); |
9cf6d335 SV |
1020 | } |
1021 | ||
b8a2486f NTND |
1022 | static void *threaded_second_pass(void *data) |
1023 | { | |
f08cbf60 JT |
1024 | if (data) |
1025 | set_thread_data(data); | |
b8a2486f | 1026 | for (;;) { |
f08cbf60 JT |
1027 | struct base_data *parent = NULL; |
1028 | struct object_entry *child_obj; | |
1029 | struct base_data *child; | |
1030 | ||
8f82aad4 | 1031 | work_lock(); |
f08cbf60 JT |
1032 | if (list_empty(&work_head)) { |
1033 | /* | |
1034 | * Take an object from the object array. | |
1035 | */ | |
1036 | while (nr_dispatched < nr_objects && | |
1037 | is_delta_type(objects[nr_dispatched].type)) | |
1038 | nr_dispatched++; | |
1039 | if (nr_dispatched >= nr_objects) { | |
1040 | work_unlock(); | |
1041 | break; | |
1042 | } | |
1043 | child_obj = &objects[nr_dispatched++]; | |
1044 | } else { | |
1045 | /* | |
1046 | * Peek at the top of the stack, and take a child from | |
1047 | * it. | |
1048 | */ | |
1049 | parent = list_first_entry(&work_head, struct base_data, | |
1050 | list); | |
1051 | ||
1052 | if (parent->ref_first <= parent->ref_last) { | |
1053 | int offset = ref_deltas[parent->ref_first++].obj_no; | |
1054 | child_obj = objects + offset; | |
1055 | if (child_obj->real_type != OBJ_REF_DELTA) | |
1056 | die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)", | |
1057 | (uintmax_t) child_obj->idx.offset, | |
1058 | oid_to_hex(&parent->obj->idx.oid)); | |
1059 | child_obj->real_type = parent->obj->real_type; | |
1060 | } else { | |
1061 | child_obj = objects + | |
1062 | ofs_deltas[parent->ofs_first++].obj_no; | |
1063 | assert(child_obj->real_type == OBJ_OFS_DELTA); | |
1064 | child_obj->real_type = parent->obj->real_type; | |
1065 | } | |
1066 | ||
1067 | if (parent->ref_first > parent->ref_last && | |
1068 | parent->ofs_first > parent->ofs_last) { | |
1069 | /* | |
1070 | * This parent has run out of children, so move | |
1071 | * it to done_head. | |
1072 | */ | |
1073 | list_del(&parent->list); | |
1074 | list_add(&parent->list, &done_head); | |
1075 | } | |
1076 | ||
1077 | /* | |
1078 | * Ensure that the parent has data, since we will need | |
1079 | * it later. | |
1080 | * | |
1081 | * NEEDSWORK: If parent data needs to be reloaded, this | |
1082 | * prolongs the time that the current thread spends in | |
1083 | * the mutex. A mitigating factor is that parent data | |
1084 | * needs to be reloaded only if the delta base cache | |
1085 | * limit is exceeded, so in the typical case, this does | |
1086 | * not happen. | |
1087 | */ | |
1088 | get_base_data(parent); | |
1089 | parent->retain_data++; | |
b8a2486f | 1090 | } |
b8a2486f NTND |
1091 | work_unlock(); |
1092 | ||
f08cbf60 JT |
1093 | if (parent) { |
1094 | child = resolve_delta(child_obj, parent); | |
1095 | if (!child->children_remaining) | |
1096 | FREE_AND_NULL(child->data); | |
1097 | } else { | |
1098 | child = make_base(child_obj, NULL); | |
1099 | if (child->children_remaining) { | |
1100 | /* | |
1101 | * Since this child has its own delta children, | |
1102 | * we will need this data in the future. | |
1103 | * Inflate now so that future iterations will | |
1104 | * have access to this object's data while | |
1105 | * outside the work mutex. | |
1106 | */ | |
1107 | child->data = get_data_from_pack(child_obj); | |
1108 | child->size = child_obj->size; | |
1109 | } | |
1110 | } | |
1111 | ||
1112 | work_lock(); | |
1113 | if (parent) | |
1114 | parent->retain_data--; | |
1115 | if (child->data) { | |
1116 | /* | |
1117 | * This child has its own children, so add it to | |
1118 | * work_head. | |
1119 | */ | |
1120 | list_add(&child->list, &work_head); | |
1121 | base_cache_used += child->size; | |
1122 | prune_base_data(NULL); | |
1123 | } else { | |
1124 | /* | |
1125 | * This child does not have its own children. It may be | |
1126 | * the last descendant of its ancestors; free those | |
1127 | * that we can. | |
1128 | */ | |
1129 | struct base_data *p = parent; | |
1130 | ||
1131 | while (p) { | |
1132 | struct base_data *next_p; | |
1133 | ||
1134 | p->children_remaining--; | |
1135 | if (p->children_remaining) | |
1136 | break; | |
1137 | ||
1138 | next_p = p->base; | |
1139 | free_base_data(p); | |
1140 | list_del(&p->list); | |
1141 | free(p); | |
1142 | ||
1143 | p = next_p; | |
1144 | } | |
1145 | } | |
1146 | work_unlock(); | |
b8a2486f NTND |
1147 | } |
1148 | return NULL; | |
1149 | } | |
b8a2486f | 1150 | |
5272f755 NTND |
1151 | /* |
1152 | * First pass: | |
1153 | * - find locations of all objects; | |
1154 | * - calculate SHA1 of all non-delta objects; | |
1155 | * - remember base (SHA1 or offset) for all deltas. | |
1156 | */ | |
454253f0 | 1157 | static void parse_pack_objects(unsigned char *hash) |
9cf6d335 | 1158 | { |
9ec2dde9 | 1159 | int i, nr_delays = 0; |
c6458e60 | 1160 | struct ofs_delta_entry *ofs_delta = ofs_deltas; |
454253f0 | 1161 | struct object_id ref_delta_oid; |
2d477051 | 1162 | struct stat st; |
9cf6d335 | 1163 | |
13aaf148 | 1164 | if (verbose) |
29e63ed3 | 1165 | progress = start_progress( |
c2b97ecf | 1166 | from_stdin ? _("Receiving objects") : _("Indexing objects"), |
29e63ed3 | 1167 | nr_objects); |
9cf6d335 SV |
1168 | for (i = 0; i < nr_objects; i++) { |
1169 | struct object_entry *obj = &objects[i]; | |
c6458e60 | 1170 | void *data = unpack_raw_entry(obj, &ofs_delta->offset, |
454253f0 | 1171 | &ref_delta_oid, |
1172 | &obj->idx.oid); | |
9cf6d335 | 1173 | obj->real_type = obj->type; |
c6458e60 NTND |
1174 | if (obj->type == OBJ_OFS_DELTA) { |
1175 | nr_ofs_deltas++; | |
1176 | ofs_delta->obj_no = i; | |
1177 | ofs_delta++; | |
1178 | } else if (obj->type == OBJ_REF_DELTA) { | |
1179 | ALLOC_GROW(ref_deltas, nr_ref_deltas + 1, ref_deltas_alloc); | |
af8caf33 | 1180 | oidcpy(&ref_deltas[nr_ref_deltas].oid, &ref_delta_oid); |
c6458e60 NTND |
1181 | ref_deltas[nr_ref_deltas].obj_no = i; |
1182 | nr_ref_deltas++; | |
9ec2dde9 NTND |
1183 | } else if (!data) { |
1184 | /* large blobs, check later */ | |
1185 | obj->real_type = OBJ_BAD; | |
1186 | nr_delays++; | |
9cf6d335 | 1187 | } else |
e6a492b7 | 1188 | sha1_object(data, NULL, obj->size, obj->type, |
3e930981 | 1189 | &obj->idx.oid); |
9cf6d335 | 1190 | free(data); |
4d4fcc54 | 1191 | display_progress(progress, i+1); |
9cf6d335 | 1192 | } |
aa7e44bf | 1193 | objects[i].idx.offset = consumed_bytes; |
4d4fcc54 | 1194 | stop_progress(&progress); |
2d477051 NP |
1195 | |
1196 | /* Check pack integrity */ | |
636171cb | 1197 | flush(); |
454253f0 | 1198 | the_hash_algo->final_fn(hash, &input_ctx); |
67947c34 | 1199 | if (!hasheq(fill(the_hash_algo->rawsz), hash)) |
c2b97ecf | 1200 | die(_("pack is corrupted (SHA1 mismatch)")); |
454253f0 | 1201 | use(the_hash_algo->rawsz); |
2d477051 NP |
1202 | |
1203 | /* If input_fd is a file, we should have reached its end now. */ | |
1204 | if (fstat(input_fd, &st)) | |
c2b97ecf | 1205 | die_errno(_("cannot fstat packfile")); |
fa257b05 JS |
1206 | if (S_ISREG(st.st_mode) && |
1207 | lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size) | |
c2b97ecf | 1208 | die(_("pack has junk at the end")); |
9ec2dde9 NTND |
1209 | |
1210 | for (i = 0; i < nr_objects; i++) { | |
1211 | struct object_entry *obj = &objects[i]; | |
1212 | if (obj->real_type != OBJ_BAD) | |
1213 | continue; | |
1214 | obj->real_type = obj->type; | |
e6a492b7 | 1215 | sha1_object(NULL, obj, obj->size, obj->type, |
3e930981 | 1216 | &obj->idx.oid); |
9ec2dde9 NTND |
1217 | nr_delays--; |
1218 | } | |
1219 | if (nr_delays) | |
1220 | die(_("confusion beyond insanity in parse_pack_objects()")); | |
5272f755 NTND |
1221 | } |
1222 | ||
1223 | /* | |
1224 | * Second pass: | |
1225 | * - for all non-delta objects, look if it is used as a base for | |
1226 | * deltas; | |
1227 | * - if used as a base, uncompress the object and apply all deltas, | |
1228 | * recursively checking if the resulting object is used as a base | |
1229 | * for some more deltas. | |
1230 | */ | |
1231 | static void resolve_deltas(void) | |
1232 | { | |
1233 | int i; | |
9cf6d335 | 1234 | |
c6458e60 | 1235 | if (!nr_ofs_deltas && !nr_ref_deltas) |
3c9af366 NP |
1236 | return; |
1237 | ||
53dda6ff | 1238 | /* Sort deltas by base SHA1/offset for fast searching */ |
9ed0d8d6 RS |
1239 | QSORT(ofs_deltas, nr_ofs_deltas, compare_ofs_delta_entry); |
1240 | QSORT(ref_deltas, nr_ref_deltas, compare_ref_delta_entry); | |
9cf6d335 | 1241 | |
e376f17f | 1242 | if (verbose || show_resolving_progress) |
c6458e60 NTND |
1243 | progress = start_progress(_("Resolving deltas"), |
1244 | nr_ref_deltas + nr_ofs_deltas); | |
b8a2486f | 1245 | |
b8a2486f | 1246 | nr_dispatched = 0; |
f08cbf60 | 1247 | base_cache_limit = delta_base_cache_limit * nr_threads; |
b8a2486f NTND |
1248 | if (nr_threads > 1 || getenv("GIT_FORCE_THREADS")) { |
1249 | init_thread(); | |
1250 | for (i = 0; i < nr_threads; i++) { | |
1251 | int ret = pthread_create(&thread_data[i].thread, NULL, | |
1252 | threaded_second_pass, thread_data + i); | |
1253 | if (ret) | |
f350df42 NTND |
1254 | die(_("unable to create thread: %s"), |
1255 | strerror(ret)); | |
b8a2486f NTND |
1256 | } |
1257 | for (i = 0; i < nr_threads; i++) | |
1258 | pthread_join(thread_data[i].thread, NULL); | |
1259 | cleanup_thread(); | |
1260 | return; | |
1261 | } | |
46e6fb1e | 1262 | threaded_second_pass(¬hread_data); |
636171cb NP |
1263 | } |
1264 | ||
5272f755 NTND |
1265 | /* |
1266 | * Third pass: | |
1267 | * - append objects to convert thin pack to full pack if required | |
454253f0 | 1268 | * - write the final pack hash |
5272f755 | 1269 | */ |
98a3beab | 1270 | static void fix_unresolved_deltas(struct hashfile *f); |
454253f0 | 1271 | static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned char *pack_hash) |
5272f755 | 1272 | { |
c6458e60 | 1273 | if (nr_ref_deltas + nr_ofs_deltas == nr_resolved_deltas) { |
5272f755 | 1274 | stop_progress(&progress); |
454253f0 | 1275 | /* Flush remaining pack final hash. */ |
5272f755 NTND |
1276 | flush(); |
1277 | return; | |
1278 | } | |
1279 | ||
1280 | if (fix_thin_pack) { | |
98a3beab | 1281 | struct hashfile *f; |
454253f0 | 1282 | unsigned char read_hash[GIT_MAX_RAWSZ], tail_hash[GIT_MAX_RAWSZ]; |
5c3459fc | 1283 | struct strbuf msg = STRBUF_INIT; |
c6458e60 | 1284 | int nr_unresolved = nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas; |
5272f755 NTND |
1285 | int nr_objects_initial = nr_objects; |
1286 | if (nr_unresolved <= 0) | |
cc13431a | 1287 | die(_("confusion beyond insanity")); |
2756ca43 | 1288 | REALLOC_ARRAY(objects, nr_objects + nr_unresolved + 1); |
57165db0 JK |
1289 | memset(objects + nr_objects + 1, 0, |
1290 | nr_unresolved * sizeof(*objects)); | |
98a3beab | 1291 | f = hashfd(output_fd, curr_pack); |
781d9306 | 1292 | fix_unresolved_deltas(f); |
71d99b81 VA |
1293 | strbuf_addf(&msg, Q_("completed with %d local object", |
1294 | "completed with %d local objects", | |
1295 | nr_objects - nr_objects_initial), | |
5c3459fc NTND |
1296 | nr_objects - nr_objects_initial); |
1297 | stop_progress_msg(&progress, msg.buf); | |
1298 | strbuf_release(&msg); | |
f2af9f5e | 1299 | finalize_hashfile(f, tail_hash, 0); |
454253f0 | 1300 | hashcpy(read_hash, pack_hash); |
1301 | fixup_pack_header_footer(output_fd, pack_hash, | |
5272f755 | 1302 | curr_pack, nr_objects, |
454253f0 | 1303 | read_hash, consumed_bytes-the_hash_algo->rawsz); |
67947c34 | 1304 | if (!hasheq(read_hash, tail_hash)) |
f350df42 NTND |
1305 | die(_("Unexpected tail checksum for %s " |
1306 | "(disk corruption?)"), curr_pack); | |
5272f755 | 1307 | } |
c6458e60 | 1308 | if (nr_ofs_deltas + nr_ref_deltas != nr_resolved_deltas) |
cc13431a JH |
1309 | die(Q_("pack has %d unresolved delta", |
1310 | "pack has %d unresolved deltas", | |
c6458e60 NTND |
1311 | nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas), |
1312 | nr_ofs_deltas + nr_ref_deltas - nr_resolved_deltas); | |
5272f755 NTND |
1313 | } |
1314 | ||
98a3beab | 1315 | static int write_compressed(struct hashfile *f, void *in, unsigned int size) |
636171cb | 1316 | { |
ef49a7a0 | 1317 | git_zstream stream; |
7734d7f2 NP |
1318 | int status; |
1319 | unsigned char outbuf[4096]; | |
636171cb | 1320 | |
55bb5c91 | 1321 | git_deflate_init(&stream, zlib_compression_level); |
636171cb NP |
1322 | stream.next_in = in; |
1323 | stream.avail_in = size; | |
636171cb | 1324 | |
7734d7f2 NP |
1325 | do { |
1326 | stream.next_out = outbuf; | |
1327 | stream.avail_out = sizeof(outbuf); | |
55bb5c91 | 1328 | status = git_deflate(&stream, Z_FINISH); |
98a3beab | 1329 | hashwrite(f, outbuf, sizeof(outbuf) - stream.avail_out); |
7734d7f2 NP |
1330 | } while (status == Z_OK); |
1331 | ||
1332 | if (status != Z_STREAM_END) | |
c2b97ecf | 1333 | die(_("unable to deflate appended object (%d)"), status); |
636171cb | 1334 | size = stream.total_out; |
55bb5c91 | 1335 | git_deflate_end(&stream); |
636171cb NP |
1336 | return size; |
1337 | } | |
1338 | ||
98a3beab | 1339 | static struct object_entry *append_obj_to_pack(struct hashfile *f, |
03993e13 | 1340 | const unsigned char *sha1, void *buf, |
636171cb NP |
1341 | unsigned long size, enum object_type type) |
1342 | { | |
1343 | struct object_entry *obj = &objects[nr_objects++]; | |
1344 | unsigned char header[10]; | |
1345 | unsigned long s = size; | |
1346 | int n = 0; | |
1347 | unsigned char c = (type << 4) | (s & 15); | |
1348 | s >>= 4; | |
1349 | while (s) { | |
1350 | header[n++] = c | 0x80; | |
1351 | c = s & 0x7f; | |
1352 | s >>= 7; | |
1353 | } | |
1354 | header[n++] = c; | |
8522148f | 1355 | crc32_begin(f); |
98a3beab | 1356 | hashwrite(f, header, n); |
72de2883 BS |
1357 | obj[0].size = size; |
1358 | obj[0].hdr_size = n; | |
1359 | obj[0].type = type; | |
1360 | obj[0].real_type = type; | |
aa7e44bf | 1361 | obj[1].idx.offset = obj[0].idx.offset + n; |
8522148f NP |
1362 | obj[1].idx.offset += write_compressed(f, buf, size); |
1363 | obj[0].idx.crc32 = crc32_end(f); | |
98a3beab | 1364 | hashflush(f); |
e6a492b7 | 1365 | hashcpy(obj->idx.oid.hash, sha1); |
03993e13 | 1366 | return obj; |
636171cb NP |
1367 | } |
1368 | ||
1369 | static int delta_pos_compare(const void *_a, const void *_b) | |
1370 | { | |
c6458e60 NTND |
1371 | struct ref_delta_entry *a = *(struct ref_delta_entry **)_a; |
1372 | struct ref_delta_entry *b = *(struct ref_delta_entry **)_b; | |
636171cb NP |
1373 | return a->obj_no - b->obj_no; |
1374 | } | |
9cf6d335 | 1375 | |
98a3beab | 1376 | static void fix_unresolved_deltas(struct hashfile *f) |
636171cb | 1377 | { |
c6458e60 | 1378 | struct ref_delta_entry **sorted_by_pos; |
781d9306 | 1379 | int i; |
636171cb NP |
1380 | |
1381 | /* | |
1382 | * Since many unresolved deltas may well be themselves base objects | |
1383 | * for more unresolved deltas, we really want to include the | |
1384 | * smallest number of base objects that would cover as much delta | |
1385 | * as possible by picking the | |
1386 | * trunc deltas first, allowing for other deltas to resolve without | |
1387 | * additional base objects. Since most base objects are to be found | |
1388 | * before deltas depending on them, a good heuristic is to start | |
1389 | * resolving deltas in the same order as their position in the pack. | |
1390 | */ | |
b32fa95f | 1391 | ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas); |
c6458e60 | 1392 | for (i = 0; i < nr_ref_deltas; i++) |
781d9306 | 1393 | sorted_by_pos[i] = &ref_deltas[i]; |
9ed0d8d6 | 1394 | QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare); |
636171cb | 1395 | |
b14ed5ad | 1396 | if (has_promisor_remote()) { |
8a30a1ef JT |
1397 | /* |
1398 | * Prefetch the delta bases. | |
1399 | */ | |
1400 | struct oid_array to_fetch = OID_ARRAY_INIT; | |
1401 | for (i = 0; i < nr_ref_deltas; i++) { | |
1402 | struct ref_delta_entry *d = sorted_by_pos[i]; | |
1403 | if (!oid_object_info_extended(the_repository, &d->oid, | |
1404 | NULL, | |
1405 | OBJECT_INFO_FOR_PREFETCH)) | |
1406 | continue; | |
1407 | oid_array_append(&to_fetch, &d->oid); | |
1408 | } | |
db7ed741 JT |
1409 | promisor_remote_get_direct(the_repository, |
1410 | to_fetch.oid, to_fetch.nr); | |
8a30a1ef JT |
1411 | oid_array_clear(&to_fetch); |
1412 | } | |
1413 | ||
781d9306 | 1414 | for (i = 0; i < nr_ref_deltas; i++) { |
c6458e60 | 1415 | struct ref_delta_entry *d = sorted_by_pos[i]; |
21666f1a | 1416 | enum object_type type; |
b4718cae JT |
1417 | void *data; |
1418 | unsigned long size; | |
636171cb NP |
1419 | |
1420 | if (objects[d->obj_no].real_type != OBJ_REF_DELTA) | |
1421 | continue; | |
b4718cae JT |
1422 | data = read_object_file(&d->oid, &type, &size); |
1423 | if (!data) | |
636171cb | 1424 | continue; |
636171cb | 1425 | |
b98d1885 | 1426 | if (check_object_signature(the_repository, &d->oid, |
b4718cae | 1427 | data, size, |
b98d1885 | 1428 | type_name(type))) |
af8caf33 | 1429 | die(_("local object %s is corrupt"), oid_to_hex(&d->oid)); |
f08cbf60 JT |
1430 | |
1431 | /* | |
1432 | * Add this as an object to the objects array and call | |
1433 | * threaded_second_pass() (which will pick up the added | |
1434 | * object). | |
1435 | */ | |
1436 | append_obj_to_pack(f, d->oid.hash, data, size, type); | |
1437 | threaded_second_pass(NULL); | |
1438 | ||
4d4fcc54 | 1439 | display_progress(progress, nr_resolved_deltas); |
636171cb NP |
1440 | } |
1441 | free(sorted_by_pos); | |
1442 | } | |
1443 | ||
8e29c7c3 JT |
1444 | static const char *derive_filename(const char *pack_name, const char *suffix, |
1445 | struct strbuf *buf) | |
1446 | { | |
1447 | size_t len; | |
1448 | if (!strip_suffix(pack_name, ".pack", &len)) | |
1449 | die(_("packfile name '%s' does not end with '.pack'"), | |
1450 | pack_name); | |
1451 | strbuf_add(buf, pack_name, len); | |
1452 | strbuf_addch(buf, '.'); | |
1453 | strbuf_addstr(buf, suffix); | |
1454 | return buf->buf; | |
1455 | } | |
1456 | ||
1457 | static void write_special_file(const char *suffix, const char *msg, | |
0fd90dab | 1458 | const char *pack_name, const unsigned char *hash, |
8e29c7c3 JT |
1459 | const char **report) |
1460 | { | |
1461 | struct strbuf name_buf = STRBUF_INIT; | |
1462 | const char *filename; | |
1463 | int fd; | |
1464 | int msg_len = strlen(msg); | |
1465 | ||
1466 | if (pack_name) | |
1467 | filename = derive_filename(pack_name, suffix, &name_buf); | |
1468 | else | |
0fd90dab | 1469 | filename = odb_pack_name(&name_buf, hash, suffix); |
8e29c7c3 JT |
1470 | |
1471 | fd = odb_pack_keep(filename); | |
1472 | if (fd < 0) { | |
1473 | if (errno != EEXIST) | |
1474 | die_errno(_("cannot write %s file '%s'"), | |
1475 | suffix, filename); | |
1476 | } else { | |
1477 | if (msg_len > 0) { | |
1478 | write_or_die(fd, msg, msg_len); | |
1479 | write_or_die(fd, "\n", 1); | |
1480 | } | |
1481 | if (close(fd) != 0) | |
1482 | die_errno(_("cannot close written %s file '%s'"), | |
1483 | suffix, filename); | |
88e2f9ed JT |
1484 | if (report) |
1485 | *report = suffix; | |
8e29c7c3 JT |
1486 | } |
1487 | strbuf_release(&name_buf); | |
1488 | } | |
1489 | ||
e42797f5 NP |
1490 | static void final(const char *final_pack_name, const char *curr_pack_name, |
1491 | const char *final_index_name, const char *curr_index_name, | |
88e2f9ed | 1492 | const char *keep_msg, const char *promisor_msg, |
454253f0 | 1493 | unsigned char *hash) |
e42797f5 | 1494 | { |
3a55602e | 1495 | const char *report = "pack"; |
f2075480 JK |
1496 | struct strbuf pack_name = STRBUF_INIT; |
1497 | struct strbuf index_name = STRBUF_INIT; | |
e42797f5 NP |
1498 | int err; |
1499 | ||
1500 | if (!from_stdin) { | |
1501 | close(input_fd); | |
1502 | } else { | |
4c81b03e | 1503 | fsync_or_die(output_fd, curr_pack_name); |
e42797f5 NP |
1504 | err = close(output_fd); |
1505 | if (err) | |
c2b97ecf | 1506 | die_errno(_("error while closing pack file")); |
e42797f5 NP |
1507 | } |
1508 | ||
8e29c7c3 | 1509 | if (keep_msg) |
0fd90dab | 1510 | write_special_file("keep", keep_msg, final_pack_name, hash, |
8e29c7c3 | 1511 | &report); |
88e2f9ed JT |
1512 | if (promisor_msg) |
1513 | write_special_file("promisor", promisor_msg, final_pack_name, | |
0fd90dab | 1514 | hash, NULL); |
b8077709 | 1515 | |
e42797f5 | 1516 | if (final_pack_name != curr_pack_name) { |
ba47a308 | 1517 | if (!final_pack_name) |
454253f0 | 1518 | final_pack_name = odb_pack_name(&pack_name, hash, "pack"); |
cb5add58 | 1519 | if (finalize_object_file(curr_pack_name, final_pack_name)) |
c2b97ecf | 1520 | die(_("cannot store pack file")); |
fb8b1936 | 1521 | } else if (from_stdin) |
33b65030 | 1522 | chmod(final_pack_name, 0444); |
e42797f5 | 1523 | |
e42797f5 | 1524 | if (final_index_name != curr_index_name) { |
ba47a308 | 1525 | if (!final_index_name) |
454253f0 | 1526 | final_index_name = odb_pack_name(&index_name, hash, "idx"); |
cb5add58 | 1527 | if (finalize_object_file(curr_index_name, final_index_name)) |
c2b97ecf | 1528 | die(_("cannot store index file")); |
fb8b1936 JH |
1529 | } else |
1530 | chmod(final_index_name, 0444); | |
576162a4 | 1531 | |
368b4e59 JK |
1532 | if (do_fsck_object) { |
1533 | struct packed_git *p; | |
1534 | p = add_packed_git(final_index_name, strlen(final_index_name), 0); | |
1535 | if (p) | |
549ca8aa | 1536 | install_packed_git(the_repository, p); |
368b4e59 | 1537 | } |
73c3f0f7 | 1538 | |
576162a4 | 1539 | if (!from_stdin) { |
69fa3370 | 1540 | printf("%s\n", hash_to_hex(hash)); |
576162a4 | 1541 | } else { |
5b1ef2ce JK |
1542 | struct strbuf buf = STRBUF_INIT; |
1543 | ||
69fa3370 | 1544 | strbuf_addf(&buf, "%s\t%s\n", report, hash_to_hex(hash)); |
5b1ef2ce JK |
1545 | write_or_die(1, buf.buf, buf.len); |
1546 | strbuf_release(&buf); | |
576162a4 NP |
1547 | |
1548 | /* | |
1549 | * Let's just mimic git-unpack-objects here and write | |
1550 | * the last part of the input buffer to stdout. | |
1551 | */ | |
1552 | while (input_len) { | |
1553 | err = xwrite(1, input_buffer + input_offset, input_len); | |
1554 | if (err <= 0) | |
1555 | break; | |
1556 | input_len -= err; | |
1557 | input_offset += err; | |
1558 | } | |
1559 | } | |
ba47a308 | 1560 | |
f2075480 JK |
1561 | strbuf_release(&index_name); |
1562 | strbuf_release(&pack_name); | |
9cf6d335 SV |
1563 | } |
1564 | ||
ef90d6d4 | 1565 | static int git_index_pack_config(const char *k, const char *v, void *cb) |
4d00bda2 | 1566 | { |
ebcfb379 JH |
1567 | struct pack_idx_option *opts = cb; |
1568 | ||
4d00bda2 | 1569 | if (!strcmp(k, "pack.indexversion")) { |
ebcfb379 JH |
1570 | opts->version = git_config_int(k, v); |
1571 | if (opts->version > 2) | |
f350df42 | 1572 | die(_("bad pack.indexversion=%"PRIu32), opts->version); |
4d00bda2 NP |
1573 | return 0; |
1574 | } | |
b8a2486f NTND |
1575 | if (!strcmp(k, "pack.threads")) { |
1576 | nr_threads = git_config_int(k, v); | |
1577 | if (nr_threads < 0) | |
f350df42 | 1578 | die(_("invalid number of threads specified (%d)"), |
b8a2486f | 1579 | nr_threads); |
2094c5e5 | 1580 | if (!HAVE_THREADS && nr_threads != 1) { |
f350df42 | 1581 | warning(_("no threads support, ignoring %s"), k); |
2094c5e5 NTND |
1582 | nr_threads = 1; |
1583 | } | |
b8a2486f NTND |
1584 | return 0; |
1585 | } | |
ef90d6d4 | 1586 | return git_default_config(k, v, cb); |
4d00bda2 NP |
1587 | } |
1588 | ||
3c9fc074 JH |
1589 | static int cmp_uint32(const void *a_, const void *b_) |
1590 | { | |
1591 | uint32_t a = *((uint32_t *)a_); | |
1592 | uint32_t b = *((uint32_t *)b_); | |
1593 | ||
1594 | return (a < b) ? -1 : (a != b); | |
1595 | } | |
1596 | ||
1597 | static void read_v2_anomalous_offsets(struct packed_git *p, | |
1598 | struct pack_idx_option *opts) | |
1599 | { | |
1600 | const uint32_t *idx1, *idx2; | |
1601 | uint32_t i; | |
1602 | ||
1603 | /* The address of the 4-byte offset table */ | |
629dffc4 | 1604 | idx1 = (((const uint32_t *)((const uint8_t *)p->index_data + p->crc_offset)) |
3c9fc074 JH |
1605 | + p->num_objects /* CRC32 table */ |
1606 | ); | |
1607 | ||
1608 | /* The address of the 8-byte offset table */ | |
1609 | idx2 = idx1 + p->num_objects; | |
1610 | ||
1611 | for (i = 0; i < p->num_objects; i++) { | |
1612 | uint32_t off = ntohl(idx1[i]); | |
1613 | if (!(off & 0x80000000)) | |
1614 | continue; | |
1615 | off = off & 0x7fffffff; | |
47fe3f6e | 1616 | check_pack_index_ptr(p, &idx2[off * 2]); |
3c9fc074 JH |
1617 | if (idx2[off * 2]) |
1618 | continue; | |
1619 | /* | |
1620 | * The real offset is ntohl(idx2[off * 2]) in high 4 | |
1621 | * octets, and ntohl(idx2[off * 2 + 1]) in low 4 | |
1622 | * octets. But idx2[off * 2] is Zero!!! | |
1623 | */ | |
1624 | ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc); | |
1625 | opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]); | |
1626 | } | |
1627 | ||
1b5294de | 1628 | QSORT(opts->anomaly, opts->anomaly_nr, cmp_uint32); |
3c9fc074 JH |
1629 | } |
1630 | ||
e337a04d JH |
1631 | static void read_idx_option(struct pack_idx_option *opts, const char *pack_name) |
1632 | { | |
1633 | struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1); | |
1634 | ||
1635 | if (!p) | |
c2b97ecf | 1636 | die(_("Cannot open existing pack file '%s'"), pack_name); |
e337a04d | 1637 | if (open_pack_index(p)) |
c2b97ecf | 1638 | die(_("Cannot open existing pack idx file for '%s'"), pack_name); |
e337a04d JH |
1639 | |
1640 | /* Read the attributes from the existing idx file */ | |
1641 | opts->version = p->index_version; | |
1642 | ||
3c9fc074 JH |
1643 | if (opts->version == 2) |
1644 | read_v2_anomalous_offsets(p, opts); | |
1645 | ||
e337a04d JH |
1646 | /* |
1647 | * Get rid of the idx file as we do not need it anymore. | |
1648 | * NEEDSWORK: extract this bit from free_pack_by_name() in | |
fc1395f4 | 1649 | * sha1-file.c, perhaps? It shouldn't matter very much as we |
e337a04d JH |
1650 | * know we haven't installed this pack (hence we never have |
1651 | * read anything from it). | |
1652 | */ | |
1653 | close_pack_index(p); | |
1654 | free(p); | |
1655 | } | |
1656 | ||
38a45561 JH |
1657 | static void show_pack_info(int stat_only) |
1658 | { | |
c6458e60 | 1659 | int i, baseobjects = nr_objects - nr_ref_deltas - nr_ofs_deltas; |
d1a0ed18 JH |
1660 | unsigned long *chain_histogram = NULL; |
1661 | ||
1662 | if (deepest_delta) | |
1663 | chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long)); | |
1664 | ||
38a45561 JH |
1665 | for (i = 0; i < nr_objects; i++) { |
1666 | struct object_entry *obj = &objects[i]; | |
1667 | ||
d1a0ed18 | 1668 | if (is_delta_type(obj->type)) |
41730576 | 1669 | chain_histogram[obj_stat[i].delta_depth - 1]++; |
38a45561 JH |
1670 | if (stat_only) |
1671 | continue; | |
ca473cef | 1672 | printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX, |
e6a492b7 | 1673 | oid_to_hex(&obj->idx.oid), |
ca473cef TB |
1674 | type_name(obj->real_type), (uintmax_t)obj->size, |
1675 | (uintmax_t)(obj[1].idx.offset - obj->idx.offset), | |
38a45561 JH |
1676 | (uintmax_t)obj->idx.offset); |
1677 | if (is_delta_type(obj->type)) { | |
41730576 | 1678 | struct object_entry *bobj = &objects[obj_stat[i].base_object_no]; |
e6a492b7 | 1679 | printf(" %u %s", obj_stat[i].delta_depth, |
1680 | oid_to_hex(&bobj->idx.oid)); | |
38a45561 JH |
1681 | } |
1682 | putchar('\n'); | |
1683 | } | |
d1a0ed18 JH |
1684 | |
1685 | if (baseobjects) | |
c2b97ecf NTND |
1686 | printf_ln(Q_("non delta: %d object", |
1687 | "non delta: %d objects", | |
1688 | baseobjects), | |
1689 | baseobjects); | |
d1a0ed18 JH |
1690 | for (i = 0; i < deepest_delta; i++) { |
1691 | if (!chain_histogram[i]) | |
1692 | continue; | |
c2b97ecf NTND |
1693 | printf_ln(Q_("chain length = %d: %lu object", |
1694 | "chain length = %d: %lu objects", | |
1695 | chain_histogram[i]), | |
1696 | i + 1, | |
1697 | chain_histogram[i]); | |
d1a0ed18 | 1698 | } |
38a45561 JH |
1699 | } |
1700 | ||
3bb72562 | 1701 | int cmd_index_pack(int argc, const char **argv, const char *prefix) |
9cf6d335 | 1702 | { |
3aba2fdd | 1703 | int i, fix_thin_pack = 0, verify = 0, stat_only = 0; |
39539495 | 1704 | const char *curr_index; |
3bb72562 | 1705 | const char *index_name = NULL, *pack_name = NULL; |
8e29c7c3 | 1706 | const char *keep_msg = NULL; |
88e2f9ed | 1707 | const char *promisor_msg = NULL; |
8e29c7c3 | 1708 | struct strbuf index_name_buf = STRBUF_INIT; |
aa7e44bf | 1709 | struct pack_idx_entry **idx_objects; |
ebcfb379 | 1710 | struct pack_idx_option opts; |
454253f0 | 1711 | unsigned char pack_hash[GIT_MAX_RAWSZ]; |
c6807a40 | 1712 | unsigned foreign_nr = 1; /* zero is a "good" value, assume bad */ |
83558686 | 1713 | int report_end_of_input = 0; |
586740aa | 1714 | int hash_algo = 0; |
9cf6d335 | 1715 | |
8b4c0103 | 1716 | /* |
8a30a1ef JT |
1717 | * index-pack never needs to fetch missing objects except when |
1718 | * REF_DELTA bases are missing (which are explicitly handled). It only | |
1719 | * accesses the repo to do hash collision checks and to check which | |
1720 | * REF_DELTA bases need to be fetched. | |
8b4c0103 JT |
1721 | */ |
1722 | fetch_if_missing = 0; | |
1723 | ||
99caeed0 JN |
1724 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1725 | usage(index_pack_usage); | |
1726 | ||
6ebd1caf | 1727 | read_replace_refs = 0; |
22410549 | 1728 | fsck_options.walk = mark_link; |
6e2a09d2 | 1729 | |
ebcfb379 JH |
1730 | reset_pack_idx_option(&opts); |
1731 | git_config(git_index_pack_config, &opts); | |
3f8099fc | 1732 | if (prefix && chdir(prefix)) |
c2b97ecf | 1733 | die(_("Cannot come back to cwd")); |
4d00bda2 | 1734 | |
9cf6d335 | 1735 | for (i = 1; i < argc; i++) { |
3bb72562 | 1736 | const char *arg = argv[i]; |
9cf6d335 SV |
1737 | |
1738 | if (*arg == '-') { | |
e42797f5 NP |
1739 | if (!strcmp(arg, "--stdin")) { |
1740 | from_stdin = 1; | |
636171cb NP |
1741 | } else if (!strcmp(arg, "--fix-thin")) { |
1742 | fix_thin_pack = 1; | |
72885a6d | 1743 | } else if (skip_to_optional_arg(arg, "--strict", &arg)) { |
5d477a33 JS |
1744 | strict = 1; |
1745 | do_fsck_object = 1; | |
1746 | fsck_set_msg_types(&fsck_options, arg); | |
c6807a40 NTND |
1747 | } else if (!strcmp(arg, "--check-self-contained-and-connected")) { |
1748 | strict = 1; | |
1749 | check_self_contained_and_connected = 1; | |
ffb2c0fe JT |
1750 | } else if (!strcmp(arg, "--fsck-objects")) { |
1751 | do_fsck_object = 1; | |
e337a04d JH |
1752 | } else if (!strcmp(arg, "--verify")) { |
1753 | verify = 1; | |
38a45561 JH |
1754 | } else if (!strcmp(arg, "--verify-stat")) { |
1755 | verify = 1; | |
3aba2fdd | 1756 | show_stat = 1; |
38a45561 JH |
1757 | } else if (!strcmp(arg, "--verify-stat-only")) { |
1758 | verify = 1; | |
3aba2fdd | 1759 | show_stat = 1; |
38a45561 | 1760 | stat_only = 1; |
72885a6d CC |
1761 | } else if (skip_to_optional_arg(arg, "--keep", &keep_msg)) { |
1762 | ; /* nothing to do */ | |
f3d618d2 JH |
1763 | } else if (skip_to_optional_arg(arg, "--promisor", &promisor_msg)) { |
1764 | ; /* already parsed */ | |
59556548 | 1765 | } else if (starts_with(arg, "--threads=")) { |
b8a2486f NTND |
1766 | char *end; |
1767 | nr_threads = strtoul(arg+10, &end, 0); | |
1768 | if (!arg[10] || *end || nr_threads < 0) | |
1769 | usage(index_pack_usage); | |
2094c5e5 NTND |
1770 | if (!HAVE_THREADS && nr_threads != 1) { |
1771 | warning(_("no threads support, ignoring %s"), arg); | |
1772 | nr_threads = 1; | |
1773 | } | |
59556548 | 1774 | } else if (starts_with(arg, "--pack_header=")) { |
bed006fb NP |
1775 | struct pack_header *hdr; |
1776 | char *c; | |
1777 | ||
1778 | hdr = (struct pack_header *)input_buffer; | |
1779 | hdr->hdr_signature = htonl(PACK_SIGNATURE); | |
1780 | hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); | |
1781 | if (*c != ',') | |
c2b97ecf | 1782 | die(_("bad %s"), arg); |
bed006fb NP |
1783 | hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); |
1784 | if (*c) | |
c2b97ecf | 1785 | die(_("bad %s"), arg); |
bed006fb | 1786 | input_len = sizeof(*hdr); |
3c9af366 NP |
1787 | } else if (!strcmp(arg, "-v")) { |
1788 | verbose = 1; | |
e376f17f JK |
1789 | } else if (!strcmp(arg, "--show-resolving-progress")) { |
1790 | show_resolving_progress = 1; | |
83558686 JK |
1791 | } else if (!strcmp(arg, "--report-end-of-input")) { |
1792 | report_end_of_input = 1; | |
e42797f5 | 1793 | } else if (!strcmp(arg, "-o")) { |
9cf6d335 SV |
1794 | if (index_name || (i+1) >= argc) |
1795 | usage(index_pack_usage); | |
1796 | index_name = argv[++i]; | |
59556548 | 1797 | } else if (starts_with(arg, "--index-version=")) { |
4ba7d711 | 1798 | char *c; |
ebcfb379 JH |
1799 | opts.version = strtoul(arg + 16, &c, 10); |
1800 | if (opts.version > 2) | |
c2b97ecf | 1801 | die(_("bad %s"), arg); |
4ba7d711 | 1802 | if (*c == ',') |
ebcfb379 JH |
1803 | opts.off32_limit = strtoul(c+1, &c, 0); |
1804 | if (*c || opts.off32_limit & 0x80000000) | |
c2b97ecf | 1805 | die(_("bad %s"), arg); |
411481be JK |
1806 | } else if (skip_prefix(arg, "--max-input-size=", &arg)) { |
1807 | max_input_size = strtoumax(arg, NULL, 10); | |
586740aa | 1808 | } else if (skip_prefix(arg, "--object-format=", &arg)) { |
1809 | hash_algo = hash_algo_by_name(arg); | |
1810 | if (hash_algo == GIT_HASH_UNKNOWN) | |
1811 | die(_("unknown hash algorithm '%s'"), arg); | |
1812 | repo_set_hash_algo(the_repository, hash_algo); | |
9cf6d335 SV |
1813 | } else |
1814 | usage(index_pack_usage); | |
1815 | continue; | |
1816 | } | |
1817 | ||
1818 | if (pack_name) | |
1819 | usage(index_pack_usage); | |
1820 | pack_name = arg; | |
1821 | } | |
1822 | ||
e42797f5 | 1823 | if (!pack_name && !from_stdin) |
9cf6d335 | 1824 | usage(index_pack_usage); |
636171cb | 1825 | if (fix_thin_pack && !from_stdin) |
c2b97ecf | 1826 | die(_("--fix-thin cannot be used without --stdin")); |
7176a314 JK |
1827 | if (from_stdin && !startup_info->have_repository) |
1828 | die(_("--stdin requires a git repository")); | |
586740aa | 1829 | if (from_stdin && hash_algo) |
1830 | die(_("--object-format cannot be used with --stdin")); | |
bfee614a | 1831 | if (!index_name && pack_name) |
8e29c7c3 | 1832 | index_name = derive_filename(pack_name, "idx", &index_name_buf); |
bfee614a | 1833 | |
e337a04d JH |
1834 | if (verify) { |
1835 | if (!index_name) | |
c2b97ecf | 1836 | die(_("--verify with no packfile name given")); |
e337a04d | 1837 | read_idx_option(&opts, index_name); |
68be2fea | 1838 | opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT; |
e337a04d | 1839 | } |
68be2fea JH |
1840 | if (strict) |
1841 | opts.flags |= WRITE_IDX_STRICT; | |
9cf6d335 | 1842 | |
2094c5e5 | 1843 | if (HAVE_THREADS && !nr_threads) { |
b8a2486f NTND |
1844 | nr_threads = online_cpus(); |
1845 | /* An experiment showed that more threads does not mean faster */ | |
1846 | if (nr_threads > 3) | |
1847 | nr_threads = 3; | |
1848 | } | |
b8a2486f | 1849 | |
e42797f5 | 1850 | curr_pack = open_pack_file(pack_name); |
9cf6d335 | 1851 | parse_pack_header(); |
50a6c8ef | 1852 | objects = xcalloc(st_add(nr_objects, 1), sizeof(struct object_entry)); |
41730576 | 1853 | if (show_stat) |
50a6c8ef | 1854 | obj_stat = xcalloc(st_add(nr_objects, 1), sizeof(struct object_stat)); |
c6458e60 | 1855 | ofs_deltas = xcalloc(nr_objects, sizeof(struct ofs_delta_entry)); |
454253f0 | 1856 | parse_pack_objects(pack_hash); |
83558686 JK |
1857 | if (report_end_of_input) |
1858 | write_in_full(2, "\0", 1); | |
5272f755 | 1859 | resolve_deltas(); |
454253f0 | 1860 | conclude_pack(fix_thin_pack, curr_pack, pack_hash); |
c6458e60 NTND |
1861 | free(ofs_deltas); |
1862 | free(ref_deltas); | |
0153be05 | 1863 | if (strict) |
c6807a40 | 1864 | foreign_nr = check_objects(); |
aa7e44bf | 1865 | |
3aba2fdd | 1866 | if (show_stat) |
38a45561 JH |
1867 | show_pack_info(stat_only); |
1868 | ||
b32fa95f | 1869 | ALLOC_ARRAY(idx_objects, nr_objects); |
aa7e44bf GB |
1870 | for (i = 0; i < nr_objects; i++) |
1871 | idx_objects[i] = &objects[i].idx; | |
454253f0 | 1872 | curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_hash); |
aa7e44bf GB |
1873 | free(idx_objects); |
1874 | ||
e337a04d JH |
1875 | if (!verify) |
1876 | final(pack_name, curr_pack, | |
1877 | index_name, curr_index, | |
88e2f9ed | 1878 | keep_msg, promisor_msg, |
454253f0 | 1879 | pack_hash); |
e337a04d JH |
1880 | else |
1881 | close(input_fd); | |
73c3f0f7 JK |
1882 | |
1883 | if (do_fsck_object && fsck_finish(&fsck_options)) | |
1884 | die(_("fsck error in pack objects")); | |
1885 | ||
9cf6d335 | 1886 | free(objects); |
592ce208 | 1887 | strbuf_release(&index_name_buf); |
c85228ed | 1888 | if (pack_name == NULL) |
3bb72562 | 1889 | free((void *) curr_pack); |
c85228ed | 1890 | if (index_name == NULL) |
3bb72562 | 1891 | free((void *) curr_index); |
9cf6d335 | 1892 | |
c6807a40 NTND |
1893 | /* |
1894 | * Let the caller know this pack is not self contained | |
1895 | */ | |
1896 | if (check_self_contained_and_connected && foreign_nr) | |
1897 | return 1; | |
1898 | ||
9cf6d335 SV |
1899 | return 0; |
1900 | } |