Line | Count | Source |
1 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "environment.h" |
5 | | #include "gettext.h" |
6 | | #include "hex.h" |
7 | | #include "list.h" |
8 | | #include "pack.h" |
9 | | #include "repository.h" |
10 | | #include "dir.h" |
11 | | #include "packfile.h" |
12 | | #include "delta.h" |
13 | | #include "hash-lookup.h" |
14 | | #include "commit.h" |
15 | | #include "object.h" |
16 | | #include "tag.h" |
17 | | #include "trace.h" |
18 | | #include "tree-walk.h" |
19 | | #include "tree.h" |
20 | | #include "object-file.h" |
21 | | #include "odb.h" |
22 | | #include "odb/streaming.h" |
23 | | #include "midx.h" |
24 | | #include "commit-graph.h" |
25 | | #include "pack-revindex.h" |
26 | | #include "promisor-remote.h" |
27 | | #include "pack-mtimes.h" |
28 | | |
29 | | char *odb_pack_name(struct repository *r, struct strbuf *buf, |
30 | | const unsigned char *hash, const char *ext) |
31 | 0 | { |
32 | 0 | strbuf_reset(buf); |
33 | 0 | strbuf_addf(buf, "%s/pack/pack-%s.%s", repo_get_object_directory(r), |
34 | 0 | hash_to_hex_algop(hash, r->hash_algo), ext); |
35 | 0 | return buf->buf; |
36 | 0 | } |
37 | | |
38 | | static unsigned int pack_used_ctr; |
39 | | static unsigned int pack_mmap_calls; |
40 | | static unsigned int peak_pack_open_windows; |
41 | | static unsigned int pack_open_windows; |
42 | | static unsigned int pack_open_fds; |
43 | | static unsigned int pack_max_fds; |
44 | | static size_t peak_pack_mapped; |
45 | | static size_t pack_mapped; |
46 | | |
47 | | #define SZ_FMT PRIuMAX |
48 | 0 | static inline uintmax_t sz_fmt(size_t s) { return s; } |
49 | | |
50 | | void pack_report(struct repository *repo) |
51 | 0 | { |
52 | 0 | fprintf(stderr, |
53 | 0 | "pack_report: getpagesize() = %10" SZ_FMT "\n" |
54 | 0 | "pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n" |
55 | 0 | "pack_report: core.packedGitLimit = %10" SZ_FMT "\n", |
56 | 0 | sz_fmt(getpagesize()), |
57 | 0 | sz_fmt(repo->settings.packed_git_window_size), |
58 | 0 | sz_fmt(repo->settings.packed_git_limit)); |
59 | 0 | fprintf(stderr, |
60 | 0 | "pack_report: pack_used_ctr = %10u\n" |
61 | 0 | "pack_report: pack_mmap_calls = %10u\n" |
62 | 0 | "pack_report: pack_open_windows = %10u / %10u\n" |
63 | 0 | "pack_report: pack_mapped = " |
64 | 0 | "%10" SZ_FMT " / %10" SZ_FMT "\n", |
65 | 0 | pack_used_ctr, |
66 | 0 | pack_mmap_calls, |
67 | 0 | pack_open_windows, peak_pack_open_windows, |
68 | 0 | sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped)); |
69 | 0 | } |
70 | | |
71 | | /* |
72 | | * Open and mmap the index file at path, perform a couple of |
73 | | * consistency checks, then record its information to p. Return 0 on |
74 | | * success. |
75 | | */ |
76 | | static int check_packed_git_idx(const char *path, struct packed_git *p) |
77 | 0 | { |
78 | 0 | void *idx_map; |
79 | 0 | size_t idx_size; |
80 | 0 | int fd = git_open(path), ret; |
81 | 0 | struct stat st; |
82 | 0 | const unsigned int hashsz = p->repo->hash_algo->rawsz; |
83 | |
|
84 | 0 | if (fd < 0) |
85 | 0 | return -1; |
86 | 0 | if (fstat(fd, &st)) { |
87 | 0 | close(fd); |
88 | 0 | return -1; |
89 | 0 | } |
90 | 0 | idx_size = xsize_t(st.st_size); |
91 | 0 | if (idx_size < 4 * 256 + hashsz + hashsz) { |
92 | 0 | close(fd); |
93 | 0 | return error("index file %s is too small", path); |
94 | 0 | } |
95 | 0 | idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0); |
96 | 0 | close(fd); |
97 | |
|
98 | 0 | ret = load_idx(path, hashsz, idx_map, idx_size, p); |
99 | |
|
100 | 0 | if (ret) |
101 | 0 | munmap(idx_map, idx_size); |
102 | |
|
103 | 0 | return ret; |
104 | 0 | } |
105 | | |
106 | | int load_idx(const char *path, const unsigned int hashsz, void *idx_map, |
107 | | size_t idx_size, struct packed_git *p) |
108 | | { |
109 | | struct pack_idx_header *hdr = idx_map; |
110 | | uint32_t version, nr, i, *index; |
111 | | |
112 | | if (idx_size < 4 * 256 + hashsz + hashsz) |
113 | | return error("index file %s is too small", path); |
114 | | if (!idx_map) |
115 | | return error("empty data"); |
116 | | |
117 | | if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) { |
118 | | version = ntohl(hdr->idx_version); |
119 | | if (version < 2 || version > 2) |
120 | | return error("index file %s is version %"PRIu32 |
121 | | " and is not supported by this binary" |
122 | | " (try upgrading GIT to a newer version)", |
123 | | path, version); |
124 | | } else |
125 | | version = 1; |
126 | | |
127 | | nr = 0; |
128 | | index = idx_map; |
129 | | if (version > 1) |
130 | | index += 2; /* skip index header */ |
131 | | for (i = 0; i < 256; i++) { |
132 | | uint32_t n = ntohl(index[i]); |
133 | | if (n < nr) |
134 | | return error("non-monotonic index %s", path); |
135 | | nr = n; |
136 | | } |
137 | | |
138 | | if (version == 1) { |
139 | | /* |
140 | | * Total size: |
141 | | * - 256 index entries 4 bytes each |
142 | | * - 24-byte entries * nr (object ID + 4-byte offset) |
143 | | * - hash of the packfile |
144 | | * - file checksum |
145 | | */ |
146 | | if (idx_size != st_add(4 * 256 + hashsz + hashsz, st_mult(nr, hashsz + 4))) |
147 | | return error("wrong index v1 file size in %s", path); |
148 | | } else if (version == 2) { |
149 | | /* |
150 | | * Minimum size: |
151 | | * - 8 bytes of header |
152 | | * - 256 index entries 4 bytes each |
153 | | * - object ID entry * nr |
154 | | * - 4-byte crc entry * nr |
155 | | * - 4-byte offset entry * nr |
156 | | * - hash of the packfile |
157 | | * - file checksum |
158 | | * And after the 4-byte offset table might be a |
159 | | * variable sized table containing 8-byte entries |
160 | | * for offsets larger than 2^31. |
161 | | */ |
162 | | size_t min_size = st_add(8 + 4*256 + hashsz + hashsz, st_mult(nr, hashsz + 4 + 4)); |
163 | | size_t max_size = min_size; |
164 | | if (nr) |
165 | | max_size = st_add(max_size, st_mult(nr - 1, 8)); |
166 | | if (idx_size < min_size || idx_size > max_size) |
167 | | return error("wrong index v2 file size in %s", path); |
168 | | if (idx_size != min_size && |
169 | | /* |
170 | | * make sure we can deal with large pack offsets. |
171 | | * 31-bit signed offset won't be enough, neither |
172 | | * 32-bit unsigned one will be. |
173 | | */ |
174 | | (sizeof(off_t) <= 4)) |
175 | | return error("pack too large for current definition of off_t in %s", path); |
176 | | p->crc_offset = st_add(8 + 4 * 256, st_mult(nr, hashsz)); |
177 | | } |
178 | | |
179 | | p->index_version = version; |
180 | | p->index_data = idx_map; |
181 | | p->index_size = idx_size; |
182 | | p->num_objects = nr; |
183 | | return 0; |
184 | | } |
185 | | |
186 | | int open_pack_index(struct packed_git *p) |
187 | 0 | { |
188 | 0 | char *idx_name; |
189 | 0 | size_t len; |
190 | 0 | int ret; |
191 | |
|
192 | 0 | if (p->index_data) |
193 | 0 | return 0; |
194 | | |
195 | 0 | if (!strip_suffix(p->pack_name, ".pack", &len)) |
196 | 0 | BUG("pack_name does not end in .pack"); |
197 | 0 | idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name); |
198 | 0 | ret = check_packed_git_idx(idx_name, p); |
199 | 0 | free(idx_name); |
200 | 0 | return ret; |
201 | 0 | } |
202 | | |
203 | | uint32_t get_pack_fanout(struct packed_git *p, uint32_t value) |
204 | 0 | { |
205 | 0 | const uint32_t *level1_ofs = p->index_data; |
206 | |
|
207 | 0 | if (!level1_ofs) { |
208 | 0 | if (open_pack_index(p)) |
209 | 0 | return 0; |
210 | 0 | level1_ofs = p->index_data; |
211 | 0 | } |
212 | | |
213 | 0 | if (p->index_version > 1) { |
214 | 0 | level1_ofs += 2; |
215 | 0 | } |
216 | |
|
217 | 0 | return ntohl(level1_ofs[value]); |
218 | 0 | } |
219 | | |
220 | | static struct packed_git *alloc_packed_git(struct repository *r, int extra) |
221 | 0 | { |
222 | 0 | struct packed_git *p = xmalloc(st_add(sizeof(*p), extra)); |
223 | 0 | memset(p, 0, sizeof(*p)); |
224 | 0 | p->pack_fd = -1; |
225 | 0 | p->repo = r; |
226 | 0 | return p; |
227 | 0 | } |
228 | | |
229 | | static char *pack_path_from_idx(const char *idx_path) |
230 | 0 | { |
231 | 0 | size_t len; |
232 | 0 | if (!strip_suffix(idx_path, ".idx", &len)) |
233 | 0 | BUG("idx path does not end in .idx: %s", idx_path); |
234 | 0 | return xstrfmt("%.*s.pack", (int)len, idx_path); |
235 | 0 | } |
236 | | |
237 | | struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1, |
238 | | const char *idx_path) |
239 | 0 | { |
240 | 0 | char *path = pack_path_from_idx(idx_path); |
241 | 0 | size_t alloc = st_add(strlen(path), 1); |
242 | 0 | struct packed_git *p = alloc_packed_git(r, alloc); |
243 | |
|
244 | 0 | memcpy(p->pack_name, path, alloc); /* includes NUL */ |
245 | 0 | free(path); |
246 | 0 | hashcpy(p->hash, sha1, p->repo->hash_algo); |
247 | 0 | if (check_packed_git_idx(idx_path, p)) { |
248 | 0 | free(p); |
249 | 0 | return NULL; |
250 | 0 | } |
251 | | |
252 | 0 | return p; |
253 | 0 | } |
254 | | |
255 | | static void scan_windows(struct packed_git *p, |
256 | | struct packed_git **lru_p, |
257 | | struct pack_window **lru_w, |
258 | | struct pack_window **lru_l) |
259 | 0 | { |
260 | 0 | struct pack_window *w, *w_l; |
261 | |
|
262 | 0 | for (w_l = NULL, w = p->windows; w; w = w->next) { |
263 | 0 | if (!w->inuse_cnt) { |
264 | 0 | if (!*lru_w || w->last_used < (*lru_w)->last_used) { |
265 | 0 | *lru_p = p; |
266 | 0 | *lru_w = w; |
267 | 0 | *lru_l = w_l; |
268 | 0 | } |
269 | 0 | } |
270 | 0 | w_l = w; |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | | static int unuse_one_window(struct object_database *odb) |
275 | 0 | { |
276 | 0 | struct odb_source *source; |
277 | 0 | struct packfile_list_entry *e; |
278 | 0 | struct packed_git *lru_p = NULL; |
279 | 0 | struct pack_window *lru_w = NULL, *lru_l = NULL; |
280 | |
|
281 | 0 | for (source = odb->sources; source; source = source->next) { |
282 | 0 | struct odb_source_files *files = odb_source_files_downcast(source); |
283 | 0 | for (e = files->packed->packs.head; e; e = e->next) |
284 | 0 | scan_windows(e->pack, &lru_p, &lru_w, &lru_l); |
285 | 0 | } |
286 | |
|
287 | 0 | if (lru_p) { |
288 | 0 | munmap(lru_w->base, lru_w->len); |
289 | 0 | pack_mapped -= lru_w->len; |
290 | 0 | if (lru_l) |
291 | 0 | lru_l->next = lru_w->next; |
292 | 0 | else |
293 | 0 | lru_p->windows = lru_w->next; |
294 | 0 | free(lru_w); |
295 | 0 | pack_open_windows--; |
296 | 0 | return 1; |
297 | 0 | } |
298 | 0 | return 0; |
299 | 0 | } |
300 | | |
301 | | void close_pack_windows(struct packed_git *p) |
302 | 0 | { |
303 | 0 | while (p->windows) { |
304 | 0 | struct pack_window *w = p->windows; |
305 | |
|
306 | 0 | if (w->inuse_cnt) |
307 | 0 | die("pack '%s' still has open windows to it", |
308 | 0 | p->pack_name); |
309 | 0 | munmap(w->base, w->len); |
310 | 0 | pack_mapped -= w->len; |
311 | 0 | pack_open_windows--; |
312 | 0 | p->windows = w->next; |
313 | 0 | free(w); |
314 | 0 | } |
315 | 0 | } |
316 | | |
317 | | int close_pack_fd(struct packed_git *p) |
318 | 0 | { |
319 | 0 | if (p->pack_fd < 0) |
320 | 0 | return 0; |
321 | | |
322 | 0 | close(p->pack_fd); |
323 | 0 | pack_open_fds--; |
324 | 0 | p->pack_fd = -1; |
325 | |
|
326 | 0 | return 1; |
327 | 0 | } |
328 | | |
329 | | void close_pack_index(struct packed_git *p) |
330 | 0 | { |
331 | 0 | if (p->index_data) { |
332 | 0 | munmap((void *)p->index_data, p->index_size); |
333 | 0 | p->index_data = NULL; |
334 | 0 | } |
335 | 0 | } |
336 | | |
337 | | static void close_pack_revindex(struct packed_git *p) |
338 | 0 | { |
339 | 0 | FREE_AND_NULL(p->revindex); |
340 | |
|
341 | 0 | if (!p->revindex_map) |
342 | 0 | return; |
343 | | |
344 | 0 | munmap((void *)p->revindex_map, p->revindex_size); |
345 | 0 | p->revindex_map = NULL; |
346 | 0 | p->revindex_data = NULL; |
347 | 0 | } |
348 | | |
349 | | static void close_pack_mtimes(struct packed_git *p) |
350 | 0 | { |
351 | 0 | if (!p->mtimes_map) |
352 | 0 | return; |
353 | | |
354 | 0 | munmap((void *)p->mtimes_map, p->mtimes_size); |
355 | 0 | p->mtimes_map = NULL; |
356 | 0 | } |
357 | | |
358 | | void close_pack(struct packed_git *p) |
359 | 0 | { |
360 | 0 | close_pack_windows(p); |
361 | 0 | close_pack_fd(p); |
362 | 0 | close_pack_index(p); |
363 | 0 | close_pack_revindex(p); |
364 | 0 | close_pack_mtimes(p); |
365 | 0 | oidset_clear(&p->bad_objects); |
366 | 0 | } |
367 | | |
368 | | void unlink_pack_path(const char *pack_name, int force_delete) |
369 | 0 | { |
370 | 0 | static const char *exts[] = {".idx", ".pack", ".rev", ".keep", ".bitmap", ".promisor", ".mtimes"}; |
371 | 0 | int i; |
372 | 0 | struct strbuf buf = STRBUF_INIT; |
373 | 0 | size_t plen; |
374 | |
|
375 | 0 | strbuf_addstr(&buf, pack_name); |
376 | 0 | strip_suffix_mem(buf.buf, &buf.len, ".pack"); |
377 | 0 | plen = buf.len; |
378 | |
|
379 | 0 | if (!force_delete) { |
380 | 0 | strbuf_addstr(&buf, ".keep"); |
381 | 0 | if (!access(buf.buf, F_OK)) { |
382 | 0 | strbuf_release(&buf); |
383 | 0 | return; |
384 | 0 | } |
385 | 0 | } |
386 | | |
387 | 0 | for (i = 0; i < ARRAY_SIZE(exts); i++) { |
388 | 0 | strbuf_setlen(&buf, plen); |
389 | 0 | strbuf_addstr(&buf, exts[i]); |
390 | 0 | unlink(buf.buf); |
391 | 0 | } |
392 | |
|
393 | 0 | strbuf_release(&buf); |
394 | 0 | } |
395 | | |
396 | | /* |
397 | | * The LRU pack is the one with the oldest MRU window, preferring packs |
398 | | * with no used windows, or the oldest mtime if it has no windows allocated. |
399 | | */ |
400 | | static void find_lru_pack(struct packed_git *p, struct packed_git **lru_p, struct pack_window **mru_w, int *accept_windows_inuse) |
401 | 0 | { |
402 | 0 | struct pack_window *w, *this_mru_w; |
403 | 0 | int has_windows_inuse = 0; |
404 | | |
405 | | /* |
406 | | * Reject this pack if it has windows and the previously selected |
407 | | * one does not. If this pack does not have windows, reject |
408 | | * it if the pack file is newer than the previously selected one. |
409 | | */ |
410 | 0 | if (*lru_p && !*mru_w && (p->windows || p->mtime > (*lru_p)->mtime)) |
411 | 0 | return; |
412 | | |
413 | 0 | for (w = this_mru_w = p->windows; w; w = w->next) { |
414 | | /* |
415 | | * Reject this pack if any of its windows are in use, |
416 | | * but the previously selected pack did not have any |
417 | | * inuse windows. Otherwise, record that this pack |
418 | | * has windows in use. |
419 | | */ |
420 | 0 | if (w->inuse_cnt) { |
421 | 0 | if (*accept_windows_inuse) |
422 | 0 | has_windows_inuse = 1; |
423 | 0 | else |
424 | 0 | return; |
425 | 0 | } |
426 | | |
427 | 0 | if (w->last_used > this_mru_w->last_used) |
428 | 0 | this_mru_w = w; |
429 | | |
430 | | /* |
431 | | * Reject this pack if it has windows that have been |
432 | | * used more recently than the previously selected pack. |
433 | | * If the previously selected pack had windows inuse and |
434 | | * we have not encountered a window in this pack that is |
435 | | * inuse, skip this check since we prefer a pack with no |
436 | | * inuse windows to one that has inuse windows. |
437 | | */ |
438 | 0 | if (*mru_w && *accept_windows_inuse == has_windows_inuse && |
439 | 0 | this_mru_w->last_used > (*mru_w)->last_used) |
440 | 0 | return; |
441 | 0 | } |
442 | | |
443 | | /* |
444 | | * Select this pack. |
445 | | */ |
446 | 0 | *mru_w = this_mru_w; |
447 | 0 | *lru_p = p; |
448 | 0 | *accept_windows_inuse = has_windows_inuse; |
449 | 0 | } |
450 | | |
451 | | static int close_one_pack(struct repository *r) |
452 | 0 | { |
453 | 0 | struct odb_source *source; |
454 | 0 | struct packfile_list_entry *e; |
455 | 0 | struct packed_git *lru_p = NULL; |
456 | 0 | struct pack_window *mru_w = NULL; |
457 | 0 | int accept_windows_inuse = 1; |
458 | |
|
459 | 0 | for (source = r->objects->sources; source; source = source->next) { |
460 | 0 | struct odb_source_files *files = odb_source_files_downcast(source); |
461 | 0 | for (e = files->packed->packs.head; e; e = e->next) { |
462 | 0 | if (e->pack->pack_fd == -1) |
463 | 0 | continue; |
464 | 0 | find_lru_pack(e->pack, &lru_p, &mru_w, &accept_windows_inuse); |
465 | 0 | } |
466 | 0 | } |
467 | |
|
468 | 0 | if (lru_p) |
469 | 0 | return close_pack_fd(lru_p); |
470 | | |
471 | 0 | return 0; |
472 | 0 | } |
473 | | |
474 | | static unsigned int get_max_fd_limit(void) |
475 | 0 | { |
476 | 0 | #ifdef RLIMIT_NOFILE |
477 | 0 | { |
478 | 0 | struct rlimit lim; |
479 | |
|
480 | 0 | if (!getrlimit(RLIMIT_NOFILE, &lim)) |
481 | 0 | return lim.rlim_cur; |
482 | 0 | } |
483 | 0 | #endif |
484 | | |
485 | 0 | #ifdef _SC_OPEN_MAX |
486 | 0 | { |
487 | 0 | long open_max = sysconf(_SC_OPEN_MAX); |
488 | 0 | if (0 < open_max) |
489 | 0 | return open_max; |
490 | | /* |
491 | | * Otherwise, we got -1 for one of the two |
492 | | * reasons: |
493 | | * |
494 | | * (1) sysconf() did not understand _SC_OPEN_MAX |
495 | | * and signaled an error with -1; or |
496 | | * (2) sysconf() said there is no limit. |
497 | | * |
498 | | * We _could_ clear errno before calling sysconf() to |
499 | | * tell these two cases apart and return a huge number |
500 | | * in the latter case to let the caller cap it to a |
501 | | * value that is not so selfish, but letting the |
502 | | * fallback OPEN_MAX codepath take care of these cases |
503 | | * is a lot simpler. |
504 | | */ |
505 | 0 | } |
506 | 0 | #endif |
507 | | |
508 | | #ifdef OPEN_MAX |
509 | | return OPEN_MAX; |
510 | | #else |
511 | 0 | return 1; /* see the caller ;-) */ |
512 | 0 | #endif |
513 | 0 | } |
514 | | |
515 | | const char *pack_basename(struct packed_git *p) |
516 | 0 | { |
517 | 0 | const char *ret = strrchr(p->pack_name, '/'); |
518 | 0 | if (ret) |
519 | 0 | ret = ret + 1; /* skip past slash */ |
520 | 0 | else |
521 | 0 | ret = p->pack_name; /* we only have a base */ |
522 | 0 | return ret; |
523 | 0 | } |
524 | | |
525 | | /* |
526 | | * Do not call this directly as this leaks p->pack_fd on error return; |
527 | | * call open_packed_git() instead. |
528 | | */ |
529 | | static int open_packed_git_1(struct packed_git *p) |
530 | 0 | { |
531 | 0 | struct stat st; |
532 | 0 | struct pack_header hdr; |
533 | 0 | unsigned char hash[GIT_MAX_RAWSZ]; |
534 | 0 | unsigned char *idx_hash; |
535 | 0 | ssize_t read_result; |
536 | 0 | const unsigned hashsz = p->repo->hash_algo->rawsz; |
537 | |
|
538 | 0 | if (open_pack_index(p)) |
539 | 0 | return error("packfile %s index unavailable", p->pack_name); |
540 | | |
541 | 0 | if (!pack_max_fds) { |
542 | 0 | unsigned int max_fds = get_max_fd_limit(); |
543 | | |
544 | | /* Save 3 for stdin/stdout/stderr, 22 for work */ |
545 | 0 | if (25 < max_fds) |
546 | 0 | pack_max_fds = max_fds - 25; |
547 | 0 | else |
548 | 0 | pack_max_fds = 1; |
549 | 0 | } |
550 | |
|
551 | 0 | while (pack_max_fds <= pack_open_fds && close_one_pack(p->repo)) |
552 | 0 | ; /* nothing */ |
553 | |
|
554 | 0 | p->pack_fd = git_open(p->pack_name); |
555 | 0 | if (p->pack_fd < 0 || fstat(p->pack_fd, &st)) |
556 | 0 | return -1; |
557 | 0 | pack_open_fds++; |
558 | | |
559 | | /* If we created the struct before we had the pack we lack size. */ |
560 | 0 | if (!p->pack_size) { |
561 | 0 | if (!S_ISREG(st.st_mode)) |
562 | 0 | return error("packfile %s not a regular file", p->pack_name); |
563 | 0 | p->pack_size = st.st_size; |
564 | 0 | } else if (p->pack_size != st.st_size) |
565 | 0 | return error("packfile %s size changed", p->pack_name); |
566 | | |
567 | | /* Verify we recognize this pack file format. */ |
568 | 0 | read_result = read_in_full(p->pack_fd, &hdr, sizeof(hdr)); |
569 | 0 | if (read_result < 0) |
570 | 0 | return error_errno("error reading from %s", p->pack_name); |
571 | 0 | if (read_result != sizeof(hdr)) |
572 | 0 | return error("file %s is far too short to be a packfile", p->pack_name); |
573 | 0 | if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) |
574 | 0 | return error("file %s is not a GIT packfile", p->pack_name); |
575 | 0 | if (!pack_version_ok(hdr.hdr_version)) |
576 | 0 | return error("packfile %s is version %"PRIu32" and not" |
577 | 0 | " supported (try upgrading GIT to a newer version)", |
578 | 0 | p->pack_name, ntohl(hdr.hdr_version)); |
579 | | |
580 | | /* Verify the pack matches its index. */ |
581 | 0 | if (p->num_objects != ntohl(hdr.hdr_entries)) |
582 | 0 | return error("packfile %s claims to have %"PRIu32" objects" |
583 | 0 | " while index indicates %"PRIu32" objects", |
584 | 0 | p->pack_name, ntohl(hdr.hdr_entries), |
585 | 0 | p->num_objects); |
586 | 0 | read_result = pread_in_full(p->pack_fd, hash, hashsz, |
587 | 0 | p->pack_size - hashsz); |
588 | 0 | if (read_result < 0) |
589 | 0 | return error_errno("error reading from %s", p->pack_name); |
590 | 0 | if (read_result != hashsz) |
591 | 0 | return error("packfile %s signature is unavailable", p->pack_name); |
592 | 0 | idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2; |
593 | 0 | if (!hasheq(hash, idx_hash, p->repo->hash_algo)) |
594 | 0 | return error("packfile %s does not match index", p->pack_name); |
595 | 0 | return 0; |
596 | 0 | } |
597 | | |
598 | | static int open_packed_git(struct packed_git *p) |
599 | 0 | { |
600 | 0 | if (!open_packed_git_1(p)) |
601 | 0 | return 0; |
602 | 0 | close_pack_fd(p); |
603 | 0 | return -1; |
604 | 0 | } |
605 | | |
606 | | static int in_window(struct repository *r, struct pack_window *win, |
607 | | off_t offset) |
608 | 0 | { |
609 | | /* We must promise at least one full hash after the |
610 | | * offset is available from this window, otherwise the offset |
611 | | * is not actually in this window and a different window (which |
612 | | * has that one hash excess) must be used. This is to support |
613 | | * the object header and delta base parsing routines below. |
614 | | */ |
615 | 0 | off_t win_off = win->offset; |
616 | 0 | return win_off <= offset |
617 | 0 | && (offset + r->hash_algo->rawsz) <= (win_off + win->len); |
618 | 0 | } |
619 | | |
620 | | unsigned char *use_pack(struct packed_git *p, |
621 | | struct pack_window **w_cursor, |
622 | | off_t offset, |
623 | | size_t *left) |
624 | 0 | { |
625 | 0 | struct pack_window *win = *w_cursor; |
626 | | |
627 | | /* Since packfiles end in a hash of their content and it's |
628 | | * pointless to ask for an offset into the middle of that |
629 | | * hash, and the in_window function above wouldn't match |
630 | | * don't allow an offset too close to the end of the file. |
631 | | */ |
632 | 0 | if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p)) |
633 | 0 | die("packfile %s cannot be accessed", p->pack_name); |
634 | 0 | if (offset > (p->pack_size - p->repo->hash_algo->rawsz)) |
635 | 0 | die("offset beyond end of packfile (truncated pack?)"); |
636 | 0 | if (offset < 0) |
637 | 0 | die(_("offset before end of packfile (broken .idx?)")); |
638 | | |
639 | 0 | if (!win || !in_window(p->repo, win, offset)) { |
640 | 0 | if (win) |
641 | 0 | win->inuse_cnt--; |
642 | 0 | for (win = p->windows; win; win = win->next) { |
643 | 0 | if (in_window(p->repo, win, offset)) |
644 | 0 | break; |
645 | 0 | } |
646 | 0 | if (!win) { |
647 | 0 | size_t window_align; |
648 | 0 | off_t len; |
649 | 0 | struct repo_settings *settings; |
650 | | |
651 | | /* lazy load the settings in case it hasn't been setup */ |
652 | 0 | prepare_repo_settings(p->repo); |
653 | 0 | settings = &p->repo->settings; |
654 | |
|
655 | 0 | window_align = settings->packed_git_window_size / 2; |
656 | |
|
657 | 0 | if (p->pack_fd == -1 && open_packed_git(p)) |
658 | 0 | die("packfile %s cannot be accessed", p->pack_name); |
659 | | |
660 | 0 | CALLOC_ARRAY(win, 1); |
661 | 0 | win->offset = (offset / window_align) * window_align; |
662 | 0 | len = p->pack_size - win->offset; |
663 | 0 | if (len > settings->packed_git_window_size) |
664 | 0 | len = settings->packed_git_window_size; |
665 | 0 | win->len = (size_t)len; |
666 | 0 | pack_mapped += win->len; |
667 | |
|
668 | 0 | while (settings->packed_git_limit < pack_mapped && |
669 | 0 | unuse_one_window(p->repo->objects)) |
670 | 0 | ; /* nothing */ |
671 | 0 | win->base = xmmap_gently(NULL, win->len, |
672 | 0 | PROT_READ, MAP_PRIVATE, |
673 | 0 | p->pack_fd, win->offset); |
674 | 0 | if (win->base == MAP_FAILED) |
675 | 0 | die_errno(_("packfile %s cannot be mapped%s"), |
676 | 0 | p->pack_name, mmap_os_err()); |
677 | 0 | if (!win->offset && win->len == p->pack_size |
678 | 0 | && !p->do_not_close) |
679 | 0 | close_pack_fd(p); |
680 | 0 | pack_mmap_calls++; |
681 | 0 | pack_open_windows++; |
682 | 0 | if (pack_mapped > peak_pack_mapped) |
683 | 0 | peak_pack_mapped = pack_mapped; |
684 | 0 | if (pack_open_windows > peak_pack_open_windows) |
685 | 0 | peak_pack_open_windows = pack_open_windows; |
686 | 0 | win->next = p->windows; |
687 | 0 | p->windows = win; |
688 | 0 | } |
689 | 0 | } |
690 | 0 | if (win != *w_cursor) { |
691 | 0 | win->last_used = pack_used_ctr++; |
692 | 0 | win->inuse_cnt++; |
693 | 0 | *w_cursor = win; |
694 | 0 | } |
695 | 0 | offset -= win->offset; |
696 | 0 | if (left) |
697 | 0 | *left = win->len - xsize_t(offset); |
698 | 0 | return win->base + offset; |
699 | 0 | } |
700 | | |
701 | | void unuse_pack(struct pack_window **w_cursor) |
702 | 0 | { |
703 | 0 | struct pack_window *w = *w_cursor; |
704 | 0 | if (w) { |
705 | 0 | w->inuse_cnt--; |
706 | 0 | *w_cursor = NULL; |
707 | 0 | } |
708 | 0 | } |
709 | | |
710 | | struct packed_git *add_packed_git(struct repository *r, const char *path, |
711 | | size_t path_len, int local) |
712 | 0 | { |
713 | 0 | struct stat st; |
714 | 0 | size_t alloc; |
715 | 0 | struct packed_git *p; |
716 | 0 | struct object_id oid; |
717 | | |
718 | | /* |
719 | | * Make sure a corresponding .pack file exists and that |
720 | | * the index looks sane. |
721 | | */ |
722 | 0 | if (!strip_suffix_mem(path, &path_len, ".idx")) |
723 | 0 | return NULL; |
724 | | |
725 | | /* |
726 | | * ".promisor" is long enough to hold any suffix we're adding (and |
727 | | * the use xsnprintf double-checks that) |
728 | | */ |
729 | 0 | alloc = st_add3(path_len, strlen(".promisor"), 1); |
730 | 0 | p = alloc_packed_git(r, alloc); |
731 | 0 | memcpy(p->pack_name, path, path_len); |
732 | | |
733 | | /* |
734 | | * Note that we have to check auxiliary data structures before we check |
735 | | * for the ".pack" file to exist to avoid races with a packfile that is |
736 | | * in the process of being deleted. The ".pack" file is unlinked before |
737 | | * its auxiliary data structures, so we know that we either get a |
738 | | * consistent snapshot of all data structures or that we'll fail to |
739 | | * stat(3p) the packfile itself and thus return `NULL`. |
740 | | * |
741 | | * As such, we cannot bail out before the access(3p) calls in case the |
742 | | * packfile doesn't exist without doing two stat(3p) calls for it. |
743 | | */ |
744 | 0 | xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep"); |
745 | 0 | if (!access(p->pack_name, F_OK)) |
746 | 0 | p->pack_keep = 1; |
747 | |
|
748 | 0 | xsnprintf(p->pack_name + path_len, alloc - path_len, ".promisor"); |
749 | 0 | if (!access(p->pack_name, F_OK)) |
750 | 0 | p->pack_promisor = 1; |
751 | |
|
752 | 0 | xsnprintf(p->pack_name + path_len, alloc - path_len, ".mtimes"); |
753 | 0 | if (!access(p->pack_name, F_OK)) |
754 | 0 | p->is_cruft = 1; |
755 | |
|
756 | 0 | xsnprintf(p->pack_name + path_len, alloc - path_len, ".pack"); |
757 | 0 | if (stat(p->pack_name, &st) || !S_ISREG(st.st_mode)) { |
758 | 0 | free(p); |
759 | 0 | return NULL; |
760 | 0 | } |
761 | | |
762 | | /* ok, it looks sane as far as we can check without |
763 | | * actually mapping the pack file. |
764 | | */ |
765 | 0 | p->pack_size = st.st_size; |
766 | 0 | p->pack_local = local; |
767 | 0 | p->mtime = st.st_mtime; |
768 | 0 | if (path_len < r->hash_algo->hexsz || |
769 | 0 | get_oid_hex_algop(path + path_len - r->hash_algo->hexsz, &oid, |
770 | 0 | r->hash_algo)) |
771 | 0 | hashclr(p->hash, r->hash_algo); |
772 | 0 | else |
773 | 0 | hashcpy(p->hash, oid.hash, r->hash_algo); |
774 | |
|
775 | 0 | return p; |
776 | 0 | } |
777 | | |
778 | | void packfile_store_add_pack(struct odb_source_packed *store, |
779 | | struct packed_git *pack) |
780 | 0 | { |
781 | 0 | if (pack->pack_fd != -1) |
782 | 0 | pack_open_fds++; |
783 | |
|
784 | 0 | packfile_list_append(&store->packs, pack, 1); |
785 | 0 | strmap_put(&store->packs_by_path, pack->pack_name, pack); |
786 | 0 | } |
787 | | |
788 | | struct packed_git *packfile_store_load_pack(struct odb_source_packed *store, |
789 | | const char *idx_path, int local) |
790 | 0 | { |
791 | 0 | struct strbuf key = STRBUF_INIT; |
792 | 0 | struct packed_git *p; |
793 | | |
794 | | /* |
795 | | * We're being called with the path to the index file, but `pack_map` |
796 | | * holds the path to the packfile itself. |
797 | | */ |
798 | 0 | strbuf_addstr(&key, idx_path); |
799 | 0 | strbuf_strip_suffix(&key, ".idx"); |
800 | 0 | strbuf_addstr(&key, ".pack"); |
801 | |
|
802 | 0 | p = strmap_get(&store->packs_by_path, key.buf); |
803 | 0 | if (!p) { |
804 | 0 | p = add_packed_git(store->base.odb->repo, idx_path, |
805 | 0 | strlen(idx_path), local); |
806 | 0 | if (p) |
807 | 0 | packfile_store_add_pack(store, p); |
808 | 0 | } |
809 | |
|
810 | 0 | strbuf_release(&key); |
811 | 0 | return p; |
812 | 0 | } |
813 | | |
814 | | void for_each_file_in_pack_subdir(const char *objdir, |
815 | | const char *subdir, |
816 | | each_file_in_pack_dir_fn fn, |
817 | | void *data) |
818 | 0 | { |
819 | 0 | struct strbuf path = STRBUF_INIT; |
820 | 0 | size_t dirnamelen; |
821 | 0 | DIR *dir; |
822 | 0 | struct dirent *de; |
823 | |
|
824 | 0 | strbuf_addstr(&path, objdir); |
825 | 0 | strbuf_addstr(&path, "/pack"); |
826 | 0 | if (subdir) |
827 | 0 | strbuf_addf(&path, "/%s", subdir); |
828 | 0 | dir = opendir(path.buf); |
829 | 0 | if (!dir) { |
830 | 0 | if (errno != ENOENT) |
831 | 0 | error_errno("unable to open object pack directory: %s", |
832 | 0 | path.buf); |
833 | 0 | strbuf_release(&path); |
834 | 0 | return; |
835 | 0 | } |
836 | 0 | strbuf_addch(&path, '/'); |
837 | 0 | dirnamelen = path.len; |
838 | 0 | while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) { |
839 | 0 | strbuf_setlen(&path, dirnamelen); |
840 | 0 | strbuf_addstr(&path, de->d_name); |
841 | |
|
842 | 0 | fn(path.buf, path.len, de->d_name, data); |
843 | 0 | } |
844 | |
|
845 | 0 | closedir(dir); |
846 | 0 | strbuf_release(&path); |
847 | 0 | } |
848 | | |
849 | | void for_each_file_in_pack_dir(const char *objdir, |
850 | | each_file_in_pack_dir_fn fn, |
851 | | void *data) |
852 | 0 | { |
853 | 0 | for_each_file_in_pack_subdir(objdir, NULL, fn, data); |
854 | 0 | } |
855 | | |
856 | | struct packfile_list_entry *packfile_store_get_packs(struct odb_source_packed *store) |
857 | 0 | { |
858 | 0 | odb_source_prepare(&store->base, 0); |
859 | |
|
860 | 0 | if (store->midx) { |
861 | 0 | struct multi_pack_index *m = store->midx; |
862 | 0 | for (uint32_t i = 0; i < m->num_packs + m->num_packs_in_base; i++) |
863 | 0 | prepare_midx_pack(m, i); |
864 | 0 | } |
865 | |
|
866 | 0 | return store->packs.head; |
867 | 0 | } |
868 | | |
869 | | size_t unpack_object_header_buffer(const unsigned char *buf, size_t len, |
870 | | enum object_type *type, size_t *sizep) |
871 | 90 | { |
872 | 90 | unsigned shift; |
873 | 90 | size_t size, c, used = 0; |
874 | | |
875 | 90 | c = buf[used++]; |
876 | 90 | *type = (c >> 4) & 7; |
877 | 90 | size = c & 15; |
878 | 90 | shift = 4; |
879 | 492 | while (c & 0x80) { |
880 | | /* |
881 | | * Each continuation byte adds 7 bits. Ensure shift won't |
882 | | * overflow size_t (use size_t not long for 64-bit on Windows). |
883 | | */ |
884 | 463 | if (len <= used || (bitsizeof(size_t) - 7) < shift) { |
885 | 61 | error("bad object header"); |
886 | 61 | size = used = 0; |
887 | 61 | break; |
888 | 61 | } |
889 | 402 | c = buf[used++]; |
890 | 402 | size = st_add(size, st_left_shift(c & 0x7f, shift)); |
891 | 402 | shift += 7; |
892 | 402 | } |
893 | 90 | *sizep = size; |
894 | 90 | return used; |
895 | 90 | } |
896 | | |
897 | | /* |
898 | | * Read a delta object's header at curpos in p (already inflated as needed) |
899 | | * and return the size of the result object (the post-application target). |
900 | | */ |
901 | | size_t get_size_from_delta(struct packed_git *p, |
902 | | struct pack_window **w_curs, |
903 | | off_t curpos) |
904 | 0 | { |
905 | 0 | const unsigned char *data; |
906 | 0 | unsigned char delta_head[20], *in; |
907 | 0 | git_zstream stream; |
908 | 0 | int st; |
909 | |
|
910 | 0 | memset(&stream, 0, sizeof(stream)); |
911 | 0 | stream.next_out = delta_head; |
912 | 0 | stream.avail_out = sizeof(delta_head); |
913 | |
|
914 | 0 | git_inflate_init(&stream); |
915 | 0 | do { |
916 | 0 | in = use_pack(p, w_curs, curpos, &stream.avail_in); |
917 | 0 | stream.next_in = in; |
918 | | /* |
919 | | * Note: the window section returned by use_pack() must be |
920 | | * available throughout git_inflate()'s unlocked execution. To |
921 | | * ensure no other thread will modify the window in the |
922 | | * meantime, we rely on the packed_window.inuse_cnt. This |
923 | | * counter is incremented before window reading and checked |
924 | | * before window disposal. |
925 | | * |
926 | | * Other worrying sections could be the call to close_pack_fd(), |
927 | | * which can close packs even with in-use windows, and to |
928 | | * odb_reprepare(). Regarding the former, mmap doc says: |
929 | | * "closing the file descriptor does not unmap the region". And |
930 | | * for the latter, it won't re-open already available packs. |
931 | | */ |
932 | 0 | obj_read_unlock(); |
933 | 0 | st = git_inflate(&stream, Z_FINISH); |
934 | 0 | obj_read_lock(); |
935 | 0 | curpos += stream.next_in - in; |
936 | 0 | } while ((st == Z_OK || st == Z_BUF_ERROR) && |
937 | 0 | stream.total_out < sizeof(delta_head)); |
938 | 0 | git_inflate_end(&stream); |
939 | 0 | if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) { |
940 | 0 | error("delta data unpack-initial failed"); |
941 | 0 | return 0; |
942 | 0 | } |
943 | | |
944 | | /* Examine the initial part of the delta to figure out |
945 | | * the result size. |
946 | | */ |
947 | 0 | data = delta_head; |
948 | | |
949 | | /* ignore base size */ |
950 | 0 | get_delta_hdr_size(&data, delta_head+sizeof(delta_head)); |
951 | | |
952 | | /* Read the result size */ |
953 | 0 | return get_delta_hdr_size(&data, delta_head+sizeof(delta_head)); |
954 | 0 | } |
955 | | |
956 | | int unpack_object_header(struct packed_git *p, |
957 | | struct pack_window **w_curs, |
958 | | off_t *curpos, |
959 | | size_t *sizep) |
960 | 0 | { |
961 | 0 | unsigned char *base; |
962 | 0 | size_t left, used; |
963 | 0 | enum object_type type; |
964 | | |
965 | | /* use_pack() assures us we have [base, base + 20) available |
966 | | * as a range that we can look at. (Its actually the hash |
967 | | * size that is assured.) With our object header encoding |
968 | | * the maximum deflated object size is 2^137, which is just |
969 | | * insane, so we know won't exceed what we have been given. |
970 | | */ |
971 | 0 | base = use_pack(p, w_curs, *curpos, &left); |
972 | 0 | used = unpack_object_header_buffer(base, left, &type, sizep); |
973 | 0 | if (!used) { |
974 | 0 | type = OBJ_BAD; |
975 | 0 | } else |
976 | 0 | *curpos += used; |
977 | |
|
978 | 0 | return type; |
979 | 0 | } |
980 | | |
981 | | void mark_bad_packed_object(struct packed_git *p, const struct object_id *oid) |
982 | 0 | { |
983 | 0 | oidset_insert(&p->bad_objects, oid); |
984 | 0 | } |
985 | | |
986 | | off_t get_delta_base(struct packed_git *p, |
987 | | struct pack_window **w_curs, |
988 | | off_t *curpos, |
989 | | enum object_type type, |
990 | | off_t delta_obj_offset) |
991 | 0 | { |
992 | 0 | unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL); |
993 | 0 | off_t base_offset; |
994 | | |
995 | | /* use_pack() assured us we have [base_info, base_info + 20) |
996 | | * as a range that we can look at without walking off the |
997 | | * end of the mapped window. Its actually the hash size |
998 | | * that is assured. An OFS_DELTA longer than the hash size |
999 | | * is stupid, as then a REF_DELTA would be smaller to store. |
1000 | | */ |
1001 | 0 | if (type == OBJ_OFS_DELTA) { |
1002 | 0 | unsigned used = 0; |
1003 | 0 | unsigned char c = base_info[used++]; |
1004 | 0 | base_offset = c & 127; |
1005 | 0 | while (c & 128) { |
1006 | 0 | base_offset += 1; |
1007 | 0 | if (!base_offset || MSB(base_offset, 7)) |
1008 | 0 | return 0; /* overflow */ |
1009 | 0 | c = base_info[used++]; |
1010 | 0 | base_offset = (base_offset << 7) + (c & 127); |
1011 | 0 | } |
1012 | 0 | base_offset = delta_obj_offset - base_offset; |
1013 | 0 | if (base_offset <= 0 || base_offset >= delta_obj_offset) |
1014 | 0 | return 0; /* out of bound */ |
1015 | 0 | *curpos += used; |
1016 | 0 | } else if (type == OBJ_REF_DELTA) { |
1017 | | /* The base entry _must_ be in the same pack */ |
1018 | 0 | struct object_id oid; |
1019 | 0 | oidread(&oid, base_info, p->repo->hash_algo); |
1020 | 0 | base_offset = find_pack_entry_one(&oid, p); |
1021 | 0 | *curpos += p->repo->hash_algo->rawsz; |
1022 | 0 | } else |
1023 | 0 | die("I am totally screwed"); |
1024 | 0 | return base_offset; |
1025 | 0 | } |
1026 | | |
1027 | | /* |
1028 | | * Like get_delta_base above, but we return the sha1 instead of the pack |
1029 | | * offset. This means it is cheaper for REF deltas (we do not have to do |
1030 | | * the final object lookup), but more expensive for OFS deltas (we |
1031 | | * have to load the revidx to convert the offset back into a sha1). |
1032 | | */ |
1033 | | static int get_delta_base_oid(struct packed_git *p, |
1034 | | struct pack_window **w_curs, |
1035 | | off_t curpos, |
1036 | | struct object_id *oid, |
1037 | | enum object_type type, |
1038 | | off_t delta_obj_offset) |
1039 | 0 | { |
1040 | 0 | if (type == OBJ_REF_DELTA) { |
1041 | 0 | unsigned char *base = use_pack(p, w_curs, curpos, NULL); |
1042 | 0 | oidread(oid, base, p->repo->hash_algo); |
1043 | 0 | return 0; |
1044 | 0 | } else if (type == OBJ_OFS_DELTA) { |
1045 | 0 | uint32_t base_pos; |
1046 | 0 | off_t base_offset = get_delta_base(p, w_curs, &curpos, |
1047 | 0 | type, delta_obj_offset); |
1048 | |
|
1049 | 0 | if (!base_offset) |
1050 | 0 | return -1; |
1051 | | |
1052 | 0 | if (offset_to_pack_pos(p, base_offset, &base_pos) < 0) |
1053 | 0 | return -1; |
1054 | | |
1055 | 0 | return nth_packed_object_id(oid, p, |
1056 | 0 | pack_pos_to_index(p, base_pos)); |
1057 | 0 | } else |
1058 | 0 | return -1; |
1059 | 0 | } |
1060 | | |
1061 | | static int retry_bad_packed_offset(struct repository *r, |
1062 | | struct packed_git *p, |
1063 | | off_t obj_offset) |
1064 | 0 | { |
1065 | 0 | int type; |
1066 | 0 | uint32_t pos; |
1067 | 0 | struct object_id oid; |
1068 | 0 | if (offset_to_pack_pos(p, obj_offset, &pos) < 0) |
1069 | 0 | return OBJ_BAD; |
1070 | 0 | nth_packed_object_id(&oid, p, pack_pos_to_index(p, pos)); |
1071 | 0 | mark_bad_packed_object(p, &oid); |
1072 | 0 | type = odb_read_object_info(r->objects, &oid, NULL); |
1073 | 0 | if (type <= OBJ_NONE) |
1074 | 0 | return OBJ_BAD; |
1075 | 0 | return type; |
1076 | 0 | } |
1077 | | |
1078 | 0 | #define POI_STACK_PREALLOC 64 |
1079 | | |
1080 | | static enum object_type packed_to_object_type(struct repository *r, |
1081 | | struct packed_git *p, |
1082 | | off_t obj_offset, |
1083 | | enum object_type type, |
1084 | | struct pack_window **w_curs, |
1085 | | off_t curpos) |
1086 | 0 | { |
1087 | 0 | off_t small_poi_stack[POI_STACK_PREALLOC]; |
1088 | 0 | off_t *poi_stack = small_poi_stack; |
1089 | 0 | int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC; |
1090 | |
|
1091 | 0 | while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
1092 | 0 | off_t base_offset; |
1093 | 0 | size_t size; |
1094 | | /* Push the object we're going to leave behind */ |
1095 | 0 | if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) { |
1096 | 0 | poi_stack_alloc = alloc_nr(poi_stack_nr); |
1097 | 0 | ALLOC_ARRAY(poi_stack, poi_stack_alloc); |
1098 | 0 | COPY_ARRAY(poi_stack, small_poi_stack, poi_stack_nr); |
1099 | 0 | } else { |
1100 | 0 | ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc); |
1101 | 0 | } |
1102 | 0 | poi_stack[poi_stack_nr++] = obj_offset; |
1103 | | /* If parsing the base offset fails, just unwind */ |
1104 | 0 | base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset); |
1105 | 0 | if (!base_offset) |
1106 | 0 | goto unwind; |
1107 | 0 | curpos = obj_offset = base_offset; |
1108 | 0 | type = unpack_object_header(p, w_curs, &curpos, &size); |
1109 | 0 | if (type <= OBJ_NONE) { |
1110 | | /* If getting the base itself fails, we first |
1111 | | * retry the base, otherwise unwind */ |
1112 | 0 | type = retry_bad_packed_offset(r, p, base_offset); |
1113 | 0 | if (type > OBJ_NONE) |
1114 | 0 | goto out; |
1115 | 0 | goto unwind; |
1116 | 0 | } |
1117 | 0 | } |
1118 | | |
1119 | 0 | switch (type) { |
1120 | 0 | case OBJ_BAD: |
1121 | 0 | case OBJ_COMMIT: |
1122 | 0 | case OBJ_TREE: |
1123 | 0 | case OBJ_BLOB: |
1124 | 0 | case OBJ_TAG: |
1125 | 0 | break; |
1126 | 0 | default: |
1127 | 0 | error("unknown object type %i at offset %"PRIuMAX" in %s", |
1128 | 0 | type, (uintmax_t)obj_offset, p->pack_name); |
1129 | 0 | type = OBJ_BAD; |
1130 | 0 | } |
1131 | | |
1132 | 0 | out: |
1133 | 0 | if (poi_stack != small_poi_stack) |
1134 | 0 | free(poi_stack); |
1135 | 0 | return type; |
1136 | | |
1137 | 0 | unwind: |
1138 | 0 | while (poi_stack_nr) { |
1139 | 0 | obj_offset = poi_stack[--poi_stack_nr]; |
1140 | 0 | type = retry_bad_packed_offset(r, p, obj_offset); |
1141 | 0 | if (type > OBJ_NONE) |
1142 | 0 | goto out; |
1143 | 0 | } |
1144 | 0 | type = OBJ_BAD; |
1145 | 0 | goto out; |
1146 | 0 | } |
1147 | | |
1148 | | static struct hashmap delta_base_cache; |
1149 | | static size_t delta_base_cached; |
1150 | | |
1151 | | static LIST_HEAD(delta_base_cache_lru); |
1152 | | |
1153 | | struct delta_base_cache_key { |
1154 | | struct packed_git *p; |
1155 | | off_t base_offset; |
1156 | | }; |
1157 | | |
1158 | | struct delta_base_cache_entry { |
1159 | | struct hashmap_entry ent; |
1160 | | struct delta_base_cache_key key; |
1161 | | struct list_head lru; |
1162 | | void *data; |
1163 | | size_t size; |
1164 | | enum object_type type; |
1165 | | }; |
1166 | | |
1167 | | static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset) |
1168 | 0 | { |
1169 | 0 | unsigned int hash; |
1170 | |
|
1171 | 0 | hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset; |
1172 | 0 | hash += (hash >> 8) + (hash >> 16); |
1173 | 0 | return hash; |
1174 | 0 | } |
1175 | | |
1176 | | static struct delta_base_cache_entry * |
1177 | | get_delta_base_cache_entry(struct packed_git *p, off_t base_offset) |
1178 | 0 | { |
1179 | 0 | struct hashmap_entry entry, *e; |
1180 | 0 | struct delta_base_cache_key key; |
1181 | |
|
1182 | 0 | if (!delta_base_cache.cmpfn) |
1183 | 0 | return NULL; |
1184 | | |
1185 | 0 | hashmap_entry_init(&entry, pack_entry_hash(p, base_offset)); |
1186 | 0 | key.p = p; |
1187 | 0 | key.base_offset = base_offset; |
1188 | 0 | e = hashmap_get(&delta_base_cache, &entry, &key); |
1189 | 0 | return e ? container_of(e, struct delta_base_cache_entry, ent) : NULL; |
1190 | 0 | } |
1191 | | |
1192 | | static int delta_base_cache_key_eq(const struct delta_base_cache_key *a, |
1193 | | const struct delta_base_cache_key *b) |
1194 | 0 | { |
1195 | 0 | return a->p == b->p && a->base_offset == b->base_offset; |
1196 | 0 | } |
1197 | | |
1198 | | static int delta_base_cache_hash_cmp(const void *cmp_data UNUSED, |
1199 | | const struct hashmap_entry *va, |
1200 | | const struct hashmap_entry *vb, |
1201 | | const void *vkey) |
1202 | 0 | { |
1203 | 0 | const struct delta_base_cache_entry *a, *b; |
1204 | 0 | const struct delta_base_cache_key *key = vkey; |
1205 | |
|
1206 | 0 | a = container_of(va, const struct delta_base_cache_entry, ent); |
1207 | 0 | b = container_of(vb, const struct delta_base_cache_entry, ent); |
1208 | |
|
1209 | 0 | if (key) |
1210 | 0 | return !delta_base_cache_key_eq(&a->key, key); |
1211 | 0 | else |
1212 | 0 | return !delta_base_cache_key_eq(&a->key, &b->key); |
1213 | 0 | } |
1214 | | |
1215 | | static int in_delta_base_cache(struct packed_git *p, off_t base_offset) |
1216 | 0 | { |
1217 | 0 | return !!get_delta_base_cache_entry(p, base_offset); |
1218 | 0 | } |
1219 | | |
1220 | | /* |
1221 | | * Remove the entry from the cache, but do _not_ free the associated |
1222 | | * entry data. The caller takes ownership of the "data" buffer, and |
1223 | | * should copy out any fields it wants before detaching. |
1224 | | */ |
1225 | | static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent) |
1226 | 0 | { |
1227 | 0 | hashmap_remove(&delta_base_cache, &ent->ent, &ent->key); |
1228 | 0 | list_del(&ent->lru); |
1229 | 0 | delta_base_cached -= ent->size; |
1230 | 0 | free(ent); |
1231 | 0 | } |
1232 | | |
1233 | | static void *cache_or_unpack_entry(struct repository *r, struct packed_git *p, |
1234 | | off_t base_offset, size_t *base_size, |
1235 | | enum object_type *type) |
1236 | 0 | { |
1237 | 0 | struct delta_base_cache_entry *ent; |
1238 | |
|
1239 | 0 | ent = get_delta_base_cache_entry(p, base_offset); |
1240 | 0 | if (!ent) |
1241 | 0 | return unpack_entry(r, p, base_offset, type, base_size); |
1242 | | |
1243 | 0 | if (type) |
1244 | 0 | *type = ent->type; |
1245 | 0 | if (base_size) |
1246 | 0 | *base_size = ent->size; |
1247 | 0 | return xmemdupz(ent->data, ent->size); |
1248 | 0 | } |
1249 | | |
1250 | | static inline void release_delta_base_cache(struct delta_base_cache_entry *ent) |
1251 | 0 | { |
1252 | 0 | free(ent->data); |
1253 | 0 | detach_delta_base_cache_entry(ent); |
1254 | 0 | } |
1255 | | |
1256 | | void clear_delta_base_cache(void) |
1257 | 0 | { |
1258 | 0 | struct list_head *lru, *tmp; |
1259 | 0 | list_for_each_safe(lru, tmp, &delta_base_cache_lru) { |
1260 | 0 | struct delta_base_cache_entry *entry = |
1261 | 0 | list_entry(lru, struct delta_base_cache_entry, lru); |
1262 | 0 | release_delta_base_cache(entry); |
1263 | 0 | } |
1264 | 0 | } |
1265 | | |
1266 | | static void add_delta_base_cache(struct packed_git *p, off_t base_offset, |
1267 | | void *base, size_t base_size, |
1268 | | size_t delta_base_cache_limit, |
1269 | | enum object_type type) |
1270 | 0 | { |
1271 | 0 | struct delta_base_cache_entry *ent; |
1272 | 0 | struct list_head *lru, *tmp; |
1273 | | |
1274 | | /* |
1275 | | * Check required to avoid redundant entries when more than one thread |
1276 | | * is unpacking the same object, in unpack_entry() (since its phases I |
1277 | | * and III might run concurrently across multiple threads). |
1278 | | */ |
1279 | 0 | if (in_delta_base_cache(p, base_offset)) { |
1280 | 0 | free(base); |
1281 | 0 | return; |
1282 | 0 | } |
1283 | | |
1284 | 0 | delta_base_cached += base_size; |
1285 | |
|
1286 | 0 | list_for_each_safe(lru, tmp, &delta_base_cache_lru) { |
1287 | 0 | struct delta_base_cache_entry *f = |
1288 | 0 | list_entry(lru, struct delta_base_cache_entry, lru); |
1289 | 0 | if (delta_base_cached <= delta_base_cache_limit) |
1290 | 0 | break; |
1291 | 0 | release_delta_base_cache(f); |
1292 | 0 | } |
1293 | |
|
1294 | 0 | ent = xmalloc(sizeof(*ent)); |
1295 | 0 | ent->key.p = p; |
1296 | 0 | ent->key.base_offset = base_offset; |
1297 | 0 | ent->type = type; |
1298 | 0 | ent->data = base; |
1299 | 0 | ent->size = base_size; |
1300 | 0 | list_add_tail(&ent->lru, &delta_base_cache_lru); |
1301 | |
|
1302 | 0 | if (!delta_base_cache.cmpfn) |
1303 | 0 | hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0); |
1304 | 0 | hashmap_entry_init(&ent->ent, pack_entry_hash(p, base_offset)); |
1305 | 0 | hashmap_add(&delta_base_cache, &ent->ent); |
1306 | 0 | } |
1307 | | |
1308 | | int packed_object_info_with_index_pos(struct odb_source_packed *source, |
1309 | | struct packed_git *p, off_t obj_offset, |
1310 | | uint32_t *maybe_index_pos, struct object_info *oi) |
1311 | 0 | { |
1312 | 0 | struct pack_window *w_curs = NULL; |
1313 | 0 | size_t size; |
1314 | 0 | off_t curpos = obj_offset; |
1315 | 0 | enum object_type type = OBJ_NONE; |
1316 | 0 | uint32_t pack_pos; |
1317 | 0 | int ret; |
1318 | | |
1319 | | /* |
1320 | | * We always get the representation type, but only convert it to |
1321 | | * a "real" type later if the caller is interested. |
1322 | | */ |
1323 | 0 | if (oi->contentp) { |
1324 | 0 | *oi->contentp = cache_or_unpack_entry(p->repo, p, obj_offset, |
1325 | 0 | oi->sizep, &type); |
1326 | 0 | if (!*oi->contentp) |
1327 | 0 | type = OBJ_BAD; |
1328 | 0 | } else if (oi->sizep || oi->typep || oi->delta_base_oid) { |
1329 | 0 | type = unpack_object_header(p, &w_curs, &curpos, &size); |
1330 | 0 | } |
1331 | |
|
1332 | 0 | if (!oi->contentp && oi->sizep) { |
1333 | 0 | if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
1334 | 0 | off_t tmp_pos = curpos; |
1335 | 0 | off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos, |
1336 | 0 | type, obj_offset); |
1337 | 0 | if (!base_offset) { |
1338 | 0 | ret = -1; |
1339 | 0 | goto out; |
1340 | 0 | } |
1341 | 0 | size = get_size_from_delta(p, &w_curs, tmp_pos); |
1342 | 0 | if (size == 0) { |
1343 | 0 | ret = -1; |
1344 | 0 | goto out; |
1345 | 0 | } |
1346 | 0 | } |
1347 | 0 | *oi->sizep = size; |
1348 | 0 | } |
1349 | | |
1350 | 0 | if (oi->disk_sizep || (oi->mtimep && p->is_cruft)) { |
1351 | 0 | if (offset_to_pack_pos(p, obj_offset, &pack_pos) < 0) { |
1352 | 0 | error("could not find object at offset %"PRIuMAX" " |
1353 | 0 | "in pack %s", (uintmax_t)obj_offset, p->pack_name); |
1354 | 0 | ret = -1; |
1355 | 0 | goto out; |
1356 | 0 | } |
1357 | 0 | } |
1358 | | |
1359 | 0 | if (oi->disk_sizep) |
1360 | 0 | *oi->disk_sizep = pack_pos_to_offset(p, pack_pos + 1) - obj_offset; |
1361 | |
|
1362 | 0 | if (oi->mtimep) { |
1363 | 0 | if (p->is_cruft) { |
1364 | 0 | uint32_t index_pos; |
1365 | |
|
1366 | 0 | if (load_pack_mtimes(p) < 0) |
1367 | 0 | die(_("could not load .mtimes for cruft pack '%s'"), |
1368 | 0 | pack_basename(p)); |
1369 | | |
1370 | 0 | if (maybe_index_pos) |
1371 | 0 | index_pos = *maybe_index_pos; |
1372 | 0 | else |
1373 | 0 | index_pos = pack_pos_to_index(p, pack_pos); |
1374 | |
|
1375 | 0 | *oi->mtimep = nth_packed_mtime(p, index_pos); |
1376 | 0 | } else { |
1377 | 0 | *oi->mtimep = p->mtime; |
1378 | 0 | } |
1379 | 0 | } |
1380 | | |
1381 | 0 | if (oi->typep) { |
1382 | 0 | enum object_type ptot; |
1383 | 0 | ptot = packed_to_object_type(p->repo, p, obj_offset, |
1384 | 0 | type, &w_curs, curpos); |
1385 | 0 | if (oi->typep) |
1386 | 0 | *oi->typep = ptot; |
1387 | 0 | if (ptot < 0) { |
1388 | 0 | ret = -1; |
1389 | 0 | goto out; |
1390 | 0 | } |
1391 | 0 | } |
1392 | | |
1393 | 0 | if (oi->delta_base_oid) { |
1394 | 0 | if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { |
1395 | 0 | if (get_delta_base_oid(p, &w_curs, curpos, |
1396 | 0 | oi->delta_base_oid, |
1397 | 0 | type, obj_offset) < 0) { |
1398 | 0 | ret = -1; |
1399 | 0 | goto out; |
1400 | 0 | } |
1401 | 0 | } else |
1402 | 0 | oidclr(oi->delta_base_oid, p->repo->hash_algo); |
1403 | 0 | } |
1404 | | |
1405 | 0 | if (oi->source_infop) { |
1406 | 0 | if (!source) |
1407 | 0 | BUG("cannot request source without an owning source"); |
1408 | 0 | oi->source_infop->source = &source->base; |
1409 | |
|
1410 | 0 | oi->source_infop->u.packed.offset = obj_offset; |
1411 | 0 | oi->source_infop->u.packed.pack = p; |
1412 | |
|
1413 | 0 | switch (type) { |
1414 | 0 | case OBJ_NONE: |
1415 | 0 | oi->source_infop->u.packed.type = PACKED_OBJECT_TYPE_UNKNOWN; |
1416 | 0 | break; |
1417 | 0 | case OBJ_REF_DELTA: |
1418 | 0 | oi->source_infop->u.packed.type = PACKED_OBJECT_TYPE_REF_DELTA; |
1419 | 0 | break; |
1420 | 0 | case OBJ_OFS_DELTA: |
1421 | 0 | oi->source_infop->u.packed.type = PACKED_OBJECT_TYPE_OFS_DELTA; |
1422 | 0 | break; |
1423 | 0 | default: |
1424 | 0 | oi->source_infop->u.packed.type = PACKED_OBJECT_TYPE_FULL; |
1425 | 0 | break; |
1426 | 0 | } |
1427 | 0 | } |
1428 | | |
1429 | 0 | ret = 0; |
1430 | |
|
1431 | 0 | out: |
1432 | 0 | unuse_pack(&w_curs); |
1433 | 0 | return ret; |
1434 | 0 | } |
1435 | | |
1436 | | int packed_object_info(struct odb_source_packed *source, |
1437 | | struct packed_git *p, off_t obj_offset, |
1438 | | struct object_info *oi) |
1439 | 0 | { |
1440 | 0 | return packed_object_info_with_index_pos(source, p, obj_offset, NULL, oi); |
1441 | 0 | } |
1442 | | |
1443 | | static void *unpack_compressed_entry(struct packed_git *p, |
1444 | | struct pack_window **w_curs, |
1445 | | off_t curpos, |
1446 | | size_t size) |
1447 | 0 | { |
1448 | 0 | int st; |
1449 | 0 | git_zstream stream; |
1450 | 0 | unsigned char *buffer, *in; |
1451 | |
|
1452 | 0 | buffer = xmallocz_gently(size); |
1453 | 0 | if (!buffer) |
1454 | 0 | return NULL; |
1455 | 0 | memset(&stream, 0, sizeof(stream)); |
1456 | 0 | stream.next_out = buffer; |
1457 | 0 | stream.avail_out = size + 1; |
1458 | |
|
1459 | 0 | git_inflate_init(&stream); |
1460 | 0 | do { |
1461 | 0 | in = use_pack(p, w_curs, curpos, &stream.avail_in); |
1462 | 0 | stream.next_in = in; |
1463 | | /* |
1464 | | * Note: we must ensure the window section returned by |
1465 | | * use_pack() will be available throughout git_inflate()'s |
1466 | | * unlocked execution. Please refer to the comment at |
1467 | | * get_size_from_delta() to see how this is done. |
1468 | | */ |
1469 | 0 | obj_read_unlock(); |
1470 | 0 | st = git_inflate(&stream, Z_FINISH); |
1471 | 0 | obj_read_lock(); |
1472 | 0 | if (!stream.avail_out) |
1473 | 0 | break; /* the payload is larger than it should be */ |
1474 | 0 | curpos += stream.next_in - in; |
1475 | 0 | } while (st == Z_OK || st == Z_BUF_ERROR); |
1476 | 0 | git_inflate_end(&stream); |
1477 | 0 | if ((st != Z_STREAM_END) || stream.total_out != size) { |
1478 | 0 | free(buffer); |
1479 | 0 | return NULL; |
1480 | 0 | } |
1481 | | |
1482 | | /* versions of zlib can clobber unconsumed portion of outbuf */ |
1483 | 0 | buffer[size] = '\0'; |
1484 | |
|
1485 | 0 | return buffer; |
1486 | 0 | } |
1487 | | |
1488 | | static void write_pack_access_log(struct packed_git *p, off_t obj_offset) |
1489 | 0 | { |
1490 | 0 | static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS); |
1491 | 0 | trace_printf_key(&pack_access, "%s %"PRIuMAX"\n", |
1492 | 0 | p->pack_name, (uintmax_t)obj_offset); |
1493 | 0 | } |
1494 | | |
1495 | | int do_check_packed_object_crc; |
1496 | | |
1497 | 0 | #define UNPACK_ENTRY_STACK_PREALLOC 64 |
1498 | | struct unpack_entry_stack_ent { |
1499 | | off_t obj_offset; |
1500 | | off_t curpos; |
1501 | | size_t size; |
1502 | | }; |
1503 | | |
1504 | | void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset, |
1505 | | enum object_type *final_type, size_t *final_size) |
1506 | 0 | { |
1507 | 0 | struct pack_window *w_curs = NULL; |
1508 | 0 | off_t curpos = obj_offset; |
1509 | 0 | void *data = NULL; |
1510 | 0 | size_t size; |
1511 | 0 | enum object_type type; |
1512 | 0 | struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC]; |
1513 | 0 | struct unpack_entry_stack_ent *delta_stack = small_delta_stack; |
1514 | 0 | int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC; |
1515 | 0 | int base_from_cache = 0; |
1516 | |
|
1517 | 0 | prepare_repo_settings(p->repo); |
1518 | |
|
1519 | 0 | write_pack_access_log(p, obj_offset); |
1520 | | |
1521 | | /* PHASE 1: drill down to the innermost base object */ |
1522 | 0 | for (;;) { |
1523 | 0 | off_t base_offset; |
1524 | 0 | int i; |
1525 | 0 | struct delta_base_cache_entry *ent; |
1526 | |
|
1527 | 0 | ent = get_delta_base_cache_entry(p, curpos); |
1528 | 0 | if (ent) { |
1529 | 0 | type = ent->type; |
1530 | 0 | data = ent->data; |
1531 | 0 | size = ent->size; |
1532 | 0 | detach_delta_base_cache_entry(ent); |
1533 | 0 | base_from_cache = 1; |
1534 | 0 | break; |
1535 | 0 | } |
1536 | | |
1537 | 0 | if (do_check_packed_object_crc && p->index_version > 1) { |
1538 | 0 | uint32_t pack_pos, index_pos; |
1539 | 0 | off_t len; |
1540 | |
|
1541 | 0 | if (offset_to_pack_pos(p, obj_offset, &pack_pos) < 0) { |
1542 | 0 | error("could not find object at offset %"PRIuMAX" in pack %s", |
1543 | 0 | (uintmax_t)obj_offset, p->pack_name); |
1544 | 0 | data = NULL; |
1545 | 0 | goto out; |
1546 | 0 | } |
1547 | | |
1548 | 0 | len = pack_pos_to_offset(p, pack_pos + 1) - obj_offset; |
1549 | 0 | index_pos = pack_pos_to_index(p, pack_pos); |
1550 | 0 | if (check_pack_crc(p, &w_curs, obj_offset, len, index_pos)) { |
1551 | 0 | struct object_id oid; |
1552 | 0 | nth_packed_object_id(&oid, p, index_pos); |
1553 | 0 | error("bad packed object CRC for %s", |
1554 | 0 | oid_to_hex(&oid)); |
1555 | 0 | mark_bad_packed_object(p, &oid); |
1556 | 0 | data = NULL; |
1557 | 0 | goto out; |
1558 | 0 | } |
1559 | 0 | } |
1560 | | |
1561 | 0 | type = unpack_object_header(p, &w_curs, &curpos, &size); |
1562 | 0 | if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA) |
1563 | 0 | break; |
1564 | | |
1565 | 0 | base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset); |
1566 | 0 | if (!base_offset) { |
1567 | 0 | error("failed to validate delta base reference " |
1568 | 0 | "at offset %"PRIuMAX" from %s", |
1569 | 0 | (uintmax_t)curpos, p->pack_name); |
1570 | | /* bail to phase 2, in hopes of recovery */ |
1571 | 0 | data = NULL; |
1572 | 0 | break; |
1573 | 0 | } |
1574 | | |
1575 | | /* push object, proceed to base */ |
1576 | 0 | if (delta_stack_nr >= delta_stack_alloc |
1577 | 0 | && delta_stack == small_delta_stack) { |
1578 | 0 | delta_stack_alloc = alloc_nr(delta_stack_nr); |
1579 | 0 | ALLOC_ARRAY(delta_stack, delta_stack_alloc); |
1580 | 0 | COPY_ARRAY(delta_stack, small_delta_stack, |
1581 | 0 | delta_stack_nr); |
1582 | 0 | } else { |
1583 | 0 | ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc); |
1584 | 0 | } |
1585 | 0 | i = delta_stack_nr++; |
1586 | 0 | delta_stack[i].obj_offset = obj_offset; |
1587 | 0 | delta_stack[i].curpos = curpos; |
1588 | 0 | delta_stack[i].size = size; |
1589 | |
|
1590 | 0 | curpos = obj_offset = base_offset; |
1591 | 0 | } |
1592 | | |
1593 | | /* PHASE 2: handle the base */ |
1594 | 0 | switch (type) { |
1595 | 0 | case OBJ_OFS_DELTA: |
1596 | 0 | case OBJ_REF_DELTA: |
1597 | 0 | if (data) |
1598 | 0 | BUG("unpack_entry: left loop at a valid delta"); |
1599 | 0 | break; |
1600 | 0 | case OBJ_COMMIT: |
1601 | 0 | case OBJ_TREE: |
1602 | 0 | case OBJ_BLOB: |
1603 | 0 | case OBJ_TAG: |
1604 | 0 | if (!base_from_cache) |
1605 | 0 | data = unpack_compressed_entry(p, &w_curs, curpos, size); |
1606 | 0 | break; |
1607 | 0 | default: |
1608 | 0 | data = NULL; |
1609 | 0 | error("unknown object type %i at offset %"PRIuMAX" in %s", |
1610 | 0 | type, (uintmax_t)obj_offset, p->pack_name); |
1611 | 0 | } |
1612 | | |
1613 | | /* PHASE 3: apply deltas in order */ |
1614 | | |
1615 | | /* invariants: |
1616 | | * 'data' holds the base data, or NULL if there was corruption |
1617 | | */ |
1618 | 0 | while (delta_stack_nr) { |
1619 | 0 | void *delta_data; |
1620 | 0 | void *base = data; |
1621 | 0 | void *external_base = NULL; |
1622 | 0 | size_t delta_size, base_size = size; |
1623 | 0 | int i; |
1624 | 0 | off_t base_obj_offset = obj_offset; |
1625 | |
|
1626 | 0 | data = NULL; |
1627 | |
|
1628 | 0 | if (!base) { |
1629 | | /* |
1630 | | * We're probably in deep shit, but let's try to fetch |
1631 | | * the required base anyway from another pack or loose. |
1632 | | * This is costly but should happen only in the presence |
1633 | | * of a corrupted pack, and is better than failing outright. |
1634 | | */ |
1635 | 0 | uint32_t pos; |
1636 | 0 | struct object_id base_oid; |
1637 | 0 | if (!(offset_to_pack_pos(p, obj_offset, &pos))) { |
1638 | 0 | struct object_info oi = OBJECT_INFO_INIT; |
1639 | |
|
1640 | 0 | nth_packed_object_id(&base_oid, p, |
1641 | 0 | pack_pos_to_index(p, pos)); |
1642 | 0 | error("failed to read delta base object %s" |
1643 | 0 | " at offset %"PRIuMAX" from %s", |
1644 | 0 | oid_to_hex(&base_oid), (uintmax_t)obj_offset, |
1645 | 0 | p->pack_name); |
1646 | 0 | mark_bad_packed_object(p, &base_oid); |
1647 | |
|
1648 | 0 | oi.typep = &type; |
1649 | 0 | oi.sizep = &base_size; |
1650 | 0 | oi.contentp = &base; |
1651 | 0 | if (odb_read_object_info_extended(r->objects, &base_oid, |
1652 | 0 | &oi, 0) < 0) |
1653 | 0 | base = NULL; |
1654 | |
|
1655 | 0 | external_base = base; |
1656 | 0 | } |
1657 | 0 | } |
1658 | |
|
1659 | 0 | i = --delta_stack_nr; |
1660 | 0 | obj_offset = delta_stack[i].obj_offset; |
1661 | 0 | curpos = delta_stack[i].curpos; |
1662 | 0 | delta_size = delta_stack[i].size; |
1663 | |
|
1664 | 0 | if (!base) |
1665 | 0 | continue; |
1666 | | |
1667 | 0 | delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size); |
1668 | |
|
1669 | 0 | if (!delta_data) { |
1670 | 0 | error("failed to unpack compressed delta " |
1671 | 0 | "at offset %"PRIuMAX" from %s", |
1672 | 0 | (uintmax_t)curpos, p->pack_name); |
1673 | 0 | data = NULL; |
1674 | 0 | } else { |
1675 | 0 | data = patch_delta(base, base_size, delta_data, |
1676 | 0 | delta_size, &size); |
1677 | | |
1678 | | /* |
1679 | | * We could not apply the delta; warn the user, but |
1680 | | * keep going. Our failure will be noticed either in |
1681 | | * the next iteration of the loop, or if this is the |
1682 | | * final delta, in the caller when we return NULL. |
1683 | | * Those code paths will take care of making a more |
1684 | | * explicit warning and retrying with another copy of |
1685 | | * the object. |
1686 | | */ |
1687 | 0 | if (!data) |
1688 | 0 | error("failed to apply delta"); |
1689 | 0 | } |
1690 | | |
1691 | | /* |
1692 | | * We delay adding `base` to the cache until the end of the loop |
1693 | | * because unpack_compressed_entry() momentarily releases the |
1694 | | * obj_read_mutex, giving another thread the chance to access |
1695 | | * the cache. Therefore, if `base` was already there, this other |
1696 | | * thread could free() it (e.g. to make space for another entry) |
1697 | | * before we are done using it. |
1698 | | */ |
1699 | 0 | if (!external_base) |
1700 | 0 | add_delta_base_cache(p, base_obj_offset, base, base_size, |
1701 | 0 | p->repo->settings.delta_base_cache_limit, |
1702 | 0 | type); |
1703 | |
|
1704 | 0 | free(delta_data); |
1705 | 0 | free(external_base); |
1706 | 0 | } |
1707 | |
|
1708 | 0 | if (final_type) |
1709 | 0 | *final_type = type; |
1710 | 0 | if (final_size) |
1711 | 0 | *final_size = size; |
1712 | |
|
1713 | 0 | out: |
1714 | 0 | unuse_pack(&w_curs); |
1715 | |
|
1716 | 0 | if (delta_stack != small_delta_stack) |
1717 | 0 | free(delta_stack); |
1718 | |
|
1719 | 0 | return data; |
1720 | 0 | } |
1721 | | |
1722 | | int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result) |
1723 | 0 | { |
1724 | 0 | const unsigned char *index_fanout = p->index_data; |
1725 | 0 | const unsigned char *index_lookup; |
1726 | 0 | const unsigned int hashsz = p->repo->hash_algo->rawsz; |
1727 | 0 | int index_lookup_width; |
1728 | |
|
1729 | 0 | if (!index_fanout) |
1730 | 0 | BUG("bsearch_pack called without a valid pack-index"); |
1731 | | |
1732 | 0 | index_lookup = index_fanout + 4 * 256; |
1733 | 0 | if (p->index_version == 1) { |
1734 | 0 | index_lookup_width = hashsz + 4; |
1735 | 0 | index_lookup += 4; |
1736 | 0 | } else { |
1737 | 0 | index_lookup_width = hashsz; |
1738 | 0 | index_fanout += 8; |
1739 | 0 | index_lookup += 8; |
1740 | 0 | } |
1741 | |
|
1742 | 0 | return bsearch_hash(oid->hash, (const uint32_t*)index_fanout, |
1743 | 0 | index_lookup, index_lookup_width, result); |
1744 | 0 | } |
1745 | | |
1746 | | int nth_packed_object_id(struct object_id *oid, |
1747 | | struct packed_git *p, |
1748 | | uint32_t n) |
1749 | 0 | { |
1750 | 0 | const unsigned char *index = p->index_data; |
1751 | 0 | const unsigned int hashsz = p->repo->hash_algo->rawsz; |
1752 | 0 | if (!index) { |
1753 | 0 | if (open_pack_index(p)) |
1754 | 0 | return -1; |
1755 | 0 | index = p->index_data; |
1756 | 0 | } |
1757 | 0 | if (n >= p->num_objects) |
1758 | 0 | return -1; |
1759 | 0 | index += 4 * 256; |
1760 | 0 | if (p->index_version == 1) { |
1761 | 0 | oidread(oid, index + st_add(st_mult(hashsz + 4, n), 4), |
1762 | 0 | p->repo->hash_algo); |
1763 | 0 | } else { |
1764 | 0 | index += 8; |
1765 | 0 | oidread(oid, index + st_mult(hashsz, n), p->repo->hash_algo); |
1766 | 0 | } |
1767 | 0 | return 0; |
1768 | 0 | } |
1769 | | |
1770 | | void check_pack_index_ptr(const struct packed_git *p, const void *vptr) |
1771 | 0 | { |
1772 | 0 | const unsigned char *ptr = vptr; |
1773 | 0 | const unsigned char *start = p->index_data; |
1774 | 0 | const unsigned char *end = start + p->index_size; |
1775 | 0 | if (ptr < start) |
1776 | 0 | die(_("offset before start of pack index for %s (corrupt index?)"), |
1777 | 0 | p->pack_name); |
1778 | | /* No need to check for underflow; .idx files must be at least 8 bytes */ |
1779 | 0 | if (ptr >= end - 8) |
1780 | 0 | die(_("offset beyond end of pack index for %s (truncated index?)"), |
1781 | 0 | p->pack_name); |
1782 | 0 | } |
1783 | | |
1784 | | off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n) |
1785 | 0 | { |
1786 | 0 | const unsigned char *index = p->index_data; |
1787 | 0 | const unsigned int hashsz = p->repo->hash_algo->rawsz; |
1788 | 0 | index += 4 * 256; |
1789 | 0 | if (p->index_version == 1) { |
1790 | 0 | return ntohl(*((uint32_t *)(index + st_mult(hashsz + 4, n)))); |
1791 | 0 | } else { |
1792 | 0 | uint32_t off; |
1793 | 0 | index += st_add(8, st_mult(p->num_objects, hashsz + 4)); |
1794 | 0 | off = ntohl(*((uint32_t *)(index + st_mult(4, n)))); |
1795 | 0 | if (!(off & 0x80000000)) |
1796 | 0 | return off; |
1797 | 0 | index += st_add(st_mult(p->num_objects, 4), |
1798 | 0 | st_mult(off & 0x7fffffff, 8)); |
1799 | 0 | check_pack_index_ptr(p, index); |
1800 | 0 | return get_be64(index); |
1801 | 0 | } |
1802 | 0 | } |
1803 | | |
1804 | | off_t find_pack_entry_one(const struct object_id *oid, |
1805 | | struct packed_git *p) |
1806 | 0 | { |
1807 | 0 | const unsigned char *index = p->index_data; |
1808 | 0 | uint32_t result; |
1809 | |
|
1810 | 0 | if (!index) { |
1811 | 0 | if (open_pack_index(p)) |
1812 | 0 | return 0; |
1813 | 0 | } |
1814 | | |
1815 | 0 | if (bsearch_pack(oid, p, &result)) |
1816 | 0 | return nth_packed_object_offset(p, result); |
1817 | 0 | return 0; |
1818 | 0 | } |
1819 | | |
1820 | | int is_pack_valid(struct packed_git *p) |
1821 | 0 | { |
1822 | | /* An already open pack is known to be valid. */ |
1823 | 0 | if (p->pack_fd != -1) |
1824 | 0 | return 1; |
1825 | | |
1826 | | /* If the pack has one window completely covering the |
1827 | | * file size, the pack is known to be valid even if |
1828 | | * the descriptor is not currently open. |
1829 | | */ |
1830 | 0 | if (p->windows) { |
1831 | 0 | struct pack_window *w = p->windows; |
1832 | |
|
1833 | 0 | if (!w->offset && w->len == p->pack_size) |
1834 | 0 | return 1; |
1835 | 0 | } |
1836 | | |
1837 | | /* Force the pack to open to prove its valid. */ |
1838 | 0 | return !open_packed_git(p); |
1839 | 0 | } |
1840 | | |
1841 | | int packfile_fill_entry(struct packed_git *p, |
1842 | | const struct object_id *oid, |
1843 | | struct pack_entry *e, |
1844 | | struct packed_git **bad_pack) |
1845 | 0 | { |
1846 | 0 | off_t offset; |
1847 | |
|
1848 | 0 | if (oidset_size(&p->bad_objects) && |
1849 | 0 | oidset_contains(&p->bad_objects, oid)) { |
1850 | 0 | if (bad_pack && !*bad_pack) |
1851 | 0 | *bad_pack = p; |
1852 | 0 | return 0; |
1853 | 0 | } |
1854 | | |
1855 | 0 | offset = find_pack_entry_one(oid, p); |
1856 | 0 | if (!offset) |
1857 | 0 | return 0; |
1858 | | |
1859 | | /* |
1860 | | * We are about to tell the caller where they can locate the |
1861 | | * requested object. We better make sure the packfile is |
1862 | | * still here and can be accessed before supplying that |
1863 | | * answer, as it may have been deleted since the index was |
1864 | | * loaded! |
1865 | | */ |
1866 | 0 | if (!is_pack_valid(p)) |
1867 | 0 | return 0; |
1868 | 0 | e->offset = offset; |
1869 | 0 | e->p = p; |
1870 | 0 | return 1; |
1871 | 0 | } |
1872 | | |
1873 | | static void maybe_invalidate_kept_pack_cache(struct odb_source_packed *store, |
1874 | | unsigned flags) |
1875 | 0 | { |
1876 | 0 | if (!store->kept_cache.packs) |
1877 | 0 | return; |
1878 | 0 | if (store->kept_cache.flags == flags) |
1879 | 0 | return; |
1880 | 0 | FREE_AND_NULL(store->kept_cache.packs); |
1881 | 0 | store->kept_cache.flags = 0; |
1882 | 0 | } |
1883 | | |
1884 | | struct packed_git **packfile_store_get_kept_pack_cache(struct odb_source_packed *store, |
1885 | | unsigned flags) |
1886 | 0 | { |
1887 | 0 | maybe_invalidate_kept_pack_cache(store, flags); |
1888 | |
|
1889 | 0 | if (!store->kept_cache.packs) { |
1890 | 0 | struct packed_git **packs = NULL; |
1891 | 0 | struct packfile_list_entry *e; |
1892 | 0 | size_t nr = 0, alloc = 0; |
1893 | | |
1894 | | /* |
1895 | | * We want "all" packs here, because we need to cover ones that |
1896 | | * are used by a midx, as well. We need to look in every one of |
1897 | | * them (instead of the midx itself) to cover duplicates. It's |
1898 | | * possible that an object is found in two packs that the midx |
1899 | | * covers, one kept and one not kept, but the midx returns only |
1900 | | * the non-kept version. |
1901 | | */ |
1902 | 0 | for (e = packfile_store_get_packs(store); e; e = e->next) { |
1903 | 0 | struct packed_git *p = e->pack; |
1904 | |
|
1905 | 0 | if ((p->pack_keep && (flags & KEPT_PACK_ON_DISK)) || |
1906 | 0 | (p->pack_keep_in_core && (flags & KEPT_PACK_IN_CORE)) || |
1907 | 0 | (p->pack_keep_in_core_open && (flags & KEPT_PACK_IN_CORE_OPEN))) { |
1908 | 0 | ALLOC_GROW(packs, nr + 1, alloc); |
1909 | 0 | packs[nr++] = p; |
1910 | 0 | } |
1911 | 0 | } |
1912 | 0 | ALLOC_GROW(packs, nr + 1, alloc); |
1913 | 0 | packs[nr] = NULL; |
1914 | |
|
1915 | 0 | store->kept_cache.packs = packs; |
1916 | 0 | store->kept_cache.flags = flags; |
1917 | 0 | } |
1918 | |
|
1919 | 0 | return store->kept_cache.packs; |
1920 | 0 | } |
1921 | | |
1922 | | int has_object_pack(struct repository *r, const struct object_id *oid) |
1923 | 0 | { |
1924 | 0 | struct odb_source *source; |
1925 | |
|
1926 | 0 | for (source = r->objects->sources; source; source = source->next) { |
1927 | 0 | struct odb_source_files *files = odb_source_files_downcast(source); |
1928 | 0 | if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0, NULL)) |
1929 | 0 | return 1; |
1930 | 0 | } |
1931 | | |
1932 | 0 | return 0; |
1933 | 0 | } |
1934 | | |
1935 | | int has_object_kept_pack(struct repository *r, const struct object_id *oid, |
1936 | | unsigned flags) |
1937 | 0 | { |
1938 | 0 | struct odb_source *source; |
1939 | 0 | struct pack_entry e; |
1940 | |
|
1941 | 0 | for (source = r->objects->sources; source; source = source->next) { |
1942 | 0 | struct odb_source_files *files = odb_source_files_downcast(source); |
1943 | 0 | struct packed_git **cache; |
1944 | |
|
1945 | 0 | cache = packfile_store_get_kept_pack_cache(files->packed, flags); |
1946 | |
|
1947 | 0 | for (; *cache; cache++) { |
1948 | 0 | struct packed_git *p = *cache; |
1949 | 0 | if (packfile_fill_entry(p, oid, &e, NULL)) |
1950 | 0 | return 1; |
1951 | 0 | } |
1952 | 0 | } |
1953 | | |
1954 | 0 | return 0; |
1955 | 0 | } |
1956 | | |
1957 | | int for_each_object_in_pack(struct packed_git *p, |
1958 | | each_packed_object_fn cb, void *data, |
1959 | | enum odb_for_each_object_flags flags) |
1960 | 0 | { |
1961 | 0 | uint32_t i; |
1962 | 0 | int r = 0; |
1963 | |
|
1964 | 0 | if (flags & ODB_FOR_EACH_OBJECT_PACK_ORDER) { |
1965 | 0 | if (load_pack_revindex(p->repo, p)) |
1966 | 0 | return -1; |
1967 | 0 | } |
1968 | | |
1969 | 0 | for (i = 0; i < p->num_objects; i++) { |
1970 | 0 | uint32_t index_pos; |
1971 | 0 | struct object_id oid; |
1972 | | |
1973 | | /* |
1974 | | * We are iterating "i" from 0 up to num_objects, but its |
1975 | | * meaning may be different, depending on the requested output |
1976 | | * order: |
1977 | | * |
1978 | | * - in object-name order, it is the same as the index order |
1979 | | * used by nth_packed_object_id(), so we can pass it |
1980 | | * directly |
1981 | | * |
1982 | | * - in pack-order, it is pack position, which we must |
1983 | | * convert to an index position in order to get the oid. |
1984 | | */ |
1985 | 0 | if (flags & ODB_FOR_EACH_OBJECT_PACK_ORDER) |
1986 | 0 | index_pos = pack_pos_to_index(p, i); |
1987 | 0 | else |
1988 | 0 | index_pos = i; |
1989 | |
|
1990 | 0 | if (nth_packed_object_id(&oid, p, index_pos) < 0) |
1991 | 0 | return error("unable to get sha1 of object %u in %s", |
1992 | 0 | index_pos, p->pack_name); |
1993 | | |
1994 | 0 | r = cb(&oid, p, index_pos, data); |
1995 | 0 | if (r) |
1996 | 0 | break; |
1997 | 0 | } |
1998 | 0 | return r; |
1999 | 0 | } |
2000 | | |
2001 | | struct add_promisor_object_data { |
2002 | | struct repository *repo; |
2003 | | struct oidset *set; |
2004 | | }; |
2005 | | |
2006 | | static int add_promisor_object(const struct object_id *oid, |
2007 | | struct object_info *oi UNUSED, |
2008 | | void *cb_data) |
2009 | 0 | { |
2010 | 0 | struct add_promisor_object_data *data = cb_data; |
2011 | 0 | struct object *obj; |
2012 | 0 | int we_parsed_object; |
2013 | |
|
2014 | 0 | obj = lookup_object(data->repo, oid); |
2015 | 0 | if (obj && obj->parsed) { |
2016 | 0 | we_parsed_object = 0; |
2017 | 0 | } else { |
2018 | 0 | we_parsed_object = 1; |
2019 | 0 | obj = parse_object_with_flags(data->repo, oid, |
2020 | 0 | PARSE_OBJECT_SKIP_HASH_CHECK); |
2021 | 0 | } |
2022 | |
|
2023 | 0 | if (!obj) |
2024 | 0 | return 1; |
2025 | | |
2026 | 0 | oidset_insert(data->set, oid); |
2027 | | |
2028 | | /* |
2029 | | * If this is a tree, commit, or tag, the objects it refers |
2030 | | * to are also promisor objects. (Blobs refer to no objects->) |
2031 | | */ |
2032 | 0 | if (obj->type == OBJ_TREE) { |
2033 | 0 | struct tree *tree = (struct tree *)obj; |
2034 | 0 | struct tree_desc desc; |
2035 | 0 | struct name_entry entry; |
2036 | 0 | if (init_tree_desc_gently(&desc, &tree->object.oid, |
2037 | 0 | tree->buffer, tree->size, 0)) |
2038 | | /* |
2039 | | * Error messages are given when packs are |
2040 | | * verified, so do not print any here. |
2041 | | */ |
2042 | 0 | return 0; |
2043 | 0 | while (tree_entry_gently(&desc, &entry)) |
2044 | 0 | oidset_insert(data->set, &entry.oid); |
2045 | 0 | if (we_parsed_object) |
2046 | 0 | free_tree_buffer(tree); |
2047 | 0 | } else if (obj->type == OBJ_COMMIT) { |
2048 | 0 | struct commit *commit = (struct commit *) obj; |
2049 | 0 | struct commit_list *parents = commit->parents; |
2050 | |
|
2051 | 0 | oidset_insert(data->set, get_commit_tree_oid(commit)); |
2052 | 0 | for (; parents; parents = parents->next) |
2053 | 0 | oidset_insert(data->set, &parents->item->object.oid); |
2054 | 0 | } else if (obj->type == OBJ_TAG) { |
2055 | 0 | struct tag *tag = (struct tag *) obj; |
2056 | 0 | oidset_insert(data->set, get_tagged_oid(tag)); |
2057 | 0 | } |
2058 | 0 | return 0; |
2059 | 0 | } |
2060 | | |
2061 | | int is_promisor_object(struct repository *r, const struct object_id *oid) |
2062 | 0 | { |
2063 | 0 | static struct oidset promisor_objects; |
2064 | 0 | static int promisor_objects_prepared; |
2065 | |
|
2066 | 0 | if (!promisor_objects_prepared) { |
2067 | 0 | if (repo_has_promisor_remote(r)) { |
2068 | 0 | struct add_promisor_object_data data = { |
2069 | 0 | .repo = r, |
2070 | 0 | .set = &promisor_objects, |
2071 | 0 | }; |
2072 | |
|
2073 | 0 | odb_for_each_object(r->objects, NULL, add_promisor_object, &data, |
2074 | 0 | ODB_FOR_EACH_OBJECT_PROMISOR_ONLY | ODB_FOR_EACH_OBJECT_PACK_ORDER); |
2075 | 0 | } |
2076 | 0 | promisor_objects_prepared = 1; |
2077 | 0 | } |
2078 | 0 | return oidset_contains(&promisor_objects, oid); |
2079 | 0 | } |
2080 | | |
2081 | | int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *len) |
2082 | 0 | { |
2083 | 0 | unsigned char *hdr; |
2084 | 0 | char *c; |
2085 | |
|
2086 | 0 | hdr = out; |
2087 | 0 | put_be32(hdr, PACK_SIGNATURE); |
2088 | 0 | hdr += 4; |
2089 | 0 | put_be32(hdr, strtoul(in, &c, 10)); |
2090 | 0 | hdr += 4; |
2091 | 0 | if (*c != ',') |
2092 | 0 | return -1; |
2093 | 0 | put_be32(hdr, strtoul(c + 1, &c, 10)); |
2094 | 0 | hdr += 4; |
2095 | 0 | if (*c) |
2096 | 0 | return -1; |
2097 | 0 | *len = hdr - out; |
2098 | 0 | return 0; |
2099 | 0 | } |
2100 | | |
2101 | | struct odb_packed_read_stream { |
2102 | | struct odb_stream base; |
2103 | | struct packed_git *pack; |
2104 | | git_zstream z; |
2105 | | enum { |
2106 | | ODB_PACKED_READ_STREAM_UNINITIALIZED, |
2107 | | ODB_PACKED_READ_STREAM_INUSE, |
2108 | | ODB_PACKED_READ_STREAM_DONE, |
2109 | | ODB_PACKED_READ_STREAM_ERROR, |
2110 | | } z_state; |
2111 | | off_t pos; |
2112 | | }; |
2113 | | |
2114 | | static ssize_t read_istream_pack_non_delta(struct odb_stream *_st, char *buf, |
2115 | | size_t sz) |
2116 | 0 | { |
2117 | 0 | struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st; |
2118 | 0 | size_t total_read = 0; |
2119 | |
|
2120 | 0 | switch (st->z_state) { |
2121 | 0 | case ODB_PACKED_READ_STREAM_UNINITIALIZED: |
2122 | 0 | memset(&st->z, 0, sizeof(st->z)); |
2123 | 0 | git_inflate_init(&st->z); |
2124 | 0 | st->z_state = ODB_PACKED_READ_STREAM_INUSE; |
2125 | 0 | break; |
2126 | 0 | case ODB_PACKED_READ_STREAM_DONE: |
2127 | 0 | return 0; |
2128 | 0 | case ODB_PACKED_READ_STREAM_ERROR: |
2129 | 0 | return -1; |
2130 | 0 | case ODB_PACKED_READ_STREAM_INUSE: |
2131 | 0 | break; |
2132 | 0 | } |
2133 | | |
2134 | 0 | while (total_read < sz) { |
2135 | 0 | int status; |
2136 | 0 | struct pack_window *window = NULL; |
2137 | 0 | unsigned char *mapped; |
2138 | |
|
2139 | 0 | mapped = use_pack(st->pack, &window, |
2140 | 0 | st->pos, &st->z.avail_in); |
2141 | |
|
2142 | 0 | st->z.next_out = (unsigned char *)buf + total_read; |
2143 | 0 | st->z.avail_out = sz - total_read; |
2144 | 0 | st->z.next_in = mapped; |
2145 | 0 | status = git_inflate(&st->z, Z_FINISH); |
2146 | |
|
2147 | 0 | st->pos += st->z.next_in - mapped; |
2148 | 0 | total_read = st->z.next_out - (unsigned char *)buf; |
2149 | 0 | unuse_pack(&window); |
2150 | |
|
2151 | 0 | if (status == Z_STREAM_END) { |
2152 | 0 | git_inflate_end(&st->z); |
2153 | 0 | st->z_state = ODB_PACKED_READ_STREAM_DONE; |
2154 | 0 | break; |
2155 | 0 | } |
2156 | | |
2157 | | /* |
2158 | | * Unlike the loose object case, we do not have to worry here |
2159 | | * about running out of input bytes and spinning infinitely. If |
2160 | | * we get Z_BUF_ERROR due to too few input bytes, then we'll |
2161 | | * replenish them in the next use_pack() call when we loop. If |
2162 | | * we truly hit the end of the pack (i.e., because it's corrupt |
2163 | | * or truncated), then use_pack() catches that and will die(). |
2164 | | */ |
2165 | 0 | if (status != Z_OK && status != Z_BUF_ERROR) { |
2166 | 0 | git_inflate_end(&st->z); |
2167 | 0 | st->z_state = ODB_PACKED_READ_STREAM_ERROR; |
2168 | 0 | return -1; |
2169 | 0 | } |
2170 | 0 | } |
2171 | 0 | return total_read; |
2172 | 0 | } |
2173 | | |
2174 | | static int close_istream_pack_non_delta(struct odb_stream *_st) |
2175 | 0 | { |
2176 | 0 | struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st; |
2177 | 0 | if (st->z_state == ODB_PACKED_READ_STREAM_INUSE) |
2178 | 0 | git_inflate_end(&st->z); |
2179 | 0 | return 0; |
2180 | 0 | } |
2181 | | |
2182 | | int packfile_read_object_stream(struct odb_stream **out, |
2183 | | const struct object_id *oid, |
2184 | | struct packed_git *pack, |
2185 | | off_t offset) |
2186 | 0 | { |
2187 | 0 | struct odb_packed_read_stream *stream; |
2188 | 0 | struct pack_window *window = NULL; |
2189 | 0 | enum object_type in_pack_type; |
2190 | 0 | size_t size; |
2191 | |
|
2192 | 0 | in_pack_type = unpack_object_header(pack, &window, &offset, &size); |
2193 | 0 | unuse_pack(&window); |
2194 | |
|
2195 | 0 | if (repo_settings_get_big_file_threshold(pack->repo) >= size) |
2196 | 0 | return -1; |
2197 | | |
2198 | 0 | switch (in_pack_type) { |
2199 | 0 | default: |
2200 | 0 | return -1; /* we do not do deltas for now */ |
2201 | 0 | case OBJ_BAD: |
2202 | 0 | mark_bad_packed_object(pack, oid); |
2203 | 0 | return -1; |
2204 | 0 | case OBJ_COMMIT: |
2205 | 0 | case OBJ_TREE: |
2206 | 0 | case OBJ_BLOB: |
2207 | 0 | case OBJ_TAG: |
2208 | 0 | break; |
2209 | 0 | } |
2210 | | |
2211 | 0 | CALLOC_ARRAY(stream, 1); |
2212 | 0 | stream->base.close = close_istream_pack_non_delta; |
2213 | 0 | stream->base.read = read_istream_pack_non_delta; |
2214 | 0 | stream->base.type = in_pack_type; |
2215 | 0 | stream->base.size = size; |
2216 | 0 | stream->z_state = ODB_PACKED_READ_STREAM_UNINITIALIZED; |
2217 | 0 | stream->pack = pack; |
2218 | 0 | stream->pos = offset; |
2219 | |
|
2220 | 0 | *out = &stream->base; |
2221 | |
|
2222 | 0 | return 0; |
2223 | 0 | } |