]>
Commit | Line | Data |
---|---|---|
8b0eca7c DH |
1 | #include "cache.h" |
2 | #include "pack.h" | |
aa7e44bf GB |
3 | #include "csum-file.h" |
4 | ||
ebcfb379 JH |
5 | void reset_pack_idx_option(struct pack_idx_option *opts) |
6 | { | |
7 | memset(opts, 0, sizeof(*opts)); | |
8 | opts->version = 2; | |
9 | opts->off32_limit = 0x7fffffff; | |
10 | } | |
aa7e44bf GB |
11 | |
12 | static int sha1_compare(const void *_a, const void *_b) | |
13 | { | |
14 | struct pack_idx_entry *a = *(struct pack_idx_entry **)_a; | |
15 | struct pack_idx_entry *b = *(struct pack_idx_entry **)_b; | |
16 | return hashcmp(a->sha1, b->sha1); | |
17 | } | |
18 | ||
3c9fc074 JH |
19 | static int cmp_uint32(const void *a_, const void *b_) |
20 | { | |
21 | uint32_t a = *((uint32_t *)a_); | |
22 | uint32_t b = *((uint32_t *)b_); | |
23 | ||
24 | return (a < b) ? -1 : (a != b); | |
25 | } | |
26 | ||
fb956c1f JH |
27 | static int need_large_offset(off_t offset, const struct pack_idx_option *opts) |
28 | { | |
3c9fc074 JH |
29 | uint32_t ofsval; |
30 | ||
31 | if ((offset >> 31) || (opts->off32_limit < offset)) | |
32 | return 1; | |
33 | if (!opts->anomaly_nr) | |
34 | return 0; | |
35 | ofsval = offset; | |
36 | return !!bsearch(&ofsval, opts->anomaly, opts->anomaly_nr, | |
37 | sizeof(ofsval), cmp_uint32); | |
fb956c1f JH |
38 | } |
39 | ||
aa7e44bf GB |
40 | /* |
41 | * On entry *sha1 contains the pack content SHA1 hash, on exit it is | |
42 | * the SHA1 hash of sorted object names. The objects array passed in | |
43 | * will be sorted by SHA1 on exit. | |
44 | */ | |
3bb72562 | 45 | const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects, |
ebcfb379 JH |
46 | int nr_objects, const struct pack_idx_option *opts, |
47 | unsigned char *sha1) | |
aa7e44bf GB |
48 | { |
49 | struct sha1file *f; | |
50 | struct pack_idx_entry **sorted_by_sha, **list, **last; | |
51 | off_t last_obj_offset = 0; | |
52 | uint32_t array[256]; | |
53 | int i, fd; | |
9126f009 | 54 | git_SHA_CTX ctx; |
aa7e44bf GB |
55 | uint32_t index_version; |
56 | ||
57 | if (nr_objects) { | |
58 | sorted_by_sha = objects; | |
59 | list = sorted_by_sha; | |
60 | last = sorted_by_sha + nr_objects; | |
61 | for (i = 0; i < nr_objects; ++i) { | |
62 | if (objects[i]->offset > last_obj_offset) | |
63 | last_obj_offset = objects[i]->offset; | |
64 | } | |
65 | qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]), | |
66 | sha1_compare); | |
67 | } | |
68 | else | |
69 | sorted_by_sha = list = last = NULL; | |
70 | ||
e337a04d JH |
71 | if (opts->flags & WRITE_IDX_VERIFY) { |
72 | assert(index_name); | |
73 | f = sha1fd_check(index_name); | |
aa7e44bf | 74 | } else { |
e337a04d JH |
75 | if (!index_name) { |
76 | static char tmpfile[PATH_MAX]; | |
77 | fd = odb_mkstemp(tmpfile, sizeof(tmpfile), "pack/tmp_idx_XXXXXX"); | |
78 | index_name = xstrdup(tmpfile); | |
79 | } else { | |
80 | unlink(index_name); | |
81 | fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600); | |
82 | } | |
83 | if (fd < 0) | |
84 | die_errno("unable to create '%s'", index_name); | |
85 | f = sha1fd(fd, index_name); | |
aa7e44bf | 86 | } |
aa7e44bf GB |
87 | |
88 | /* if last object's offset is >= 2^31 we should use index V2 */ | |
fb956c1f | 89 | index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version; |
aa7e44bf GB |
90 | |
91 | /* index versions 2 and above need a header */ | |
92 | if (index_version >= 2) { | |
93 | struct pack_idx_header hdr; | |
94 | hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); | |
95 | hdr.idx_version = htonl(index_version); | |
96 | sha1write(f, &hdr, sizeof(hdr)); | |
97 | } | |
98 | ||
99 | /* | |
100 | * Write the first-level table (the list is sorted, | |
101 | * but we use a 256-entry lookup to be able to avoid | |
102 | * having to do eight extra binary search iterations). | |
103 | */ | |
104 | for (i = 0; i < 256; i++) { | |
105 | struct pack_idx_entry **next = list; | |
106 | while (next < last) { | |
107 | struct pack_idx_entry *obj = *next; | |
108 | if (obj->sha1[0] != i) | |
109 | break; | |
110 | next++; | |
111 | } | |
112 | array[i] = htonl(next - sorted_by_sha); | |
113 | list = next; | |
114 | } | |
115 | sha1write(f, array, 256 * 4); | |
116 | ||
117 | /* compute the SHA1 hash of sorted object names. */ | |
9126f009 | 118 | git_SHA1_Init(&ctx); |
aa7e44bf GB |
119 | |
120 | /* | |
121 | * Write the actual SHA1 entries.. | |
122 | */ | |
123 | list = sorted_by_sha; | |
124 | for (i = 0; i < nr_objects; i++) { | |
125 | struct pack_idx_entry *obj = *list++; | |
126 | if (index_version < 2) { | |
127 | uint32_t offset = htonl(obj->offset); | |
128 | sha1write(f, &offset, 4); | |
129 | } | |
130 | sha1write(f, obj->sha1, 20); | |
9126f009 | 131 | git_SHA1_Update(&ctx, obj->sha1, 20); |
aa7e44bf GB |
132 | } |
133 | ||
134 | if (index_version >= 2) { | |
135 | unsigned int nr_large_offset = 0; | |
136 | ||
137 | /* write the crc32 table */ | |
138 | list = sorted_by_sha; | |
139 | for (i = 0; i < nr_objects; i++) { | |
140 | struct pack_idx_entry *obj = *list++; | |
141 | uint32_t crc32_val = htonl(obj->crc32); | |
142 | sha1write(f, &crc32_val, 4); | |
143 | } | |
144 | ||
145 | /* write the 32-bit offset table */ | |
146 | list = sorted_by_sha; | |
147 | for (i = 0; i < nr_objects; i++) { | |
148 | struct pack_idx_entry *obj = *list++; | |
fb956c1f JH |
149 | uint32_t offset; |
150 | ||
151 | offset = (need_large_offset(obj->offset, opts) | |
152 | ? (0x80000000 | nr_large_offset++) | |
153 | : obj->offset); | |
aa7e44bf GB |
154 | offset = htonl(offset); |
155 | sha1write(f, &offset, 4); | |
156 | } | |
157 | ||
158 | /* write the large offset table */ | |
159 | list = sorted_by_sha; | |
160 | while (nr_large_offset) { | |
161 | struct pack_idx_entry *obj = *list++; | |
162 | uint64_t offset = obj->offset; | |
fb956c1f JH |
163 | uint32_t split[2]; |
164 | ||
165 | if (!need_large_offset(offset, opts)) | |
166 | continue; | |
167 | split[0] = htonl(offset >> 32); | |
168 | split[1] = htonl(offset & 0xffffffff); | |
169 | sha1write(f, split, 8); | |
170 | nr_large_offset--; | |
aa7e44bf GB |
171 | } |
172 | } | |
173 | ||
174 | sha1write(f, sha1, 20); | |
e337a04d JH |
175 | sha1close(f, NULL, ((opts->flags & WRITE_IDX_VERIFY) |
176 | ? CSUM_CLOSE : CSUM_FSYNC)); | |
9126f009 | 177 | git_SHA1_Final(sha1, &ctx); |
aa7e44bf GB |
178 | return index_name; |
179 | } | |
8b0eca7c | 180 | |
abeb40e5 NP |
181 | /* |
182 | * Update pack header with object_count and compute new SHA1 for pack data | |
183 | * associated to pack_fd, and write that SHA1 at the end. That new SHA1 | |
184 | * is also returned in new_pack_sha1. | |
185 | * | |
186 | * If partial_pack_sha1 is non null, then the SHA1 of the existing pack | |
187 | * (without the header update) is computed and validated against the | |
188 | * one provided in partial_pack_sha1. The validation is performed at | |
189 | * partial_pack_offset bytes in the pack file. The SHA1 of the remaining | |
190 | * data (i.e. from partial_pack_offset to the end) is then computed and | |
191 | * returned in partial_pack_sha1. | |
192 | * | |
193 | * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and | |
194 | * partial_pack_sha1 can refer to the same buffer if the caller is not | |
195 | * interested in the resulting SHA1 of pack data above partial_pack_offset. | |
196 | */ | |
8b0eca7c | 197 | void fixup_pack_header_footer(int pack_fd, |
abeb40e5 | 198 | unsigned char *new_pack_sha1, |
8b0eca7c | 199 | const char *pack_name, |
abeb40e5 NP |
200 | uint32_t object_count, |
201 | unsigned char *partial_pack_sha1, | |
202 | off_t partial_pack_offset) | |
8b0eca7c | 203 | { |
d35825da | 204 | int aligned_sz, buf_sz = 8 * 1024; |
9126f009 | 205 | git_SHA_CTX old_sha1_ctx, new_sha1_ctx; |
8b0eca7c DH |
206 | struct pack_header hdr; |
207 | char *buf; | |
208 | ||
9126f009 NP |
209 | git_SHA1_Init(&old_sha1_ctx); |
210 | git_SHA1_Init(&new_sha1_ctx); | |
abeb40e5 | 211 | |
8b0eca7c | 212 | if (lseek(pack_fd, 0, SEEK_SET) != 0) |
d824cbba | 213 | die_errno("Failed seeking to start of '%s'", pack_name); |
8b0eca7c | 214 | if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr)) |
d824cbba | 215 | die_errno("Unable to reread header of '%s'", pack_name); |
8b0eca7c | 216 | if (lseek(pack_fd, 0, SEEK_SET) != 0) |
d824cbba | 217 | die_errno("Failed seeking to start of '%s'", pack_name); |
9126f009 | 218 | git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr)); |
8b0eca7c | 219 | hdr.hdr_entries = htonl(object_count); |
9126f009 | 220 | git_SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr)); |
8b0eca7c | 221 | write_or_die(pack_fd, &hdr, sizeof(hdr)); |
abeb40e5 | 222 | partial_pack_offset -= sizeof(hdr); |
8b0eca7c DH |
223 | |
224 | buf = xmalloc(buf_sz); | |
d35825da | 225 | aligned_sz = buf_sz - sizeof(hdr); |
8b0eca7c | 226 | for (;;) { |
abeb40e5 | 227 | ssize_t m, n; |
d35825da NP |
228 | m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ? |
229 | partial_pack_offset : aligned_sz; | |
abeb40e5 | 230 | n = xread(pack_fd, buf, m); |
8b0eca7c DH |
231 | if (!n) |
232 | break; | |
233 | if (n < 0) | |
d824cbba | 234 | die_errno("Failed to checksum '%s'", pack_name); |
9126f009 | 235 | git_SHA1_Update(&new_sha1_ctx, buf, n); |
abeb40e5 | 236 | |
d35825da NP |
237 | aligned_sz -= n; |
238 | if (!aligned_sz) | |
239 | aligned_sz = buf_sz; | |
240 | ||
abeb40e5 NP |
241 | if (!partial_pack_sha1) |
242 | continue; | |
243 | ||
9126f009 | 244 | git_SHA1_Update(&old_sha1_ctx, buf, n); |
abeb40e5 NP |
245 | partial_pack_offset -= n; |
246 | if (partial_pack_offset == 0) { | |
247 | unsigned char sha1[20]; | |
9126f009 | 248 | git_SHA1_Final(sha1, &old_sha1_ctx); |
abeb40e5 NP |
249 | if (hashcmp(sha1, partial_pack_sha1) != 0) |
250 | die("Unexpected checksum for %s " | |
251 | "(disk corruption?)", pack_name); | |
252 | /* | |
253 | * Now let's compute the SHA1 of the remainder of the | |
254 | * pack, which also means making partial_pack_offset | |
255 | * big enough not to matter anymore. | |
256 | */ | |
9126f009 | 257 | git_SHA1_Init(&old_sha1_ctx); |
abeb40e5 NP |
258 | partial_pack_offset = ~partial_pack_offset; |
259 | partial_pack_offset -= MSB(partial_pack_offset, 1); | |
260 | } | |
8b0eca7c DH |
261 | } |
262 | free(buf); | |
263 | ||
abeb40e5 | 264 | if (partial_pack_sha1) |
9126f009 NP |
265 | git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx); |
266 | git_SHA1_Final(new_pack_sha1, &new_sha1_ctx); | |
abeb40e5 | 267 | write_or_die(pack_fd, new_pack_sha1, 20); |
0c68d386 | 268 | fsync_or_die(pack_fd, pack_name); |
8b0eca7c | 269 | } |
106764e6 SP |
270 | |
271 | char *index_pack_lockfile(int ip_out) | |
272 | { | |
106764e6 SP |
273 | char packname[46]; |
274 | ||
275 | /* | |
6e180cdc | 276 | * The first thing we expect from index-pack's output |
106764e6 SP |
277 | * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where |
278 | * %40s is the newly created pack SHA1 name. In the "keep" | |
279 | * case, we need it to remove the corresponding .keep file | |
280 | * later on. If we don't get that then tough luck with it. | |
281 | */ | |
c697ad14 HO |
282 | if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' && |
283 | memcmp(packname, "keep\t", 5) == 0) { | |
106764e6 SP |
284 | char path[PATH_MAX]; |
285 | packname[45] = 0; | |
286 | snprintf(path, sizeof(path), "%s/pack/pack-%s.keep", | |
287 | get_object_directory(), packname + 5); | |
288 | return xstrdup(path); | |
289 | } | |
290 | return NULL; | |
291 | } | |
f965c525 NP |
292 | |
293 | /* | |
294 | * The per-object header is a pretty dense thing, which is | |
295 | * - first byte: low four bits are "size", then three bits of "type", | |
296 | * and the high bit is "size continues". | |
297 | * - each byte afterwards: low seven bits are size continuation, | |
298 | * with the high bit being "size continues" | |
299 | */ | |
300 | int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr) | |
301 | { | |
302 | int n = 1; | |
303 | unsigned char c; | |
304 | ||
305 | if (type < OBJ_COMMIT || type > OBJ_REF_DELTA) | |
306 | die("bad type %d", type); | |
307 | ||
308 | c = (type << 4) | (size & 15); | |
309 | size >>= 4; | |
310 | while (size) { | |
311 | *hdr++ = c | 0x80; | |
312 | c = size & 0x7f; | |
313 | size >>= 7; | |
314 | n++; | |
315 | } | |
316 | *hdr = c; | |
317 | return n; | |
318 | } |