2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 668a20f6 2020-03-18 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
5 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
6 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
7 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
9 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 93658fb9 2020-03-18 stsp #include <sys/queue.h>
19 93658fb9 2020-03-18 stsp #include <sys/stat.h>
20 93658fb9 2020-03-18 stsp #include <sys/time.h>
21 93658fb9 2020-03-18 stsp #include <sys/types.h>
22 93658fb9 2020-03-18 stsp #include <sys/uio.h>
23 2e5a6fad 2020-03-18 stsp #include <sys/mman.h>
25 93658fb9 2020-03-18 stsp #include <stdint.h>
26 93658fb9 2020-03-18 stsp #include <errno.h>
27 93658fb9 2020-03-18 stsp #include <imsg.h>
28 93658fb9 2020-03-18 stsp #include <limits.h>
29 93658fb9 2020-03-18 stsp #include <signal.h>
30 93658fb9 2020-03-18 stsp #include <stdio.h>
31 93658fb9 2020-03-18 stsp #include <stdlib.h>
32 93658fb9 2020-03-18 stsp #include <string.h>
33 93658fb9 2020-03-18 stsp #include <ctype.h>
34 93658fb9 2020-03-18 stsp #include <sha1.h>
35 78fb0967 2020-09-09 naddy #include <endian.h>
36 93658fb9 2020-03-18 stsp #include <fcntl.h>
37 81a12da5 2020-09-09 naddy #include <unistd.h>
38 93658fb9 2020-03-18 stsp #include <zlib.h>
39 93658fb9 2020-03-18 stsp #include <err.h>
40 93658fb9 2020-03-18 stsp #include <assert.h>
41 93658fb9 2020-03-18 stsp #include <dirent.h>
43 93658fb9 2020-03-18 stsp #include "got_error.h"
44 93658fb9 2020-03-18 stsp #include "got_object.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
50 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
51 93658fb9 2020-03-18 stsp #include "got_lib_object_idset.h"
52 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
53 668a20f6 2020-03-18 stsp #include "got_lib_pack.h"
54 668a20f6 2020-03-18 stsp #include "got_lib_delta_cache.h"
56 d582f26c 2020-03-18 stsp #ifndef nitems
57 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 668a20f6 2020-03-18 stsp struct got_indexed_object {
61 668a20f6 2020-03-18 stsp struct got_object_id id;
64 668a20f6 2020-03-18 stsp * Has this object been fully resolved?
65 668a20f6 2020-03-18 stsp * If so, we know its ID, otherwise we don't and 'id' is invalid.
69 668a20f6 2020-03-18 stsp /* Offset of type+size field for this object in pack file. */
72 668a20f6 2020-03-18 stsp /* Type+size values parsed from pack file. */
73 668a20f6 2020-03-18 stsp uint8_t type;
74 668a20f6 2020-03-18 stsp uint64_t size;
76 668a20f6 2020-03-18 stsp /* Length of on-disk type+size data. */
77 668a20f6 2020-03-18 stsp size_t tslen;
79 668a20f6 2020-03-18 stsp /* Length of object data following type+size. */
82 668a20f6 2020-03-18 stsp uint32_t crc;
86 836f2c92 2020-03-18 stsp /* For ref deltas. */
87 836f2c92 2020-03-18 stsp struct got_object_id ref_id;
90 836f2c92 2020-03-18 stsp /* For offset deltas. */
91 836f2c92 2020-03-18 stsp off_t base_offset;
92 836f2c92 2020-03-18 stsp size_t base_offsetlen;
98 b102634c 2020-03-18 stsp putbe32(char *b, uint32_t n)
100 b102634c 2020-03-18 stsp b[0] = n >> 24;
101 b102634c 2020-03-18 stsp b[1] = n >> 16;
102 b102634c 2020-03-18 stsp b[2] = n >> 8;
103 b102634c 2020-03-18 stsp b[3] = n >> 0;
106 668a20f6 2020-03-18 stsp static const struct got_error *
107 668a20f6 2020-03-18 stsp get_obj_type_label(const char **label, int obj_type)
109 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
111 668a20f6 2020-03-18 stsp switch (obj_type) {
112 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
113 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_BLOB;
115 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
116 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TREE;
118 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
119 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_COMMIT;
121 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
122 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TAG;
125 668a20f6 2020-03-18 stsp *label = NULL;
126 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
130 668a20f6 2020-03-18 stsp return err;
133 1e87a3c3 2020-03-18 stsp static const struct got_error *
134 6ad68bce 2020-03-24 stsp read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
136 1e87a3c3 2020-03-18 stsp uint8_t buf[8192];
140 1e87a3c3 2020-03-18 stsp for (n = len; n > 0; n -= r){
141 1e87a3c3 2020-03-18 stsp r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
142 1e87a3c3 2020-03-18 stsp if (r == -1)
143 1e87a3c3 2020-03-18 stsp return got_error_from_errno("read");
144 1e87a3c3 2020-03-18 stsp if (r == 0)
147 6ad68bce 2020-03-24 stsp *crc = crc32(*crc, buf, r);
148 6ad68bce 2020-03-24 stsp if (sha1_ctx)
149 6ad68bce 2020-03-24 stsp SHA1Update(sha1_ctx, buf, r);
152 1e87a3c3 2020-03-18 stsp return NULL;
155 668a20f6 2020-03-18 stsp static const struct got_error *
156 d582f26c 2020-03-18 stsp read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
158 4788f1ce 2020-03-18 stsp uint8_t buf[8192];
159 d582f26c 2020-03-18 stsp size_t n, r;
161 d582f26c 2020-03-18 stsp for (n = len; n > 0; n -= r) {
162 d582f26c 2020-03-18 stsp r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
163 4788f1ce 2020-03-18 stsp if (r == 0) {
164 4788f1ce 2020-03-18 stsp if (feof(f))
165 4788f1ce 2020-03-18 stsp return NULL;
166 4788f1ce 2020-03-18 stsp return got_ferror(f, GOT_ERR_IO);
168 4788f1ce 2020-03-18 stsp SHA1Update(ctx, buf, r);
171 4788f1ce 2020-03-18 stsp return NULL;
174 4788f1ce 2020-03-18 stsp static const struct got_error *
175 4788f1ce 2020-03-18 stsp read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
176 6ad68bce 2020-03-24 stsp FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
178 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
179 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
180 4788f1ce 2020-03-18 stsp uint8_t *data = NULL;
181 d582f26c 2020-03-18 stsp size_t datalen = 0;
183 668a20f6 2020-03-18 stsp char *header;
184 668a20f6 2020-03-18 stsp size_t headerlen;
185 668a20f6 2020-03-18 stsp const char *obj_label;
186 2e5a6fad 2020-03-18 stsp size_t mapoff = obj->off;
187 6ad68bce 2020-03-24 stsp struct got_inflate_checksum csum;
189 d5c81d44 2021-07-08 stsp memset(&csum, 0, sizeof(csum));
190 6ad68bce 2020-03-24 stsp csum.input_sha1 = pack_sha1_ctx;
191 6ad68bce 2020-03-24 stsp csum.input_crc = &obj->crc;
193 cbc66309 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
194 cbc66309 2020-03-18 stsp &obj->tslen, pack, obj->off);
196 668a20f6 2020-03-18 stsp return err;
198 2e5a6fad 2020-03-18 stsp if (pack->map) {
199 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
200 6ad68bce 2020-03-24 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
201 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen;
203 2e5a6fad 2020-03-18 stsp /* XXX Seek back and get the CRC of on-disk type+size bytes. */
204 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
205 2e5a6fad 2020-03-18 stsp return got_error_from_errno("lseek");
206 6ad68bce 2020-03-24 stsp err = read_checksum(&obj->crc, pack_sha1_ctx,
207 6ad68bce 2020-03-24 stsp pack->fd, obj->tslen);
209 2e5a6fad 2020-03-18 stsp return err;
212 668a20f6 2020-03-18 stsp switch (obj->type) {
213 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
214 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
215 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
216 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
217 4788f1ce 2020-03-18 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
218 4788f1ce 2020-03-18 stsp if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
219 4788f1ce 2020-03-18 stsp err = got_error_from_errno("fseek");
222 4788f1ce 2020-03-18 stsp if (pack->map) {
223 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_mmap(&datalen,
224 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->map, mapoff,
225 4788f1ce 2020-03-18 stsp pack->filesize - mapoff, tmpfile);
227 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_fd(&datalen,
228 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->fd, tmpfile);
231 4788f1ce 2020-03-18 stsp if (pack->map) {
232 4788f1ce 2020-03-18 stsp err = got_inflate_to_mem_mmap(&data, &datalen,
233 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->map, mapoff,
234 4788f1ce 2020-03-18 stsp pack->filesize - mapoff);
236 4788f1ce 2020-03-18 stsp err = got_inflate_to_mem_fd(&data, &datalen,
237 6ad68bce 2020-03-24 stsp &obj->len, &csum, obj->size, pack->fd);
242 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
243 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, obj->type);
245 7cd14ea0 2020-03-18 stsp free(data);
248 1367695b 2020-09-26 naddy if (asprintf(&header, "%s %lld", obj_label,
249 1367695b 2020-09-26 naddy (long long)obj->size) == -1) {
250 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
251 668a20f6 2020-03-18 stsp free(data);
254 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
255 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
256 4788f1ce 2020-03-18 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
257 d582f26c 2020-03-18 stsp err = read_file_sha1(&ctx, tmpfile, datalen);
259 8347f8e7 2021-10-15 stsp free(header);
260 8347f8e7 2021-10-15 stsp free(data);
264 4788f1ce 2020-03-18 stsp SHA1Update(&ctx, data, datalen);
265 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
266 668a20f6 2020-03-18 stsp free(header);
267 668a20f6 2020-03-18 stsp free(data);
269 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_REF_DELTA:
270 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
271 2e5a6fad 2020-03-18 stsp if (pack->map) {
272 2e5a6fad 2020-03-18 stsp if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
273 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
276 2e5a6fad 2020-03-18 stsp memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
277 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
278 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
279 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
280 6ad68bce 2020-03-24 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff,
281 6ad68bce 2020-03-24 stsp SHA1_DIGEST_LENGTH);
282 2e5a6fad 2020-03-18 stsp mapoff += SHA1_DIGEST_LENGTH;
283 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
284 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->map, mapoff,
285 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
289 2e5a6fad 2020-03-18 stsp n = read(pack->fd, obj->delta.ref.ref_id.sha1,
290 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
291 2e5a6fad 2020-03-18 stsp if (n == -1) {
292 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("read");
295 2e5a6fad 2020-03-18 stsp if (n < sizeof(obj->id)) {
296 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
299 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
300 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
301 6ad68bce 2020-03-24 stsp SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
302 6ad68bce 2020-03-24 stsp SHA1_DIGEST_LENGTH);
303 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
304 6ad68bce 2020-03-24 stsp &csum, obj->size, pack->fd);
308 668a20f6 2020-03-18 stsp obj->len += SHA1_DIGEST_LENGTH;
310 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
311 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
312 836f2c92 2020-03-18 stsp err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
313 836f2c92 2020-03-18 stsp &obj->delta.ofs.base_offsetlen, pack, obj->off,
314 836f2c92 2020-03-18 stsp obj->tslen);
318 2e5a6fad 2020-03-18 stsp if (pack->map) {
319 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
320 2e5a6fad 2020-03-18 stsp obj->delta.ofs.base_offsetlen);
321 6ad68bce 2020-03-24 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff,
322 6ad68bce 2020-03-24 stsp obj->delta.ofs.base_offsetlen);
323 2e5a6fad 2020-03-18 stsp mapoff += obj->delta.ofs.base_offsetlen;
324 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
325 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->map, mapoff,
326 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
331 6ad68bce 2020-03-24 stsp * XXX Seek back and get CRC and SHA1 of on-disk
332 2e5a6fad 2020-03-18 stsp * offset bytes.
334 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
336 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
339 6ad68bce 2020-03-24 stsp err = read_checksum(&obj->crc, pack_sha1_ctx,
340 6ad68bce 2020-03-24 stsp pack->fd, obj->delta.ofs.base_offsetlen);
344 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
345 6ad68bce 2020-03-24 stsp &csum, obj->size, pack->fd);
349 836f2c92 2020-03-18 stsp obj->len += obj->delta.ofs.base_offsetlen;
352 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
356 668a20f6 2020-03-18 stsp return err;
359 668a20f6 2020-03-18 stsp static const struct got_error *
360 668a20f6 2020-03-18 stsp hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
364 668a20f6 2020-03-18 stsp SHA1Update(ctx, buf, len);
366 668a20f6 2020-03-18 stsp w = write(fd, buf, len);
367 668a20f6 2020-03-18 stsp if (w == -1)
368 668a20f6 2020-03-18 stsp return got_error_from_errno("write");
369 668a20f6 2020-03-18 stsp if (w != len)
370 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_IO);
372 668a20f6 2020-03-18 stsp return NULL;
375 668a20f6 2020-03-18 stsp static const struct got_error *
376 668a20f6 2020-03-18 stsp resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
377 d582f26c 2020-03-18 stsp struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
378 d582f26c 2020-03-18 stsp FILE *delta_accum_file)
380 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
381 668a20f6 2020-03-18 stsp struct got_delta_chain deltas;
382 668a20f6 2020-03-18 stsp struct got_delta *delta;
383 668a20f6 2020-03-18 stsp uint8_t *buf = NULL;
384 d582f26c 2020-03-18 stsp size_t len = 0;
385 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
386 4d0fef1d 2020-03-18 stsp char *header = NULL;
387 668a20f6 2020-03-18 stsp size_t headerlen;
388 d582f26c 2020-03-18 stsp uint64_t max_size;
389 668a20f6 2020-03-18 stsp int base_obj_type;
390 668a20f6 2020-03-18 stsp const char *obj_label;
392 668a20f6 2020-03-18 stsp deltas.nentries = 0;
393 dbdddfee 2021-06-23 naddy STAILQ_INIT(&deltas.entries);
395 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
396 668a20f6 2020-03-18 stsp obj->off, obj->tslen, obj->type, obj->size,
397 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
401 d582f26c 2020-03-18 stsp err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
404 d582f26c 2020-03-18 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
405 d582f26c 2020-03-18 stsp rewind(tmpfile);
406 d582f26c 2020-03-18 stsp rewind(delta_base_file);
407 d582f26c 2020-03-18 stsp rewind(delta_accum_file);
408 d582f26c 2020-03-18 stsp err = got_pack_dump_delta_chain_to_file(&len, &deltas,
409 d582f26c 2020-03-18 stsp pack, tmpfile, delta_base_file, delta_accum_file);
413 d582f26c 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(&buf, &len,
414 d582f26c 2020-03-18 stsp &deltas, pack);
419 668a20f6 2020-03-18 stsp err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
422 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, base_obj_type);
425 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
426 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
429 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
430 d582f26c 2020-03-18 stsp SHA1Init(&ctx);
431 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
432 d582f26c 2020-03-18 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
433 d582f26c 2020-03-18 stsp err = read_file_sha1(&ctx, tmpfile, len);
437 d582f26c 2020-03-18 stsp SHA1Update(&ctx, buf, len);
438 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
441 4d0fef1d 2020-03-18 stsp free(header);
442 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&deltas.entries)) {
443 dbdddfee 2021-06-23 naddy delta = STAILQ_FIRST(&deltas.entries);
444 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&deltas.entries, entry);
445 668a20f6 2020-03-18 stsp free(delta);
447 668a20f6 2020-03-18 stsp return err;
450 668a20f6 2020-03-18 stsp /* Determine the slot in the pack index a given object ID should use. */
452 668a20f6 2020-03-18 stsp find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
454 668a20f6 2020-03-18 stsp u_int8_t id0 = sha1[0];
455 78fb0967 2020-09-09 naddy uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
456 668a20f6 2020-03-18 stsp int left = 0, right = nindexed - 1;
457 668a20f6 2020-03-18 stsp int cmp = 0, i = 0;
459 668a20f6 2020-03-18 stsp if (id0 > 0)
460 78fb0967 2020-09-09 naddy left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
462 668a20f6 2020-03-18 stsp while (left <= right) {
463 668a20f6 2020-03-18 stsp struct got_packidx_object_id *oid;
465 668a20f6 2020-03-18 stsp i = ((left + right) / 2);
466 668a20f6 2020-03-18 stsp oid = &packidx->hdr.sorted_ids[i];
468 668a20f6 2020-03-18 stsp cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
469 668a20f6 2020-03-18 stsp if (cmp == 0)
470 668a20f6 2020-03-18 stsp return -1; /* object already indexed */
471 668a20f6 2020-03-18 stsp else if (cmp > 0)
472 668a20f6 2020-03-18 stsp left = i + 1;
473 668a20f6 2020-03-18 stsp else if (cmp < 0)
474 668a20f6 2020-03-18 stsp right = i - 1;
477 668a20f6 2020-03-18 stsp return left;
481 668a20f6 2020-03-18 stsp static void
482 668a20f6 2020-03-18 stsp print_packidx(struct got_packidx *packidx)
484 78fb0967 2020-09-09 naddy uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
487 668a20f6 2020-03-18 stsp fprintf(stderr, "object IDs:\n");
488 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
489 668a20f6 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
490 668a20f6 2020-03-18 stsp got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
491 668a20f6 2020-03-18 stsp hex, sizeof(hex));
492 668a20f6 2020-03-18 stsp fprintf(stderr, "%s\n", hex);
494 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
496 668a20f6 2020-03-18 stsp fprintf(stderr, "object offsets:\n");
497 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
498 668a20f6 2020-03-18 stsp uint32_t offset = be32toh(packidx->hdr.offsets[i]);
499 668a20f6 2020-03-18 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
500 668a20f6 2020-03-18 stsp int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
501 668a20f6 2020-03-18 stsp fprintf(stderr, "%u -> %llu\n", offset,
502 668a20f6 2020-03-18 stsp be64toh(packidx->hdr.large_offsets[j]));
504 668a20f6 2020-03-18 stsp fprintf(stderr, "%u\n", offset);
506 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
508 668a20f6 2020-03-18 stsp fprintf(stderr, "fanout table:");
509 668a20f6 2020-03-18 stsp for (i = 0; i <= 0xff; i++)
510 668a20f6 2020-03-18 stsp fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
511 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
515 668a20f6 2020-03-18 stsp static void
516 950de2cd 2020-03-18 stsp add_indexed_object(struct got_packidx *packidx, uint32_t idx,
517 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
521 668a20f6 2020-03-18 stsp memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
522 668a20f6 2020-03-18 stsp SHA1_DIGEST_LENGTH);
523 97684601 2020-03-18 stsp packidx->hdr.crc32[idx] = htobe32(obj->crc);
524 668a20f6 2020-03-18 stsp if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
525 668a20f6 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(obj->off);
527 7bad1537 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
528 668a20f6 2020-03-18 stsp GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
529 7bad1537 2020-03-18 stsp packidx->hdr.large_offsets[packidx->nlargeobj] =
530 7bad1537 2020-03-18 stsp htobe64(obj->off);
531 7bad1537 2020-03-18 stsp packidx->nlargeobj++;
534 668a20f6 2020-03-18 stsp for (i = obj->id.sha1[0]; i <= 0xff; i++) {
535 950de2cd 2020-03-18 stsp uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
536 668a20f6 2020-03-18 stsp packidx->hdr.fanout_table[i] = htobe32(n + 1);
541 8bb2b40c 2020-03-18 stsp indexed_obj_cmp(const void *pa, const void *pb)
543 8bb2b40c 2020-03-18 stsp struct got_indexed_object *a, *b;
545 262c582a 2020-03-18 stsp a = (struct got_indexed_object *)pa;
546 262c582a 2020-03-18 stsp b = (struct got_indexed_object *)pb;
547 8bb2b40c 2020-03-18 stsp return got_object_id_cmp(&a->id, &b->id);
550 950de2cd 2020-03-18 stsp static void
551 0fd91daf 2020-03-18 stsp make_packidx(struct got_packidx *packidx, int nobj,
552 262c582a 2020-03-18 stsp struct got_indexed_object *objects)
554 950de2cd 2020-03-18 stsp struct got_indexed_object *obj;
556 950de2cd 2020-03-18 stsp uint32_t idx = 0;
558 93bba072 2020-03-18 stsp qsort(objects, nobj, sizeof(struct got_indexed_object),
559 950de2cd 2020-03-18 stsp indexed_obj_cmp);
561 0fd91daf 2020-03-18 stsp memset(packidx->hdr.fanout_table, 0,
562 0fd91daf 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
563 0fd91daf 2020-03-18 stsp packidx->nlargeobj = 0;
565 950de2cd 2020-03-18 stsp for (i = 0; i < nobj; i++) {
566 262c582a 2020-03-18 stsp obj = &objects[i];
567 0fd91daf 2020-03-18 stsp if (obj->valid)
568 0fd91daf 2020-03-18 stsp add_indexed_object(packidx, idx++, obj);
572 950de2cd 2020-03-18 stsp static void
573 950de2cd 2020-03-18 stsp update_packidx(struct got_packidx *packidx, int nobj,
574 950de2cd 2020-03-18 stsp struct got_indexed_object *obj)
577 78fb0967 2020-09-09 naddy uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
579 950de2cd 2020-03-18 stsp idx = find_object_idx(packidx, obj->id.sha1);
580 950de2cd 2020-03-18 stsp if (idx == -1) {
581 950de2cd 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
582 950de2cd 2020-03-18 stsp got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
583 950de2cd 2020-03-18 stsp return; /* object already indexed */
586 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.sorted_ids[idx + 1],
587 950de2cd 2020-03-18 stsp &packidx->hdr.sorted_ids[idx],
588 950de2cd 2020-03-18 stsp sizeof(struct got_packidx_object_id) * (nindexed - idx));
589 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
590 950de2cd 2020-03-18 stsp sizeof(uint32_t) * (nindexed - idx));
592 950de2cd 2020-03-18 stsp add_indexed_object(packidx, idx, obj);
595 e70bf110 2020-03-22 stsp static const struct got_error *
596 e70bf110 2020-03-22 stsp send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
597 e70bf110 2020-03-22 stsp int nobj_indexed, int nobj_loose, int nobj_resolved)
599 e70bf110 2020-03-22 stsp struct got_imsg_index_pack_progress iprogress;
601 e70bf110 2020-03-22 stsp iprogress.nobj_total = nobj_total;
602 e70bf110 2020-03-22 stsp iprogress.nobj_indexed = nobj_indexed;
603 e70bf110 2020-03-22 stsp iprogress.nobj_loose = nobj_loose;
604 e70bf110 2020-03-22 stsp iprogress.nobj_resolved = nobj_resolved;
606 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
607 e70bf110 2020-03-22 stsp &iprogress, sizeof(iprogress)) == -1)
608 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
610 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
613 e70bf110 2020-03-22 stsp static const struct got_error *
614 e70bf110 2020-03-22 stsp send_index_pack_done(struct imsgbuf *ibuf)
616 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
617 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
618 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
622 668a20f6 2020-03-18 stsp static const struct got_error *
623 4788f1ce 2020-03-18 stsp index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
624 3abebdc2 2020-03-24 stsp FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
625 d582f26c 2020-03-18 stsp struct imsgbuf *ibuf)
627 668a20f6 2020-03-18 stsp const struct got_error *err;
628 668a20f6 2020-03-18 stsp struct got_packfile_hdr hdr;
629 668a20f6 2020-03-18 stsp struct got_packidx packidx;
630 668a20f6 2020-03-18 stsp char buf[8];
631 6ad68bce 2020-03-24 stsp char pack_sha1[SHA1_DIGEST_LENGTH];
632 7bad1537 2020-03-18 stsp int nobj, nvalid, nloose, nresolved = 0, i;
633 262c582a 2020-03-18 stsp struct got_indexed_object *objects = NULL, *obj;
634 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
635 668a20f6 2020-03-18 stsp uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
636 668a20f6 2020-03-18 stsp ssize_t r, w;
637 160bbe2e 2020-03-18 stsp int pass, have_ref_deltas = 0, first_delta_idx = -1;
638 2e5a6fad 2020-03-18 stsp size_t mapoff = 0;
639 5672d305 2020-03-18 stsp int p_indexed = 0, last_p_indexed = -1;
640 5672d305 2020-03-18 stsp int p_resolved = 0, last_p_resolved = -1;
642 6ad68bce 2020-03-24 stsp /* Require that pack file header and SHA1 trailer are present. */
643 6ad68bce 2020-03-24 stsp if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
644 6ad68bce 2020-03-24 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
645 6ad68bce 2020-03-24 stsp "short pack file");
647 2e5a6fad 2020-03-18 stsp if (pack->map) {
648 2e5a6fad 2020-03-18 stsp memcpy(&hdr, pack->map, sizeof(hdr));
649 2e5a6fad 2020-03-18 stsp mapoff += sizeof(hdr);
651 2e5a6fad 2020-03-18 stsp r = read(pack->fd, &hdr, sizeof(hdr));
652 2e5a6fad 2020-03-18 stsp if (r == -1)
653 2e5a6fad 2020-03-18 stsp return got_error_from_errno("read");
654 2e5a6fad 2020-03-18 stsp if (r < sizeof(hdr))
655 2e5a6fad 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
656 6ad68bce 2020-03-24 stsp "short pack file");
659 668a20f6 2020-03-18 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
660 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
661 668a20f6 2020-03-18 stsp "bad packfile signature");
662 668a20f6 2020-03-18 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
663 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
664 668a20f6 2020-03-18 stsp "bad packfile version");
665 78fb0967 2020-09-09 naddy nobj = be32toh(hdr.nobjects);
666 668a20f6 2020-03-18 stsp if (nobj == 0)
667 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
668 668a20f6 2020-03-18 stsp "bad packfile with zero objects");
670 6ad68bce 2020-03-24 stsp /* We compute the SHA1 of pack file contents and verify later on. */
671 6ad68bce 2020-03-24 stsp SHA1Init(&ctx);
672 6ad68bce 2020-03-24 stsp SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
675 668a20f6 2020-03-18 stsp * Create an in-memory pack index which will grow as objects
676 668a20f6 2020-03-18 stsp * IDs in the pack file are discovered. Only fields used to
677 668a20f6 2020-03-18 stsp * read deltified objects will be needed by the pack.c library
678 668a20f6 2020-03-18 stsp * code, so setting up just a pack index header is sufficient.
680 668a20f6 2020-03-18 stsp memset(&packidx, 0, sizeof(packidx));
681 668a20f6 2020-03-18 stsp packidx.hdr.magic = malloc(sizeof(uint32_t));
682 668a20f6 2020-03-18 stsp if (packidx.hdr.magic == NULL)
683 07965a0d 2021-10-15 stsp return got_error_from_errno("malloc");
684 668a20f6 2020-03-18 stsp *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
685 668a20f6 2020-03-18 stsp packidx.hdr.version = malloc(sizeof(uint32_t));
686 668a20f6 2020-03-18 stsp if (packidx.hdr.version == NULL) {
687 668a20f6 2020-03-18 stsp err = got_error_from_errno("malloc");
690 668a20f6 2020-03-18 stsp *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
691 668a20f6 2020-03-18 stsp packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
692 668a20f6 2020-03-18 stsp sizeof(uint32_t));
693 668a20f6 2020-03-18 stsp if (packidx.hdr.fanout_table == NULL) {
694 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
697 668a20f6 2020-03-18 stsp packidx.hdr.sorted_ids = calloc(nobj,
698 668a20f6 2020-03-18 stsp sizeof(struct got_packidx_object_id));
699 668a20f6 2020-03-18 stsp if (packidx.hdr.sorted_ids == NULL) {
700 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
703 97684601 2020-03-18 stsp packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
704 97684601 2020-03-18 stsp if (packidx.hdr.crc32 == NULL) {
705 97684601 2020-03-18 stsp err = got_error_from_errno("calloc");
708 668a20f6 2020-03-18 stsp packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
709 668a20f6 2020-03-18 stsp if (packidx.hdr.offsets == NULL) {
710 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
713 668a20f6 2020-03-18 stsp /* Large offsets table is empty for pack files < 2 GB. */
714 668a20f6 2020-03-18 stsp if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
715 668a20f6 2020-03-18 stsp packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
716 668a20f6 2020-03-18 stsp if (packidx.hdr.large_offsets == NULL) {
717 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
722 668a20f6 2020-03-18 stsp nvalid = 0;
723 668a20f6 2020-03-18 stsp nloose = 0;
724 262c582a 2020-03-18 stsp objects = calloc(nobj, sizeof(struct got_indexed_object));
725 668a20f6 2020-03-18 stsp if (objects == NULL)
726 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
729 668a20f6 2020-03-18 stsp * First pass: locate all objects and identify un-deltified objects.
731 668a20f6 2020-03-18 stsp * When this pass has completed we will know offset, type, size, and
732 668a20f6 2020-03-18 stsp * CRC information for all objects in this pack file. We won't know
733 668a20f6 2020-03-18 stsp * any of the actual object IDs of deltified objects yet since we
734 668a20f6 2020-03-18 stsp * will not yet attempt to combine deltas.
737 668a20f6 2020-03-18 stsp for (i = 0; i < nobj; i++) {
738 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
739 5672d305 2020-03-18 stsp p_indexed = ((i + 1) * 100) / nobj;
740 5672d305 2020-03-18 stsp if (p_indexed != last_p_indexed) {
741 e70bf110 2020-03-22 stsp err = send_index_pack_progress(ibuf, nobj, i + 1,
742 e70bf110 2020-03-22 stsp nloose, 0);
745 5672d305 2020-03-18 stsp last_p_indexed = p_indexed;
748 262c582a 2020-03-18 stsp obj = &objects[i];
749 1e87a3c3 2020-03-18 stsp obj->crc = crc32(0L, NULL, 0);
751 668a20f6 2020-03-18 stsp /* Store offset to type+size information for this object. */
752 2e5a6fad 2020-03-18 stsp if (pack->map) {
753 2e5a6fad 2020-03-18 stsp obj->off = mapoff;
755 2e5a6fad 2020-03-18 stsp obj->off = lseek(pack->fd, 0, SEEK_CUR);
756 2e5a6fad 2020-03-18 stsp if (obj->off == -1) {
757 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
762 6ad68bce 2020-03-24 stsp err = read_packed_object(pack, obj, tmpfile, &ctx);
766 2e5a6fad 2020-03-18 stsp if (pack->map) {
767 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen + obj->len;
769 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
770 2e5a6fad 2020-03-18 stsp SEEK_SET) == -1) {
771 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
776 668a20f6 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_BLOB ||
777 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TREE ||
778 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_COMMIT ||
779 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TAG) {
780 262c582a 2020-03-18 stsp obj->valid = 1;
783 160bbe2e 2020-03-18 stsp if (first_delta_idx == -1)
784 160bbe2e 2020-03-18 stsp first_delta_idx = i;
785 160bbe2e 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
786 160bbe2e 2020-03-18 stsp have_ref_deltas = 1;
789 668a20f6 2020-03-18 stsp nvalid = nloose;
792 6ad68bce 2020-03-24 stsp * Having done a full pass over the pack file and can now
793 6ad68bce 2020-03-24 stsp * verify its checksum.
795 6ad68bce 2020-03-24 stsp SHA1Final(pack_sha1, &ctx);
796 3abebdc2 2020-03-24 stsp if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
797 3abebdc2 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
798 3abebdc2 2020-03-24 stsp "pack file checksum mismatch");
802 3abebdc2 2020-03-24 stsp /* Verify the SHA1 checksum stored at the end of the pack file. */
803 6ad68bce 2020-03-24 stsp if (pack->map) {
804 6ad68bce 2020-03-24 stsp memcpy(pack_sha1_expected, pack->map +
805 6ad68bce 2020-03-24 stsp pack->filesize - SHA1_DIGEST_LENGTH,
806 6ad68bce 2020-03-24 stsp SHA1_DIGEST_LENGTH);
809 6ad68bce 2020-03-24 stsp if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
810 6ad68bce 2020-03-24 stsp err = got_error_from_errno("lseek");
813 6ad68bce 2020-03-24 stsp n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
814 6ad68bce 2020-03-24 stsp if (n == -1) {
815 6ad68bce 2020-03-24 stsp err = got_error_from_errno("read");
818 6ad68bce 2020-03-24 stsp if (n != SHA1_DIGEST_LENGTH) {
819 6ad68bce 2020-03-24 stsp err = got_error(GOT_ERR_IO);
823 6ad68bce 2020-03-24 stsp if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
824 6ad68bce 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
825 3abebdc2 2020-03-24 stsp "bad checksum in pack file trailer");
829 160bbe2e 2020-03-18 stsp if (first_delta_idx == -1)
830 160bbe2e 2020-03-18 stsp first_delta_idx = 0;
832 0fd91daf 2020-03-18 stsp /* In order to resolve ref deltas we need an in-progress pack index. */
833 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
834 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
837 668a20f6 2020-03-18 stsp * Second pass: We can now resolve deltas to compute the IDs of
838 668a20f6 2020-03-18 stsp * objects which appear in deltified form. Because deltas can be
839 668a20f6 2020-03-18 stsp * chained this pass may require a couple of iterations until all
840 668a20f6 2020-03-18 stsp * IDs of deltified objects have been discovered.
843 668a20f6 2020-03-18 stsp while (nvalid != nobj) {
846 e6c1173d 2020-03-18 stsp * This loop will only run once unless the pack file
847 e6c1173d 2020-03-18 stsp * contains ref deltas which refer to objects located
848 e6c1173d 2020-03-18 stsp * later in the pack file, which is unusual.
849 e6c1173d 2020-03-18 stsp * Offset deltas can always be resolved in one pass
850 e6c1173d 2020-03-18 stsp * unless the packfile is corrupt.
852 160bbe2e 2020-03-18 stsp for (i = first_delta_idx; i < nobj; i++) {
853 262c582a 2020-03-18 stsp obj = &objects[i];
854 262c582a 2020-03-18 stsp if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
855 262c582a 2020-03-18 stsp obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
858 262c582a 2020-03-18 stsp if (obj->valid)
861 b3e1118b 2020-03-18 stsp if (pack->map == NULL && lseek(pack->fd,
862 b3e1118b 2020-03-18 stsp obj->off + obj->tslen, SEEK_SET) == -1) {
863 b3e1118b 2020-03-18 stsp err = got_error_from_errno("lseek");
867 d582f26c 2020-03-18 stsp err = resolve_deltified_object(pack, &packidx, obj,
868 d582f26c 2020-03-18 stsp tmpfile, delta_base_file, delta_accum_file);
870 668a20f6 2020-03-18 stsp if (err->code != GOT_ERR_NO_OBJ)
873 668a20f6 2020-03-18 stsp * We cannot resolve this object yet because
874 668a20f6 2020-03-18 stsp * a delta base is unknown. Try again later.
879 262c582a 2020-03-18 stsp obj->valid = 1;
881 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
882 0fd91daf 2020-03-18 stsp update_packidx(&packidx, nobj, obj);
883 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
884 5672d305 2020-03-18 stsp p_resolved = ((nresolved + n) * 100) / nobj;
885 5672d305 2020-03-18 stsp if (p_resolved != last_p_resolved) {
886 e70bf110 2020-03-22 stsp err = send_index_pack_progress(ibuf, nobj,
887 e70bf110 2020-03-22 stsp nobj, nloose, nresolved + n);
890 5672d305 2020-03-18 stsp last_p_resolved = p_resolved;
894 668a20f6 2020-03-18 stsp if (pass++ > 3 && n == 0) {
895 668a20f6 2020-03-18 stsp static char msg[64];
896 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg), "could not resolve "
897 668a20f6 2020-03-18 stsp "any of deltas; packfile could be corrupt");
898 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
902 668a20f6 2020-03-18 stsp nresolved += n;
903 668a20f6 2020-03-18 stsp nvalid += nresolved;
906 668a20f6 2020-03-18 stsp if (nloose + nresolved != nobj) {
907 668a20f6 2020-03-18 stsp static char msg[64];
908 ec92f929 2020-03-18 stsp snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
909 cbc66309 2020-03-18 stsp nloose + nresolved, nobj);
910 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
914 e70bf110 2020-03-22 stsp err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved);
918 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
920 97684601 2020-03-18 stsp free(objects);
921 97684601 2020-03-18 stsp objects = NULL;
923 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
924 b102634c 2020-03-18 stsp putbe32(buf, GOT_PACKIDX_V2_MAGIC);
925 b102634c 2020-03-18 stsp putbe32(buf + 4, GOT_PACKIDX_VERSION);
926 b102634c 2020-03-18 stsp err = hwrite(idxfd, buf, 8, &ctx);
929 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.fanout_table,
930 668a20f6 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
933 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.sorted_ids,
934 668a20f6 2020-03-18 stsp nobj * SHA1_DIGEST_LENGTH, &ctx);
937 97684601 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
940 cbc66309 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
944 7bad1537 2020-03-18 stsp if (packidx.nlargeobj > 0) {
945 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.large_offsets,
946 7bad1537 2020-03-18 stsp packidx.nlargeobj * sizeof(uint64_t), &ctx);
950 3abebdc2 2020-03-24 stsp err = hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
954 668a20f6 2020-03-18 stsp SHA1Final(packidx_hash, &ctx);
955 668a20f6 2020-03-18 stsp w = write(idxfd, packidx_hash, sizeof(packidx_hash));
956 668a20f6 2020-03-18 stsp if (w == -1) {
957 668a20f6 2020-03-18 stsp err = got_error_from_errno("write");
960 668a20f6 2020-03-18 stsp if (w != sizeof(packidx_hash)) {
961 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_IO);
965 37ebab0a 2020-03-18 stsp free(objects);
966 668a20f6 2020-03-18 stsp free(packidx.hdr.magic);
967 668a20f6 2020-03-18 stsp free(packidx.hdr.version);
968 668a20f6 2020-03-18 stsp free(packidx.hdr.fanout_table);
969 668a20f6 2020-03-18 stsp free(packidx.hdr.sorted_ids);
970 668a20f6 2020-03-18 stsp free(packidx.hdr.offsets);
971 668a20f6 2020-03-18 stsp free(packidx.hdr.large_offsets);
972 668a20f6 2020-03-18 stsp return err;
976 93658fb9 2020-03-18 stsp main(int argc, char **argv)
978 668a20f6 2020-03-18 stsp const struct got_error *err = NULL, *close_err;
979 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
980 93658fb9 2020-03-18 stsp struct imsg imsg;
982 6059809a 2020-12-17 stsp int idxfd = -1, tmpfd = -1;
983 d582f26c 2020-03-18 stsp FILE *tmpfiles[3];
984 668a20f6 2020-03-18 stsp struct got_pack pack;
985 668a20f6 2020-03-18 stsp uint8_t pack_hash[SHA1_DIGEST_LENGTH];
986 668a20f6 2020-03-18 stsp off_t packfile_size;
988 668a20f6 2020-03-18 stsp static int attached;
989 668a20f6 2020-03-18 stsp while (!attached)
993 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++)
994 d582f26c 2020-03-18 stsp tmpfiles[i] = NULL;
996 668a20f6 2020-03-18 stsp memset(&pack, 0, sizeof(pack));
997 668a20f6 2020-03-18 stsp pack.fd = -1;
998 18d4da03 2020-03-18 stsp pack.delta_cache = got_delta_cache_alloc(500,
999 668a20f6 2020-03-18 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1000 668a20f6 2020-03-18 stsp if (pack.delta_cache == NULL) {
1001 668a20f6 2020-03-18 stsp err = got_error_from_errno("got_delta_cache_alloc");
1002 93658fb9 2020-03-18 stsp goto done;
1005 668a20f6 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1006 861f3006 2020-03-18 stsp #ifndef PROFILE
1007 861f3006 2020-03-18 stsp /* revoke access to most system calls */
1008 861f3006 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
1009 861f3006 2020-03-18 stsp err = got_error_from_errno("pledge");
1010 861f3006 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1014 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1016 668a20f6 2020-03-18 stsp goto done;
1017 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1018 93658fb9 2020-03-18 stsp goto done;
1019 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
1020 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1021 93658fb9 2020-03-18 stsp goto done;
1023 668a20f6 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
1024 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1025 93658fb9 2020-03-18 stsp goto done;
1027 668a20f6 2020-03-18 stsp memcpy(pack_hash, imsg.data, sizeof(pack_hash));
1028 668a20f6 2020-03-18 stsp pack.fd = imsg.fd;
1030 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1032 93658fb9 2020-03-18 stsp goto done;
1033 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1034 93658fb9 2020-03-18 stsp goto done;
1035 73ab1060 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
1036 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1037 93658fb9 2020-03-18 stsp goto done;
1039 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1040 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1041 93658fb9 2020-03-18 stsp goto done;
1043 93658fb9 2020-03-18 stsp idxfd = imsg.fd;
1045 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
1046 d582f26c 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1048 d582f26c 2020-03-18 stsp goto done;
1049 d582f26c 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1050 d582f26c 2020-03-18 stsp goto done;
1051 d582f26c 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
1052 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1053 d582f26c 2020-03-18 stsp goto done;
1055 d582f26c 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1056 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1057 d582f26c 2020-03-18 stsp goto done;
1059 d582f26c 2020-03-18 stsp tmpfd = imsg.fd;
1060 d582f26c 2020-03-18 stsp tmpfiles[i] = fdopen(tmpfd, "w+");
1061 d582f26c 2020-03-18 stsp if (tmpfiles[i] == NULL) {
1062 d582f26c 2020-03-18 stsp err = got_error_from_errno("fdopen");
1063 d582f26c 2020-03-18 stsp goto done;
1065 d582f26c 2020-03-18 stsp tmpfd = -1;
1068 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_END) == -1) {
1069 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
1070 668a20f6 2020-03-18 stsp goto done;
1072 668a20f6 2020-03-18 stsp packfile_size = lseek(pack.fd, 0, SEEK_CUR);
1073 668a20f6 2020-03-18 stsp if (packfile_size == -1) {
1074 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
1075 668a20f6 2020-03-18 stsp goto done;
1077 668a20f6 2020-03-18 stsp pack.filesize = packfile_size; /* XXX off_t vs size_t */
1079 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_SET) == -1) {
1080 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
1081 668a20f6 2020-03-18 stsp goto done;
1084 2e5a6fad 2020-03-18 stsp #ifndef GOT_PACK_NO_MMAP
1085 2e5a6fad 2020-03-18 stsp pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
1086 2e5a6fad 2020-03-18 stsp pack.fd, 0);
1087 2e5a6fad 2020-03-18 stsp if (pack.map == MAP_FAILED)
1088 2e5a6fad 2020-03-18 stsp pack.map = NULL; /* fall back to read(2) */
1090 d582f26c 2020-03-18 stsp err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1091 d582f26c 2020-03-18 stsp pack_hash, &ibuf);
1093 668a20f6 2020-03-18 stsp close_err = got_pack_close(&pack);
1094 668a20f6 2020-03-18 stsp if (close_err && err == NULL)
1095 668a20f6 2020-03-18 stsp err = close_err;
1096 668a20f6 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1097 668a20f6 2020-03-18 stsp err = got_error_from_errno("close");
1098 4788f1ce 2020-03-18 stsp if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1099 4788f1ce 2020-03-18 stsp err = got_error_from_errno("close");
1100 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
1101 d582f26c 2020-03-18 stsp if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1102 d582f26c 2020-03-18 stsp err == NULL)
1103 07965a0d 2021-10-15 stsp err = got_error_from_errno("fclose");
1106 668a20f6 2020-03-18 stsp if (err == NULL)
1107 e70bf110 2020-03-22 stsp err = send_index_pack_done(&ibuf);
1108 668a20f6 2020-03-18 stsp if (err) {
1109 668a20f6 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1110 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1111 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);