]>
Commit | Line | Data |
---|---|---|
8b0eca7c | 1 | #include "cache.h" |
32a8f510 | 2 | #include "environment.h" |
f394e093 | 3 | #include "gettext.h" |
41771fa4 | 4 | #include "hex.h" |
8b0eca7c | 5 | #include "pack.h" |
aa7e44bf | 6 | #include "csum-file.h" |
33add2ad | 7 | #include "remote.h" |
d9fef9d9 | 8 | #include "chunk-format.h" |
5dfaf49a TB |
9 | #include "pack-mtimes.h" |
10 | #include "oidmap.h" | |
5dfaf49a | 11 | #include "pack-objects.h" |
aa7e44bf | 12 | |
ebcfb379 JH |
13 | void reset_pack_idx_option(struct pack_idx_option *opts) |
14 | { | |
15 | memset(opts, 0, sizeof(*opts)); | |
16 | opts->version = 2; | |
17 | opts->off32_limit = 0x7fffffff; | |
18 | } | |
aa7e44bf GB |
19 | |
20 | static int sha1_compare(const void *_a, const void *_b) | |
21 | { | |
22 | struct pack_idx_entry *a = *(struct pack_idx_entry **)_a; | |
23 | struct pack_idx_entry *b = *(struct pack_idx_entry **)_b; | |
e6a492b7 | 24 | return oidcmp(&a->oid, &b->oid); |
aa7e44bf GB |
25 | } |
26 | ||
3c9fc074 JH |
27 | static int cmp_uint32(const void *a_, const void *b_) |
28 | { | |
29 | uint32_t a = *((uint32_t *)a_); | |
30 | uint32_t b = *((uint32_t *)b_); | |
31 | ||
32 | return (a < b) ? -1 : (a != b); | |
33 | } | |
34 | ||
fb956c1f JH |
35 | static int need_large_offset(off_t offset, const struct pack_idx_option *opts) |
36 | { | |
3c9fc074 JH |
37 | uint32_t ofsval; |
38 | ||
39 | if ((offset >> 31) || (opts->off32_limit < offset)) | |
40 | return 1; | |
41 | if (!opts->anomaly_nr) | |
42 | return 0; | |
43 | ofsval = offset; | |
44 | return !!bsearch(&ofsval, opts->anomaly, opts->anomaly_nr, | |
45 | sizeof(ofsval), cmp_uint32); | |
fb956c1f JH |
46 | } |
47 | ||
aa7e44bf | 48 | /* |
e2bfa50a JB |
49 | * The *sha1 contains the pack content SHA1 hash. |
50 | * The objects array passed in will be sorted by SHA1 on exit. | |
aa7e44bf | 51 | */ |
3bb72562 | 52 | const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects, |
ebcfb379 | 53 | int nr_objects, const struct pack_idx_option *opts, |
1190a1ac | 54 | const unsigned char *sha1) |
aa7e44bf | 55 | { |
98a3beab | 56 | struct hashfile *f; |
aa7e44bf GB |
57 | struct pack_idx_entry **sorted_by_sha, **list, **last; |
58 | off_t last_obj_offset = 0; | |
aa7e44bf | 59 | int i, fd; |
aa7e44bf GB |
60 | uint32_t index_version; |
61 | ||
62 | if (nr_objects) { | |
63 | sorted_by_sha = objects; | |
64 | list = sorted_by_sha; | |
65 | last = sorted_by_sha + nr_objects; | |
66 | for (i = 0; i < nr_objects; ++i) { | |
67 | if (objects[i]->offset > last_obj_offset) | |
68 | last_obj_offset = objects[i]->offset; | |
69 | } | |
9ed0d8d6 | 70 | QSORT(sorted_by_sha, nr_objects, sha1_compare); |
aa7e44bf GB |
71 | } |
72 | else | |
73 | sorted_by_sha = list = last = NULL; | |
74 | ||
e337a04d JH |
75 | if (opts->flags & WRITE_IDX_VERIFY) { |
76 | assert(index_name); | |
98a3beab | 77 | f = hashfd_check(index_name); |
aa7e44bf | 78 | } else { |
e337a04d | 79 | if (!index_name) { |
594fa999 JK |
80 | struct strbuf tmp_file = STRBUF_INIT; |
81 | fd = odb_mkstemp(&tmp_file, "pack/tmp_idx_XXXXXX"); | |
82 | index_name = strbuf_detach(&tmp_file, NULL); | |
e337a04d JH |
83 | } else { |
84 | unlink(index_name); | |
66e905b7 | 85 | fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600); |
e337a04d | 86 | } |
98a3beab | 87 | f = hashfd(fd, index_name); |
aa7e44bf | 88 | } |
aa7e44bf GB |
89 | |
90 | /* if last object's offset is >= 2^31 we should use index V2 */ | |
fb956c1f | 91 | index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version; |
aa7e44bf GB |
92 | |
93 | /* index versions 2 and above need a header */ | |
94 | if (index_version >= 2) { | |
95 | struct pack_idx_header hdr; | |
96 | hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); | |
97 | hdr.idx_version = htonl(index_version); | |
98a3beab | 98 | hashwrite(f, &hdr, sizeof(hdr)); |
aa7e44bf GB |
99 | } |
100 | ||
101 | /* | |
102 | * Write the first-level table (the list is sorted, | |
103 | * but we use a 256-entry lookup to be able to avoid | |
104 | * having to do eight extra binary search iterations). | |
105 | */ | |
106 | for (i = 0; i < 256; i++) { | |
107 | struct pack_idx_entry **next = list; | |
108 | while (next < last) { | |
109 | struct pack_idx_entry *obj = *next; | |
e6a492b7 | 110 | if (obj->oid.hash[0] != i) |
aa7e44bf GB |
111 | break; |
112 | next++; | |
113 | } | |
06d43fad | 114 | hashwrite_be32(f, next - sorted_by_sha); |
aa7e44bf GB |
115 | list = next; |
116 | } | |
aa7e44bf | 117 | |
aa7e44bf GB |
118 | /* |
119 | * Write the actual SHA1 entries.. | |
120 | */ | |
121 | list = sorted_by_sha; | |
122 | for (i = 0; i < nr_objects; i++) { | |
123 | struct pack_idx_entry *obj = *list++; | |
389cf68c RS |
124 | if (index_version < 2) |
125 | hashwrite_be32(f, obj->offset); | |
98a3beab | 126 | hashwrite(f, obj->oid.hash, the_hash_algo->rawsz); |
68be2fea | 127 | if ((opts->flags & WRITE_IDX_STRICT) && |
4a7e27e9 | 128 | (i && oideq(&list[-2]->oid, &obj->oid))) |
68be2fea | 129 | die("The same object %s appears twice in the pack", |
e6a492b7 | 130 | oid_to_hex(&obj->oid)); |
aa7e44bf GB |
131 | } |
132 | ||
133 | if (index_version >= 2) { | |
134 | unsigned int nr_large_offset = 0; | |
135 | ||
136 | /* write the crc32 table */ | |
137 | list = sorted_by_sha; | |
138 | for (i = 0; i < nr_objects; i++) { | |
139 | struct pack_idx_entry *obj = *list++; | |
389cf68c | 140 | hashwrite_be32(f, obj->crc32); |
aa7e44bf GB |
141 | } |
142 | ||
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++; | |
fb956c1f JH |
147 | uint32_t offset; |
148 | ||
149 | offset = (need_large_offset(obj->offset, opts) | |
150 | ? (0x80000000 | nr_large_offset++) | |
151 | : obj->offset); | |
389cf68c | 152 | hashwrite_be32(f, offset); |
aa7e44bf GB |
153 | } |
154 | ||
155 | /* write the large offset table */ | |
156 | list = sorted_by_sha; | |
157 | while (nr_large_offset) { | |
158 | struct pack_idx_entry *obj = *list++; | |
159 | uint64_t offset = obj->offset; | |
fb956c1f JH |
160 | |
161 | if (!need_large_offset(offset, opts)) | |
162 | continue; | |
970909c2 | 163 | hashwrite_be64(f, offset); |
fb956c1f | 164 | nr_large_offset--; |
aa7e44bf GB |
165 | } |
166 | } | |
167 | ||
98a3beab | 168 | hashwrite(f, sha1, the_hash_algo->rawsz); |
020406ea NS |
169 | finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, |
170 | CSUM_HASH_IN_STREAM | CSUM_CLOSE | | |
171 | ((opts->flags & WRITE_IDX_VERIFY) ? 0 : CSUM_FSYNC)); | |
aa7e44bf GB |
172 | return index_name; |
173 | } | |
8b0eca7c | 174 | |
8ef50d99 TB |
175 | static int pack_order_cmp(const void *va, const void *vb, void *ctx) |
176 | { | |
177 | struct pack_idx_entry **objects = ctx; | |
178 | ||
179 | off_t oa = objects[*(uint32_t*)va]->offset; | |
180 | off_t ob = objects[*(uint32_t*)vb]->offset; | |
181 | ||
182 | if (oa < ob) | |
183 | return -1; | |
184 | if (oa > ob) | |
185 | return 1; | |
186 | return 0; | |
187 | } | |
188 | ||
189 | static void write_rev_header(struct hashfile *f) | |
190 | { | |
8ef50d99 TB |
191 | hashwrite_be32(f, RIDX_SIGNATURE); |
192 | hashwrite_be32(f, RIDX_VERSION); | |
d9fef9d9 | 193 | hashwrite_be32(f, oid_version(the_hash_algo)); |
8ef50d99 TB |
194 | } |
195 | ||
196 | static void write_rev_index_positions(struct hashfile *f, | |
a587b5a7 | 197 | uint32_t *pack_order, |
8ef50d99 TB |
198 | uint32_t nr_objects) |
199 | { | |
8ef50d99 | 200 | uint32_t i; |
8ef50d99 TB |
201 | for (i = 0; i < nr_objects; i++) |
202 | hashwrite_be32(f, pack_order[i]); | |
8ef50d99 TB |
203 | } |
204 | ||
205 | static void write_rev_trailer(struct hashfile *f, const unsigned char *hash) | |
206 | { | |
207 | hashwrite(f, hash, the_hash_algo->rawsz); | |
208 | } | |
209 | ||
210 | const char *write_rev_file(const char *rev_name, | |
211 | struct pack_idx_entry **objects, | |
212 | uint32_t nr_objects, | |
213 | const unsigned char *hash, | |
214 | unsigned flags) | |
a587b5a7 TB |
215 | { |
216 | uint32_t *pack_order; | |
217 | uint32_t i; | |
218 | const char *ret; | |
219 | ||
8fe8bae9 ÆAB |
220 | if (!(flags & WRITE_REV) && !(flags & WRITE_REV_VERIFY)) |
221 | return NULL; | |
222 | ||
a587b5a7 TB |
223 | ALLOC_ARRAY(pack_order, nr_objects); |
224 | for (i = 0; i < nr_objects; i++) | |
225 | pack_order[i] = i; | |
226 | QSORT_S(pack_order, nr_objects, pack_order_cmp, objects); | |
227 | ||
228 | ret = write_rev_file_order(rev_name, pack_order, nr_objects, hash, | |
229 | flags); | |
230 | ||
231 | free(pack_order); | |
232 | ||
233 | return ret; | |
234 | } | |
235 | ||
236 | const char *write_rev_file_order(const char *rev_name, | |
237 | uint32_t *pack_order, | |
238 | uint32_t nr_objects, | |
239 | const unsigned char *hash, | |
240 | unsigned flags) | |
8ef50d99 TB |
241 | { |
242 | struct hashfile *f; | |
243 | int fd; | |
244 | ||
245 | if ((flags & WRITE_REV) && (flags & WRITE_REV_VERIFY)) | |
246 | die(_("cannot both write and verify reverse index")); | |
247 | ||
248 | if (flags & WRITE_REV) { | |
249 | if (!rev_name) { | |
250 | struct strbuf tmp_file = STRBUF_INIT; | |
251 | fd = odb_mkstemp(&tmp_file, "pack/tmp_rev_XXXXXX"); | |
252 | rev_name = strbuf_detach(&tmp_file, NULL); | |
253 | } else { | |
254 | unlink(rev_name); | |
66e905b7 | 255 | fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600); |
8ef50d99 TB |
256 | } |
257 | f = hashfd(fd, rev_name); | |
258 | } else if (flags & WRITE_REV_VERIFY) { | |
259 | struct stat statbuf; | |
260 | if (stat(rev_name, &statbuf)) { | |
261 | if (errno == ENOENT) { | |
262 | /* .rev files are optional */ | |
263 | return NULL; | |
264 | } else | |
265 | die_errno(_("could not stat: %s"), rev_name); | |
266 | } | |
267 | f = hashfd_check(rev_name); | |
268 | } else | |
269 | return NULL; | |
270 | ||
271 | write_rev_header(f); | |
272 | ||
a587b5a7 | 273 | write_rev_index_positions(f, pack_order, nr_objects); |
8ef50d99 TB |
274 | write_rev_trailer(f, hash); |
275 | ||
276 | if (rev_name && adjust_shared_perm(rev_name) < 0) | |
277 | die(_("failed to make %s readable"), rev_name); | |
278 | ||
020406ea NS |
279 | finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, |
280 | CSUM_HASH_IN_STREAM | CSUM_CLOSE | | |
281 | ((flags & WRITE_IDX_VERIFY) ? 0 : CSUM_FSYNC)); | |
8ef50d99 TB |
282 | |
283 | return rev_name; | |
284 | } | |
285 | ||
5dfaf49a TB |
286 | static void write_mtimes_header(struct hashfile *f) |
287 | { | |
288 | hashwrite_be32(f, MTIMES_SIGNATURE); | |
289 | hashwrite_be32(f, MTIMES_VERSION); | |
290 | hashwrite_be32(f, oid_version(the_hash_algo)); | |
291 | } | |
292 | ||
293 | /* | |
294 | * Writes the object mtimes of "objects" for use in a .mtimes file. | |
295 | * Note that objects must be in lexicographic (index) order, which is | |
296 | * the expected ordering of these values in the .mtimes file. | |
297 | */ | |
298 | static void write_mtimes_objects(struct hashfile *f, | |
299 | struct packing_data *to_pack, | |
300 | struct pack_idx_entry **objects, | |
301 | uint32_t nr_objects) | |
302 | { | |
303 | uint32_t i; | |
304 | for (i = 0; i < nr_objects; i++) { | |
305 | struct object_entry *e = (struct object_entry*)objects[i]; | |
306 | hashwrite_be32(f, oe_cruft_mtime(to_pack, e)); | |
307 | } | |
308 | } | |
309 | ||
310 | static void write_mtimes_trailer(struct hashfile *f, const unsigned char *hash) | |
311 | { | |
312 | hashwrite(f, hash, the_hash_algo->rawsz); | |
313 | } | |
314 | ||
82db195e | 315 | static const char *write_mtimes_file(struct packing_data *to_pack, |
5dfaf49a TB |
316 | struct pack_idx_entry **objects, |
317 | uint32_t nr_objects, | |
318 | const unsigned char *hash) | |
319 | { | |
82db195e DS |
320 | struct strbuf tmp_file = STRBUF_INIT; |
321 | const char *mtimes_name; | |
5dfaf49a TB |
322 | struct hashfile *f; |
323 | int fd; | |
324 | ||
325 | if (!to_pack) | |
326 | BUG("cannot call write_mtimes_file with NULL packing_data"); | |
327 | ||
82db195e DS |
328 | fd = odb_mkstemp(&tmp_file, "pack/tmp_mtimes_XXXXXX"); |
329 | mtimes_name = strbuf_detach(&tmp_file, NULL); | |
5dfaf49a TB |
330 | f = hashfd(fd, mtimes_name); |
331 | ||
332 | write_mtimes_header(f); | |
333 | write_mtimes_objects(f, to_pack, objects, nr_objects); | |
334 | write_mtimes_trailer(f, hash); | |
335 | ||
336 | if (adjust_shared_perm(mtimes_name) < 0) | |
337 | die(_("failed to make %s readable"), mtimes_name); | |
338 | ||
339 | finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, | |
340 | CSUM_HASH_IN_STREAM | CSUM_CLOSE | CSUM_FSYNC); | |
341 | ||
342 | return mtimes_name; | |
343 | } | |
344 | ||
98a3beab | 345 | off_t write_pack_header(struct hashfile *f, uint32_t nr_entries) |
c0ad4657 JH |
346 | { |
347 | struct pack_header hdr; | |
348 | ||
349 | hdr.hdr_signature = htonl(PACK_SIGNATURE); | |
350 | hdr.hdr_version = htonl(PACK_VERSION); | |
351 | hdr.hdr_entries = htonl(nr_entries); | |
98a3beab | 352 | hashwrite(f, &hdr, sizeof(hdr)); |
c0ad4657 JH |
353 | return sizeof(hdr); |
354 | } | |
355 | ||
abeb40e5 NP |
356 | /* |
357 | * Update pack header with object_count and compute new SHA1 for pack data | |
358 | * associated to pack_fd, and write that SHA1 at the end. That new SHA1 | |
359 | * is also returned in new_pack_sha1. | |
360 | * | |
361 | * If partial_pack_sha1 is non null, then the SHA1 of the existing pack | |
362 | * (without the header update) is computed and validated against the | |
363 | * one provided in partial_pack_sha1. The validation is performed at | |
364 | * partial_pack_offset bytes in the pack file. The SHA1 of the remaining | |
365 | * data (i.e. from partial_pack_offset to the end) is then computed and | |
366 | * returned in partial_pack_sha1. | |
367 | * | |
368 | * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and | |
369 | * partial_pack_sha1 can refer to the same buffer if the caller is not | |
370 | * interested in the resulting SHA1 of pack data above partial_pack_offset. | |
371 | */ | |
8b0eca7c | 372 | void fixup_pack_header_footer(int pack_fd, |
81c58cd4 | 373 | unsigned char *new_pack_hash, |
8b0eca7c | 374 | const char *pack_name, |
abeb40e5 | 375 | uint32_t object_count, |
81c58cd4 | 376 | unsigned char *partial_pack_hash, |
abeb40e5 | 377 | off_t partial_pack_offset) |
8b0eca7c | 378 | { |
d35825da | 379 | int aligned_sz, buf_sz = 8 * 1024; |
81c58cd4 | 380 | git_hash_ctx old_hash_ctx, new_hash_ctx; |
8b0eca7c DH |
381 | struct pack_header hdr; |
382 | char *buf; | |
90dca671 | 383 | ssize_t read_result; |
8b0eca7c | 384 | |
81c58cd4 | 385 | the_hash_algo->init_fn(&old_hash_ctx); |
386 | the_hash_algo->init_fn(&new_hash_ctx); | |
abeb40e5 | 387 | |
8b0eca7c | 388 | if (lseek(pack_fd, 0, SEEK_SET) != 0) |
d824cbba | 389 | die_errno("Failed seeking to start of '%s'", pack_name); |
90dca671 JK |
390 | read_result = read_in_full(pack_fd, &hdr, sizeof(hdr)); |
391 | if (read_result < 0) | |
d824cbba | 392 | die_errno("Unable to reread header of '%s'", pack_name); |
90dca671 JK |
393 | else if (read_result != sizeof(hdr)) |
394 | die_errno("Unexpected short read for header of '%s'", | |
395 | pack_name); | |
8b0eca7c | 396 | if (lseek(pack_fd, 0, SEEK_SET) != 0) |
d824cbba | 397 | die_errno("Failed seeking to start of '%s'", pack_name); |
81c58cd4 | 398 | the_hash_algo->update_fn(&old_hash_ctx, &hdr, sizeof(hdr)); |
8b0eca7c | 399 | hdr.hdr_entries = htonl(object_count); |
81c58cd4 | 400 | the_hash_algo->update_fn(&new_hash_ctx, &hdr, sizeof(hdr)); |
8b0eca7c | 401 | write_or_die(pack_fd, &hdr, sizeof(hdr)); |
abeb40e5 | 402 | partial_pack_offset -= sizeof(hdr); |
8b0eca7c DH |
403 | |
404 | buf = xmalloc(buf_sz); | |
d35825da | 405 | aligned_sz = buf_sz - sizeof(hdr); |
8b0eca7c | 406 | for (;;) { |
abeb40e5 | 407 | ssize_t m, n; |
81c58cd4 | 408 | m = (partial_pack_hash && partial_pack_offset < aligned_sz) ? |
d35825da | 409 | partial_pack_offset : aligned_sz; |
abeb40e5 | 410 | n = xread(pack_fd, buf, m); |
8b0eca7c DH |
411 | if (!n) |
412 | break; | |
413 | if (n < 0) | |
d824cbba | 414 | die_errno("Failed to checksum '%s'", pack_name); |
81c58cd4 | 415 | the_hash_algo->update_fn(&new_hash_ctx, buf, n); |
abeb40e5 | 416 | |
d35825da NP |
417 | aligned_sz -= n; |
418 | if (!aligned_sz) | |
419 | aligned_sz = buf_sz; | |
420 | ||
81c58cd4 | 421 | if (!partial_pack_hash) |
abeb40e5 NP |
422 | continue; |
423 | ||
81c58cd4 | 424 | the_hash_algo->update_fn(&old_hash_ctx, buf, n); |
abeb40e5 NP |
425 | partial_pack_offset -= n; |
426 | if (partial_pack_offset == 0) { | |
81c58cd4 | 427 | unsigned char hash[GIT_MAX_RAWSZ]; |
428 | the_hash_algo->final_fn(hash, &old_hash_ctx); | |
67947c34 | 429 | if (!hasheq(hash, partial_pack_hash)) |
abeb40e5 NP |
430 | die("Unexpected checksum for %s " |
431 | "(disk corruption?)", pack_name); | |
432 | /* | |
433 | * Now let's compute the SHA1 of the remainder of the | |
434 | * pack, which also means making partial_pack_offset | |
435 | * big enough not to matter anymore. | |
436 | */ | |
81c58cd4 | 437 | the_hash_algo->init_fn(&old_hash_ctx); |
abeb40e5 NP |
438 | partial_pack_offset = ~partial_pack_offset; |
439 | partial_pack_offset -= MSB(partial_pack_offset, 1); | |
440 | } | |
8b0eca7c DH |
441 | } |
442 | free(buf); | |
443 | ||
81c58cd4 | 444 | if (partial_pack_hash) |
445 | the_hash_algo->final_fn(partial_pack_hash, &old_hash_ctx); | |
446 | the_hash_algo->final_fn(new_pack_hash, &new_hash_ctx); | |
447 | write_or_die(pack_fd, new_pack_hash, the_hash_algo->rawsz); | |
020406ea | 448 | fsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name); |
8b0eca7c | 449 | } |
106764e6 | 450 | |
5476e1ef | 451 | char *index_pack_lockfile(int ip_out, int *is_well_formed) |
106764e6 | 452 | { |
81c58cd4 | 453 | char packname[GIT_MAX_HEXSZ + 6]; |
454 | const int len = the_hash_algo->hexsz + 6; | |
106764e6 SP |
455 | |
456 | /* | |
6e180cdc | 457 | * The first thing we expect from index-pack's output |
106764e6 SP |
458 | * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where |
459 | * %40s is the newly created pack SHA1 name. In the "keep" | |
460 | * case, we need it to remove the corresponding .keep file | |
461 | * later on. If we don't get that then tough luck with it. | |
462 | */ | |
81c58cd4 | 463 | if (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\n') { |
d7731444 | 464 | const char *name; |
5476e1ef JT |
465 | |
466 | if (is_well_formed) | |
467 | *is_well_formed = 1; | |
81c58cd4 | 468 | packname[len-1] = 0; |
d7731444 RS |
469 | if (skip_prefix(packname, "keep\t", &name)) |
470 | return xstrfmt("%s/pack/pack-%s.keep", | |
471 | get_object_directory(), name); | |
5476e1ef | 472 | return NULL; |
106764e6 | 473 | } |
5476e1ef JT |
474 | if (is_well_formed) |
475 | *is_well_formed = 0; | |
106764e6 SP |
476 | return NULL; |
477 | } | |
f965c525 NP |
478 | |
479 | /* | |
480 | * The per-object header is a pretty dense thing, which is | |
481 | * - first byte: low four bits are "size", then three bits of "type", | |
482 | * and the high bit is "size continues". | |
483 | * - each byte afterwards: low seven bits are size continuation, | |
484 | * with the high bit being "size continues" | |
485 | */ | |
7202a6fa JK |
486 | int encode_in_pack_object_header(unsigned char *hdr, int hdr_len, |
487 | enum object_type type, uintmax_t size) | |
f965c525 NP |
488 | { |
489 | int n = 1; | |
490 | unsigned char c; | |
491 | ||
492 | if (type < OBJ_COMMIT || type > OBJ_REF_DELTA) | |
493 | die("bad type %d", type); | |
494 | ||
495 | c = (type << 4) | (size & 15); | |
496 | size >>= 4; | |
497 | while (size) { | |
7202a6fa JK |
498 | if (n == hdr_len) |
499 | die("object size is too enormous to format"); | |
f965c525 NP |
500 | *hdr++ = c | 0x80; |
501 | c = size & 0x7f; | |
502 | size >>= 7; | |
503 | n++; | |
504 | } | |
505 | *hdr = c; | |
506 | return n; | |
507 | } | |
cdf9db3c | 508 | |
98a3beab | 509 | struct hashfile *create_tmp_packfile(char **pack_tmp_name) |
cdf9db3c | 510 | { |
594fa999 | 511 | struct strbuf tmpname = STRBUF_INIT; |
cdf9db3c JH |
512 | int fd; |
513 | ||
594fa999 JK |
514 | fd = odb_mkstemp(&tmpname, "pack/tmp_pack_XXXXXX"); |
515 | *pack_tmp_name = strbuf_detach(&tmpname, NULL); | |
98a3beab | 516 | return hashfd(fd, *pack_tmp_name); |
cdf9db3c | 517 | } |
0e990530 | 518 | |
66833f0e ÆAB |
519 | static void rename_tmp_packfile(struct strbuf *name_prefix, const char *source, |
520 | const char *ext) | |
521 | { | |
522 | size_t name_prefix_len = name_prefix->len; | |
523 | ||
524 | strbuf_addstr(name_prefix, ext); | |
525 | if (rename(source, name_prefix->buf)) | |
526 | die_errno("unable to rename temporary file to '%s'", | |
527 | name_prefix->buf); | |
528 | strbuf_setlen(name_prefix, name_prefix_len); | |
529 | } | |
530 | ||
2ec02dd5 ÆAB |
531 | void rename_tmp_packfile_idx(struct strbuf *name_buffer, |
532 | char **idx_tmp_name) | |
533 | { | |
534 | rename_tmp_packfile(name_buffer, *idx_tmp_name, "idx"); | |
535 | } | |
536 | ||
537 | void stage_tmp_packfiles(struct strbuf *name_buffer, | |
0e990530 JH |
538 | const char *pack_tmp_name, |
539 | struct pack_idx_entry **written_list, | |
540 | uint32_t nr_written, | |
1c573cdd | 541 | struct packing_data *to_pack, |
0e990530 | 542 | struct pack_idx_option *pack_idx_opts, |
2ec02dd5 ÆAB |
543 | unsigned char hash[], |
544 | char **idx_tmp_name) | |
0e990530 | 545 | { |
2ec02dd5 | 546 | const char *rev_tmp_name = NULL; |
5dfaf49a | 547 | const char *mtimes_tmp_name = NULL; |
0e990530 JH |
548 | |
549 | if (adjust_shared_perm(pack_tmp_name)) | |
550 | die_errno("unable to make temporary pack file readable"); | |
551 | ||
2ec02dd5 ÆAB |
552 | *idx_tmp_name = (char *)write_idx_file(NULL, written_list, nr_written, |
553 | pack_idx_opts, hash); | |
554 | if (adjust_shared_perm(*idx_tmp_name)) | |
0e990530 JH |
555 | die_errno("unable to make temporary index file readable"); |
556 | ||
8ef50d99 TB |
557 | rev_tmp_name = write_rev_file(NULL, written_list, nr_written, hash, |
558 | pack_idx_opts->flags); | |
559 | ||
5dfaf49a | 560 | if (pack_idx_opts->flags & WRITE_MTIMES) { |
82db195e | 561 | mtimes_tmp_name = write_mtimes_file(to_pack, written_list, |
5dfaf49a TB |
562 | nr_written, |
563 | hash); | |
564 | } | |
565 | ||
66833f0e | 566 | rename_tmp_packfile(name_buffer, pack_tmp_name, "pack"); |
66833f0e ÆAB |
567 | if (rev_tmp_name) |
568 | rename_tmp_packfile(name_buffer, rev_tmp_name, "rev"); | |
5dfaf49a TB |
569 | if (mtimes_tmp_name) |
570 | rename_tmp_packfile(name_buffer, mtimes_tmp_name, "mtimes"); | |
0e990530 | 571 | } |
33add2ad CC |
572 | |
573 | void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought) | |
574 | { | |
7c99bc23 | 575 | int i, err; |
33add2ad CC |
576 | FILE *output = xfopen(promisor_name, "w"); |
577 | ||
578 | for (i = 0; i < nr_sought; i++) | |
579 | fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid), | |
580 | sought[i]->name); | |
7c99bc23 CC |
581 | |
582 | err = ferror(output); | |
583 | err |= fclose(output); | |
584 | if (err) | |
585 | die(_("could not write '%s' promisor file"), promisor_name); | |
33add2ad | 586 | } |