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