improve index-pack tests
[git/spearce.git] / pack-write.c
blobb426006c5851c98fce8894bd9f76cd51a7cde170
1 #include "cache.h"
2 #include "pack.h"
3 #include "csum-file.h"
5 uint32_t pack_idx_default_version = 2;
6 uint32_t pack_idx_off32_limit = 0x7fffffff;
8 static int sha1_compare(const void *_a, const void *_b)
10 struct pack_idx_entry *a = *(struct pack_idx_entry **)_a;
11 struct pack_idx_entry *b = *(struct pack_idx_entry **)_b;
12 return hashcmp(a->sha1, b->sha1);
16 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
17 * the SHA1 hash of sorted object names. The objects array passed in
18 * will be sorted by SHA1 on exit.
20 char *write_idx_file(char *index_name, struct pack_idx_entry **objects,
21 int nr_objects, unsigned char *sha1)
23 struct sha1file *f;
24 struct pack_idx_entry **sorted_by_sha, **list, **last;
25 off_t last_obj_offset = 0;
26 uint32_t array[256];
27 int i, fd;
28 git_SHA_CTX ctx;
29 uint32_t index_version;
31 if (nr_objects) {
32 sorted_by_sha = objects;
33 list = sorted_by_sha;
34 last = sorted_by_sha + nr_objects;
35 for (i = 0; i < nr_objects; ++i) {
36 if (objects[i]->offset > last_obj_offset)
37 last_obj_offset = objects[i]->offset;
39 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
40 sha1_compare);
42 else
43 sorted_by_sha = list = last = NULL;
45 if (!index_name) {
46 static char tmpfile[PATH_MAX];
47 snprintf(tmpfile, sizeof(tmpfile),
48 "%s/pack/tmp_idx_XXXXXX", get_object_directory());
49 fd = xmkstemp(tmpfile);
50 index_name = xstrdup(tmpfile);
51 } else {
52 unlink(index_name);
53 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
55 if (fd < 0)
56 die("unable to create %s: %s", index_name, strerror(errno));
57 f = sha1fd(fd, index_name);
59 /* if last object's offset is >= 2^31 we should use index V2 */
60 index_version = (last_obj_offset >> 31) ? 2 : pack_idx_default_version;
62 /* index versions 2 and above need a header */
63 if (index_version >= 2) {
64 struct pack_idx_header hdr;
65 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
66 hdr.idx_version = htonl(index_version);
67 sha1write(f, &hdr, sizeof(hdr));
71 * Write the first-level table (the list is sorted,
72 * but we use a 256-entry lookup to be able to avoid
73 * having to do eight extra binary search iterations).
75 for (i = 0; i < 256; i++) {
76 struct pack_idx_entry **next = list;
77 while (next < last) {
78 struct pack_idx_entry *obj = *next;
79 if (obj->sha1[0] != i)
80 break;
81 next++;
83 array[i] = htonl(next - sorted_by_sha);
84 list = next;
86 sha1write(f, array, 256 * 4);
88 /* compute the SHA1 hash of sorted object names. */
89 git_SHA1_Init(&ctx);
92 * Write the actual SHA1 entries..
94 list = sorted_by_sha;
95 for (i = 0; i < nr_objects; i++) {
96 struct pack_idx_entry *obj = *list++;
97 if (index_version < 2) {
98 uint32_t offset = htonl(obj->offset);
99 sha1write(f, &offset, 4);
101 sha1write(f, obj->sha1, 20);
102 git_SHA1_Update(&ctx, obj->sha1, 20);
105 if (index_version >= 2) {
106 unsigned int nr_large_offset = 0;
108 /* write the crc32 table */
109 list = sorted_by_sha;
110 for (i = 0; i < nr_objects; i++) {
111 struct pack_idx_entry *obj = *list++;
112 uint32_t crc32_val = htonl(obj->crc32);
113 sha1write(f, &crc32_val, 4);
116 /* write the 32-bit offset table */
117 list = sorted_by_sha;
118 for (i = 0; i < nr_objects; i++) {
119 struct pack_idx_entry *obj = *list++;
120 uint32_t offset = (obj->offset <= pack_idx_off32_limit) ?
121 obj->offset : (0x80000000 | nr_large_offset++);
122 offset = htonl(offset);
123 sha1write(f, &offset, 4);
126 /* write the large offset table */
127 list = sorted_by_sha;
128 while (nr_large_offset) {
129 struct pack_idx_entry *obj = *list++;
130 uint64_t offset = obj->offset;
131 if (offset > pack_idx_off32_limit) {
132 uint32_t split[2];
133 split[0] = htonl(offset >> 32);
134 split[1] = htonl(offset & 0xffffffff);
135 sha1write(f, split, 8);
136 nr_large_offset--;
141 sha1write(f, sha1, 20);
142 sha1close(f, NULL, CSUM_FSYNC);
143 git_SHA1_Final(sha1, &ctx);
144 return index_name;
148 * Update pack header with object_count and compute new SHA1 for pack data
149 * associated to pack_fd, and write that SHA1 at the end. That new SHA1
150 * is also returned in new_pack_sha1.
152 * If partial_pack_sha1 is non null, then the SHA1 of the existing pack
153 * (without the header update) is computed and validated against the
154 * one provided in partial_pack_sha1. The validation is performed at
155 * partial_pack_offset bytes in the pack file. The SHA1 of the remaining
156 * data (i.e. from partial_pack_offset to the end) is then computed and
157 * returned in partial_pack_sha1.
159 * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and
160 * partial_pack_sha1 can refer to the same buffer if the caller is not
161 * interested in the resulting SHA1 of pack data above partial_pack_offset.
163 void fixup_pack_header_footer(int pack_fd,
164 unsigned char *new_pack_sha1,
165 const char *pack_name,
166 uint32_t object_count,
167 unsigned char *partial_pack_sha1,
168 off_t partial_pack_offset)
170 int aligned_sz, buf_sz = 8 * 1024;
171 git_SHA_CTX old_sha1_ctx, new_sha1_ctx;
172 struct pack_header hdr;
173 char *buf;
175 git_SHA1_Init(&old_sha1_ctx);
176 git_SHA1_Init(&new_sha1_ctx);
178 if (lseek(pack_fd, 0, SEEK_SET) != 0)
179 die("Failed seeking to start of %s: %s", pack_name, strerror(errno));
180 if (read_in_full(pack_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
181 die("Unable to reread header of %s: %s", pack_name, strerror(errno));
182 if (lseek(pack_fd, 0, SEEK_SET) != 0)
183 die("Failed seeking to start of %s: %s", pack_name, strerror(errno));
184 git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
185 hdr.hdr_entries = htonl(object_count);
186 git_SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr));
187 write_or_die(pack_fd, &hdr, sizeof(hdr));
188 partial_pack_offset -= sizeof(hdr);
190 buf = xmalloc(buf_sz);
191 aligned_sz = buf_sz - sizeof(hdr);
192 for (;;) {
193 ssize_t m, n;
194 m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ?
195 partial_pack_offset : aligned_sz;
196 n = xread(pack_fd, buf, m);
197 if (!n)
198 break;
199 if (n < 0)
200 die("Failed to checksum %s: %s", pack_name, strerror(errno));
201 git_SHA1_Update(&new_sha1_ctx, buf, n);
203 aligned_sz -= n;
204 if (!aligned_sz)
205 aligned_sz = buf_sz;
207 if (!partial_pack_sha1)
208 continue;
210 git_SHA1_Update(&old_sha1_ctx, buf, n);
211 partial_pack_offset -= n;
212 if (partial_pack_offset == 0) {
213 unsigned char sha1[20];
214 git_SHA1_Final(sha1, &old_sha1_ctx);
215 if (hashcmp(sha1, partial_pack_sha1) != 0)
216 die("Unexpected checksum for %s "
217 "(disk corruption?)", pack_name);
219 * Now let's compute the SHA1 of the remainder of the
220 * pack, which also means making partial_pack_offset
221 * big enough not to matter anymore.
223 git_SHA1_Init(&old_sha1_ctx);
224 partial_pack_offset = ~partial_pack_offset;
225 partial_pack_offset -= MSB(partial_pack_offset, 1);
228 free(buf);
230 if (partial_pack_sha1)
231 git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx);
232 git_SHA1_Final(new_pack_sha1, &new_sha1_ctx);
233 write_or_die(pack_fd, new_pack_sha1, 20);
234 fsync_or_die(pack_fd, pack_name);
237 char *index_pack_lockfile(int ip_out)
239 char packname[46];
242 * The first thing we expects from index-pack's output
243 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
244 * %40s is the newly created pack SHA1 name. In the "keep"
245 * case, we need it to remove the corresponding .keep file
246 * later on. If we don't get that then tough luck with it.
248 if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' &&
249 memcmp(packname, "keep\t", 5) == 0) {
250 char path[PATH_MAX];
251 packname[45] = 0;
252 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
253 get_object_directory(), packname + 5);
254 return xstrdup(path);
256 return NULL;