]>
Commit | Line | Data |
---|---|---|
8b0eca7c DH |
1 | #include "cache.h" |
2 | #include "pack.h" | |
aa7e44bf GB |
3 | #include "csum-file.h" |
4 | ||
c0a5e2d4 | 5 | uint32_t pack_idx_default_version = 2; |
aa7e44bf GB |
6 | uint32_t pack_idx_off32_limit = 0x7fffffff; |
7 | ||
8 | static 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 |
20 | char *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; | |
9126f009 | 28 | git_SHA_CTX ctx; |
aa7e44bf GB |
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), | |
8b4eb6b6 | 48 | "%s/pack/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. */ | |
9126f009 | 89 | git_SHA1_Init(&ctx); |
aa7e44bf GB |
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); | |
9126f009 | 102 | git_SHA1_Update(&ctx, obj->sha1, 20); |
aa7e44bf GB |
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); |
9126f009 | 143 | git_SHA1_Final(sha1, &ctx); |
aa7e44bf GB |
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 | 163 | void 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 | 169 | { |
d35825da | 170 | int aligned_sz, buf_sz = 8 * 1024; |
9126f009 | 171 | git_SHA_CTX old_sha1_ctx, new_sha1_ctx; |
8b0eca7c DH |
172 | struct pack_header hdr; |
173 | char *buf; | |
174 | ||
9126f009 NP |
175 | git_SHA1_Init(&old_sha1_ctx); |
176 | git_SHA1_Init(&new_sha1_ctx); | |
abeb40e5 | 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 | 183 | die("Failed seeking to start of %s: %s", pack_name, strerror(errno)); |
9126f009 | 184 | git_SHA1_Update(&old_sha1_ctx, &hdr, sizeof(hdr)); |
8b0eca7c | 185 | hdr.hdr_entries = htonl(object_count); |
9126f009 | 186 | git_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); | |
d35825da | 191 | aligned_sz = buf_sz - sizeof(hdr); |
8b0eca7c | 192 | for (;;) { |
abeb40e5 | 193 | ssize_t m, n; |
d35825da NP |
194 | m = (partial_pack_sha1 && partial_pack_offset < aligned_sz) ? |
195 | partial_pack_offset : aligned_sz; | |
abeb40e5 | 196 | n = xread(pack_fd, buf, m); |
8b0eca7c DH |
197 | if (!n) |
198 | break; | |
199 | if (n < 0) | |
200 | die("Failed to checksum %s: %s", pack_name, strerror(errno)); | |
9126f009 | 201 | git_SHA1_Update(&new_sha1_ctx, buf, n); |
abeb40e5 | 202 | |
d35825da NP |
203 | aligned_sz -= n; |
204 | if (!aligned_sz) | |
205 | aligned_sz = buf_sz; | |
206 | ||
abeb40e5 NP |
207 | if (!partial_pack_sha1) |
208 | continue; | |
209 | ||
9126f009 | 210 | git_SHA1_Update(&old_sha1_ctx, buf, n); |
abeb40e5 NP |
211 | partial_pack_offset -= n; |
212 | if (partial_pack_offset == 0) { | |
213 | unsigned char sha1[20]; | |
9126f009 | 214 | git_SHA1_Final(sha1, &old_sha1_ctx); |
abeb40e5 NP |
215 | if (hashcmp(sha1, partial_pack_sha1) != 0) |
216 | die("Unexpected checksum for %s " | |
217 | "(disk corruption?)", pack_name); | |
218 | /* | |
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. | |
222 | */ | |
9126f009 | 223 | git_SHA1_Init(&old_sha1_ctx); |
abeb40e5 NP |
224 | partial_pack_offset = ~partial_pack_offset; |
225 | partial_pack_offset -= MSB(partial_pack_offset, 1); | |
226 | } | |
8b0eca7c DH |
227 | } |
228 | free(buf); | |
229 | ||
abeb40e5 | 230 | if (partial_pack_sha1) |
9126f009 NP |
231 | git_SHA1_Final(partial_pack_sha1, &old_sha1_ctx); |
232 | git_SHA1_Final(new_pack_sha1, &new_sha1_ctx); | |
abeb40e5 | 233 | write_or_die(pack_fd, new_pack_sha1, 20); |
0c68d386 | 234 | fsync_or_die(pack_fd, pack_name); |
8b0eca7c | 235 | } |
106764e6 SP |
236 | |
237 | char *index_pack_lockfile(int ip_out) | |
238 | { | |
106764e6 SP |
239 | char packname[46]; |
240 | ||
241 | /* | |
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. | |
247 | */ | |
c697ad14 HO |
248 | if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' && |
249 | memcmp(packname, "keep\t", 5) == 0) { | |
106764e6 SP |
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); | |
255 | } | |
256 | return NULL; | |
257 | } |