2 0136599f 2022-10-20 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 0136599f 2022-10-20 stsp * Copyright (c) 2020, 2022 Stefan Sperling <stsp@openbsd.org>
5 0136599f 2022-10-20 stsp * Permission to use, copy, modify, and distribute this software for any
6 0136599f 2022-10-20 stsp * purpose with or without fee is hereby granted, provided that the above
7 0136599f 2022-10-20 stsp * copyright notice and this permission notice appear in all copies.
9 0136599f 2022-10-20 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 0136599f 2022-10-20 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 0136599f 2022-10-20 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 0136599f 2022-10-20 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 0136599f 2022-10-20 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 0136599f 2022-10-20 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 0136599f 2022-10-20 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 0136599f 2022-10-20 stsp #include <sys/queue.h>
19 0136599f 2022-10-20 stsp #include <sys/stat.h>
20 0136599f 2022-10-20 stsp #include <sys/time.h>
21 0136599f 2022-10-20 stsp #include <sys/types.h>
22 0136599f 2022-10-20 stsp #include <sys/uio.h>
23 0136599f 2022-10-20 stsp #include <sys/mman.h>
25 0136599f 2022-10-20 stsp #include <stdint.h>
26 0136599f 2022-10-20 stsp #include <errno.h>
27 0136599f 2022-10-20 stsp #include <imsg.h>
28 0136599f 2022-10-20 stsp #include <limits.h>
29 0136599f 2022-10-20 stsp #include <signal.h>
30 0136599f 2022-10-20 stsp #include <stdio.h>
31 0136599f 2022-10-20 stsp #include <stdlib.h>
32 0136599f 2022-10-20 stsp #include <string.h>
33 0136599f 2022-10-20 stsp #include <ctype.h>
34 0136599f 2022-10-20 stsp #include <sha1.h>
35 5822e79e 2023-02-23 op #include <sha2.h>
36 0136599f 2022-10-20 stsp #include <endian.h>
37 0136599f 2022-10-20 stsp #include <fcntl.h>
38 0136599f 2022-10-20 stsp #include <unistd.h>
39 0136599f 2022-10-20 stsp #include <zlib.h>
40 0136599f 2022-10-20 stsp #include <err.h>
41 0136599f 2022-10-20 stsp #include <assert.h>
42 0136599f 2022-10-20 stsp #include <dirent.h>
44 0136599f 2022-10-20 stsp #include "got_error.h"
45 0136599f 2022-10-20 stsp #include "got_object.h"
47 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
48 0136599f 2022-10-20 stsp #include "got_lib_delta.h"
49 0136599f 2022-10-20 stsp #include "got_lib_inflate.h"
50 0136599f 2022-10-20 stsp #include "got_lib_object.h"
51 0136599f 2022-10-20 stsp #include "got_lib_object_parse.h"
52 0136599f 2022-10-20 stsp #include "got_lib_object_idset.h"
53 0136599f 2022-10-20 stsp #include "got_lib_privsep.h"
54 0136599f 2022-10-20 stsp #include "got_lib_pack.h"
55 0136599f 2022-10-20 stsp #include "got_lib_ratelimit.h"
56 0136599f 2022-10-20 stsp #include "got_lib_pack_index.h"
57 0136599f 2022-10-20 stsp #include "got_lib_delta_cache.h"
59 0136599f 2022-10-20 stsp struct got_indexed_object {
60 0136599f 2022-10-20 stsp struct got_object_id id;
63 0136599f 2022-10-20 stsp * Has this object been fully resolved?
64 0136599f 2022-10-20 stsp * If so, we know its ID, otherwise we don't and 'id' is invalid.
68 0136599f 2022-10-20 stsp /* Offset of type+size field for this object in pack file. */
71 0136599f 2022-10-20 stsp /* Type+size values parsed from pack file. */
72 0136599f 2022-10-20 stsp uint8_t type;
73 0136599f 2022-10-20 stsp uint64_t size;
75 0136599f 2022-10-20 stsp /* Length of on-disk type+size data. */
76 0136599f 2022-10-20 stsp size_t tslen;
78 0136599f 2022-10-20 stsp /* Length of object data following type+size. */
81 0136599f 2022-10-20 stsp uint32_t crc;
85 0136599f 2022-10-20 stsp /* For ref deltas. */
86 0136599f 2022-10-20 stsp struct got_object_id ref_id;
89 0136599f 2022-10-20 stsp /* For offset deltas. */
90 0136599f 2022-10-20 stsp off_t base_offset;
91 0136599f 2022-10-20 stsp size_t base_offsetlen;
97 0136599f 2022-10-20 stsp putbe32(char *b, uint32_t n)
99 0136599f 2022-10-20 stsp b[0] = n >> 24;
100 0136599f 2022-10-20 stsp b[1] = n >> 16;
101 0136599f 2022-10-20 stsp b[2] = n >> 8;
102 0136599f 2022-10-20 stsp b[3] = n >> 0;
105 0136599f 2022-10-20 stsp static const struct got_error *
106 0136599f 2022-10-20 stsp get_obj_type_label(const char **label, int obj_type)
108 0136599f 2022-10-20 stsp const struct got_error *err = NULL;
110 0136599f 2022-10-20 stsp switch (obj_type) {
111 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_BLOB:
112 0136599f 2022-10-20 stsp *label = GOT_OBJ_LABEL_BLOB;
114 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_TREE:
115 0136599f 2022-10-20 stsp *label = GOT_OBJ_LABEL_TREE;
117 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_COMMIT:
118 0136599f 2022-10-20 stsp *label = GOT_OBJ_LABEL_COMMIT;
120 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_TAG:
121 0136599f 2022-10-20 stsp *label = GOT_OBJ_LABEL_TAG;
124 0136599f 2022-10-20 stsp *label = NULL;
125 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_OBJ_TYPE);
129 0136599f 2022-10-20 stsp return err;
132 0136599f 2022-10-20 stsp static const struct got_error *
133 ae25a666 2023-02-23 op read_checksum(uint32_t *crc, struct got_hash *ctx, int fd, size_t len)
135 0136599f 2022-10-20 stsp uint8_t buf[8192];
139 0136599f 2022-10-20 stsp for (n = len; n > 0; n -= r){
140 0136599f 2022-10-20 stsp r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
141 0136599f 2022-10-20 stsp if (r == -1)
142 0136599f 2022-10-20 stsp return got_error_from_errno("read");
143 0136599f 2022-10-20 stsp if (r == 0)
146 0136599f 2022-10-20 stsp *crc = crc32(*crc, buf, r);
148 ae25a666 2023-02-23 op got_hash_update(ctx, buf, r);
151 0136599f 2022-10-20 stsp return NULL;
154 0136599f 2022-10-20 stsp static const struct got_error *
155 ae25a666 2023-02-23 op read_file_digest(struct got_hash *ctx, FILE *f, size_t len)
157 0136599f 2022-10-20 stsp uint8_t buf[8192];
158 0136599f 2022-10-20 stsp size_t n, r;
160 0136599f 2022-10-20 stsp for (n = len; n > 0; n -= r) {
161 0136599f 2022-10-20 stsp r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
162 0136599f 2022-10-20 stsp if (r == 0) {
163 0136599f 2022-10-20 stsp if (feof(f))
164 0136599f 2022-10-20 stsp return NULL;
165 0136599f 2022-10-20 stsp return got_ferror(f, GOT_ERR_IO);
167 ae25a666 2023-02-23 op got_hash_update(ctx, buf, r);
170 0136599f 2022-10-20 stsp return NULL;
173 0136599f 2022-10-20 stsp static const struct got_error *
174 0136599f 2022-10-20 stsp read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
175 ae25a666 2023-02-23 op FILE *tmpfile, struct got_hash *pack_sha1_ctx)
177 0136599f 2022-10-20 stsp const struct got_error *err = NULL;
178 ae25a666 2023-02-23 op struct got_hash ctx;
179 0136599f 2022-10-20 stsp uint8_t *data = NULL;
180 0136599f 2022-10-20 stsp size_t datalen = 0;
182 0136599f 2022-10-20 stsp char *header;
183 0136599f 2022-10-20 stsp size_t headerlen;
184 0136599f 2022-10-20 stsp const char *obj_label;
185 0136599f 2022-10-20 stsp size_t mapoff = obj->off;
186 0136599f 2022-10-20 stsp struct got_inflate_checksum csum;
188 0136599f 2022-10-20 stsp memset(&csum, 0, sizeof(csum));
189 ae25a666 2023-02-23 op csum.input_ctx = pack_sha1_ctx;
190 0136599f 2022-10-20 stsp csum.input_crc = &obj->crc;
192 0136599f 2022-10-20 stsp err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
193 0136599f 2022-10-20 stsp &obj->tslen, pack, obj->off);
195 0136599f 2022-10-20 stsp return err;
197 0136599f 2022-10-20 stsp if (pack->map) {
198 0136599f 2022-10-20 stsp obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
199 ae25a666 2023-02-23 op got_hash_update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
200 0136599f 2022-10-20 stsp mapoff += obj->tslen;
202 0136599f 2022-10-20 stsp /* XXX Seek back and get the CRC of on-disk type+size bytes. */
203 0136599f 2022-10-20 stsp if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
204 0136599f 2022-10-20 stsp return got_error_from_errno("lseek");
205 0136599f 2022-10-20 stsp err = read_checksum(&obj->crc, pack_sha1_ctx,
206 0136599f 2022-10-20 stsp pack->fd, obj->tslen);
208 0136599f 2022-10-20 stsp return err;
211 0136599f 2022-10-20 stsp switch (obj->type) {
212 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_BLOB:
213 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_COMMIT:
214 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_TREE:
215 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_TAG:
216 0136599f 2022-10-20 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
217 0136599f 2022-10-20 stsp if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
218 0136599f 2022-10-20 stsp err = got_error_from_errno("fseek");
221 0136599f 2022-10-20 stsp if (pack->map) {
222 0136599f 2022-10-20 stsp err = got_inflate_to_file_mmap(&datalen,
223 0136599f 2022-10-20 stsp &obj->len, &csum, pack->map, mapoff,
224 0136599f 2022-10-20 stsp pack->filesize - mapoff, tmpfile);
226 0136599f 2022-10-20 stsp err = got_inflate_to_file_fd(&datalen,
227 0136599f 2022-10-20 stsp &obj->len, &csum, pack->fd, tmpfile);
230 0136599f 2022-10-20 stsp if (pack->map) {
231 0136599f 2022-10-20 stsp err = got_inflate_to_mem_mmap(&data, &datalen,
232 0136599f 2022-10-20 stsp &obj->len, &csum, pack->map, mapoff,
233 0136599f 2022-10-20 stsp pack->filesize - mapoff);
235 0136599f 2022-10-20 stsp err = got_inflate_to_mem_fd(&data, &datalen,
236 0136599f 2022-10-20 stsp &obj->len, &csum, obj->size, pack->fd);
241 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
242 0136599f 2022-10-20 stsp err = get_obj_type_label(&obj_label, obj->type);
244 0136599f 2022-10-20 stsp free(data);
247 0136599f 2022-10-20 stsp if (asprintf(&header, "%s %lld", obj_label,
248 0136599f 2022-10-20 stsp (long long)obj->size) == -1) {
249 0136599f 2022-10-20 stsp err = got_error_from_errno("asprintf");
250 0136599f 2022-10-20 stsp free(data);
253 0136599f 2022-10-20 stsp headerlen = strlen(header) + 1;
254 ae25a666 2023-02-23 op got_hash_update(&ctx, header, headerlen);
255 0136599f 2022-10-20 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
256 ae25a666 2023-02-23 op err = read_file_digest(&ctx, tmpfile, datalen);
258 0136599f 2022-10-20 stsp free(header);
259 0136599f 2022-10-20 stsp free(data);
263 ae25a666 2023-02-23 op got_hash_update(&ctx, data, datalen);
264 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &obj->id);
265 0136599f 2022-10-20 stsp free(header);
266 0136599f 2022-10-20 stsp free(data);
268 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_REF_DELTA:
269 0136599f 2022-10-20 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
270 0136599f 2022-10-20 stsp if (pack->map) {
271 0136599f 2022-10-20 stsp if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
272 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
275 ad4cc361 2022-10-27 op if (mapoff + SHA1_DIGEST_LENGTH > SIZE_MAX) {
276 ad4cc361 2022-10-27 op err = got_error_fmt(GOT_ERR_RANGE,
277 ad4cc361 2022-10-27 op "mapoff %lld would overflow size_t",
278 ad4cc361 2022-10-27 op (long long)mapoff + SHA1_DIGEST_LENGTH);
281 0136599f 2022-10-20 stsp memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
282 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
283 0136599f 2022-10-20 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
284 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
285 ae25a666 2023-02-23 op got_hash_update(pack_sha1_ctx, pack->map + mapoff,
286 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
287 0136599f 2022-10-20 stsp mapoff += SHA1_DIGEST_LENGTH;
288 0136599f 2022-10-20 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
289 0136599f 2022-10-20 stsp &obj->len, &csum, pack->map, mapoff,
290 0136599f 2022-10-20 stsp pack->filesize - mapoff);
294 0136599f 2022-10-20 stsp n = read(pack->fd, obj->delta.ref.ref_id.sha1,
295 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
296 0136599f 2022-10-20 stsp if (n == -1) {
297 0136599f 2022-10-20 stsp err = got_error_from_errno("read");
300 0136599f 2022-10-20 stsp if (n < sizeof(obj->id)) {
301 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
304 0136599f 2022-10-20 stsp obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
305 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
306 ae25a666 2023-02-23 op got_hash_update(pack_sha1_ctx,
307 ae25a666 2023-02-23 op obj->delta.ref.ref_id.sha1, SHA1_DIGEST_LENGTH);
308 0136599f 2022-10-20 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
309 0136599f 2022-10-20 stsp &csum, obj->size, pack->fd);
313 0136599f 2022-10-20 stsp obj->len += SHA1_DIGEST_LENGTH;
315 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
316 0136599f 2022-10-20 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
317 0136599f 2022-10-20 stsp err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
318 0136599f 2022-10-20 stsp &obj->delta.ofs.base_offsetlen, pack, obj->off,
319 0136599f 2022-10-20 stsp obj->tslen);
323 0136599f 2022-10-20 stsp if (pack->map) {
324 5326bd73 2022-10-24 op if (mapoff + obj->delta.ofs.base_offsetlen >=
325 5326bd73 2022-10-24 op pack->filesize) {
326 5326bd73 2022-10-24 op err = got_error(GOT_ERR_BAD_PACKFILE);
330 ad4cc361 2022-10-27 op if (mapoff + obj->delta.ofs.base_offsetlen >
332 ad4cc361 2022-10-27 op err = got_error_fmt(GOT_ERR_RANGE,
333 ad4cc361 2022-10-27 op "mapoff %lld would overflow size_t",
334 ad4cc361 2022-10-27 op (long long)mapoff
335 ad4cc361 2022-10-27 op + obj->delta.ofs.base_offsetlen);
338 0136599f 2022-10-20 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
339 0136599f 2022-10-20 stsp obj->delta.ofs.base_offsetlen);
340 ae25a666 2023-02-23 op got_hash_update(pack_sha1_ctx, pack->map + mapoff,
341 0136599f 2022-10-20 stsp obj->delta.ofs.base_offsetlen);
342 0136599f 2022-10-20 stsp mapoff += obj->delta.ofs.base_offsetlen;
343 0136599f 2022-10-20 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
344 0136599f 2022-10-20 stsp &obj->len, &csum, pack->map, mapoff,
345 0136599f 2022-10-20 stsp pack->filesize - mapoff);
350 0136599f 2022-10-20 stsp * XXX Seek back and get CRC and SHA1 of on-disk
351 0136599f 2022-10-20 stsp * offset bytes.
353 0136599f 2022-10-20 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
355 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
358 0136599f 2022-10-20 stsp err = read_checksum(&obj->crc, pack_sha1_ctx,
359 0136599f 2022-10-20 stsp pack->fd, obj->delta.ofs.base_offsetlen);
363 0136599f 2022-10-20 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
364 0136599f 2022-10-20 stsp &csum, obj->size, pack->fd);
368 0136599f 2022-10-20 stsp obj->len += obj->delta.ofs.base_offsetlen;
371 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_OBJ_TYPE);
375 0136599f 2022-10-20 stsp return err;
378 13b2bc37 2022-10-23 stsp const struct got_error *
379 ae25a666 2023-02-23 op got_pack_hwrite(int fd, void *buf, int len, struct got_hash *ctx)
383 ae25a666 2023-02-23 op got_hash_update(ctx, buf, len);
385 0136599f 2022-10-20 stsp w = write(fd, buf, len);
386 0136599f 2022-10-20 stsp if (w == -1)
387 0136599f 2022-10-20 stsp return got_error_from_errno("write");
388 0136599f 2022-10-20 stsp if (w != len)
389 0136599f 2022-10-20 stsp return got_error(GOT_ERR_IO);
391 0136599f 2022-10-20 stsp return NULL;
394 0136599f 2022-10-20 stsp static const struct got_error *
395 0136599f 2022-10-20 stsp resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
396 0136599f 2022-10-20 stsp struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
397 0136599f 2022-10-20 stsp FILE *delta_accum_file)
399 0136599f 2022-10-20 stsp const struct got_error *err = NULL;
400 0136599f 2022-10-20 stsp struct got_delta_chain deltas;
401 0136599f 2022-10-20 stsp struct got_delta *delta;
402 0136599f 2022-10-20 stsp uint8_t *buf = NULL;
403 0136599f 2022-10-20 stsp size_t len = 0;
404 ae25a666 2023-02-23 op struct got_hash ctx;
405 0136599f 2022-10-20 stsp char *header = NULL;
406 0136599f 2022-10-20 stsp size_t headerlen;
407 0136599f 2022-10-20 stsp uint64_t max_size;
408 0136599f 2022-10-20 stsp int base_obj_type;
409 0136599f 2022-10-20 stsp const char *obj_label;
411 0136599f 2022-10-20 stsp deltas.nentries = 0;
412 0136599f 2022-10-20 stsp STAILQ_INIT(&deltas.entries);
414 0136599f 2022-10-20 stsp err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
415 0136599f 2022-10-20 stsp obj->off, obj->tslen, obj->type, obj->size,
416 0136599f 2022-10-20 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
420 0136599f 2022-10-20 stsp err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
423 0136599f 2022-10-20 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
424 0136599f 2022-10-20 stsp rewind(tmpfile);
425 0136599f 2022-10-20 stsp rewind(delta_base_file);
426 0136599f 2022-10-20 stsp rewind(delta_accum_file);
427 0136599f 2022-10-20 stsp err = got_pack_dump_delta_chain_to_file(&len, &deltas,
428 0136599f 2022-10-20 stsp pack, tmpfile, delta_base_file, delta_accum_file);
432 0136599f 2022-10-20 stsp err = got_pack_dump_delta_chain_to_mem(&buf, &len,
433 0136599f 2022-10-20 stsp &deltas, pack);
438 0136599f 2022-10-20 stsp err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
441 0136599f 2022-10-20 stsp err = get_obj_type_label(&obj_label, base_obj_type);
444 0136599f 2022-10-20 stsp if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
445 0136599f 2022-10-20 stsp err = got_error_from_errno("asprintf");
448 0136599f 2022-10-20 stsp headerlen = strlen(header) + 1;
449 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
450 ae25a666 2023-02-23 op got_hash_update(&ctx, header, headerlen);
451 0136599f 2022-10-20 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
452 ae25a666 2023-02-23 op err = read_file_digest(&ctx, tmpfile, len);
456 ae25a666 2023-02-23 op got_hash_update(&ctx, buf, len);
457 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &obj->id);
460 0136599f 2022-10-20 stsp free(header);
461 0136599f 2022-10-20 stsp while (!STAILQ_EMPTY(&deltas.entries)) {
462 0136599f 2022-10-20 stsp delta = STAILQ_FIRST(&deltas.entries);
463 0136599f 2022-10-20 stsp STAILQ_REMOVE_HEAD(&deltas.entries, entry);
464 0136599f 2022-10-20 stsp free(delta);
466 0136599f 2022-10-20 stsp return err;
469 0136599f 2022-10-20 stsp /* Determine the slot in the pack index a given object ID should use. */
471 0136599f 2022-10-20 stsp find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
473 0136599f 2022-10-20 stsp u_int8_t id0 = sha1[0];
474 0136599f 2022-10-20 stsp uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
475 0136599f 2022-10-20 stsp int left = 0, right = nindexed - 1;
476 0136599f 2022-10-20 stsp int cmp = 0, i = 0;
478 0136599f 2022-10-20 stsp if (id0 > 0)
479 0136599f 2022-10-20 stsp left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
481 0136599f 2022-10-20 stsp while (left <= right) {
482 0136599f 2022-10-20 stsp struct got_packidx_object_id *oid;
484 0136599f 2022-10-20 stsp i = ((left + right) / 2);
485 0136599f 2022-10-20 stsp oid = &packidx->hdr.sorted_ids[i];
487 0136599f 2022-10-20 stsp cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
488 0136599f 2022-10-20 stsp if (cmp == 0)
489 0136599f 2022-10-20 stsp return -1; /* object already indexed */
490 0136599f 2022-10-20 stsp else if (cmp > 0)
491 0136599f 2022-10-20 stsp left = i + 1;
492 0136599f 2022-10-20 stsp else if (cmp < 0)
493 0136599f 2022-10-20 stsp right = i - 1;
496 0136599f 2022-10-20 stsp return left;
500 0136599f 2022-10-20 stsp static void
501 0136599f 2022-10-20 stsp print_packidx(struct got_packidx *packidx)
503 0136599f 2022-10-20 stsp uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
506 0136599f 2022-10-20 stsp fprintf(stderr, "object IDs:\n");
507 0136599f 2022-10-20 stsp for (i = 0; i < nindexed; i++) {
508 0136599f 2022-10-20 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
509 0136599f 2022-10-20 stsp got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
510 0136599f 2022-10-20 stsp hex, sizeof(hex));
511 0136599f 2022-10-20 stsp fprintf(stderr, "%s\n", hex);
513 0136599f 2022-10-20 stsp fprintf(stderr, "\n");
515 0136599f 2022-10-20 stsp fprintf(stderr, "object offsets:\n");
516 0136599f 2022-10-20 stsp for (i = 0; i < nindexed; i++) {
517 0136599f 2022-10-20 stsp uint32_t offset = be32toh(packidx->hdr.offsets[i]);
518 0136599f 2022-10-20 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
519 0136599f 2022-10-20 stsp int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
520 0136599f 2022-10-20 stsp fprintf(stderr, "%u -> %llu\n", offset,
521 0136599f 2022-10-20 stsp be64toh(packidx->hdr.large_offsets[j]));
523 0136599f 2022-10-20 stsp fprintf(stderr, "%u\n", offset);
525 0136599f 2022-10-20 stsp fprintf(stderr, "\n");
527 0136599f 2022-10-20 stsp fprintf(stderr, "fanout table:");
528 0136599f 2022-10-20 stsp for (i = 0; i <= 0xff; i++)
529 0136599f 2022-10-20 stsp fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
530 0136599f 2022-10-20 stsp fprintf(stderr, "\n");
534 0136599f 2022-10-20 stsp static void
535 0136599f 2022-10-20 stsp add_indexed_object(struct got_packidx *packidx, uint32_t idx,
536 0136599f 2022-10-20 stsp struct got_indexed_object *obj)
540 0136599f 2022-10-20 stsp memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
541 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
542 0136599f 2022-10-20 stsp packidx->hdr.crc32[idx] = htobe32(obj->crc);
543 0136599f 2022-10-20 stsp if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
544 0136599f 2022-10-20 stsp packidx->hdr.offsets[idx] = htobe32(obj->off);
546 0136599f 2022-10-20 stsp packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
547 0136599f 2022-10-20 stsp GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
548 0136599f 2022-10-20 stsp packidx->hdr.large_offsets[packidx->nlargeobj] =
549 0136599f 2022-10-20 stsp htobe64(obj->off);
550 0136599f 2022-10-20 stsp packidx->nlargeobj++;
553 0136599f 2022-10-20 stsp for (i = obj->id.sha1[0]; i <= 0xff; i++) {
554 0136599f 2022-10-20 stsp uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
555 0136599f 2022-10-20 stsp packidx->hdr.fanout_table[i] = htobe32(n + 1);
560 0136599f 2022-10-20 stsp indexed_obj_cmp(const void *pa, const void *pb)
562 0136599f 2022-10-20 stsp struct got_indexed_object *a, *b;
564 0136599f 2022-10-20 stsp a = (struct got_indexed_object *)pa;
565 0136599f 2022-10-20 stsp b = (struct got_indexed_object *)pb;
566 0136599f 2022-10-20 stsp return got_object_id_cmp(&a->id, &b->id);
569 0136599f 2022-10-20 stsp static void
570 9316cc27 2022-10-20 stsp make_packidx(struct got_packidx *packidx, uint32_t nobj,
571 0136599f 2022-10-20 stsp struct got_indexed_object *objects)
573 0136599f 2022-10-20 stsp struct got_indexed_object *obj;
575 0136599f 2022-10-20 stsp uint32_t idx = 0;
577 0136599f 2022-10-20 stsp qsort(objects, nobj, sizeof(struct got_indexed_object),
578 0136599f 2022-10-20 stsp indexed_obj_cmp);
580 0136599f 2022-10-20 stsp memset(packidx->hdr.fanout_table, 0,
581 0136599f 2022-10-20 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
582 0136599f 2022-10-20 stsp packidx->nlargeobj = 0;
584 0136599f 2022-10-20 stsp for (i = 0; i < nobj; i++) {
585 0136599f 2022-10-20 stsp obj = &objects[i];
586 0136599f 2022-10-20 stsp if (obj->valid)
587 0136599f 2022-10-20 stsp add_indexed_object(packidx, idx++, obj);
591 0136599f 2022-10-20 stsp static void
592 9316cc27 2022-10-20 stsp update_packidx(struct got_packidx *packidx, uint32_t nobj,
593 0136599f 2022-10-20 stsp struct got_indexed_object *obj)
596 0136599f 2022-10-20 stsp uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
598 0136599f 2022-10-20 stsp idx = find_object_idx(packidx, obj->id.sha1);
599 09cbb981 2023-02-03 op if (idx == -1)
600 0136599f 2022-10-20 stsp return; /* object already indexed */
602 0136599f 2022-10-20 stsp memmove(&packidx->hdr.sorted_ids[idx + 1],
603 0136599f 2022-10-20 stsp &packidx->hdr.sorted_ids[idx],
604 0136599f 2022-10-20 stsp sizeof(struct got_packidx_object_id) * (nindexed - idx));
605 0136599f 2022-10-20 stsp memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
606 0136599f 2022-10-20 stsp sizeof(uint32_t) * (nindexed - idx));
608 0136599f 2022-10-20 stsp add_indexed_object(packidx, idx, obj);
611 0136599f 2022-10-20 stsp static const struct got_error *
612 9316cc27 2022-10-20 stsp report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
613 9316cc27 2022-10-20 stsp uint32_t nobj_resolved, struct got_ratelimit *rl,
614 0136599f 2022-10-20 stsp got_pack_index_progress_cb progress_cb, void *progress_arg)
616 0136599f 2022-10-20 stsp const struct got_error *err;
617 0136599f 2022-10-20 stsp int elapsed = 0;
620 0136599f 2022-10-20 stsp err = got_ratelimit_check(&elapsed, rl);
621 0136599f 2022-10-20 stsp if (err || !elapsed)
622 0136599f 2022-10-20 stsp return err;
625 0136599f 2022-10-20 stsp return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
626 0136599f 2022-10-20 stsp nobj_resolved);
629 0136599f 2022-10-20 stsp const struct got_error *
630 0136599f 2022-10-20 stsp got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
631 0136599f 2022-10-20 stsp FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
632 713d6e11 2022-10-20 stsp got_pack_index_progress_cb progress_cb, void *progress_arg,
633 713d6e11 2022-10-20 stsp struct got_ratelimit *rl)
635 0136599f 2022-10-20 stsp const struct got_error *err;
636 0136599f 2022-10-20 stsp struct got_packfile_hdr hdr;
637 0136599f 2022-10-20 stsp struct got_packidx packidx;
638 0136599f 2022-10-20 stsp char buf[8];
639 0136599f 2022-10-20 stsp char pack_sha1[SHA1_DIGEST_LENGTH];
640 9316cc27 2022-10-20 stsp uint32_t nobj, nvalid, nloose, nresolved = 0, i;
641 0136599f 2022-10-20 stsp struct got_indexed_object *objects = NULL, *obj;
642 ae25a666 2023-02-23 op struct got_hash ctx;
643 0136599f 2022-10-20 stsp uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
644 0136599f 2022-10-20 stsp ssize_t r, w;
645 0136599f 2022-10-20 stsp int pass, have_ref_deltas = 0, first_delta_idx = -1;
646 0136599f 2022-10-20 stsp size_t mapoff = 0;
647 0136599f 2022-10-20 stsp int p_indexed = 0, last_p_indexed = -1;
648 0136599f 2022-10-20 stsp int p_resolved = 0, last_p_resolved = -1;
650 0136599f 2022-10-20 stsp /* Require that pack file header and SHA1 trailer are present. */
651 0136599f 2022-10-20 stsp if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
652 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
653 0136599f 2022-10-20 stsp "short pack file");
655 0136599f 2022-10-20 stsp if (pack->map) {
656 0136599f 2022-10-20 stsp memcpy(&hdr, pack->map, sizeof(hdr));
657 0136599f 2022-10-20 stsp mapoff += sizeof(hdr);
659 0136599f 2022-10-20 stsp r = read(pack->fd, &hdr, sizeof(hdr));
660 0136599f 2022-10-20 stsp if (r == -1)
661 0136599f 2022-10-20 stsp return got_error_from_errno("read");
662 0136599f 2022-10-20 stsp if (r < sizeof(hdr))
663 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
664 0136599f 2022-10-20 stsp "short pack file");
667 0136599f 2022-10-20 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
668 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
669 0136599f 2022-10-20 stsp "bad packfile signature");
670 0136599f 2022-10-20 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
671 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
672 0136599f 2022-10-20 stsp "bad packfile version");
673 0136599f 2022-10-20 stsp nobj = be32toh(hdr.nobjects);
674 0136599f 2022-10-20 stsp if (nobj == 0)
675 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
676 0136599f 2022-10-20 stsp "bad packfile with zero objects");
678 0136599f 2022-10-20 stsp /* We compute the SHA1 of pack file contents and verify later on. */
679 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
680 ae25a666 2023-02-23 op got_hash_update(&ctx, &hdr, sizeof(hdr));
683 0136599f 2022-10-20 stsp * Create an in-memory pack index which will grow as objects
684 0136599f 2022-10-20 stsp * IDs in the pack file are discovered. Only fields used to
685 0136599f 2022-10-20 stsp * read deltified objects will be needed by the pack.c library
686 0136599f 2022-10-20 stsp * code, so setting up just a pack index header is sufficient.
688 0136599f 2022-10-20 stsp memset(&packidx, 0, sizeof(packidx));
689 0136599f 2022-10-20 stsp packidx.hdr.magic = malloc(sizeof(uint32_t));
690 0136599f 2022-10-20 stsp if (packidx.hdr.magic == NULL)
691 0136599f 2022-10-20 stsp return got_error_from_errno("malloc");
692 0136599f 2022-10-20 stsp *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
693 0136599f 2022-10-20 stsp packidx.hdr.version = malloc(sizeof(uint32_t));
694 0136599f 2022-10-20 stsp if (packidx.hdr.version == NULL) {
695 0136599f 2022-10-20 stsp err = got_error_from_errno("malloc");
698 0136599f 2022-10-20 stsp *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
699 0136599f 2022-10-20 stsp packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
700 0136599f 2022-10-20 stsp sizeof(uint32_t));
701 0136599f 2022-10-20 stsp if (packidx.hdr.fanout_table == NULL) {
702 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
705 0136599f 2022-10-20 stsp packidx.hdr.sorted_ids = calloc(nobj,
706 0136599f 2022-10-20 stsp sizeof(struct got_packidx_object_id));
707 0136599f 2022-10-20 stsp if (packidx.hdr.sorted_ids == NULL) {
708 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
711 0136599f 2022-10-20 stsp packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
712 0136599f 2022-10-20 stsp if (packidx.hdr.crc32 == NULL) {
713 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
716 0136599f 2022-10-20 stsp packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
717 0136599f 2022-10-20 stsp if (packidx.hdr.offsets == NULL) {
718 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
721 0136599f 2022-10-20 stsp /* Large offsets table is empty for pack files < 2 GB. */
722 0136599f 2022-10-20 stsp if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
723 0136599f 2022-10-20 stsp packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
724 0136599f 2022-10-20 stsp if (packidx.hdr.large_offsets == NULL) {
725 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
730 0136599f 2022-10-20 stsp nvalid = 0;
731 0136599f 2022-10-20 stsp nloose = 0;
732 0136599f 2022-10-20 stsp objects = calloc(nobj, sizeof(struct got_indexed_object));
733 0136599f 2022-10-20 stsp if (objects == NULL)
734 0136599f 2022-10-20 stsp return got_error_from_errno("calloc");
737 0136599f 2022-10-20 stsp * First pass: locate all objects and identify un-deltified objects.
739 0136599f 2022-10-20 stsp * When this pass has completed we will know offset, type, size, and
740 0136599f 2022-10-20 stsp * CRC information for all objects in this pack file. We won't know
741 0136599f 2022-10-20 stsp * any of the actual object IDs of deltified objects yet since we
742 0136599f 2022-10-20 stsp * will not yet attempt to combine deltas.
745 0136599f 2022-10-20 stsp for (i = 0; i < nobj; i++) {
746 0136599f 2022-10-20 stsp /* Don't send too many progress privsep messages. */
747 0136599f 2022-10-20 stsp p_indexed = ((i + 1) * 100) / nobj;
748 0136599f 2022-10-20 stsp if (p_indexed != last_p_indexed) {
749 0136599f 2022-10-20 stsp err = report_progress(nobj, i + 1, nloose, 0,
750 713d6e11 2022-10-20 stsp rl, progress_cb, progress_arg);
753 0136599f 2022-10-20 stsp last_p_indexed = p_indexed;
756 0136599f 2022-10-20 stsp obj = &objects[i];
757 0136599f 2022-10-20 stsp obj->crc = crc32(0L, NULL, 0);
759 0136599f 2022-10-20 stsp /* Store offset to type+size information for this object. */
760 0136599f 2022-10-20 stsp if (pack->map) {
761 0136599f 2022-10-20 stsp obj->off = mapoff;
763 0136599f 2022-10-20 stsp obj->off = lseek(pack->fd, 0, SEEK_CUR);
764 0136599f 2022-10-20 stsp if (obj->off == -1) {
765 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
770 0136599f 2022-10-20 stsp err = read_packed_object(pack, obj, tmpfile, &ctx);
774 0136599f 2022-10-20 stsp if (pack->map) {
775 0136599f 2022-10-20 stsp mapoff += obj->tslen + obj->len;
777 0136599f 2022-10-20 stsp if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
778 0136599f 2022-10-20 stsp SEEK_SET) == -1) {
779 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
784 0136599f 2022-10-20 stsp if (obj->type == GOT_OBJ_TYPE_BLOB ||
785 0136599f 2022-10-20 stsp obj->type == GOT_OBJ_TYPE_TREE ||
786 0136599f 2022-10-20 stsp obj->type == GOT_OBJ_TYPE_COMMIT ||
787 0136599f 2022-10-20 stsp obj->type == GOT_OBJ_TYPE_TAG) {
788 0136599f 2022-10-20 stsp obj->valid = 1;
791 0136599f 2022-10-20 stsp if (first_delta_idx == -1)
792 0136599f 2022-10-20 stsp first_delta_idx = i;
793 0136599f 2022-10-20 stsp if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
794 0136599f 2022-10-20 stsp have_ref_deltas = 1;
797 0136599f 2022-10-20 stsp nvalid = nloose;
800 0136599f 2022-10-20 stsp * Having done a full pass over the pack file and can now
801 0136599f 2022-10-20 stsp * verify its checksum.
803 ae25a666 2023-02-23 op got_hash_final(&ctx, pack_sha1);
805 0136599f 2022-10-20 stsp if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
806 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PACKFILE_CSUM);
810 0136599f 2022-10-20 stsp /* Verify the SHA1 checksum stored at the end of the pack file. */
811 0136599f 2022-10-20 stsp if (pack->map) {
812 ad4cc361 2022-10-27 op if (pack->filesize > SIZE_MAX) {
813 ad4cc361 2022-10-27 op err = got_error_fmt(GOT_ERR_RANGE,
814 ad4cc361 2022-10-27 op "filesize %lld overflows size_t",
815 ad4cc361 2022-10-27 op (long long)pack->filesize);
819 0136599f 2022-10-20 stsp memcpy(pack_sha1_expected, pack->map +
820 0136599f 2022-10-20 stsp pack->filesize - SHA1_DIGEST_LENGTH,
821 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
824 0136599f 2022-10-20 stsp if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
825 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
828 0136599f 2022-10-20 stsp n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
829 0136599f 2022-10-20 stsp if (n == -1) {
830 0136599f 2022-10-20 stsp err = got_error_from_errno("read");
833 0136599f 2022-10-20 stsp if (n != SHA1_DIGEST_LENGTH) {
834 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_IO);
838 0136599f 2022-10-20 stsp if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
839 0136599f 2022-10-20 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
840 0136599f 2022-10-20 stsp "bad checksum in pack file trailer");
844 0136599f 2022-10-20 stsp if (first_delta_idx == -1)
845 0136599f 2022-10-20 stsp first_delta_idx = 0;
847 0136599f 2022-10-20 stsp /* In order to resolve ref deltas we need an in-progress pack index. */
848 0136599f 2022-10-20 stsp if (have_ref_deltas)
849 0136599f 2022-10-20 stsp make_packidx(&packidx, nobj, objects);
852 0136599f 2022-10-20 stsp * Second pass: We can now resolve deltas to compute the IDs of
853 0136599f 2022-10-20 stsp * objects which appear in deltified form. Because deltas can be
854 0136599f 2022-10-20 stsp * chained this pass may require a couple of iterations until all
855 0136599f 2022-10-20 stsp * IDs of deltified objects have been discovered.
858 0136599f 2022-10-20 stsp while (nvalid != nobj) {
861 0136599f 2022-10-20 stsp * This loop will only run once unless the pack file
862 0136599f 2022-10-20 stsp * contains ref deltas which refer to objects located
863 0136599f 2022-10-20 stsp * later in the pack file, which is unusual.
864 0136599f 2022-10-20 stsp * Offset deltas can always be resolved in one pass
865 0136599f 2022-10-20 stsp * unless the packfile is corrupt.
867 0136599f 2022-10-20 stsp for (i = first_delta_idx; i < nobj; i++) {
868 0136599f 2022-10-20 stsp obj = &objects[i];
869 0136599f 2022-10-20 stsp if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
870 0136599f 2022-10-20 stsp obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
873 0136599f 2022-10-20 stsp if (obj->valid)
876 0136599f 2022-10-20 stsp if (pack->map == NULL && lseek(pack->fd,
877 0136599f 2022-10-20 stsp obj->off + obj->tslen, SEEK_SET) == -1) {
878 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
882 0136599f 2022-10-20 stsp err = resolve_deltified_object(pack, &packidx, obj,
883 0136599f 2022-10-20 stsp tmpfile, delta_base_file, delta_accum_file);
885 0136599f 2022-10-20 stsp if (err->code != GOT_ERR_NO_OBJ)
888 0136599f 2022-10-20 stsp * We cannot resolve this object yet because
889 0136599f 2022-10-20 stsp * a delta base is unknown. Try again later.
894 0136599f 2022-10-20 stsp obj->valid = 1;
896 0136599f 2022-10-20 stsp if (have_ref_deltas)
897 0136599f 2022-10-20 stsp update_packidx(&packidx, nobj, obj);
898 0136599f 2022-10-20 stsp /* Don't send too many progress privsep messages. */
899 0136599f 2022-10-20 stsp p_resolved = ((nresolved + n) * 100) / nobj;
900 0136599f 2022-10-20 stsp if (p_resolved != last_p_resolved) {
901 0136599f 2022-10-20 stsp err = report_progress(nobj, nobj,
902 713d6e11 2022-10-20 stsp nloose, nresolved + n, rl,
903 0136599f 2022-10-20 stsp progress_cb, progress_arg);
906 0136599f 2022-10-20 stsp last_p_resolved = p_resolved;
910 0136599f 2022-10-20 stsp if (pass++ > 3 && n == 0) {
911 0136599f 2022-10-20 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
912 0136599f 2022-10-20 stsp "could not resolve any of deltas; packfile could "
913 0136599f 2022-10-20 stsp "be corrupt");
916 0136599f 2022-10-20 stsp nresolved += n;
917 cf534ab8 2023-01-17 stsp nvalid += n;
920 0136599f 2022-10-20 stsp if (nloose + nresolved != nobj) {
921 0136599f 2022-10-20 stsp static char msg[64];
922 0136599f 2022-10-20 stsp snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
923 0136599f 2022-10-20 stsp nloose + nresolved, nobj);
924 0136599f 2022-10-20 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
928 0136599f 2022-10-20 stsp err = report_progress(nobj, nobj, nloose, nresolved, NULL,
929 0136599f 2022-10-20 stsp progress_cb, progress_arg);
933 0136599f 2022-10-20 stsp make_packidx(&packidx, nobj, objects);
935 0136599f 2022-10-20 stsp free(objects);
936 0136599f 2022-10-20 stsp objects = NULL;
938 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
939 0136599f 2022-10-20 stsp putbe32(buf, GOT_PACKIDX_V2_MAGIC);
940 0136599f 2022-10-20 stsp putbe32(buf + 4, GOT_PACKIDX_VERSION);
941 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, buf, 8, &ctx);
944 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
945 0136599f 2022-10-20 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
948 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
949 0136599f 2022-10-20 stsp nobj * SHA1_DIGEST_LENGTH, &ctx);
952 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
953 13b2bc37 2022-10-23 stsp nobj * sizeof(uint32_t), &ctx);
956 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
957 13b2bc37 2022-10-23 stsp nobj * sizeof(uint32_t), &ctx);
960 0136599f 2022-10-20 stsp if (packidx.nlargeobj > 0) {
961 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
962 0136599f 2022-10-20 stsp packidx.nlargeobj * sizeof(uint64_t), &ctx);
966 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
970 ae25a666 2023-02-23 op got_hash_final(&ctx, packidx_hash);
971 0136599f 2022-10-20 stsp w = write(idxfd, packidx_hash, sizeof(packidx_hash));
972 0136599f 2022-10-20 stsp if (w == -1) {
973 0136599f 2022-10-20 stsp err = got_error_from_errno("write");
976 0136599f 2022-10-20 stsp if (w != sizeof(packidx_hash)) {
977 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_IO);
981 0136599f 2022-10-20 stsp free(objects);
982 0136599f 2022-10-20 stsp free(packidx.hdr.magic);
983 0136599f 2022-10-20 stsp free(packidx.hdr.version);
984 0136599f 2022-10-20 stsp free(packidx.hdr.fanout_table);
985 0136599f 2022-10-20 stsp free(packidx.hdr.sorted_ids);
986 0136599f 2022-10-20 stsp free(packidx.hdr.offsets);
987 0136599f 2022-10-20 stsp free(packidx.hdr.large_offsets);
988 0136599f 2022-10-20 stsp return err;