]> git.ipfire.org Git - thirdparty/git.git/blame - pack-write.c
index-pack: use fixup_pack_header_footer()'s validation mode
[thirdparty/git.git] / pack-write.c
CommitLineData
8b0eca7c
DH
1#include "cache.h"
2#include "pack.h"
aa7e44bf
GB
3#include "csum-file.h"
4
c0a5e2d4 5uint32_t pack_idx_default_version = 2;
aa7e44bf
GB
6uint32_t pack_idx_off32_limit = 0x7fffffff;
7
8static int sha1_compare(const void *_a, const void *_b)
9{
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);
13}
14
15/*
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.
19 */
4049b9cf
NP
20char *write_idx_file(char *index_name, struct pack_idx_entry **objects,
21 int nr_objects, unsigned char *sha1)
aa7e44bf
GB
22{
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 SHA_CTX ctx;
29 uint32_t index_version;
30
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;
38 }
39 qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
40 sha1_compare);
41 }
42 else
43 sorted_by_sha = list = last = NULL;
44
45 if (!index_name) {
46 static char tmpfile[PATH_MAX];
47 snprintf(tmpfile, sizeof(tmpfile),
48 "%s/tmp_idx_XXXXXX", get_object_directory());
7647b17f 49 fd = xmkstemp(tmpfile);
aa7e44bf
GB
50 index_name = xstrdup(tmpfile);
51 } else {
52 unlink(index_name);
53 fd = open(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600);
54 }
55 if (fd < 0)
56 die("unable to create %s: %s", index_name, strerror(errno));
57 f = sha1fd(fd, index_name);
58
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;
61
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));
68 }
69
70 /*
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).
74 */
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++;
82 }
83 array[i] = htonl(next - sorted_by_sha);
84 list = next;
85 }
86 sha1write(f, array, 256 * 4);
87
88 /* compute the SHA1 hash of sorted object names. */
89 SHA1_Init(&ctx);
90
91 /*
92 * Write the actual SHA1 entries..
93 */
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);
100 }
101 sha1write(f, obj->sha1, 20);
102 SHA1_Update(&ctx, obj->sha1, 20);
103 }
104
105 if (index_version >= 2) {
106 unsigned int nr_large_offset = 0;
107
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);
114 }
115
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);
124 }
125
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--;
137 }
138 }
139 }
140
141 sha1write(f, sha1, 20);
4c81b03e 142 sha1close(f, NULL, CSUM_FSYNC);
aa7e44bf
GB
143 SHA1_Final(sha1, &ctx);
144 return index_name;
145}
8b0eca7c 146
abeb40e5
NP
147/*
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.
151 *
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.
158 *
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.
162 */
8b0eca7c 163void fixup_pack_header_footer(int pack_fd,
abeb40e5 164 unsigned char *new_pack_sha1,
8b0eca7c 165 const char *pack_name,
abeb40e5
NP
166 uint32_t object_count,
167 unsigned char *partial_pack_sha1,
168 off_t partial_pack_offset)
8b0eca7c
DH
169{
170 static const int buf_sz = 128 * 1024;
abeb40e5 171 SHA_CTX old_sha1_ctx, new_sha1_ctx;
8b0eca7c
DH
172 struct pack_header hdr;
173 char *buf;
174
abeb40e5
NP
175 SHA1_Init(&old_sha1_ctx);
176 SHA1_Init(&new_sha1_ctx);
177
8b0eca7c 178 if (lseek(pack_fd, 0, SEEK_SET) != 0)
abeb40e5 179 die("Failed seeking to start of %s: %s", pack_name, strerror(errno));
8b0eca7c
DH
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)
abeb40e5
NP
183 die("Failed seeking to start of %s: %s", pack_name, strerror(errno));
184 SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr));
8b0eca7c 185 hdr.hdr_entries = htonl(object_count);
abeb40e5 186 SHA1_Update(&new_sha1_ctx, &hdr, sizeof(hdr));
8b0eca7c 187 write_or_die(pack_fd, &hdr, sizeof(hdr));
abeb40e5 188 partial_pack_offset -= sizeof(hdr);
8b0eca7c
DH
189
190 buf = xmalloc(buf_sz);
191 for (;;) {
abeb40e5
NP
192 ssize_t m, n;
193 m = (partial_pack_sha1 && partial_pack_offset < buf_sz) ?
194 partial_pack_offset : buf_sz;
195 n = xread(pack_fd, buf, m);
8b0eca7c
DH
196 if (!n)
197 break;
198 if (n < 0)
199 die("Failed to checksum %s: %s", pack_name, strerror(errno));
abeb40e5
NP
200 SHA1_Update(&new_sha1_ctx, buf, n);
201
202 if (!partial_pack_sha1)
203 continue;
204
205 SHA1_Update(&old_sha1_ctx, buf, n);
206 partial_pack_offset -= n;
207 if (partial_pack_offset == 0) {
208 unsigned char sha1[20];
209 SHA1_Final(sha1, &old_sha1_ctx);
210 if (hashcmp(sha1, partial_pack_sha1) != 0)
211 die("Unexpected checksum for %s "
212 "(disk corruption?)", pack_name);
213 /*
214 * Now let's compute the SHA1 of the remainder of the
215 * pack, which also means making partial_pack_offset
216 * big enough not to matter anymore.
217 */
218 SHA1_Init(&old_sha1_ctx);
219 partial_pack_offset = ~partial_pack_offset;
220 partial_pack_offset -= MSB(partial_pack_offset, 1);
221 }
8b0eca7c
DH
222 }
223 free(buf);
224
abeb40e5
NP
225 if (partial_pack_sha1)
226 SHA1_Final(partial_pack_sha1, &old_sha1_ctx);
227 SHA1_Final(new_pack_sha1, &new_sha1_ctx);
228 write_or_die(pack_fd, new_pack_sha1, 20);
0c68d386 229 fsync_or_die(pack_fd, pack_name);
8b0eca7c 230}
106764e6
SP
231
232char *index_pack_lockfile(int ip_out)
233{
106764e6
SP
234 char packname[46];
235
236 /*
237 * The first thing we expects from index-pack's output
238 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
239 * %40s is the newly created pack SHA1 name. In the "keep"
240 * case, we need it to remove the corresponding .keep file
241 * later on. If we don't get that then tough luck with it.
242 */
c697ad14
HO
243 if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' &&
244 memcmp(packname, "keep\t", 5) == 0) {
106764e6
SP
245 char path[PATH_MAX];
246 packname[45] = 0;
247 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
248 get_object_directory(), packname + 5);
249 return xstrdup(path);
250 }
251 return NULL;
252}