Coverage Report

Created: 2026-03-21 06:46

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
/*
1105
 * Give a fast, rough count of the number of objects in the repository. This
1106
 * ignores loose objects completely. If you have a lot of them, then either
1107
 * you should repack because your performance will be awful, or they are
1108
 * all unreachable objects about to be pruned, in which case they're not really
1109
 * interesting as a measure of repo size in the first place.
1110
 */
1111
unsigned long repo_approximate_object_count(struct repository *r)
1112
0
{
1113
0
  if (!r->objects->approximate_object_count_valid) {
1114
0
    struct odb_source *source;
1115
0
    unsigned long count = 0;
1116
0
    struct packed_git *p;
1117
1118
0
    odb_prepare_alternates(r->objects);
1119
1120
0
    for (source = r->objects->sources; source; source = source->next) {
1121
0
      struct multi_pack_index *m = get_multi_pack_index(source);
1122
0
      if (m)
1123
0
        count += m->num_objects + m->num_objects_in_base;
1124
0
    }
1125
1126
0
    repo_for_each_pack(r, p) {
1127
0
      if (p->multi_pack_index || open_pack_index(p))
1128
0
        continue;
1129
0
      count += p->num_objects;
1130
0
    }
1131
0
    r->objects->approximate_object_count = count;
1132
0
    r->objects->approximate_object_count_valid = 1;
1133
0
  }
1134
0
  return r->objects->approximate_object_count;
1135
0
}
1136
1137
unsigned long unpack_object_header_buffer(const unsigned char *buf,
1138
    unsigned long len, enum object_type *type, unsigned long *sizep)
1139
0
{
1140
0
  unsigned shift;
1141
0
  size_t size, c;
1142
0
  unsigned long used = 0;
1143
1144
0
  c = buf[used++];
1145
0
  *type = (c >> 4) & 7;
1146
0
  size = c & 15;
1147
0
  shift = 4;
1148
0
  while (c & 0x80) {
1149
0
    if (len <= used || (bitsizeof(long) - 7) < shift) {
1150
0
      error("bad object header");
1151
0
      size = used = 0;
1152
0
      break;
1153
0
    }
1154
0
    c = buf[used++];
1155
0
    size = st_add(size, st_left_shift(c & 0x7f, shift));
1156
0
    shift += 7;
1157
0
  }
1158
0
  *sizep = cast_size_t_to_ulong(size);
1159
0
  return used;
1160
0
}
1161
1162
unsigned long get_size_from_delta(struct packed_git *p,
1163
          struct pack_window **w_curs,
1164
          off_t curpos)
1165
0
{
1166
0
  const unsigned char *data;
1167
0
  unsigned char delta_head[20], *in;
1168
0
  git_zstream stream;
1169
0
  int st;
1170
1171
0
  memset(&stream, 0, sizeof(stream));
1172
0
  stream.next_out = delta_head;
1173
0
  stream.avail_out = sizeof(delta_head);
1174
1175
0
  git_inflate_init(&stream);
1176
0
  do {
1177
0
    in = use_pack(p, w_curs, curpos, &stream.avail_in);
1178
0
    stream.next_in = in;
1179
    /*
1180
     * Note: the window section returned by use_pack() must be
1181
     * available throughout git_inflate()'s unlocked execution. To
1182
     * ensure no other thread will modify the window in the
1183
     * meantime, we rely on the packed_window.inuse_cnt. This
1184
     * counter is incremented before window reading and checked
1185
     * before window disposal.
1186
     *
1187
     * Other worrying sections could be the call to close_pack_fd(),
1188
     * which can close packs even with in-use windows, and to
1189
     * odb_reprepare(). Regarding the former, mmap doc says:
1190
     * "closing the file descriptor does not unmap the region". And
1191
     * for the latter, it won't re-open already available packs.
1192
     */
1193
0
    obj_read_unlock();
1194
0
    st = git_inflate(&stream, Z_FINISH);
1195
0
    obj_read_lock();
1196
0
    curpos += stream.next_in - in;
1197
0
  } while ((st == Z_OK || st == Z_BUF_ERROR) &&
1198
0
     stream.total_out < sizeof(delta_head));
1199
0
  git_inflate_end(&stream);
1200
0
  if ((st != Z_STREAM_END) && stream.total_out != sizeof(delta_head)) {
1201
0
    error("delta data unpack-initial failed");
1202
0
    return 0;
1203
0
  }
1204
1205
  /* Examine the initial part of the delta to figure out
1206
   * the result size.
1207
   */
1208
0
  data = delta_head;
1209
1210
  /* ignore base size */
1211
0
  get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1212
1213
  /* Read the result size */
1214
0
  return get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1215
0
}
1216
1217
int unpack_object_header(struct packed_git *p,
1218
       struct pack_window **w_curs,
1219
       off_t *curpos,
1220
       unsigned long *sizep)
1221
0
{
1222
0
  unsigned char *base;
1223
0
  unsigned long left;
1224
0
  unsigned long used;
1225
0
  enum object_type type;
1226
1227
  /* use_pack() assures us we have [base, base + 20) available
1228
   * as a range that we can look at.  (Its actually the hash
1229
   * size that is assured.)  With our object header encoding
1230
   * the maximum deflated object size is 2^137, which is just
1231
   * insane, so we know won't exceed what we have been given.
1232
   */
1233
0
  base = use_pack(p, w_curs, *curpos, &left);
1234
0
  used = unpack_object_header_buffer(base, left, &type, sizep);
1235
0
  if (!used) {
1236
0
    type = OBJ_BAD;
1237
0
  } else
1238
0
    *curpos += used;
1239
1240
0
  return type;
1241
0
}
1242
1243
void mark_bad_packed_object(struct packed_git *p, const struct object_id *oid)
1244
0
{
1245
0
  oidset_insert(&p->bad_objects, oid);
1246
0
}
1247
1248
const struct packed_git *has_packed_and_bad(struct repository *r,
1249
              const struct object_id *oid)
1250
0
{
1251
0
  struct odb_source *source;
1252
1253
0
  for (source = r->objects->sources; source; source = source->next) {
1254
0
    struct odb_source_files *files = odb_source_files_downcast(source);
1255
0
    struct packfile_list_entry *e;
1256
1257
0
    for (e = files->packed->packs.head; e; e = e->next)
1258
0
      if (oidset_contains(&e->pack->bad_objects, oid))
1259
0
        return e->pack;
1260
0
  }
1261
1262
0
  return NULL;
1263
0
}
1264
1265
off_t get_delta_base(struct packed_git *p,
1266
         struct pack_window **w_curs,
1267
         off_t *curpos,
1268
         enum object_type type,
1269
         off_t delta_obj_offset)
1270
0
{
1271
0
  unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
1272
0
  off_t base_offset;
1273
1274
  /* use_pack() assured us we have [base_info, base_info + 20)
1275
   * as a range that we can look at without walking off the
1276
   * end of the mapped window.  Its actually the hash size
1277
   * that is assured.  An OFS_DELTA longer than the hash size
1278
   * is stupid, as then a REF_DELTA would be smaller to store.
1279
   */
1280
0
  if (type == OBJ_OFS_DELTA) {
1281
0
    unsigned used = 0;
1282
0
    unsigned char c = base_info[used++];
1283
0
    base_offset = c & 127;
1284
0
    while (c & 128) {
1285
0
      base_offset += 1;
1286
0
      if (!base_offset || MSB(base_offset, 7))
1287
0
        return 0;  /* overflow */
1288
0
      c = base_info[used++];
1289
0
      base_offset = (base_offset << 7) + (c & 127);
1290
0
    }
1291
0
    base_offset = delta_obj_offset - base_offset;
1292
0
    if (base_offset <= 0 || base_offset >= delta_obj_offset)
1293
0
      return 0;  /* out of bound */
1294
0
    *curpos += used;
1295
0
  } else if (type == OBJ_REF_DELTA) {
1296
    /* The base entry _must_ be in the same pack */
1297
0
    struct object_id oid;
1298
0
    oidread(&oid, base_info, p->repo->hash_algo);
1299
0
    base_offset = find_pack_entry_one(&oid, p);
1300
0
    *curpos += p->repo->hash_algo->rawsz;
1301
0
  } else
1302
0
    die("I am totally screwed");
1303
0
  return base_offset;
1304
0
}
1305
1306
/*
1307
 * Like get_delta_base above, but we return the sha1 instead of the pack
1308
 * offset. This means it is cheaper for REF deltas (we do not have to do
1309
 * the final object lookup), but more expensive for OFS deltas (we
1310
 * have to load the revidx to convert the offset back into a sha1).
1311
 */
1312
static int get_delta_base_oid(struct packed_git *p,
1313
            struct pack_window **w_curs,
1314
            off_t curpos,
1315
            struct object_id *oid,
1316
            enum object_type type,
1317
            off_t delta_obj_offset)
1318
0
{
1319
0
  if (type == OBJ_REF_DELTA) {
1320
0
    unsigned char *base = use_pack(p, w_curs, curpos, NULL);
1321
0
    oidread(oid, base, p->repo->hash_algo);
1322
0
    return 0;
1323
0
  } else if (type == OBJ_OFS_DELTA) {
1324
0
    uint32_t base_pos;
1325
0
    off_t base_offset = get_delta_base(p, w_curs, &curpos,
1326
0
               type, delta_obj_offset);
1327
1328
0
    if (!base_offset)
1329
0
      return -1;
1330
1331
0
    if (offset_to_pack_pos(p, base_offset, &base_pos) < 0)
1332
0
      return -1;
1333
1334
0
    return nth_packed_object_id(oid, p,
1335
0
              pack_pos_to_index(p, base_pos));
1336
0
  } else
1337
0
    return -1;
1338
0
}
1339
1340
static int retry_bad_packed_offset(struct repository *r,
1341
           struct packed_git *p,
1342
           off_t obj_offset)
1343
0
{
1344
0
  int type;
1345
0
  uint32_t pos;
1346
0
  struct object_id oid;
1347
0
  if (offset_to_pack_pos(p, obj_offset, &pos) < 0)
1348
0
    return OBJ_BAD;
1349
0
  nth_packed_object_id(&oid, p, pack_pos_to_index(p, pos));
1350
0
  mark_bad_packed_object(p, &oid);
1351
0
  type = odb_read_object_info(r->objects, &oid, NULL);
1352
0
  if (type <= OBJ_NONE)
1353
0
    return OBJ_BAD;
1354
0
  return type;
1355
0
}
1356
1357
0
#define POI_STACK_PREALLOC 64
1358
1359
static enum object_type packed_to_object_type(struct repository *r,
1360
                struct packed_git *p,
1361
                off_t obj_offset,
1362
                enum object_type type,
1363
                struct pack_window **w_curs,
1364
                off_t curpos)
1365
0
{
1366
0
  off_t small_poi_stack[POI_STACK_PREALLOC];
1367
0
  off_t *poi_stack = small_poi_stack;
1368
0
  int poi_stack_nr = 0, poi_stack_alloc = POI_STACK_PREALLOC;
1369
1370
0
  while (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
1371
0
    off_t base_offset;
1372
0
    unsigned long size;
1373
    /* Push the object we're going to leave behind */
1374
0
    if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {
1375
0
      poi_stack_alloc = alloc_nr(poi_stack_nr);
1376
0
      ALLOC_ARRAY(poi_stack, poi_stack_alloc);
1377
0
      COPY_ARRAY(poi_stack, small_poi_stack, poi_stack_nr);
1378
0
    } else {
1379
0
      ALLOC_GROW(poi_stack, poi_stack_nr+1, poi_stack_alloc);
1380
0
    }
1381
0
    poi_stack[poi_stack_nr++] = obj_offset;
1382
    /* If parsing the base offset fails, just unwind */
1383
0
    base_offset = get_delta_base(p, w_curs, &curpos, type, obj_offset);
1384
0
    if (!base_offset)
1385
0
      goto unwind;
1386
0
    curpos = obj_offset = base_offset;
1387
0
    type = unpack_object_header(p, w_curs, &curpos, &size);
1388
0
    if (type <= OBJ_NONE) {
1389
      /* If getting the base itself fails, we first
1390
       * retry the base, otherwise unwind */
1391
0
      type = retry_bad_packed_offset(r, p, base_offset);
1392
0
      if (type > OBJ_NONE)
1393
0
        goto out;
1394
0
      goto unwind;
1395
0
    }
1396
0
  }
1397
1398
0
  switch (type) {
1399
0
  case OBJ_BAD:
1400
0
  case OBJ_COMMIT:
1401
0
  case OBJ_TREE:
1402
0
  case OBJ_BLOB:
1403
0
  case OBJ_TAG:
1404
0
    break;
1405
0
  default:
1406
0
    error("unknown object type %i at offset %"PRIuMAX" in %s",
1407
0
          type, (uintmax_t)obj_offset, p->pack_name);
1408
0
    type = OBJ_BAD;
1409
0
  }
1410
1411
0
out:
1412
0
  if (poi_stack != small_poi_stack)
1413
0
    free(poi_stack);
1414
0
  return type;
1415
1416
0
unwind:
1417
0
  while (poi_stack_nr) {
1418
0
    obj_offset = poi_stack[--poi_stack_nr];
1419
0
    type = retry_bad_packed_offset(r, p, obj_offset);
1420
0
    if (type > OBJ_NONE)
1421
0
      goto out;
1422
0
  }
1423
0
  type = OBJ_BAD;
1424
0
  goto out;
1425
0
}
1426
1427
static struct hashmap delta_base_cache;
1428
static size_t delta_base_cached;
1429
1430
static LIST_HEAD(delta_base_cache_lru);
1431
1432
struct delta_base_cache_key {
1433
  struct packed_git *p;
1434
  off_t base_offset;
1435
};
1436
1437
struct delta_base_cache_entry {
1438
  struct hashmap_entry ent;
1439
  struct delta_base_cache_key key;
1440
  struct list_head lru;
1441
  void *data;
1442
  unsigned long size;
1443
  enum object_type type;
1444
};
1445
1446
static unsigned int pack_entry_hash(struct packed_git *p, off_t base_offset)
1447
0
{
1448
0
  unsigned int hash;
1449
1450
0
  hash = (unsigned int)(intptr_t)p + (unsigned int)base_offset;
1451
0
  hash += (hash >> 8) + (hash >> 16);
1452
0
  return hash;
1453
0
}
1454
1455
static struct delta_base_cache_entry *
1456
get_delta_base_cache_entry(struct packed_git *p, off_t base_offset)
1457
0
{
1458
0
  struct hashmap_entry entry, *e;
1459
0
  struct delta_base_cache_key key;
1460
1461
0
  if (!delta_base_cache.cmpfn)
1462
0
    return NULL;
1463
1464
0
  hashmap_entry_init(&entry, pack_entry_hash(p, base_offset));
1465
0
  key.p = p;
1466
0
  key.base_offset = base_offset;
1467
0
  e = hashmap_get(&delta_base_cache, &entry, &key);
1468
0
  return e ? container_of(e, struct delta_base_cache_entry, ent) : NULL;
1469
0
}
1470
1471
static int delta_base_cache_key_eq(const struct delta_base_cache_key *a,
1472
           const struct delta_base_cache_key *b)
1473
0
{
1474
0
  return a->p == b->p && a->base_offset == b->base_offset;
1475
0
}
1476
1477
static int delta_base_cache_hash_cmp(const void *cmp_data UNUSED,
1478
             const struct hashmap_entry *va,
1479
             const struct hashmap_entry *vb,
1480
             const void *vkey)
1481
0
{
1482
0
  const struct delta_base_cache_entry *a, *b;
1483
0
  const struct delta_base_cache_key *key = vkey;
1484
1485
0
  a = container_of(va, const struct delta_base_cache_entry, ent);
1486
0
  b = container_of(vb, const struct delta_base_cache_entry, ent);
1487
1488
0
  if (key)
1489
0
    return !delta_base_cache_key_eq(&a->key, key);
1490
0
  else
1491
0
    return !delta_base_cache_key_eq(&a->key, &b->key);
1492
0
}
1493
1494
static int in_delta_base_cache(struct packed_git *p, off_t base_offset)
1495
0
{
1496
0
  return !!get_delta_base_cache_entry(p, base_offset);
1497
0
}
1498
1499
/*
1500
 * Remove the entry from the cache, but do _not_ free the associated
1501
 * entry data. The caller takes ownership of the "data" buffer, and
1502
 * should copy out any fields it wants before detaching.
1503
 */
1504
static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)
1505
0
{
1506
0
  hashmap_remove(&delta_base_cache, &ent->ent, &ent->key);
1507
0
  list_del(&ent->lru);
1508
0
  delta_base_cached -= ent->size;
1509
0
  free(ent);
1510
0
}
1511
1512
static void *cache_or_unpack_entry(struct repository *r, struct packed_git *p,
1513
           off_t base_offset, unsigned long *base_size,
1514
           enum object_type *type)
1515
0
{
1516
0
  struct delta_base_cache_entry *ent;
1517
1518
0
  ent = get_delta_base_cache_entry(p, base_offset);
1519
0
  if (!ent)
1520
0
    return unpack_entry(r, p, base_offset, type, base_size);
1521
1522
0
  if (type)
1523
0
    *type = ent->type;
1524
0
  if (base_size)
1525
0
    *base_size = ent->size;
1526
0
  return xmemdupz(ent->data, ent->size);
1527
0
}
1528
1529
static inline void release_delta_base_cache(struct delta_base_cache_entry *ent)
1530
0
{
1531
0
  free(ent->data);
1532
0
  detach_delta_base_cache_entry(ent);
1533
0
}
1534
1535
void clear_delta_base_cache(void)
1536
0
{
1537
0
  struct list_head *lru, *tmp;
1538
0
  list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
1539
0
    struct delta_base_cache_entry *entry =
1540
0
      list_entry(lru, struct delta_base_cache_entry, lru);
1541
0
    release_delta_base_cache(entry);
1542
0
  }
1543
0
}
1544
1545
static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1546
         void *base, unsigned long base_size,
1547
         unsigned long delta_base_cache_limit,
1548
         enum object_type type)
1549
0
{
1550
0
  struct delta_base_cache_entry *ent;
1551
0
  struct list_head *lru, *tmp;
1552
1553
  /*
1554
   * Check required to avoid redundant entries when more than one thread
1555
   * is unpacking the same object, in unpack_entry() (since its phases I
1556
   * and III might run concurrently across multiple threads).
1557
   */
1558
0
  if (in_delta_base_cache(p, base_offset)) {
1559
0
    free(base);
1560
0
    return;
1561
0
  }
1562
1563
0
  delta_base_cached += base_size;
1564
1565
0
  list_for_each_safe(lru, tmp, &delta_base_cache_lru) {
1566
0
    struct delta_base_cache_entry *f =
1567
0
      list_entry(lru, struct delta_base_cache_entry, lru);
1568
0
    if (delta_base_cached <= delta_base_cache_limit)
1569
0
      break;
1570
0
    release_delta_base_cache(f);
1571
0
  }
1572
1573
0
  ent = xmalloc(sizeof(*ent));
1574
0
  ent->key.p = p;
1575
0
  ent->key.base_offset = base_offset;
1576
0
  ent->type = type;
1577
0
  ent->data = base;
1578
0
  ent->size = base_size;
1579
0
  list_add_tail(&ent->lru, &delta_base_cache_lru);
1580
1581
0
  if (!delta_base_cache.cmpfn)
1582
0
    hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
1583
0
  hashmap_entry_init(&ent->ent, pack_entry_hash(p, base_offset));
1584
0
  hashmap_add(&delta_base_cache, &ent->ent);
1585
0
}
1586
1587
static int packed_object_info_with_index_pos(struct packed_git *p, off_t obj_offset,
1588
               uint32_t *maybe_index_pos, struct object_info *oi)
1589
0
{
1590
0
  struct pack_window *w_curs = NULL;
1591
0
  unsigned long size;
1592
0
  off_t curpos = obj_offset;
1593
0
  enum object_type type = OBJ_NONE;
1594
0
  uint32_t pack_pos;
1595
0
  int ret;
1596
1597
  /*
1598
   * We always get the representation type, but only convert it to
1599
   * a "real" type later if the caller is interested.
1600
   */
1601
0
  if (oi->contentp) {
1602
0
    *oi->contentp = cache_or_unpack_entry(p->repo, p, obj_offset, oi->sizep,
1603
0
                  &type);
1604
0
    if (!*oi->contentp)
1605
0
      type = OBJ_BAD;
1606
0
  } else if (oi->sizep || oi->typep || oi->delta_base_oid) {
1607
0
    type = unpack_object_header(p, &w_curs, &curpos, &size);
1608
0
  }
1609
1610
0
  if (!oi->contentp && oi->sizep) {
1611
0
    if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
1612
0
      off_t tmp_pos = curpos;
1613
0
      off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos,
1614
0
                 type, obj_offset);
1615
0
      if (!base_offset) {
1616
0
        ret = -1;
1617
0
        goto out;
1618
0
      }
1619
0
      *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos);
1620
0
      if (*oi->sizep == 0) {
1621
0
        ret = -1;
1622
0
        goto out;
1623
0
      }
1624
0
    } else {
1625
0
      *oi->sizep = size;
1626
0
    }
1627
0
  }
1628
1629
0
  if (oi->disk_sizep || (oi->mtimep && p->is_cruft)) {
1630
0
    if (offset_to_pack_pos(p, obj_offset, &pack_pos) < 0) {
1631
0
      error("could not find object at offset %"PRIuMAX" "
1632
0
            "in pack %s", (uintmax_t)obj_offset, p->pack_name);
1633
0
      ret = -1;
1634
0
      goto out;
1635
0
    }
1636
0
  }
1637
1638
0
  if (oi->disk_sizep)
1639
0
    *oi->disk_sizep = pack_pos_to_offset(p, pack_pos + 1) - obj_offset;
1640
1641
0
  if (oi->mtimep) {
1642
0
    if (p->is_cruft) {
1643
0
      uint32_t index_pos;
1644
1645
0
      if (load_pack_mtimes(p) < 0)
1646
0
        die(_("could not load .mtimes for cruft pack '%s'"),
1647
0
            pack_basename(p));
1648
1649
0
      if (maybe_index_pos)
1650
0
        index_pos = *maybe_index_pos;
1651
0
      else
1652
0
        index_pos = pack_pos_to_index(p, pack_pos);
1653
1654
0
      *oi->mtimep = nth_packed_mtime(p, index_pos);
1655
0
    } else {
1656
0
      *oi->mtimep = p->mtime;
1657
0
    }
1658
0
  }
1659
1660
0
  if (oi->typep) {
1661
0
    enum object_type ptot;
1662
0
    ptot = packed_to_object_type(p->repo, p, obj_offset,
1663
0
               type, &w_curs, curpos);
1664
0
    if (oi->typep)
1665
0
      *oi->typep = ptot;
1666
0
    if (ptot < 0) {
1667
0
      ret = -1;
1668
0
      goto out;
1669
0
    }
1670
0
  }
1671
1672
0
  if (oi->delta_base_oid) {
1673
0
    if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {
1674
0
      if (get_delta_base_oid(p, &w_curs, curpos,
1675
0
                 oi->delta_base_oid,
1676
0
                 type, obj_offset) < 0) {
1677
0
        ret = -1;
1678
0
        goto out;
1679
0
      }
1680
0
    } else
1681
0
      oidclr(oi->delta_base_oid, p->repo->hash_algo);
1682
0
  }
1683
1684
0
  oi->whence = OI_PACKED;
1685
0
  oi->u.packed.offset = obj_offset;
1686
0
  oi->u.packed.pack = p;
1687
1688
0
  switch (type) {
1689
0
  case OBJ_NONE:
1690
0
    oi->u.packed.type = PACKED_OBJECT_TYPE_UNKNOWN;
1691
0
    break;
1692
0
  case OBJ_REF_DELTA:
1693
0
    oi->u.packed.type = PACKED_OBJECT_TYPE_REF_DELTA;
1694
0
    break;
1695
0
  case OBJ_OFS_DELTA:
1696
0
    oi->u.packed.type = PACKED_OBJECT_TYPE_OFS_DELTA;
1697
0
    break;
1698
0
  default:
1699
0
    oi->u.packed.type = PACKED_OBJECT_TYPE_FULL;
1700
0
    break;
1701
0
  }
1702
1703
0
  ret = 0;
1704
1705
0
out:
1706
0
  unuse_pack(&w_curs);
1707
0
  return ret;
1708
0
}
1709
1710
int packed_object_info(struct packed_git *p, off_t obj_offset,
1711
           struct object_info *oi)
1712
0
{
1713
0
  return packed_object_info_with_index_pos(p, obj_offset, NULL, oi);
1714
0
}
1715
1716
static void *unpack_compressed_entry(struct packed_git *p,
1717
            struct pack_window **w_curs,
1718
            off_t curpos,
1719
            unsigned long size)
1720
0
{
1721
0
  int st;
1722
0
  git_zstream stream;
1723
0
  unsigned char *buffer, *in;
1724
1725
0
  buffer = xmallocz_gently(size);
1726
0
  if (!buffer)
1727
0
    return NULL;
1728
0
  memset(&stream, 0, sizeof(stream));
1729
0
  stream.next_out = buffer;
1730
0
  stream.avail_out = size + 1;
1731
1732
0
  git_inflate_init(&stream);
1733
0
  do {
1734
0
    in = use_pack(p, w_curs, curpos, &stream.avail_in);
1735
0
    stream.next_in = in;
1736
    /*
1737
     * Note: we must ensure the window section returned by
1738
     * use_pack() will be available throughout git_inflate()'s
1739
     * unlocked execution. Please refer to the comment at
1740
     * get_size_from_delta() to see how this is done.
1741
     */
1742
0
    obj_read_unlock();
1743
0
    st = git_inflate(&stream, Z_FINISH);
1744
0
    obj_read_lock();
1745
0
    if (!stream.avail_out)
1746
0
      break; /* the payload is larger than it should be */
1747
0
    curpos += stream.next_in - in;
1748
0
  } while (st == Z_OK || st == Z_BUF_ERROR);
1749
0
  git_inflate_end(&stream);
1750
0
  if ((st != Z_STREAM_END) || stream.total_out != size) {
1751
0
    free(buffer);
1752
0
    return NULL;
1753
0
  }
1754
1755
  /* versions of zlib can clobber unconsumed portion of outbuf */
1756
0
  buffer[size] = '\0';
1757
1758
0
  return buffer;
1759
0
}
1760
1761
static void write_pack_access_log(struct packed_git *p, off_t obj_offset)
1762
0
{
1763
0
  static struct trace_key pack_access = TRACE_KEY_INIT(PACK_ACCESS);
1764
0
  trace_printf_key(&pack_access, "%s %"PRIuMAX"\n",
1765
0
       p->pack_name, (uintmax_t)obj_offset);
1766
0
}
1767
1768
int do_check_packed_object_crc;
1769
1770
0
#define UNPACK_ENTRY_STACK_PREALLOC 64
1771
struct unpack_entry_stack_ent {
1772
  off_t obj_offset;
1773
  off_t curpos;
1774
  unsigned long size;
1775
};
1776
1777
void *unpack_entry(struct repository *r, struct packed_git *p, off_t obj_offset,
1778
       enum object_type *final_type, unsigned long *final_size)
1779
0
{
1780
0
  struct pack_window *w_curs = NULL;
1781
0
  off_t curpos = obj_offset;
1782
0
  void *data = NULL;
1783
0
  unsigned long size;
1784
0
  enum object_type type;
1785
0
  struct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];
1786
0
  struct unpack_entry_stack_ent *delta_stack = small_delta_stack;
1787
0
  int delta_stack_nr = 0, delta_stack_alloc = UNPACK_ENTRY_STACK_PREALLOC;
1788
0
  int base_from_cache = 0;
1789
1790
0
  prepare_repo_settings(p->repo);
1791
1792
0
  write_pack_access_log(p, obj_offset);
1793
1794
  /* PHASE 1: drill down to the innermost base object */
1795
0
  for (;;) {
1796
0
    off_t base_offset;
1797
0
    int i;
1798
0
    struct delta_base_cache_entry *ent;
1799
1800
0
    ent = get_delta_base_cache_entry(p, curpos);
1801
0
    if (ent) {
1802
0
      type = ent->type;
1803
0
      data = ent->data;
1804
0
      size = ent->size;
1805
0
      detach_delta_base_cache_entry(ent);
1806
0
      base_from_cache = 1;
1807
0
      break;
1808
0
    }
1809
1810
0
    if (do_check_packed_object_crc && p->index_version > 1) {
1811
0
      uint32_t pack_pos, index_pos;
1812
0
      off_t len;
1813
1814
0
      if (offset_to_pack_pos(p, obj_offset, &pack_pos) < 0) {
1815
0
        error("could not find object at offset %"PRIuMAX" in pack %s",
1816
0
              (uintmax_t)obj_offset, p->pack_name);
1817
0
        data = NULL;
1818
0
        goto out;
1819
0
      }
1820
1821
0
      len = pack_pos_to_offset(p, pack_pos + 1) - obj_offset;
1822
0
      index_pos = pack_pos_to_index(p, pack_pos);
1823
0
      if (check_pack_crc(p, &w_curs, obj_offset, len, index_pos)) {
1824
0
        struct object_id oid;
1825
0
        nth_packed_object_id(&oid, p, index_pos);
1826
0
        error("bad packed object CRC for %s",
1827
0
              oid_to_hex(&oid));
1828
0
        mark_bad_packed_object(p, &oid);
1829
0
        data = NULL;
1830
0
        goto out;
1831
0
      }
1832
0
    }
1833
1834
0
    type = unpack_object_header(p, &w_curs, &curpos, &size);
1835
0
    if (type != OBJ_OFS_DELTA && type != OBJ_REF_DELTA)
1836
0
      break;
1837
1838
0
    base_offset = get_delta_base(p, &w_curs, &curpos, type, obj_offset);
1839
0
    if (!base_offset) {
1840
0
      error("failed to validate delta base reference "
1841
0
            "at offset %"PRIuMAX" from %s",
1842
0
            (uintmax_t)curpos, p->pack_name);
1843
      /* bail to phase 2, in hopes of recovery */
1844
0
      data = NULL;
1845
0
      break;
1846
0
    }
1847
1848
    /* push object, proceed to base */
1849
0
    if (delta_stack_nr >= delta_stack_alloc
1850
0
        && delta_stack == small_delta_stack) {
1851
0
      delta_stack_alloc = alloc_nr(delta_stack_nr);
1852
0
      ALLOC_ARRAY(delta_stack, delta_stack_alloc);
1853
0
      COPY_ARRAY(delta_stack, small_delta_stack,
1854
0
           delta_stack_nr);
1855
0
    } else {
1856
0
      ALLOC_GROW(delta_stack, delta_stack_nr+1, delta_stack_alloc);
1857
0
    }
1858
0
    i = delta_stack_nr++;
1859
0
    delta_stack[i].obj_offset = obj_offset;
1860
0
    delta_stack[i].curpos = curpos;
1861
0
    delta_stack[i].size = size;
1862
1863
0
    curpos = obj_offset = base_offset;
1864
0
  }
1865
1866
  /* PHASE 2: handle the base */
1867
0
  switch (type) {
1868
0
  case OBJ_OFS_DELTA:
1869
0
  case OBJ_REF_DELTA:
1870
0
    if (data)
1871
0
      BUG("unpack_entry: left loop at a valid delta");
1872
0
    break;
1873
0
  case OBJ_COMMIT:
1874
0
  case OBJ_TREE:
1875
0
  case OBJ_BLOB:
1876
0
  case OBJ_TAG:
1877
0
    if (!base_from_cache)
1878
0
      data = unpack_compressed_entry(p, &w_curs, curpos, size);
1879
0
    break;
1880
0
  default:
1881
0
    data = NULL;
1882
0
    error("unknown object type %i at offset %"PRIuMAX" in %s",
1883
0
          type, (uintmax_t)obj_offset, p->pack_name);
1884
0
  }
1885
1886
  /* PHASE 3: apply deltas in order */
1887
1888
  /* invariants:
1889
   *   'data' holds the base data, or NULL if there was corruption
1890
   */
1891
0
  while (delta_stack_nr) {
1892
0
    void *delta_data;
1893
0
    void *base = data;
1894
0
    void *external_base = NULL;
1895
0
    unsigned long delta_size, base_size = size;
1896
0
    int i;
1897
0
    off_t base_obj_offset = obj_offset;
1898
1899
0
    data = NULL;
1900
1901
0
    if (!base) {
1902
      /*
1903
       * We're probably in deep shit, but let's try to fetch
1904
       * the required base anyway from another pack or loose.
1905
       * This is costly but should happen only in the presence
1906
       * of a corrupted pack, and is better than failing outright.
1907
       */
1908
0
      uint32_t pos;
1909
0
      struct object_id base_oid;
1910
0
      if (!(offset_to_pack_pos(p, obj_offset, &pos))) {
1911
0
        struct object_info oi = OBJECT_INFO_INIT;
1912
1913
0
        nth_packed_object_id(&base_oid, p,
1914
0
                 pack_pos_to_index(p, pos));
1915
0
        error("failed to read delta base object %s"
1916
0
              " at offset %"PRIuMAX" from %s",
1917
0
              oid_to_hex(&base_oid), (uintmax_t)obj_offset,
1918
0
              p->pack_name);
1919
0
        mark_bad_packed_object(p, &base_oid);
1920
1921
0
        oi.typep = &type;
1922
0
        oi.sizep = &base_size;
1923
0
        oi.contentp = &base;
1924
0
        if (odb_read_object_info_extended(r->objects, &base_oid,
1925
0
                  &oi, 0) < 0)
1926
0
          base = NULL;
1927
1928
0
        external_base = base;
1929
0
      }
1930
0
    }
1931
1932
0
    i = --delta_stack_nr;
1933
0
    obj_offset = delta_stack[i].obj_offset;
1934
0
    curpos = delta_stack[i].curpos;
1935
0
    delta_size = delta_stack[i].size;
1936
1937
0
    if (!base)
1938
0
      continue;
1939
1940
0
    delta_data = unpack_compressed_entry(p, &w_curs, curpos, delta_size);
1941
1942
0
    if (!delta_data) {
1943
0
      error("failed to unpack compressed delta "
1944
0
            "at offset %"PRIuMAX" from %s",
1945
0
            (uintmax_t)curpos, p->pack_name);
1946
0
      data = NULL;
1947
0
    } else {
1948
0
      data = patch_delta(base, base_size, delta_data,
1949
0
             delta_size, &size);
1950
1951
      /*
1952
       * We could not apply the delta; warn the user, but
1953
       * keep going. Our failure will be noticed either in
1954
       * the next iteration of the loop, or if this is the
1955
       * final delta, in the caller when we return NULL.
1956
       * Those code paths will take care of making a more
1957
       * explicit warning and retrying with another copy of
1958
       * the object.
1959
       */
1960
0
      if (!data)
1961
0
        error("failed to apply delta");
1962
0
    }
1963
1964
    /*
1965
     * We delay adding `base` to the cache until the end of the loop
1966
     * because unpack_compressed_entry() momentarily releases the
1967
     * obj_read_mutex, giving another thread the chance to access
1968
     * the cache. Therefore, if `base` was already there, this other
1969
     * thread could free() it (e.g. to make space for another entry)
1970
     * before we are done using it.
1971
     */
1972
0
    if (!external_base)
1973
0
      add_delta_base_cache(p, base_obj_offset, base, base_size,
1974
0
               p->repo->settings.delta_base_cache_limit,
1975
0
               type);
1976
1977
0
    free(delta_data);
1978
0
    free(external_base);
1979
0
  }
1980
1981
0
  if (final_type)
1982
0
    *final_type = type;
1983
0
  if (final_size)
1984
0
    *final_size = size;
1985
1986
0
out:
1987
0
  unuse_pack(&w_curs);
1988
1989
0
  if (delta_stack != small_delta_stack)
1990
0
    free(delta_stack);
1991
1992
0
  return data;
1993
0
}
1994
1995
int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result)
1996
0
{
1997
0
  const unsigned char *index_fanout = p->index_data;
1998
0
  const unsigned char *index_lookup;
1999
0
  const unsigned int hashsz = p->repo->hash_algo->rawsz;
2000
0
  int index_lookup_width;
2001
2002
0
  if (!index_fanout)
2003
0
    BUG("bsearch_pack called without a valid pack-index");
2004
2005
0
  index_lookup = index_fanout + 4 * 256;
2006
0
  if (p->index_version == 1) {
2007
0
    index_lookup_width = hashsz + 4;
2008
0
    index_lookup += 4;
2009
0
  } else {
2010
0
    index_lookup_width = hashsz;
2011
0
    index_fanout += 8;
2012
0
    index_lookup += 8;
2013
0
  }
2014
2015
0
  return bsearch_hash(oid->hash, (const uint32_t*)index_fanout,
2016
0
          index_lookup, index_lookup_width, result);
2017
0
}
2018
2019
int nth_packed_object_id(struct object_id *oid,
2020
       struct packed_git *p,
2021
       uint32_t n)
2022
0
{
2023
0
  const unsigned char *index = p->index_data;
2024
0
  const unsigned int hashsz = p->repo->hash_algo->rawsz;
2025
0
  if (!index) {
2026
0
    if (open_pack_index(p))
2027
0
      return -1;
2028
0
    index = p->index_data;
2029
0
  }
2030
0
  if (n >= p->num_objects)
2031
0
    return -1;
2032
0
  index += 4 * 256;
2033
0
  if (p->index_version == 1) {
2034
0
    oidread(oid, index + st_add(st_mult(hashsz + 4, n), 4),
2035
0
      p->repo->hash_algo);
2036
0
  } else {
2037
0
    index += 8;
2038
0
    oidread(oid, index + st_mult(hashsz, n), p->repo->hash_algo);
2039
0
  }
2040
0
  return 0;
2041
0
}
2042
2043
void check_pack_index_ptr(const struct packed_git *p, const void *vptr)
2044
0
{
2045
0
  const unsigned char *ptr = vptr;
2046
0
  const unsigned char *start = p->index_data;
2047
0
  const unsigned char *end = start + p->index_size;
2048
0
  if (ptr < start)
2049
0
    die(_("offset before start of pack index for %s (corrupt index?)"),
2050
0
        p->pack_name);
2051
  /* No need to check for underflow; .idx files must be at least 8 bytes */
2052
0
  if (ptr >= end - 8)
2053
0
    die(_("offset beyond end of pack index for %s (truncated index?)"),
2054
0
        p->pack_name);
2055
0
}
2056
2057
off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
2058
0
{
2059
0
  const unsigned char *index = p->index_data;
2060
0
  const unsigned int hashsz = p->repo->hash_algo->rawsz;
2061
0
  index += 4 * 256;
2062
0
  if (p->index_version == 1) {
2063
0
    return ntohl(*((uint32_t *)(index + st_mult(hashsz + 4, n))));
2064
0
  } else {
2065
0
    uint32_t off;
2066
0
    index += st_add(8, st_mult(p->num_objects, hashsz + 4));
2067
0
    off = ntohl(*((uint32_t *)(index + st_mult(4, n))));
2068
0
    if (!(off & 0x80000000))
2069
0
      return off;
2070
0
    index += st_add(st_mult(p->num_objects, 4),
2071
0
        st_mult(off & 0x7fffffff, 8));
2072
0
    check_pack_index_ptr(p, index);
2073
0
    return get_be64(index);
2074
0
  }
2075
0
}
2076
2077
off_t find_pack_entry_one(const struct object_id *oid,
2078
        struct packed_git *p)
2079
0
{
2080
0
  const unsigned char *index = p->index_data;
2081
0
  uint32_t result;
2082
2083
0
  if (!index) {
2084
0
    if (open_pack_index(p))
2085
0
      return 0;
2086
0
  }
2087
2088
0
  if (bsearch_pack(oid, p, &result))
2089
0
    return nth_packed_object_offset(p, result);
2090
0
  return 0;
2091
0
}
2092
2093
int is_pack_valid(struct packed_git *p)
2094
0
{
2095
  /* An already open pack is known to be valid. */
2096
0
  if (p->pack_fd != -1)
2097
0
    return 1;
2098
2099
  /* If the pack has one window completely covering the
2100
   * file size, the pack is known to be valid even if
2101
   * the descriptor is not currently open.
2102
   */
2103
0
  if (p->windows) {
2104
0
    struct pack_window *w = p->windows;
2105
2106
0
    if (!w->offset && w->len == p->pack_size)
2107
0
      return 1;
2108
0
  }
2109
2110
  /* Force the pack to open to prove its valid. */
2111
0
  return !open_packed_git(p);
2112
0
}
2113
2114
static int fill_pack_entry(const struct object_id *oid,
2115
         struct pack_entry *e,
2116
         struct packed_git *p)
2117
0
{
2118
0
  off_t offset;
2119
2120
0
  if (oidset_size(&p->bad_objects) &&
2121
0
      oidset_contains(&p->bad_objects, oid))
2122
0
    return 0;
2123
2124
0
  offset = find_pack_entry_one(oid, p);
2125
0
  if (!offset)
2126
0
    return 0;
2127
2128
  /*
2129
   * We are about to tell the caller where they can locate the
2130
   * requested object.  We better make sure the packfile is
2131
   * still here and can be accessed before supplying that
2132
   * answer, as it may have been deleted since the index was
2133
   * loaded!
2134
   */
2135
0
  if (!is_pack_valid(p))
2136
0
    return 0;
2137
0
  e->offset = offset;
2138
0
  e->p = p;
2139
0
  return 1;
2140
0
}
2141
2142
static int find_pack_entry(struct packfile_store *store,
2143
         const struct object_id *oid,
2144
         struct pack_entry *e)
2145
0
{
2146
0
  struct packfile_list_entry *l;
2147
2148
0
  packfile_store_prepare(store);
2149
0
  if (store->midx && fill_midx_entry(store->midx, oid, e))
2150
0
    return 1;
2151
2152
0
  for (l = store->packs.head; l; l = l->next) {
2153
0
    struct packed_git *p = l->pack;
2154
2155
0
    if (!p->multi_pack_index && fill_pack_entry(oid, e, p)) {
2156
0
      if (!store->skip_mru_updates)
2157
0
        packfile_list_prepend(&store->packs, p);
2158
0
      return 1;
2159
0
    }
2160
0
  }
2161
2162
0
  return 0;
2163
0
}
2164
2165
int packfile_store_freshen_object(struct packfile_store *store,
2166
          const struct object_id *oid)
2167
0
{
2168
0
  struct pack_entry e;
2169
0
  if (!find_pack_entry(store, oid, &e))
2170
0
    return 0;
2171
0
  if (e.p->is_cruft)
2172
0
    return 0;
2173
0
  if (e.p->freshened)
2174
0
    return 1;
2175
0
  if (utime(e.p->pack_name, NULL))
2176
0
    return 0;
2177
0
  e.p->freshened = 1;
2178
0
  return 1;
2179
0
}
2180
2181
int packfile_store_read_object_info(struct packfile_store *store,
2182
            const struct object_id *oid,
2183
            struct object_info *oi,
2184
            enum object_info_flags flags)
2185
0
{
2186
0
  struct pack_entry e;
2187
0
  int ret;
2188
2189
  /*
2190
   * In case the first read didn't surface the object, we have to reload
2191
   * packfiles. This may cause us to discover new packfiles that have
2192
   * been added since the last time we have prepared the packfile store.
2193
   */
2194
0
  if (flags & OBJECT_INFO_SECOND_READ)
2195
0
    packfile_store_reprepare(store);
2196
2197
0
  if (!find_pack_entry(store, oid, &e))
2198
0
    return 1;
2199
2200
  /*
2201
   * We know that the caller doesn't actually need the
2202
   * information below, so return early.
2203
   */
2204
0
  if (!oi)
2205
0
    return 0;
2206
2207
0
  ret = packed_object_info(e.p, e.offset, oi);
2208
0
  if (ret < 0) {
2209
0
    mark_bad_packed_object(e.p, oid);
2210
0
    return -1;
2211
0
  }
2212
2213
0
  return 0;
2214
0
}
2215
2216
static void maybe_invalidate_kept_pack_cache(struct packfile_store *store,
2217
               unsigned flags)
2218
0
{
2219
0
  if (!store->kept_cache.packs)
2220
0
    return;
2221
0
  if (store->kept_cache.flags == flags)
2222
0
    return;
2223
0
  FREE_AND_NULL(store->kept_cache.packs);
2224
0
  store->kept_cache.flags = 0;
2225
0
}
2226
2227
struct packed_git **packfile_store_get_kept_pack_cache(struct packfile_store *store,
2228
                   unsigned flags)
2229
0
{
2230
0
  maybe_invalidate_kept_pack_cache(store, flags);
2231
2232
0
  if (!store->kept_cache.packs) {
2233
0
    struct packed_git **packs = NULL;
2234
0
    struct packfile_list_entry *e;
2235
0
    size_t nr = 0, alloc = 0;
2236
2237
    /*
2238
     * We want "all" packs here, because we need to cover ones that
2239
     * are used by a midx, as well. We need to look in every one of
2240
     * them (instead of the midx itself) to cover duplicates. It's
2241
     * possible that an object is found in two packs that the midx
2242
     * covers, one kept and one not kept, but the midx returns only
2243
     * the non-kept version.
2244
     */
2245
0
    for (e = packfile_store_get_packs(store); e; e = e->next) {
2246
0
      struct packed_git *p = e->pack;
2247
2248
0
      if ((p->pack_keep && (flags & KEPT_PACK_ON_DISK)) ||
2249
0
          (p->pack_keep_in_core && (flags & KEPT_PACK_IN_CORE))) {
2250
0
        ALLOC_GROW(packs, nr + 1, alloc);
2251
0
        packs[nr++] = p;
2252
0
      }
2253
0
    }
2254
0
    ALLOC_GROW(packs, nr + 1, alloc);
2255
0
    packs[nr] = NULL;
2256
2257
0
    store->kept_cache.packs = packs;
2258
0
    store->kept_cache.flags = flags;
2259
0
  }
2260
2261
0
  return store->kept_cache.packs;
2262
0
}
2263
2264
int has_object_pack(struct repository *r, const struct object_id *oid)
2265
0
{
2266
0
  struct odb_source *source;
2267
0
  struct pack_entry e;
2268
2269
0
  odb_prepare_alternates(r->objects);
2270
0
  for (source = r->objects->sources; source; source = source->next) {
2271
0
    struct odb_source_files *files = odb_source_files_downcast(source);
2272
0
    int ret = find_pack_entry(files->packed, oid, &e);
2273
0
    if (ret)
2274
0
      return ret;
2275
0
  }
2276
2277
0
  return 0;
2278
0
}
2279
2280
int has_object_kept_pack(struct repository *r, const struct object_id *oid,
2281
       unsigned flags)
2282
0
{
2283
0
  struct odb_source *source;
2284
0
  struct pack_entry e;
2285
2286
0
  for (source = r->objects->sources; source; source = source->next) {
2287
0
    struct odb_source_files *files = odb_source_files_downcast(source);
2288
0
    struct packed_git **cache;
2289
2290
0
    cache = packfile_store_get_kept_pack_cache(files->packed, flags);
2291
2292
0
    for (; *cache; cache++) {
2293
0
      struct packed_git *p = *cache;
2294
0
      if (fill_pack_entry(oid, &e, p))
2295
0
        return 1;
2296
0
    }
2297
0
  }
2298
2299
0
  return 0;
2300
0
}
2301
2302
int for_each_object_in_pack(struct packed_git *p,
2303
          each_packed_object_fn cb, void *data,
2304
          unsigned flags)
2305
0
{
2306
0
  uint32_t i;
2307
0
  int r = 0;
2308
2309
0
  if (flags & ODB_FOR_EACH_OBJECT_PACK_ORDER) {
2310
0
    if (load_pack_revindex(p->repo, p))
2311
0
      return -1;
2312
0
  }
2313
2314
0
  for (i = 0; i < p->num_objects; i++) {
2315
0
    uint32_t index_pos;
2316
0
    struct object_id oid;
2317
2318
    /*
2319
     * We are iterating "i" from 0 up to num_objects, but its
2320
     * meaning may be different, depending on the requested output
2321
     * order:
2322
     *
2323
     *   - in object-name order, it is the same as the index order
2324
     *     used by nth_packed_object_id(), so we can pass it
2325
     *     directly
2326
     *
2327
     *   - in pack-order, it is pack position, which we must
2328
     *     convert to an index position in order to get the oid.
2329
     */
2330
0
    if (flags & ODB_FOR_EACH_OBJECT_PACK_ORDER)
2331
0
      index_pos = pack_pos_to_index(p, i);
2332
0
    else
2333
0
      index_pos = i;
2334
2335
0
    if (nth_packed_object_id(&oid, p, index_pos) < 0)
2336
0
      return error("unable to get sha1 of object %u in %s",
2337
0
             index_pos, p->pack_name);
2338
2339
0
    r = cb(&oid, p, index_pos, data);
2340
0
    if (r)
2341
0
      break;
2342
0
  }
2343
0
  return r;
2344
0
}
2345
2346
struct packfile_store_for_each_object_wrapper_data {
2347
  struct packfile_store *store;
2348
  const struct object_info *request;
2349
  odb_for_each_object_cb cb;
2350
  void *cb_data;
2351
};
2352
2353
static int packfile_store_for_each_object_wrapper(const struct object_id *oid,
2354
              struct packed_git *pack,
2355
              uint32_t index_pos,
2356
              void *cb_data)
2357
0
{
2358
0
  struct packfile_store_for_each_object_wrapper_data *data = cb_data;
2359
2360
0
  if (data->request) {
2361
0
    off_t offset = nth_packed_object_offset(pack, index_pos);
2362
0
    struct object_info oi = *data->request;
2363
2364
0
    if (packed_object_info_with_index_pos(pack, offset,
2365
0
                  &index_pos, &oi) < 0) {
2366
0
      mark_bad_packed_object(pack, oid);
2367
0
      return -1;
2368
0
    }
2369
2370
0
    return data->cb(oid, &oi, data->cb_data);
2371
0
  } else {
2372
0
    return data->cb(oid, NULL, data->cb_data);
2373
0
  }
2374
0
}
2375
2376
int packfile_store_for_each_object(struct packfile_store *store,
2377
           const struct object_info *request,
2378
           odb_for_each_object_cb cb,
2379
           void *cb_data,
2380
           unsigned flags)
2381
0
{
2382
0
  struct packfile_store_for_each_object_wrapper_data data = {
2383
0
    .store = store,
2384
0
    .request = request,
2385
0
    .cb = cb,
2386
0
    .cb_data = cb_data,
2387
0
  };
2388
0
  struct packfile_list_entry *e;
2389
0
  int pack_errors = 0, ret;
2390
2391
0
  store->skip_mru_updates = true;
2392
2393
0
  for (e = packfile_store_get_packs(store); e; e = e->next) {
2394
0
    struct packed_git *p = e->pack;
2395
2396
0
    if ((flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY) && !p->pack_local)
2397
0
      continue;
2398
0
    if ((flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY) &&
2399
0
        !p->pack_promisor)
2400
0
      continue;
2401
0
    if ((flags & ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS) &&
2402
0
        p->pack_keep_in_core)
2403
0
      continue;
2404
0
    if ((flags & ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS) &&
2405
0
        p->pack_keep)
2406
0
      continue;
2407
0
    if (open_pack_index(p)) {
2408
0
      pack_errors = 1;
2409
0
      continue;
2410
0
    }
2411
2412
0
    ret = for_each_object_in_pack(p, packfile_store_for_each_object_wrapper,
2413
0
                &data, flags);
2414
0
    if (ret)
2415
0
      goto out;
2416
0
  }
2417
2418
0
  ret = 0;
2419
2420
0
out:
2421
0
  store->skip_mru_updates = false;
2422
2423
0
  if (!ret && pack_errors)
2424
0
    ret = -1;
2425
0
  return ret;
2426
0
}
2427
2428
struct add_promisor_object_data {
2429
  struct repository *repo;
2430
  struct oidset *set;
2431
};
2432
2433
static int add_promisor_object(const struct object_id *oid,
2434
             struct object_info *oi UNUSED,
2435
             void *cb_data)
2436
0
{
2437
0
  struct add_promisor_object_data *data = cb_data;
2438
0
  struct object *obj;
2439
0
  int we_parsed_object;
2440
2441
0
  obj = lookup_object(data->repo, oid);
2442
0
  if (obj && obj->parsed) {
2443
0
    we_parsed_object = 0;
2444
0
  } else {
2445
0
    we_parsed_object = 1;
2446
0
    obj = parse_object_with_flags(data->repo, oid,
2447
0
                PARSE_OBJECT_SKIP_HASH_CHECK);
2448
0
  }
2449
2450
0
  if (!obj)
2451
0
    return 1;
2452
2453
0
  oidset_insert(data->set, oid);
2454
2455
  /*
2456
   * If this is a tree, commit, or tag, the objects it refers
2457
   * to are also promisor objects. (Blobs refer to no objects->)
2458
   */
2459
0
  if (obj->type == OBJ_TREE) {
2460
0
    struct tree *tree = (struct tree *)obj;
2461
0
    struct tree_desc desc;
2462
0
    struct name_entry entry;
2463
0
    if (init_tree_desc_gently(&desc, &tree->object.oid,
2464
0
            tree->buffer, tree->size, 0))
2465
      /*
2466
       * Error messages are given when packs are
2467
       * verified, so do not print any here.
2468
       */
2469
0
      return 0;
2470
0
    while (tree_entry_gently(&desc, &entry))
2471
0
      oidset_insert(data->set, &entry.oid);
2472
0
    if (we_parsed_object)
2473
0
      free_tree_buffer(tree);
2474
0
  } else if (obj->type == OBJ_COMMIT) {
2475
0
    struct commit *commit = (struct commit *) obj;
2476
0
    struct commit_list *parents = commit->parents;
2477
2478
0
    oidset_insert(data->set, get_commit_tree_oid(commit));
2479
0
    for (; parents; parents = parents->next)
2480
0
      oidset_insert(data->set, &parents->item->object.oid);
2481
0
  } else if (obj->type == OBJ_TAG) {
2482
0
    struct tag *tag = (struct tag *) obj;
2483
0
    oidset_insert(data->set, get_tagged_oid(tag));
2484
0
  }
2485
0
  return 0;
2486
0
}
2487
2488
int is_promisor_object(struct repository *r, const struct object_id *oid)
2489
0
{
2490
0
  static struct oidset promisor_objects;
2491
0
  static int promisor_objects_prepared;
2492
2493
0
  if (!promisor_objects_prepared) {
2494
0
    if (repo_has_promisor_remote(r)) {
2495
0
      struct add_promisor_object_data data = {
2496
0
        .repo = r,
2497
0
        .set = &promisor_objects,
2498
0
      };
2499
2500
0
      odb_for_each_object(r->objects, NULL, add_promisor_object, &data,
2501
0
              ODB_FOR_EACH_OBJECT_PROMISOR_ONLY | ODB_FOR_EACH_OBJECT_PACK_ORDER);
2502
0
    }
2503
0
    promisor_objects_prepared = 1;
2504
0
  }
2505
0
  return oidset_contains(&promisor_objects, oid);
2506
0
}
2507
2508
int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *len)
2509
0
{
2510
0
  unsigned char *hdr;
2511
0
  char *c;
2512
2513
0
  hdr = out;
2514
0
  put_be32(hdr, PACK_SIGNATURE);
2515
0
  hdr += 4;
2516
0
  put_be32(hdr, strtoul(in, &c, 10));
2517
0
  hdr += 4;
2518
0
  if (*c != ',')
2519
0
    return -1;
2520
0
  put_be32(hdr, strtoul(c + 1, &c, 10));
2521
0
  hdr += 4;
2522
0
  if (*c)
2523
0
    return -1;
2524
0
  *len = hdr - out;
2525
0
  return 0;
2526
0
}
2527
2528
struct packfile_store *packfile_store_new(struct odb_source *source)
2529
0
{
2530
0
  struct packfile_store *store;
2531
0
  CALLOC_ARRAY(store, 1);
2532
0
  store->source = source;
2533
0
  strmap_init(&store->packs_by_path);
2534
0
  return store;
2535
0
}
2536
2537
void packfile_store_free(struct packfile_store *store)
2538
0
{
2539
0
  for (struct packfile_list_entry *e = store->packs.head; e; e = e->next)
2540
0
    free(e->pack);
2541
0
  packfile_list_clear(&store->packs);
2542
2543
0
  strmap_clear(&store->packs_by_path, 0);
2544
0
  free(store);
2545
0
}
2546
2547
void packfile_store_close(struct packfile_store *store)
2548
0
{
2549
0
  for (struct packfile_list_entry *e = store->packs.head; e; e = e->next) {
2550
0
    if (e->pack->do_not_close)
2551
0
      BUG("want to close pack marked 'do-not-close'");
2552
0
    close_pack(e->pack);
2553
0
  }
2554
0
  if (store->midx)
2555
0
    close_midx(store->midx);
2556
0
  store->midx = NULL;
2557
0
}
2558
2559
struct odb_packed_read_stream {
2560
  struct odb_read_stream base;
2561
  struct packed_git *pack;
2562
  git_zstream z;
2563
  enum {
2564
    ODB_PACKED_READ_STREAM_UNINITIALIZED,
2565
    ODB_PACKED_READ_STREAM_INUSE,
2566
    ODB_PACKED_READ_STREAM_DONE,
2567
    ODB_PACKED_READ_STREAM_ERROR,
2568
  } z_state;
2569
  off_t pos;
2570
};
2571
2572
static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *buf,
2573
             size_t sz)
2574
0
{
2575
0
  struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
2576
0
  size_t total_read = 0;
2577
2578
0
  switch (st->z_state) {
2579
0
  case ODB_PACKED_READ_STREAM_UNINITIALIZED:
2580
0
    memset(&st->z, 0, sizeof(st->z));
2581
0
    git_inflate_init(&st->z);
2582
0
    st->z_state = ODB_PACKED_READ_STREAM_INUSE;
2583
0
    break;
2584
0
  case ODB_PACKED_READ_STREAM_DONE:
2585
0
    return 0;
2586
0
  case ODB_PACKED_READ_STREAM_ERROR:
2587
0
    return -1;
2588
0
  case ODB_PACKED_READ_STREAM_INUSE:
2589
0
    break;
2590
0
  }
2591
2592
0
  while (total_read < sz) {
2593
0
    int status;
2594
0
    struct pack_window *window = NULL;
2595
0
    unsigned char *mapped;
2596
2597
0
    mapped = use_pack(st->pack, &window,
2598
0
          st->pos, &st->z.avail_in);
2599
2600
0
    st->z.next_out = (unsigned char *)buf + total_read;
2601
0
    st->z.avail_out = sz - total_read;
2602
0
    st->z.next_in = mapped;
2603
0
    status = git_inflate(&st->z, Z_FINISH);
2604
2605
0
    st->pos += st->z.next_in - mapped;
2606
0
    total_read = st->z.next_out - (unsigned char *)buf;
2607
0
    unuse_pack(&window);
2608
2609
0
    if (status == Z_STREAM_END) {
2610
0
      git_inflate_end(&st->z);
2611
0
      st->z_state = ODB_PACKED_READ_STREAM_DONE;
2612
0
      break;
2613
0
    }
2614
2615
    /*
2616
     * Unlike the loose object case, we do not have to worry here
2617
     * about running out of input bytes and spinning infinitely. If
2618
     * we get Z_BUF_ERROR due to too few input bytes, then we'll
2619
     * replenish them in the next use_pack() call when we loop. If
2620
     * we truly hit the end of the pack (i.e., because it's corrupt
2621
     * or truncated), then use_pack() catches that and will die().
2622
     */
2623
0
    if (status != Z_OK && status != Z_BUF_ERROR) {
2624
0
      git_inflate_end(&st->z);
2625
0
      st->z_state = ODB_PACKED_READ_STREAM_ERROR;
2626
0
      return -1;
2627
0
    }
2628
0
  }
2629
0
  return total_read;
2630
0
}
2631
2632
static int close_istream_pack_non_delta(struct odb_read_stream *_st)
2633
0
{
2634
0
  struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
2635
0
  if (st->z_state == ODB_PACKED_READ_STREAM_INUSE)
2636
0
    git_inflate_end(&st->z);
2637
0
  return 0;
2638
0
}
2639
2640
int packfile_read_object_stream(struct odb_read_stream **out,
2641
        const struct object_id *oid,
2642
        struct packed_git *pack,
2643
        off_t offset)
2644
0
{
2645
0
  struct odb_packed_read_stream *stream;
2646
0
  struct pack_window *window = NULL;
2647
0
  enum object_type in_pack_type;
2648
0
  unsigned long size;
2649
2650
0
  in_pack_type = unpack_object_header(pack, &window, &offset, &size);
2651
0
  unuse_pack(&window);
2652
2653
0
  if (repo_settings_get_big_file_threshold(pack->repo) >= size)
2654
0
    return -1;
2655
2656
0
  switch (in_pack_type) {
2657
0
  default:
2658
0
    return -1; /* we do not do deltas for now */
2659
0
  case OBJ_BAD:
2660
0
    mark_bad_packed_object(pack, oid);
2661
0
    return -1;
2662
0
  case OBJ_COMMIT:
2663
0
  case OBJ_TREE:
2664
0
  case OBJ_BLOB:
2665
0
  case OBJ_TAG:
2666
0
    break;
2667
0
  }
2668
2669
0
  CALLOC_ARRAY(stream, 1);
2670
0
  stream->base.close = close_istream_pack_non_delta;
2671
0
  stream->base.read = read_istream_pack_non_delta;
2672
0
  stream->base.type = in_pack_type;
2673
0
  stream->base.size = size;
2674
0
  stream->z_state = ODB_PACKED_READ_STREAM_UNINITIALIZED;
2675
0
  stream->pack = pack;
2676
0
  stream->pos = offset;
2677
2678
0
  *out = &stream->base;
2679
2680
0
  return 0;
2681
0
}
2682
2683
int packfile_store_read_object_stream(struct odb_read_stream **out,
2684
              struct packfile_store *store,
2685
              const struct object_id *oid)
2686
0
{
2687
0
  struct pack_entry e;
2688
2689
0
  if (!find_pack_entry(store, oid, &e))
2690
0
    return -1;
2691
2692
0
  return packfile_read_object_stream(out, oid, e.p, e.offset);
2693
0
}