1#include "cache.h"
2#include "pack.h"
3#include "csum-file.h"
4
5void 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}
11
12static 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
19static 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
27static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
28{
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);
38}
39
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 */
45const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
46 int nr_objects, const struct pack_idx_option *opts,
47 const unsigned char *sha1)
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;
54 uint32_t index_version;
55
56 if (nr_objects) {
57 sorted_by_sha = objects;
58 list = sorted_by_sha;
59 last = sorted_by_sha + nr_objects;
60 for (i = 0; i < nr_objects; ++i) {
61 if (objects[i]->offset > last_obj_offset)
62 last_obj_offset = objects[i]->offset;
63 }
64 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
65 sha1_compare);
66 }
67 else
68 sorted_by_sha = list = last = NULL;
69
70 if (opts->flags & WRITE_IDX_VERIFY) {
71 assert(index_name);
72 f = sha1fd_check(index_name);
73 } else {
74 if (!index_name) {
75 static char tmp_file[PATH_MAX];
76 fd = odb_mkstemp(tmp_file, sizeof(tmp_file), "pack/tmp_idx_XXXXXX");
77 index_name = xstrdup(tmp_file);
78 } else {
79 unlink(index_name);
80 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
81 }
82 if (fd < 0)
83 die_errno("unable to create '%s'", index_name);
84 f = sha1fd(fd, index_name);
85 }
86
87 /* if last object's offset is >= 2^31 we should use index V2 */
88 index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version;
89
90 /* index versions 2 and above need a header */
91 if (index_version >= 2) {
92 struct pack_idx_header hdr;
93 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
94 hdr.idx_version = htonl(index_version);
95 sha1write(f, &hdr, sizeof(hdr));
96 }
97
98 /*
99 * Write the first-level table (the list is sorted,
100 * but we use a 256-entry lookup to be able to avoid
101 * having to do eight extra binary search iterations).
102 */
103 for (i = 0; i < 256; i++) {
104 struct pack_idx_entry **next = list;
105 while (next < last) {
106 struct pack_idx_entry *obj = *next;
107 if (obj->sha1[0] != i)
108 break;
109 next++;
110 }
111 array[i] = htonl(next - sorted_by_sha);
112 list = next;
113 }
114 sha1write(f, array, 256 * 4);
115
116 /*
117 * Write the actual SHA1 entries..
118 */
119 list = sorted_by_sha;
120 for (i = 0; i < nr_objects; i++) {
121 struct pack_idx_entry *obj = *list++;
122 if (index_version < 2) {
123 uint32_t offset = htonl(obj->offset);
124 sha1write(f, &offset, 4);
125 }
126 sha1write(f, obj->sha1, 20);
127 if ((opts->flags & WRITE_IDX_STRICT) &&
128 (i && !hashcmp(list[-2]->sha1, obj->sha1)))
129 die("The same object %s appears twice in the pack",
130 sha1_to_hex(obj->sha1));
131 }
132
133 if (index_version >= 2) {
134 unsigned int nr_large_offset = 0;
135
136 /* write the crc32 table */
137 list = sorted_by_sha;
138 for (i = 0; i < nr_objects; i++) {
139 struct pack_idx_entry *obj = *list++;
140 uint32_t crc32_val = htonl(obj->crc32);
141 sha1write(f, &crc32_val, 4);
142 }
143
144 /* write the 32-bit offset table */
145 list = sorted_by_sha;
146 for (i = 0; i < nr_objects; i++) {
147 struct pack_idx_entry *obj = *list++;
148 uint32_t offset;
149
150 offset = (need_large_offset(obj->offset, opts)
151 ? (0x80000000 | nr_large_offset++)
152 : obj->offset);
153 offset = htonl(offset);
154 sha1write(f, &offset, 4);
155 }
156
157 /* write the large offset table */
158 list = sorted_by_sha;
159 while (nr_large_offset) {
160 struct pack_idx_entry *obj = *list++;
161 uint64_t offset = obj->offset;
162 uint32_t split[2];
163
164 if (!need_large_offset(offset, opts))
165 continue;
166 split[0] = htonl(offset >> 32);
167 split[1] = htonl(offset & 0xffffffff);
168 sha1write(f, split, 8);
169 nr_large_offset--;
170 }
171 }
172
173 sha1write(f, sha1, 20);
174 sha1close(f, NULL, ((opts->flags & WRITE_IDX_VERIFY)
175 ? CSUM_CLOSE : CSUM_FSYNC));
176 return index_name;
177}
178
179off_t write_pack_header(struct sha1file *f, uint32_t nr_entries)
180{
181 struct pack_header hdr;
182
183 hdr.hdr_signature = htonl(PACK_SIGNATURE);
184 hdr.hdr_version = htonl(PACK_VERSION);
185 hdr.hdr_entries = htonl(nr_entries);
186 if (sha1write(f, &hdr, sizeof(hdr)))
187 return 0;
188 return sizeof(hdr);
189}
190
191/*
192 * Update pack header with object_count and compute new SHA1 for pack data
193 * associated to pack_fd, and write that SHA1 at the end. That new SHA1
194 * is also returned in new_pack_sha1.
195 *
196 * If partial_pack_sha1 is non null, then the SHA1 of the existing pack
197 * (without the header update) is computed and validated against the
198 * one provided in partial_pack_sha1. The validation is performed at
199 * partial_pack_offset bytes in the pack file. The SHA1 of the remaining
200 * data (i.e. from partial_pack_offset to the end) is then computed and
201 * returned in partial_pack_sha1.
202 *
203 * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and
204 * partial_pack_sha1 can refer to the same buffer if the caller is not
205 * interested in the resulting SHA1 of pack data above partial_pack_offset.
206 */
207void fixup_pack_header_footer(int pack_fd,
208 unsigned char *new_pack_sha1,
209 const char *pack_name,
210 uint32_t object_count,
211 unsigned char *partial_pack_sha1,
212 off_t partial_pack_offset)
213{
214 int aligned_sz, buf_sz = 8 * 1024;
215 git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
216 struct pack_header hdr;
217 char *buf;
218
219 git_SHA1_Init(&old_sha1_ctx);
220 git_SHA1_Init(&new_sha1_ctx);
221
222 if (lseek(pack_fd, 0, SEEK_SET) != 0)
223 die_errno("Failed seeking to start of '%s'", pack_name);
224 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
225 die_errno("Unable to reread header of '%s'", pack_name);
226 if (lseek(pack_fd, 0, SEEK_SET) != 0)
227 die_errno("Failed seeking to start of '%s'", pack_name);
228 git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
229 hdr.hdr_entries = htonl(object_count);
230 git_SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr));
231 write_or_die(pack_fd, &hdr, sizeof(hdr));
232 partial_pack_offset -= sizeof(hdr);
233
234 buf = xmalloc(buf_sz);
235 aligned_sz = buf_sz - sizeof(hdr);
236 for (;;) {
237 ssize_t m, n;
238 m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ?
239 partial_pack_offset : aligned_sz;
240 n = xread(pack_fd, buf, m);
241 if (!n)
242 break;
243 if (n < 0)
244 die_errno("Failed to checksum '%s'", pack_name);
245 git_SHA1_Update(&new_sha1_ctx, buf, n);
246
247 aligned_sz -= n;
248 if (!aligned_sz)
249 aligned_sz = buf_sz;
250
251 if (!partial_pack_sha1)
252 continue;
253
254 git_SHA1_Update(&old_sha1_ctx, buf, n);
255 partial_pack_offset -= n;
256 if (partial_pack_offset == 0) {
257 unsigned char sha1[20];
258 git_SHA1_Final(sha1, &old_sha1_ctx);
259 if (hashcmp(sha1, partial_pack_sha1) != 0)
260 die("Unexpected checksum for %s "
261 "(disk corruption?)", pack_name);
262 /*
263 * Now let's compute the SHA1 of the remainder of the
264 * pack, which also means making partial_pack_offset
265 * big enough not to matter anymore.
266 */
267 git_SHA1_Init(&old_sha1_ctx);
268 partial_pack_offset = ~partial_pack_offset;
269 partial_pack_offset -= MSB(partial_pack_offset, 1);
270 }
271 }
272 free(buf);
273
274 if (partial_pack_sha1)
275 git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx);
276 git_SHA1_Final(new_pack_sha1, &new_sha1_ctx);
277 write_or_die(pack_fd, new_pack_sha1, 20);
278 fsync_or_die(pack_fd, pack_name);
279}
280
281char *index_pack_lockfile(int ip_out)
282{
283 char packname[46];
284
285 /*
286 * The first thing we expect from index-pack's output
287 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
288 * %40s is the newly created pack SHA1 name. In the "keep"
289 * case, we need it to remove the corresponding .keep file
290 * later on. If we don't get that then tough luck with it.
291 */
292 if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' &&
293 memcmp(packname, "keep\t", 5) == 0) {
294 char path[PATH_MAX];
295 packname[45] = 0;
296 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
297 get_object_directory(), packname + 5);
298 return xstrdup(path);
299 }
300 return NULL;
301}
302
303/*
304 * The per-object header is a pretty dense thing, which is
305 * - first byte: low four bits are "size", then three bits of "type",
306 * and the high bit is "size continues".
307 * - each byte afterwards: low seven bits are size continuation,
308 * with the high bit being "size continues"
309 */
310int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
311{
312 int n = 1;
313 unsigned char c;
314
315 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
316 die("bad type %d", type);
317
318 c = (type << 4) | (size & 15);
319 size >>= 4;
320 while (size) {
321 *hdr++ = c | 0x80;
322 c = size & 0x7f;
323 size >>= 7;
324 n++;
325 }
326 *hdr = c;
327 return n;
328}
329
330struct sha1file *create_tmp_packfile(char **pack_tmp_name)
331{
332 char tmpname[PATH_MAX];
333 int fd;
334
335 fd = odb_mkstemp(tmpname, sizeof(tmpname), "pack/tmp_pack_XXXXXX");
336 *pack_tmp_name = xstrdup(tmpname);
337 return sha1fd(fd, *pack_tmp_name);
338}
339
340void finish_tmp_packfile(char *name_buffer,
341 const char *pack_tmp_name,
342 struct pack_idx_entry **written_list,
343 uint32_t nr_written,
344 struct pack_idx_option *pack_idx_opts,
345 unsigned char sha1[])
346{
347 const char *idx_tmp_name;
348 char *end_of_name_prefix = strrchr(name_buffer, 0);
349
350 if (adjust_shared_perm(pack_tmp_name))
351 die_errno("unable to make temporary pack file readable");
352
353 idx_tmp_name = write_idx_file(NULL, written_list, nr_written,
354 pack_idx_opts, sha1);
355 if (adjust_shared_perm(idx_tmp_name))
356 die_errno("unable to make temporary index file readable");
357
358 sprintf(end_of_name_prefix, "%s.pack", sha1_to_hex(sha1));
359 free_pack_by_name(name_buffer);
360
361 if (rename(pack_tmp_name, name_buffer))
362 die_errno("unable to rename temporary pack file");
363
364 sprintf(end_of_name_prefix, "%s.idx", sha1_to_hex(sha1));
365 if (rename(idx_tmp_name, name_buffer))
366 die_errno("unable to rename temporary index file");
367
368 free((void *)idx_tmp_name);
369}