Coverage Report

Created: 2026-01-10 06:18

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