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 need_large_offset(off_t offset, const struct pack_idx_option *opts)
21 return (offset >> 31) || (opts->off32_limit < offset);
25 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
26 * the SHA1 hash of sorted object names. The objects array passed in
27 * will be sorted by SHA1 on exit.
29 const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
30 int nr_objects, const struct pack_idx_option *opts,
34 struct pack_idx_entry **sorted_by_sha, **list, **last;
35 off_t last_obj_offset = 0;
39 uint32_t index_version;
42 sorted_by_sha = objects;
44 last = sorted_by_sha + nr_objects;
45 for (i = 0; i < nr_objects; ++i) {
46 if (objects[i]->offset > last_obj_offset)
47 last_obj_offset = objects[i]->offset;
49 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
53 sorted_by_sha = list = last = NULL;
55 if (opts->flags & WRITE_IDX_VERIFY) {
57 f = sha1fd_check(index_name);
60 static char tmpfile[PATH_MAX];
61 fd = odb_mkstemp(tmpfile, sizeof(tmpfile), "pack/tmp_idx_XXXXXX");
62 index_name = xstrdup(tmpfile);
65 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
68 die_errno("unable to create '%s'", index_name);
69 f = sha1fd(fd, index_name);
72 /* if last object's offset is >= 2^31 we should use index V2 */
73 index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version;
75 /* index versions 2 and above need a header */
76 if (index_version >= 2) {
77 struct pack_idx_header hdr;
78 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
79 hdr.idx_version = htonl(index_version);
80 sha1write(f, &hdr, sizeof(hdr));
84 * Write the first-level table (the list is sorted,
85 * but we use a 256-entry lookup to be able to avoid
86 * having to do eight extra binary search iterations).
88 for (i = 0; i < 256; i++) {
89 struct pack_idx_entry **next = list;
91 struct pack_idx_entry *obj = *next;
92 if (obj->sha1[0] != i)
96 array[i] = htonl(next - sorted_by_sha);
99 sha1write(f, array, 256 * 4);
101 /* compute the SHA1 hash of sorted object names. */
105 * Write the actual SHA1 entries..
107 list = sorted_by_sha;
108 for (i = 0; i < nr_objects; i++) {
109 struct pack_idx_entry *obj = *list++;
110 if (index_version < 2) {
111 uint32_t offset = htonl(obj->offset);
112 sha1write(f, &offset, 4);
114 sha1write(f, obj->sha1, 20);
115 git_SHA1_Update(&ctx, obj->sha1, 20);
118 if (index_version >= 2) {
119 unsigned int nr_large_offset = 0;
121 /* write the crc32 table */
122 list = sorted_by_sha;
123 for (i = 0; i < nr_objects; i++) {
124 struct pack_idx_entry *obj = *list++;
125 uint32_t crc32_val = htonl(obj->crc32);
126 sha1write(f, &crc32_val, 4);
129 /* write the 32-bit offset table */
130 list = sorted_by_sha;
131 for (i = 0; i < nr_objects; i++) {
132 struct pack_idx_entry *obj = *list++;
135 offset = (need_large_offset(obj->offset, opts)
136 ? (0x80000000 | nr_large_offset++)
138 offset = htonl(offset);
139 sha1write(f, &offset, 4);
142 /* write the large offset table */
143 list = sorted_by_sha;
144 while (nr_large_offset) {
145 struct pack_idx_entry *obj = *list++;
146 uint64_t offset = obj->offset;
149 if (!need_large_offset(offset, opts))
151 split[0] = htonl(offset >> 32);
152 split[1] = htonl(offset & 0xffffffff);
153 sha1write(f, split, 8);
158 sha1write(f, sha1, 20);
159 sha1close(f, NULL, ((opts->flags & WRITE_IDX_VERIFY)
160 ? CSUM_CLOSE : CSUM_FSYNC));
161 git_SHA1_Final(sha1, &ctx);
166 * Update pack header with object_count and compute new SHA1 for pack data
167 * associated to pack_fd, and write that SHA1 at the end. That new SHA1
168 * is also returned in new_pack_sha1.
170 * If partial_pack_sha1 is non null, then the SHA1 of the existing pack
171 * (without the header update) is computed and validated against the
172 * one provided in partial_pack_sha1. The validation is performed at
173 * partial_pack_offset bytes in the pack file. The SHA1 of the remaining
174 * data (i.e. from partial_pack_offset to the end) is then computed and
175 * returned in partial_pack_sha1.
177 * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and
178 * partial_pack_sha1 can refer to the same buffer if the caller is not
179 * interested in the resulting SHA1 of pack data above partial_pack_offset.
181 void fixup_pack_header_footer(int pack_fd,
182 unsigned char *new_pack_sha1,
183 const char *pack_name,
184 uint32_t object_count,
185 unsigned char *partial_pack_sha1,
186 off_t partial_pack_offset)
188 int aligned_sz, buf_sz = 8 * 1024;
189 git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
190 struct pack_header hdr;
193 git_SHA1_Init(&old_sha1_ctx);
194 git_SHA1_Init(&new_sha1_ctx);
196 if (lseek(pack_fd, 0, SEEK_SET) != 0)
197 die_errno("Failed seeking to start of '%s'", pack_name);
198 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
199 die_errno("Unable to reread header of '%s'", pack_name);
200 if (lseek(pack_fd, 0, SEEK_SET) != 0)
201 die_errno("Failed seeking to start of '%s'", pack_name);
202 git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
203 hdr.hdr_entries = htonl(object_count);
204 git_SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr));
205 write_or_die(pack_fd, &hdr, sizeof(hdr));
206 partial_pack_offset -= sizeof(hdr);
208 buf = xmalloc(buf_sz);
209 aligned_sz = buf_sz - sizeof(hdr);
212 m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ?
213 partial_pack_offset : aligned_sz;
214 n = xread(pack_fd, buf, m);
218 die_errno("Failed to checksum '%s'", pack_name);
219 git_SHA1_Update(&new_sha1_ctx, buf, n);
225 if (!partial_pack_sha1)
228 git_SHA1_Update(&old_sha1_ctx, buf, n);
229 partial_pack_offset -= n;
230 if (partial_pack_offset == 0) {
231 unsigned char sha1[20];
232 git_SHA1_Final(sha1, &old_sha1_ctx);
233 if (hashcmp(sha1, partial_pack_sha1) != 0)
234 die("Unexpected checksum for %s "
235 "(disk corruption?)", pack_name);
237 * Now let's compute the SHA1 of the remainder of the
238 * pack, which also means making partial_pack_offset
239 * big enough not to matter anymore.
241 git_SHA1_Init(&old_sha1_ctx);
242 partial_pack_offset = ~partial_pack_offset;
243 partial_pack_offset -= MSB(partial_pack_offset, 1);
248 if (partial_pack_sha1)
249 git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx);
250 git_SHA1_Final(new_pack_sha1, &new_sha1_ctx);
251 write_or_die(pack_fd, new_pack_sha1, 20);
252 fsync_or_die(pack_fd, pack_name);
255 char *index_pack_lockfile(int ip_out)
260 * The first thing we expect from index-pack's output
261 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
262 * %40s is the newly created pack SHA1 name. In the "keep"
263 * case, we need it to remove the corresponding .keep file
264 * later on. If we don't get that then tough luck with it.
266 if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' &&
267 memcmp(packname, "keep\t", 5) == 0) {
270 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
271 get_object_directory(), packname + 5);
272 return xstrdup(path);
278 * The per-object header is a pretty dense thing, which is
279 * - first byte: low four bits are "size", then three bits of "type",
280 * and the high bit is "size continues".
281 * - each byte afterwards: low seven bits are size continuation,
282 * with the high bit being "size continues"
284 int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
289 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
290 die("bad type %d", type);
292 c = (type << 4) | (size & 15);