Coverage Report

Created: 2026-03-31 06:24

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