5 void reset_pack_idx_option(struct pack_idx_option *opts)
7 memset(opts, 0, sizeof(*opts));
9 opts->off32_limit = 0x7fffffff;
12 static int sha1_compare(const void *_a, const void *_b)
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);
19 static int cmp_uint32(const void *a_, const void *b_)
21 uint32_t a = *((uint32_t *)a_);
22 uint32_t b = *((uint32_t *)b_);
24 return (a < b) ? -1 : (a != b);
27 static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
31 if ((offset >> 31) || (opts->off32_limit < offset))
33 if (!opts->anomaly_nr)
36 return !!bsearch(&ofsval, opts->anomaly, opts->anomaly_nr,
37 sizeof(ofsval), cmp_uint32);
41 * The *sha1 contains the pack content SHA1 hash.
42 * The objects array passed in will be sorted by SHA1 on exit.
44 const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
45 int nr_objects, const struct pack_idx_option *opts,
46 const unsigned char *sha1)
49 struct pack_idx_entry **sorted_by_sha, **list, **last;
50 off_t last_obj_offset = 0;
53 uint32_t index_version;
56 sorted_by_sha = objects;
58 last = sorted_by_sha + nr_objects;
59 for (i = 0; i < nr_objects; ++i) {
60 if (objects[i]->offset > last_obj_offset)
61 last_obj_offset = objects[i]->offset;
63 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
67 sorted_by_sha = list = last = NULL;
69 if (opts->flags & WRITE_IDX_VERIFY) {
71 f = sha1fd_check(index_name);
74 static char tmp_file[PATH_MAX];
75 fd = odb_mkstemp(tmp_file, sizeof(tmp_file), "pack/tmp_idx_XXXXXX");
76 index_name = xstrdup(tmp_file);
79 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
82 die_errno("unable to create '%s'", index_name);
83 f = sha1fd(fd, index_name);
86 /* if last object's offset is >= 2^31 we should use index V2 */
87 index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version;
89 /* index versions 2 and above need a header */
90 if (index_version >= 2) {
91 struct pack_idx_header hdr;
92 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
93 hdr.idx_version = htonl(index_version);
94 sha1write(f, &hdr, sizeof(hdr));
98 * Write the first-level table (the list is sorted,
99 * but we use a 256-entry lookup to be able to avoid
100 * having to do eight extra binary search iterations).
102 for (i = 0; i < 256; i++) {
103 struct pack_idx_entry **next = list;
104 while (next < last) {
105 struct pack_idx_entry *obj = *next;
106 if (obj->sha1[0] != i)
110 array[i] = htonl(next - sorted_by_sha);
113 sha1write(f, array, 256 * 4);
116 * Write the actual SHA1 entries..
118 list = sorted_by_sha;
119 for (i = 0; i < nr_objects; i++) {
120 struct pack_idx_entry *obj = *list++;
121 if (index_version < 2) {
122 uint32_t offset = htonl(obj->offset);
123 sha1write(f, &offset, 4);
125 sha1write(f, obj->sha1, 20);
126 if ((opts->flags & WRITE_IDX_STRICT) &&
127 (i && !hashcmp(list[-2]->sha1, obj->sha1)))
128 die("The same object %s appears twice in the pack",
129 sha1_to_hex(obj->sha1));
132 if (index_version >= 2) {
133 unsigned int nr_large_offset = 0;
135 /* write the crc32 table */
136 list = sorted_by_sha;
137 for (i = 0; i < nr_objects; i++) {
138 struct pack_idx_entry *obj = *list++;
139 uint32_t crc32_val = htonl(obj->crc32);
140 sha1write(f, &crc32_val, 4);
143 /* write the 32-bit offset table */
144 list = sorted_by_sha;
145 for (i = 0; i < nr_objects; i++) {
146 struct pack_idx_entry *obj = *list++;
149 offset = (need_large_offset(obj->offset, opts)
150 ? (0x80000000 | nr_large_offset++)
152 offset = htonl(offset);
153 sha1write(f, &offset, 4);
156 /* write the large offset table */
157 list = sorted_by_sha;
158 while (nr_large_offset) {
159 struct pack_idx_entry *obj = *list++;
160 uint64_t offset = obj->offset;
163 if (!need_large_offset(offset, opts))
165 split[0] = htonl(offset >> 32);
166 split[1] = htonl(offset & 0xffffffff);
167 sha1write(f, split, 8);
172 sha1write(f, sha1, 20);
173 sha1close(f, NULL, ((opts->flags & WRITE_IDX_VERIFY)
174 ? CSUM_CLOSE : CSUM_FSYNC));
178 off_t write_pack_header(struct sha1file *f, uint32_t nr_entries)
180 struct pack_header hdr;
182 hdr.hdr_signature = htonl(PACK_SIGNATURE);
183 hdr.hdr_version = htonl(PACK_VERSION);
184 hdr.hdr_entries = htonl(nr_entries);
185 if (sha1write(f, &hdr, sizeof(hdr)))
191 * Update pack header with object_count and compute new SHA1 for pack data
192 * associated to pack_fd, and write that SHA1 at the end. That new SHA1
193 * is also returned in new_pack_sha1.
195 * If partial_pack_sha1 is non null, then the SHA1 of the existing pack
196 * (without the header update) is computed and validated against the
197 * one provided in partial_pack_sha1. The validation is performed at
198 * partial_pack_offset bytes in the pack file. The SHA1 of the remaining
199 * data (i.e. from partial_pack_offset to the end) is then computed and
200 * returned in partial_pack_sha1.
202 * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and
203 * partial_pack_sha1 can refer to the same buffer if the caller is not
204 * interested in the resulting SHA1 of pack data above partial_pack_offset.
206 void fixup_pack_header_footer(int pack_fd,
207 unsigned char *new_pack_sha1,
208 const char *pack_name,
209 uint32_t object_count,
210 unsigned char *partial_pack_sha1,
211 off_t partial_pack_offset)
213 int aligned_sz, buf_sz = 8 * 1024;
214 git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
215 struct pack_header hdr;
218 git_SHA1_Init(&old_sha1_ctx);
219 git_SHA1_Init(&new_sha1_ctx);
221 if (lseek(pack_fd, 0, SEEK_SET) != 0)
222 die_errno("Failed seeking to start of '%s'", pack_name);
223 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
224 die_errno("Unable to reread header of '%s'", pack_name);
225 if (lseek(pack_fd, 0, SEEK_SET) != 0)
226 die_errno("Failed seeking to start of '%s'", pack_name);
227 git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
228 hdr.hdr_entries = htonl(object_count);
229 git_SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr));
230 write_or_die(pack_fd, &hdr, sizeof(hdr));
231 partial_pack_offset -= sizeof(hdr);
233 buf = xmalloc(buf_sz);
234 aligned_sz = buf_sz - sizeof(hdr);
237 m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ?
238 partial_pack_offset : aligned_sz;
239 n = xread(pack_fd, buf, m);
243 die_errno("Failed to checksum '%s'", pack_name);
244 git_SHA1_Update(&new_sha1_ctx, buf, n);
250 if (!partial_pack_sha1)
253 git_SHA1_Update(&old_sha1_ctx, buf, n);
254 partial_pack_offset -= n;
255 if (partial_pack_offset == 0) {
256 unsigned char sha1[20];
257 git_SHA1_Final(sha1, &old_sha1_ctx);
258 if (hashcmp(sha1, partial_pack_sha1) != 0)
259 die("Unexpected checksum for %s "
260 "(disk corruption?)", pack_name);
262 * Now let's compute the SHA1 of the remainder of the
263 * pack, which also means making partial_pack_offset
264 * big enough not to matter anymore.
266 git_SHA1_Init(&old_sha1_ctx);
267 partial_pack_offset = ~partial_pack_offset;
268 partial_pack_offset -= MSB(partial_pack_offset, 1);
273 if (partial_pack_sha1)
274 git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx);
275 git_SHA1_Final(new_pack_sha1, &new_sha1_ctx);
276 write_or_die(pack_fd, new_pack_sha1, 20);
277 fsync_or_die(pack_fd, pack_name);
280 char *index_pack_lockfile(int ip_out)
285 * The first thing we expect from index-pack's output
286 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
287 * %40s is the newly created pack SHA1 name. In the "keep"
288 * case, we need it to remove the corresponding .keep file
289 * later on. If we don't get that then tough luck with it.
291 if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' &&
292 memcmp(packname, "keep\t", 5) == 0) {
295 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
296 get_object_directory(), packname + 5);
297 return xstrdup(path);
303 * The per-object header is a pretty dense thing, which is
304 * - first byte: low four bits are "size", then three bits of "type",
305 * and the high bit is "size continues".
306 * - each byte afterwards: low seven bits are size continuation,
307 * with the high bit being "size continues"
309 int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
314 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
315 die("bad type %d", type);
317 c = (type << 4) | (size & 15);
329 struct sha1file *create_tmp_packfile(char **pack_tmp_name)
331 char tmpname[PATH_MAX];
334 fd = odb_mkstemp(tmpname, sizeof(tmpname), "pack/tmp_pack_XXXXXX");
335 *pack_tmp_name = xstrdup(tmpname);
336 return sha1fd(fd, *pack_tmp_name);
339 void finish_tmp_packfile(char *name_buffer,
340 const char *pack_tmp_name,
341 struct pack_idx_entry **written_list,
343 struct pack_idx_option *pack_idx_opts,
344 unsigned char sha1[])
346 const char *idx_tmp_name;
347 char *end_of_name_prefix = strrchr(name_buffer, 0);
349 if (adjust_shared_perm(pack_tmp_name))
350 die_errno("unable to make temporary pack file readable");
352 idx_tmp_name = write_idx_file(NULL, written_list, nr_written,
353 pack_idx_opts, sha1);
354 if (adjust_shared_perm(idx_tmp_name))
355 die_errno("unable to make temporary index file readable");
357 sprintf(end_of_name_prefix, "%s.pack", sha1_to_hex(sha1));
358 free_pack_by_name(name_buffer);
360 if (rename(pack_tmp_name, name_buffer))
361 die_errno("unable to rename temporary pack file");
363 sprintf(end_of_name_prefix, "%s.idx", sha1_to_hex(sha1));
364 if (rename(idx_tmp_name, name_buffer))
365 die_errno("unable to rename temporary index file");
367 free((void *)idx_tmp_name);