1#include "cache.h"
2#include "mru.h"
3#include "pack.h"
45
char *odb_pack_name(struct strbuf *buf,
6const unsigned char *sha1,
7const char *ext)
8{
9strbuf_reset(buf);
10strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
11sha1_to_hex(sha1), ext);
12return buf->buf;
13}
1415
char *sha1_pack_name(const unsigned char *sha1)
16{
17static struct strbuf buf = STRBUF_INIT;
18return odb_pack_name(&buf, sha1, "pack");
19}
2021
char *sha1_pack_index_name(const unsigned char *sha1)
22{
23static struct strbuf buf = STRBUF_INIT;
24return odb_pack_name(&buf, sha1, "idx");
25}
2627
unsigned int pack_used_ctr;
28unsigned int pack_mmap_calls;
29unsigned int peak_pack_open_windows;
30unsigned int pack_open_windows;
31unsigned int pack_open_fds;
32unsigned int pack_max_fds;
33size_t peak_pack_mapped;
34size_t pack_mapped;
35struct packed_git *packed_git;
3637
static struct mru packed_git_mru_storage;
38struct mru *packed_git_mru = &packed_git_mru_storage;
3940
#define SZ_FMT PRIuMAX
41static inline uintmax_t sz_fmt(size_t s) { return s; }
4243
void pack_report(void)
44{
45fprintf(stderr,
46"pack_report: getpagesize() = %10" SZ_FMT "\n"
47"pack_report: core.packedGitWindowSize = %10" SZ_FMT "\n"
48"pack_report: core.packedGitLimit = %10" SZ_FMT "\n",
49sz_fmt(getpagesize()),
50sz_fmt(packed_git_window_size),
51sz_fmt(packed_git_limit));
52fprintf(stderr,
53"pack_report: pack_used_ctr = %10u\n"
54"pack_report: pack_mmap_calls = %10u\n"
55"pack_report: pack_open_windows = %10u / %10u\n"
56"pack_report: pack_mapped = "
57"%10" SZ_FMT " / %10" SZ_FMT "\n",
58pack_used_ctr,
59pack_mmap_calls,
60pack_open_windows, peak_pack_open_windows,
61sz_fmt(pack_mapped), sz_fmt(peak_pack_mapped));
62}
6364
/*
65* Open and mmap the index file at path, perform a couple of
66* consistency checks, then record its information to p. Return 0 on
67* success.
68*/
69static int check_packed_git_idx(const char *path, struct packed_git *p)
70{
71void *idx_map;
72struct pack_idx_header *hdr;
73size_t idx_size;
74uint32_t version, nr, i, *index;
75int fd = git_open(path);
76struct stat st;
7778
if (fd < 0)
79return -1;
80if (fstat(fd, &st)) {
81close(fd);
82return -1;
83}
84idx_size = xsize_t(st.st_size);
85if (idx_size < 4 * 256 + 20 + 20) {
86close(fd);
87return error("index file %s is too small", path);
88}
89idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
90close(fd);
9192
hdr = idx_map;
93if (hdr->idx_signature == htonl(PACK_IDX_SIGNATURE)) {
94version = ntohl(hdr->idx_version);
95if (version < 2 || version > 2) {
96munmap(idx_map, idx_size);
97return error("index file %s is version %"PRIu32
98" and is not supported by this binary"
99" (try upgrading GIT to a newer version)",
100path, version);
101}
102} else
103version = 1;
104105
nr = 0;
106index = idx_map;
107if (version > 1)
108index += 2; /* skip index header */
109for (i = 0; i < 256; i++) {
110uint32_t n = ntohl(index[i]);
111if (n < nr) {
112munmap(idx_map, idx_size);
113return error("non-monotonic index %s", path);
114}
115nr = n;
116}
117118
if (version == 1) {
119/*
120* Total size:
121* - 256 index entries 4 bytes each
122* - 24-byte entries * nr (20-byte sha1 + 4-byte offset)
123* - 20-byte SHA1 of the packfile
124* - 20-byte SHA1 file checksum
125*/
126if (idx_size != 4*256 + nr * 24 + 20 + 20) {
127munmap(idx_map, idx_size);
128return error("wrong index v1 file size in %s", path);
129}
130} else if (version == 2) {
131/*
132* Minimum size:
133* - 8 bytes of header
134* - 256 index entries 4 bytes each
135* - 20-byte sha1 entry * nr
136* - 4-byte crc entry * nr
137* - 4-byte offset entry * nr
138* - 20-byte SHA1 of the packfile
139* - 20-byte SHA1 file checksum
140* And after the 4-byte offset table might be a
141* variable sized table containing 8-byte entries
142* for offsets larger than 2^31.
143*/
144unsigned long min_size = 8 + 4*256 + nr*(20 + 4 + 4) + 20 + 20;
145unsigned long max_size = min_size;
146if (nr)
147max_size += (nr - 1)*8;
148if (idx_size < min_size || idx_size > max_size) {
149munmap(idx_map, idx_size);
150return error("wrong index v2 file size in %s", path);
151}
152if (idx_size != min_size &&
153/*
154* make sure we can deal with large pack offsets.
155* 31-bit signed offset won't be enough, neither
156* 32-bit unsigned one will be.
157*/
158(sizeof(off_t) <= 4)) {
159munmap(idx_map, idx_size);
160return error("pack too large for current definition of off_t in %s", path);
161}
162}
163164
p->index_version = version;
165p->index_data = idx_map;
166p->index_size = idx_size;
167p->num_objects = nr;
168return 0;
169}
170171
int open_pack_index(struct packed_git *p)
172{
173char *idx_name;
174size_t len;
175int ret;
176177
if (p->index_data)
178return 0;
179180
if (!strip_suffix(p->pack_name, ".pack", &len))
181die("BUG: pack_name does not end in .pack");
182idx_name = xstrfmt("%.*s.idx", (int)len, p->pack_name);
183ret = check_packed_git_idx(idx_name, p);
184free(idx_name);
185return ret;
186}
187188
static struct packed_git *alloc_packed_git(int extra)
189{
190struct packed_git *p = xmalloc(st_add(sizeof(*p), extra));
191memset(p, 0, sizeof(*p));
192p->pack_fd = -1;
193return p;
194}
195196
struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
197{
198const char *path = sha1_pack_name(sha1);
199size_t alloc = st_add(strlen(path), 1);
200struct packed_git *p = alloc_packed_git(alloc);
201202
memcpy(p->pack_name, path, alloc); /* includes NUL */
203hashcpy(p->sha1, sha1);
204if (check_packed_git_idx(idx_path, p)) {
205free(p);
206return NULL;
207}
208209
return p;
210}