Coverage Report

Created: 2026-03-31 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/pack-mtimes.c
Line
Count
Source
1
#include "git-compat-util.h"
2
#include "gettext.h"
3
#include "pack-mtimes.h"
4
#include "odb.h"
5
#include "packfile.h"
6
#include "strbuf.h"
7
8
static char *pack_mtimes_filename(struct packed_git *p)
9
0
{
10
0
  size_t len;
11
0
  if (!strip_suffix(p->pack_name, ".pack", &len))
12
0
    BUG("pack_name does not end in .pack");
13
0
  return xstrfmt("%.*s.mtimes", (int)len, p->pack_name);
14
0
}
15
16
0
#define MTIMES_HEADER_SIZE (12)
17
18
struct mtimes_header {
19
  uint32_t signature;
20
  uint32_t version;
21
  uint32_t hash_id;
22
};
23
24
static int load_pack_mtimes_file(char *mtimes_file,
25
         uint32_t num_objects,
26
         const uint32_t **data_p, size_t *len_p)
27
0
{
28
0
  int fd, ret = 0;
29
0
  struct stat st;
30
0
  uint32_t *data = NULL;
31
0
  size_t mtimes_size, expected_size;
32
0
  struct mtimes_header header;
33
34
0
  fd = git_open(mtimes_file);
35
36
0
  if (fd < 0) {
37
0
    ret = -1;
38
0
    goto cleanup;
39
0
  }
40
0
  if (fstat(fd, &st)) {
41
0
    ret = error_errno(_("failed to read %s"), mtimes_file);
42
0
    goto cleanup;
43
0
  }
44
45
0
  mtimes_size = xsize_t(st.st_size);
46
47
0
  if (mtimes_size < MTIMES_HEADER_SIZE) {
48
0
    ret = error(_("mtimes file %s is too small"), mtimes_file);
49
0
    goto cleanup;
50
0
  }
51
52
0
  data = xmmap(NULL, mtimes_size, PROT_READ, MAP_PRIVATE, fd, 0);
53
54
0
  header.signature = ntohl(data[0]);
55
0
  header.version = ntohl(data[1]);
56
0
  header.hash_id = ntohl(data[2]);
57
58
0
  if (header.signature != MTIMES_SIGNATURE) {
59
0
    ret = error(_("mtimes file %s has unknown signature"), mtimes_file);
60
0
    goto cleanup;
61
0
  }
62
63
0
  if (header.version != 1) {
64
0
    ret = error(_("mtimes file %s has unsupported version %"PRIu32),
65
0
          mtimes_file, header.version);
66
0
    goto cleanup;
67
0
  }
68
69
0
  if (!(header.hash_id == 1 || header.hash_id == 2)) {
70
0
    ret = error(_("mtimes file %s has unsupported hash id %"PRIu32),
71
0
          mtimes_file, header.hash_id);
72
0
    goto cleanup;
73
0
  }
74
75
76
0
  expected_size = MTIMES_HEADER_SIZE;
77
0
  expected_size = st_add(expected_size, st_mult(sizeof(uint32_t), num_objects));
78
0
  expected_size = st_add(expected_size, 2 * (header.hash_id == 1 ? GIT_SHA1_RAWSZ : GIT_SHA256_RAWSZ));
79
80
0
  if (mtimes_size != expected_size) {
81
0
    ret = error(_("mtimes file %s is corrupt"), mtimes_file);
82
0
    goto cleanup;
83
0
  }
84
85
0
cleanup:
86
0
  if (ret) {
87
0
    if (data)
88
0
      munmap(data, mtimes_size);
89
0
  } else {
90
0
    *len_p = mtimes_size;
91
0
    *data_p = data;
92
0
  }
93
94
0
  if (fd >= 0)
95
0
    close(fd);
96
0
  return ret;
97
0
}
98
99
int load_pack_mtimes(struct packed_git *p)
100
0
{
101
0
  char *mtimes_name = NULL;
102
0
  int ret = 0;
103
104
0
  if (!p->is_cruft)
105
0
    return ret; /* not a cruft pack */
106
0
  if (p->mtimes_map)
107
0
    return ret; /* already loaded */
108
109
0
  ret = open_pack_index(p);
110
0
  if (ret < 0)
111
0
    goto cleanup;
112
113
0
  mtimes_name = pack_mtimes_filename(p);
114
0
  ret = load_pack_mtimes_file(mtimes_name,
115
0
            p->num_objects,
116
0
            &p->mtimes_map,
117
0
            &p->mtimes_size);
118
0
cleanup:
119
0
  free(mtimes_name);
120
0
  return ret;
121
0
}
122
123
uint32_t nth_packed_mtime(struct packed_git *p, uint32_t pos)
124
0
{
125
0
  if (!p->mtimes_map)
126
0
    BUG("pack .mtimes file not loaded for %s", p->pack_name);
127
0
  if (p->num_objects <= pos)
128
0
    BUG("pack .mtimes out-of-bounds (%"PRIu32" vs %"PRIu32")",
129
0
        pos, p->num_objects);
130
131
0
  return get_be32(p->mtimes_map + pos + 3);
132
0
}