Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/packfile.h
Line
Count
Source
1
#ifndef PACKFILE_H
2
#define PACKFILE_H
3
4
#include "list.h"
5
#include "object.h"
6
#include "odb.h"
7
#include "odb/source-files.h"
8
#include "odb/source-packed.h"
9
#include "oidset.h"
10
#include "packfile-list.h"
11
#include "repository.h"
12
13
/* in odb.h */
14
struct object_info;
15
struct odb_stream;
16
17
struct packed_git {
18
  struct pack_window *windows;
19
  off_t pack_size;
20
  const void *index_data;
21
  size_t index_size;
22
  uint32_t num_objects;
23
  size_t crc_offset;
24
  struct oidset bad_objects;
25
  int index_version;
26
  time_t mtime;
27
  int pack_fd;
28
  int index;              /* for builtin/pack-objects.c */
29
  unsigned pack_local:1,
30
     pack_keep:1,
31
     pack_keep_in_core:1,
32
     pack_keep_in_core_open:1,
33
     freshened:1,
34
     do_not_close:1,
35
     pack_promisor:1,
36
     multi_pack_index:1,
37
     is_cruft:1;
38
  unsigned char hash[GIT_MAX_RAWSZ];
39
  struct revindex_entry *revindex;
40
  const uint32_t *revindex_data;
41
  const uint32_t *revindex_map;
42
  size_t revindex_size;
43
  /*
44
   * mtimes_map points at the beginning of the memory mapped region of
45
   * this pack's corresponding .mtimes file, and mtimes_size is the size
46
   * of that .mtimes file
47
   */
48
  const uint32_t *mtimes_map;
49
  size_t mtimes_size;
50
51
  /* repo denotes the repository this packfile belongs to */
52
  struct repository *repo;
53
54
  /* something like ".git/objects/pack/xxxxx.pack" */
55
  char pack_name[FLEX_ARRAY]; /* more */
56
};
57
58
/*
59
 * Add the pack to the store so that contained objects become accessible via
60
 * the store. This moves ownership into the store.
61
 */
62
void packfile_store_add_pack(struct odb_source_packed *store,
63
           struct packed_git *pack);
64
65
/*
66
 * Get all packs managed by the given store, including packfiles that are
67
 * referenced by multi-pack indices.
68
 */
69
struct packfile_list_entry *packfile_store_get_packs(struct odb_source_packed *store);
70
71
struct repo_for_each_pack_data {
72
  struct odb_files_dir *dir;
73
  struct packfile_list_entry *entry;
74
};
75
76
static inline struct repo_for_each_pack_data repo_for_eack_pack_data_init(struct repository *repo)
77
0
{
78
0
  struct repo_for_each_pack_data data = { 0 };
79
0
  struct odb_source_files *files = odb_source_files_downcast(repo->objects->source);
80
81
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
82
0
    struct packfile_list_entry *entry = packfile_store_get_packs(dir->packed);
83
0
    if (!entry)
84
0
      continue;
85
0
    data.dir = dir;
86
0
    data.entry = entry;
87
0
    break;
88
0
  }
89
90
0
  return data;
91
0
}
Unexecuted instantiation: object-file.c:repo_for_eack_pack_data_init
Unexecuted instantiation: source.c:repo_for_eack_pack_data_init
Unexecuted instantiation: source-files.c:repo_for_eack_pack_data_init
Unexecuted instantiation: source-packed.c:repo_for_eack_pack_data_init
Unexecuted instantiation: pack-bitmap.c:repo_for_eack_pack_data_init
Unexecuted instantiation: pack-check.c:repo_for_eack_pack_data_init
Unexecuted instantiation: pack-objects.c:repo_for_eack_pack_data_init
Unexecuted instantiation: pack-revindex.c:repo_for_eack_pack_data_init
Unexecuted instantiation: pack-write.c:repo_for_eack_pack_data_init
Unexecuted instantiation: packfile.c:repo_for_eack_pack_data_init
Unexecuted instantiation: packfile-list.c:repo_for_eack_pack_data_init
Unexecuted instantiation: path.c:repo_for_eack_pack_data_init
Unexecuted instantiation: promisor-remote.c:repo_for_eack_pack_data_init
Unexecuted instantiation: pseudo-merge.c:repo_for_eack_pack_data_init
Unexecuted instantiation: repack.c:repo_for_eack_pack_data_init
Unexecuted instantiation: repack-geometry.c:repo_for_eack_pack_data_init
Unexecuted instantiation: repo-settings.c:repo_for_eack_pack_data_init
Unexecuted instantiation: revision.c:repo_for_eack_pack_data_init
Unexecuted instantiation: run-command.c:repo_for_eack_pack_data_init
Unexecuted instantiation: tag.c:repo_for_eack_pack_data_init
Unexecuted instantiation: commit-graph.c:repo_for_eack_pack_data_init
Unexecuted instantiation: connected.c:repo_for_eack_pack_data_init
Unexecuted instantiation: diff.c:repo_for_eack_pack_data_init
Unexecuted instantiation: fetch-pack.c:repo_for_eack_pack_data_init
Unexecuted instantiation: fsck.c:repo_for_eack_pack_data_init
Unexecuted instantiation: list-objects.c:repo_for_eack_pack_data_init
Unexecuted instantiation: midx.c:repo_for_eack_pack_data_init
Unexecuted instantiation: pack-bitmap-write.c:repo_for_eack_pack_data_init
Unexecuted instantiation: pack-mtimes.c:repo_for_eack_pack_data_init
Unexecuted instantiation: transport-helper.c:repo_for_eack_pack_data_init
92
93
static inline void repo_for_each_pack_data_next(struct repo_for_each_pack_data *data)
94
0
{
95
0
  struct odb_files_dir *dir;
96
97
0
  data->entry = data->entry->next;
98
0
  if (data->entry)
99
0
    return;
100
101
0
  for (dir = data->dir->next; dir; dir = dir->next) {
102
0
    struct packfile_list_entry *entry = packfile_store_get_packs(dir->packed);
103
0
    if (!entry)
104
0
      continue;
105
0
    data->dir = dir;
106
0
    data->entry = entry;
107
0
    return;
108
0
  }
109
110
0
  data->dir = NULL;
111
0
  data->entry = NULL;
112
0
}
Unexecuted instantiation: object-file.c:repo_for_each_pack_data_next
Unexecuted instantiation: source.c:repo_for_each_pack_data_next
Unexecuted instantiation: source-files.c:repo_for_each_pack_data_next
Unexecuted instantiation: source-packed.c:repo_for_each_pack_data_next
Unexecuted instantiation: pack-bitmap.c:repo_for_each_pack_data_next
Unexecuted instantiation: pack-check.c:repo_for_each_pack_data_next
Unexecuted instantiation: pack-objects.c:repo_for_each_pack_data_next
Unexecuted instantiation: pack-revindex.c:repo_for_each_pack_data_next
Unexecuted instantiation: pack-write.c:repo_for_each_pack_data_next
Unexecuted instantiation: packfile.c:repo_for_each_pack_data_next
Unexecuted instantiation: packfile-list.c:repo_for_each_pack_data_next
Unexecuted instantiation: path.c:repo_for_each_pack_data_next
Unexecuted instantiation: promisor-remote.c:repo_for_each_pack_data_next
Unexecuted instantiation: pseudo-merge.c:repo_for_each_pack_data_next
Unexecuted instantiation: repack.c:repo_for_each_pack_data_next
Unexecuted instantiation: repack-geometry.c:repo_for_each_pack_data_next
Unexecuted instantiation: repo-settings.c:repo_for_each_pack_data_next
Unexecuted instantiation: revision.c:repo_for_each_pack_data_next
Unexecuted instantiation: run-command.c:repo_for_each_pack_data_next
Unexecuted instantiation: tag.c:repo_for_each_pack_data_next
Unexecuted instantiation: commit-graph.c:repo_for_each_pack_data_next
Unexecuted instantiation: connected.c:repo_for_each_pack_data_next
Unexecuted instantiation: diff.c:repo_for_each_pack_data_next
Unexecuted instantiation: fetch-pack.c:repo_for_each_pack_data_next
Unexecuted instantiation: fsck.c:repo_for_each_pack_data_next
Unexecuted instantiation: list-objects.c:repo_for_each_pack_data_next
Unexecuted instantiation: midx.c:repo_for_each_pack_data_next
Unexecuted instantiation: pack-bitmap-write.c:repo_for_each_pack_data_next
Unexecuted instantiation: pack-mtimes.c:repo_for_each_pack_data_next
Unexecuted instantiation: transport-helper.c:repo_for_each_pack_data_next
113
114
/*
115
 * Load and iterate through all packs of the given repository. This helper
116
 * function will yield packfiles from all object sources connected to the
117
 * repository.
118
 */
119
#define repo_for_each_pack(repo, p) \
120
0
  for (struct repo_for_each_pack_data eack_pack_data = repo_for_eack_pack_data_init(repo); \
121
0
       ((p) = (eack_pack_data.entry ? eack_pack_data.entry->pack : NULL)); \
122
0
       repo_for_each_pack_data_next(&eack_pack_data))
123
124
/*
125
 * Open the packfile and add it to the store if it isn't yet known. Returns
126
 * either the newly opened packfile or the preexisting packfile. Returns a
127
 * `NULL` pointer in case the packfile could not be opened.
128
 */
129
struct packed_git *packfile_store_load_pack(struct odb_source_packed *store,
130
              const char *idx_path, int local);
131
132
enum kept_pack_type {
133
  KEPT_PACK_ON_DISK = (1 << 0),
134
  KEPT_PACK_IN_CORE = (1 << 1),
135
  KEPT_PACK_IN_CORE_OPEN = (1 << 2),
136
};
137
138
/*
139
 * Retrieve the cache of kept packs from the given packfile store. Accepts a
140
 * combination of `kept_pack_type` flags. The cache is computed on demand and
141
 * will be recomputed whenever the flags change.
142
 */
143
struct packed_git **packfile_store_get_kept_pack_cache(struct odb_source_packed *store,
144
                   unsigned flags);
145
146
struct pack_window {
147
  struct pack_window *next;
148
  unsigned char *base;
149
  off_t offset;
150
  size_t len;
151
  unsigned int last_used;
152
  unsigned int inuse_cnt;
153
};
154
155
struct pack_entry {
156
  off_t offset;
157
  struct packed_git *p;
158
};
159
160
/*
161
 * Generate the filename to be used for a pack file with checksum "sha1" and
162
 * extension "ext". The result is written into the strbuf "buf", overwriting
163
 * any existing contents. A pointer to buf->buf is returned as a convenience.
164
 *
165
 * Example: odb_pack_name(out, sha1, "idx") => ".git/objects/pack/pack-1234..idx"
166
 */
167
char *odb_pack_name(struct repository *r, struct strbuf *buf,
168
        const unsigned char *hash, const char *ext);
169
170
/*
171
 * Return the basename of the packfile, omitting any containing directory
172
 * (e.g., "pack-1234abcd[...].pack").
173
 */
174
const char *pack_basename(struct packed_git *p);
175
176
/*
177
 * Parse the pack idx file found at idx_path and create a packed_git struct
178
 * which can be used with find_pack_entry_one().
179
 *
180
 * You probably don't want to use this function! It skips most of the normal
181
 * sanity checks (including whether we even have the matching .pack file),
182
 * and does not add the resulting packed_git struct to the internal list of
183
 * packs. You probably want add_packed_git() instead.
184
 */
185
struct packed_git *parse_pack_index(struct repository *r, unsigned char *sha1,
186
            const char *idx_path);
187
188
typedef void each_file_in_pack_dir_fn(const char *full_path, size_t full_path_len,
189
              const char *file_name, void *data);
190
void for_each_file_in_pack_subdir(const char *objdir,
191
          const char *subdir,
192
          each_file_in_pack_dir_fn fn,
193
          void *data);
194
void for_each_file_in_pack_dir(const char *objdir,
195
             each_file_in_pack_dir_fn fn,
196
             void *data);
197
198
/*
199
 * Iterate over all accessible packed objects without respect to reachability.
200
 * By default, this includes both local and alternate packs.
201
 *
202
 * Note that some objects may appear twice if they are found in multiple packs.
203
 * Each pack is visited in an unspecified order. By default, objects within a
204
 * pack are visited in pack-idx order (i.e., sorted by oid).
205
 */
206
typedef int each_packed_object_fn(const struct object_id *oid,
207
          struct packed_git *pack,
208
          uint32_t pos,
209
          void *data);
210
int for_each_object_in_pack(struct packed_git *p,
211
          each_packed_object_fn, void *data,
212
          enum odb_for_each_object_flags flags);
213
214
/* A hook to report invalid files in pack directory */
215
0
#define PACKDIR_FILE_PACK 1
216
0
#define PACKDIR_FILE_IDX 2
217
0
#define PACKDIR_FILE_GARBAGE 4
218
extern void (*report_garbage)(unsigned seen_bits, const char *path);
219
220
void pack_report(struct repository *repo);
221
222
/*
223
 * mmap the index file for the specified packfile (if it is not
224
 * already mmapped).  Return 0 on success.
225
 */
226
int open_pack_index(struct packed_git *);
227
228
/*
229
 * munmap the index file for the specified packfile (if it is
230
 * currently mmapped).
231
 */
232
void close_pack_index(struct packed_git *);
233
234
int close_pack_fd(struct packed_git *p);
235
236
uint32_t get_pack_fanout(struct packed_git *p, uint32_t value);
237
238
struct object_database;
239
240
unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t,
241
      size_t *);
242
void close_pack_windows(struct packed_git *);
243
void close_pack(struct packed_git *);
244
void unuse_pack(struct pack_window **);
245
void clear_delta_base_cache(void);
246
struct packed_git *add_packed_git(struct repository *r, const char *path,
247
          size_t path_len, int local);
248
249
/*
250
 * Unlink the .pack and associated extension files.
251
 * Does not unlink if 'force_delete' is false and the pack-file is
252
 * marked as ".keep".
253
 */
254
void unlink_pack_path(const char *pack_name, int force_delete);
255
256
/*
257
 * Make sure that a pointer access into an mmap'd index file is within bounds,
258
 * and can provide at least 8 bytes of data.
259
 *
260
 * Note that this is only necessary for variable-length segments of the file
261
 * (like the 64-bit extended offset table), as we compare the size to the
262
 * fixed-length parts when we open the file.
263
 */
264
void check_pack_index_ptr(const struct packed_git *p, const void *ptr);
265
266
/*
267
 * Perform binary search on a pack-index for a given oid. Packfile is expected to
268
 * have a valid pack-index.
269
 *
270
 * See 'bsearch_hash' for more information.
271
 */
272
int bsearch_pack(const struct object_id *oid, const struct packed_git *p, uint32_t *result);
273
274
/*
275
 * Write the oid of the nth object within the specified packfile into the first
276
 * parameter. Open the index if it is not already open.  Returns 0 on success,
277
 * negative otherwise.
278
 */
279
int nth_packed_object_id(struct object_id *, struct packed_git *, uint32_t n);
280
281
/*
282
 * Return the offset of the nth object within the specified packfile.
283
 * The index must already be opened.
284
 */
285
off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
286
287
/*
288
 * If the object named by oid is present in the specified packfile,
289
 * return its offset within the packfile; otherwise, return 0.
290
 */
291
off_t find_pack_entry_one(const struct object_id *oid, struct packed_git *);
292
293
int packfile_fill_entry(struct packed_git *p,
294
      const struct object_id *oid,
295
      struct pack_entry *e,
296
      struct packed_git **bad_pack);
297
298
int is_pack_valid(struct packed_git *);
299
void *unpack_entry(struct repository *r, struct packed_git *, off_t,
300
       enum object_type *, size_t *);
301
size_t unpack_object_header_buffer(const unsigned char *buf, size_t len,
302
           enum object_type *type, size_t *sizep);
303
size_t get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
304
int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, size_t *);
305
off_t get_delta_base(struct packed_git *p, struct pack_window **w_curs,
306
         off_t *curpos, enum object_type type,
307
         off_t delta_obj_offset);
308
309
int packfile_read_object_stream(struct odb_stream **out,
310
        const struct object_id *oid,
311
        struct packed_git *pack,
312
        off_t offset);
313
314
void release_pack_memory(size_t);
315
316
/* global flag to enable extra checks when accessing packed objects */
317
extern int do_check_packed_object_crc;
318
319
/*
320
 * Look up the object info for a specific offset in the packfile.
321
 * Returns zero on success, a negative error code otherwise.
322
 */
323
int packed_object_info(struct odb_source_packed *source,
324
           struct packed_git *pack,
325
           off_t offset, struct object_info *);
326
int packed_object_info_with_index_pos(struct odb_source_packed *source,
327
              struct packed_git *p, off_t obj_offset,
328
              uint32_t *maybe_index_pos, struct object_info *oi);
329
330
void mark_bad_packed_object(struct packed_git *, const struct object_id *);
331
332
int has_object_pack(struct repository *r, const struct object_id *oid);
333
int has_object_kept_pack(struct repository *r, const struct object_id *oid,
334
       unsigned flags);
335
336
/*
337
 * Return 1 if an object in a promisor packfile is or refers to the given
338
 * object, 0 otherwise.
339
 */
340
int is_promisor_object(struct repository *r, const struct object_id *oid);
341
342
/*
343
 * Expose a function for fuzz testing.
344
 *
345
 * load_idx() parses a block of memory as a packfile index and puts the results
346
 * into a struct packed_git.
347
 *
348
 * This function should not be used directly. It is exposed here only so that we
349
 * have a convenient entry-point for fuzz testing. For real uses, you should
350
 * probably use open_pack_index() instead.
351
 */
352
int load_idx(const char *path, const unsigned int hashsz, void *idx_map,
353
       size_t idx_size, struct packed_git *p);
354
355
/*
356
 * Parse a --pack_header option as accepted by index-pack and unpack-objects,
357
 * turning it into the matching bytes we'd find in a pack.
358
 */
359
int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *len);
360
361
#endif