2 142012ed 2022-10-20 thomas * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 142012ed 2022-10-20 thomas * Copyright (c) 2020, 2022 Stefan Sperling <stsp@openbsd.org>
5 142012ed 2022-10-20 thomas * Permission to use, copy, modify, and distribute this software for any
6 142012ed 2022-10-20 thomas * purpose with or without fee is hereby granted, provided that the above
7 142012ed 2022-10-20 thomas * copyright notice and this permission notice appear in all copies.
9 142012ed 2022-10-20 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 142012ed 2022-10-20 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 142012ed 2022-10-20 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 142012ed 2022-10-20 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 142012ed 2022-10-20 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 142012ed 2022-10-20 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 142012ed 2022-10-20 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
20 142012ed 2022-10-20 thomas #include <sys/queue.h>
21 142012ed 2022-10-20 thomas #include <sys/stat.h>
22 142012ed 2022-10-20 thomas #include <sys/time.h>
23 142012ed 2022-10-20 thomas #include <sys/types.h>
24 142012ed 2022-10-20 thomas #include <sys/uio.h>
25 142012ed 2022-10-20 thomas #include <sys/mman.h>
27 142012ed 2022-10-20 thomas #include <stdint.h>
28 142012ed 2022-10-20 thomas #include <errno.h>
29 142012ed 2022-10-20 thomas #include <imsg.h>
30 142012ed 2022-10-20 thomas #include <limits.h>
31 142012ed 2022-10-20 thomas #include <signal.h>
32 142012ed 2022-10-20 thomas #include <stdio.h>
33 142012ed 2022-10-20 thomas #include <stdlib.h>
34 142012ed 2022-10-20 thomas #include <string.h>
35 142012ed 2022-10-20 thomas #include <ctype.h>
36 142012ed 2022-10-20 thomas #include <fcntl.h>
37 142012ed 2022-10-20 thomas #include <unistd.h>
38 142012ed 2022-10-20 thomas #include <zlib.h>
39 142012ed 2022-10-20 thomas #include <err.h>
40 142012ed 2022-10-20 thomas #include <assert.h>
41 142012ed 2022-10-20 thomas #include <dirent.h>
43 142012ed 2022-10-20 thomas #include "got_error.h"
44 142012ed 2022-10-20 thomas #include "got_object.h"
46 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
47 142012ed 2022-10-20 thomas #include "got_lib_delta.h"
48 142012ed 2022-10-20 thomas #include "got_lib_inflate.h"
49 142012ed 2022-10-20 thomas #include "got_lib_object.h"
50 142012ed 2022-10-20 thomas #include "got_lib_object_parse.h"
51 142012ed 2022-10-20 thomas #include "got_lib_object_idset.h"
52 142012ed 2022-10-20 thomas #include "got_lib_privsep.h"
53 142012ed 2022-10-20 thomas #include "got_lib_pack.h"
54 142012ed 2022-10-20 thomas #include "got_lib_ratelimit.h"
55 142012ed 2022-10-20 thomas #include "got_lib_pack_index.h"
56 142012ed 2022-10-20 thomas #include "got_lib_delta_cache.h"
58 142012ed 2022-10-20 thomas struct got_indexed_object {
59 142012ed 2022-10-20 thomas struct got_object_id id;
62 142012ed 2022-10-20 thomas * Has this object been fully resolved?
63 142012ed 2022-10-20 thomas * If so, we know its ID, otherwise we don't and 'id' is invalid.
65 142012ed 2022-10-20 thomas int valid;
67 142012ed 2022-10-20 thomas /* Offset of type+size field for this object in pack file. */
68 142012ed 2022-10-20 thomas off_t off;
70 142012ed 2022-10-20 thomas /* Type+size values parsed from pack file. */
71 142012ed 2022-10-20 thomas uint8_t type;
72 142012ed 2022-10-20 thomas uint64_t size;
74 142012ed 2022-10-20 thomas /* Length of on-disk type+size data. */
75 142012ed 2022-10-20 thomas size_t tslen;
77 142012ed 2022-10-20 thomas /* Length of object data following type+size. */
78 142012ed 2022-10-20 thomas size_t len;
80 142012ed 2022-10-20 thomas uint32_t crc;
84 142012ed 2022-10-20 thomas /* For ref deltas. */
85 142012ed 2022-10-20 thomas struct got_object_id ref_id;
88 142012ed 2022-10-20 thomas /* For offset deltas. */
89 142012ed 2022-10-20 thomas off_t base_offset;
90 142012ed 2022-10-20 thomas size_t base_offsetlen;
95 142012ed 2022-10-20 thomas static void
96 142012ed 2022-10-20 thomas putbe32(char *b, uint32_t n)
98 142012ed 2022-10-20 thomas b[0] = n >> 24;
99 142012ed 2022-10-20 thomas b[1] = n >> 16;
100 142012ed 2022-10-20 thomas b[2] = n >> 8;
101 142012ed 2022-10-20 thomas b[3] = n >> 0;
104 142012ed 2022-10-20 thomas static const struct got_error *
105 b16893ba 2023-02-24 thomas read_checksum(uint32_t *crc, struct got_hash *ctx, int fd, size_t len)
107 142012ed 2022-10-20 thomas uint8_t buf[8192];
108 142012ed 2022-10-20 thomas size_t n;
109 142012ed 2022-10-20 thomas ssize_t r;
111 142012ed 2022-10-20 thomas for (n = len; n > 0; n -= r){
112 142012ed 2022-10-20 thomas r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
113 142012ed 2022-10-20 thomas if (r == -1)
114 142012ed 2022-10-20 thomas return got_error_from_errno("read");
115 142012ed 2022-10-20 thomas if (r == 0)
118 142012ed 2022-10-20 thomas *crc = crc32(*crc, buf, r);
120 b16893ba 2023-02-24 thomas got_hash_update(ctx, buf, r);
123 142012ed 2022-10-20 thomas return NULL;
126 142012ed 2022-10-20 thomas static const struct got_error *
127 b16893ba 2023-02-24 thomas read_file_digest(struct got_hash *ctx, FILE *f, size_t len)
129 142012ed 2022-10-20 thomas uint8_t buf[8192];
130 142012ed 2022-10-20 thomas size_t n, r;
132 142012ed 2022-10-20 thomas for (n = len; n > 0; n -= r) {
133 142012ed 2022-10-20 thomas r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
134 142012ed 2022-10-20 thomas if (r == 0) {
135 142012ed 2022-10-20 thomas if (feof(f))
136 142012ed 2022-10-20 thomas return NULL;
137 142012ed 2022-10-20 thomas return got_ferror(f, GOT_ERR_IO);
139 b16893ba 2023-02-24 thomas got_hash_update(ctx, buf, r);
142 142012ed 2022-10-20 thomas return NULL;
145 142012ed 2022-10-20 thomas static const struct got_error *
146 142012ed 2022-10-20 thomas read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
147 b16893ba 2023-02-24 thomas FILE *tmpfile, struct got_hash *pack_sha1_ctx)
149 142012ed 2022-10-20 thomas const struct got_error *err = NULL;
150 b16893ba 2023-02-24 thomas struct got_hash ctx;
151 142012ed 2022-10-20 thomas uint8_t *data = NULL;
152 142012ed 2022-10-20 thomas size_t datalen = 0;
153 142012ed 2022-10-20 thomas ssize_t n;
154 142012ed 2022-10-20 thomas char *header;
155 142012ed 2022-10-20 thomas size_t headerlen;
156 142012ed 2022-10-20 thomas const char *obj_label;
157 142012ed 2022-10-20 thomas size_t mapoff = obj->off;
158 142012ed 2022-10-20 thomas struct got_inflate_checksum csum;
160 142012ed 2022-10-20 thomas memset(&csum, 0, sizeof(csum));
161 b16893ba 2023-02-24 thomas csum.input_ctx = pack_sha1_ctx;
162 142012ed 2022-10-20 thomas csum.input_crc = &obj->crc;
164 142012ed 2022-10-20 thomas err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
165 142012ed 2022-10-20 thomas &obj->tslen, pack, obj->off);
167 142012ed 2022-10-20 thomas return err;
169 142012ed 2022-10-20 thomas if (pack->map) {
170 142012ed 2022-10-20 thomas obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
171 b16893ba 2023-02-24 thomas got_hash_update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
172 142012ed 2022-10-20 thomas mapoff += obj->tslen;
174 142012ed 2022-10-20 thomas /* XXX Seek back and get the CRC of on-disk type+size bytes. */
175 142012ed 2022-10-20 thomas if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
176 142012ed 2022-10-20 thomas return got_error_from_errno("lseek");
177 142012ed 2022-10-20 thomas err = read_checksum(&obj->crc, pack_sha1_ctx,
178 142012ed 2022-10-20 thomas pack->fd, obj->tslen);
180 142012ed 2022-10-20 thomas return err;
183 142012ed 2022-10-20 thomas switch (obj->type) {
184 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_BLOB:
185 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_COMMIT:
186 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_TREE:
187 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_TAG:
188 142012ed 2022-10-20 thomas if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
189 142012ed 2022-10-20 thomas if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
190 142012ed 2022-10-20 thomas err = got_error_from_errno("fseek");
193 142012ed 2022-10-20 thomas if (pack->map) {
194 142012ed 2022-10-20 thomas err = got_inflate_to_file_mmap(&datalen,
195 142012ed 2022-10-20 thomas &obj->len, &csum, pack->map, mapoff,
196 142012ed 2022-10-20 thomas pack->filesize - mapoff, tmpfile);
198 142012ed 2022-10-20 thomas err = got_inflate_to_file_fd(&datalen,
199 142012ed 2022-10-20 thomas &obj->len, &csum, pack->fd, tmpfile);
202 142012ed 2022-10-20 thomas if (pack->map) {
203 142012ed 2022-10-20 thomas err = got_inflate_to_mem_mmap(&data, &datalen,
204 142012ed 2022-10-20 thomas &obj->len, &csum, pack->map, mapoff,
205 142012ed 2022-10-20 thomas pack->filesize - mapoff);
207 142012ed 2022-10-20 thomas err = got_inflate_to_mem_fd(&data, &datalen,
208 142012ed 2022-10-20 thomas &obj->len, &csum, obj->size, pack->fd);
213 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
214 6d7eb4f7 2023-04-04 thomas err = got_object_type_label(&obj_label, obj->type);
215 142012ed 2022-10-20 thomas if (err) {
216 142012ed 2022-10-20 thomas free(data);
219 142012ed 2022-10-20 thomas if (asprintf(&header, "%s %lld", obj_label,
220 142012ed 2022-10-20 thomas (long long)obj->size) == -1) {
221 142012ed 2022-10-20 thomas err = got_error_from_errno("asprintf");
222 142012ed 2022-10-20 thomas free(data);
225 142012ed 2022-10-20 thomas headerlen = strlen(header) + 1;
226 b16893ba 2023-02-24 thomas got_hash_update(&ctx, header, headerlen);
227 142012ed 2022-10-20 thomas if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
228 b16893ba 2023-02-24 thomas err = read_file_digest(&ctx, tmpfile, datalen);
229 142012ed 2022-10-20 thomas if (err) {
230 142012ed 2022-10-20 thomas free(header);
231 142012ed 2022-10-20 thomas free(data);
235 b16893ba 2023-02-24 thomas got_hash_update(&ctx, data, datalen);
236 b16893ba 2023-02-24 thomas got_hash_final_object_id(&ctx, &obj->id);
237 142012ed 2022-10-20 thomas free(header);
238 142012ed 2022-10-20 thomas free(data);
240 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_REF_DELTA:
241 142012ed 2022-10-20 thomas memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
242 142012ed 2022-10-20 thomas if (pack->map) {
243 142012ed 2022-10-20 thomas if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
244 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_BAD_PACKFILE);
247 66e6097f 2022-10-27 thomas if (mapoff + SHA1_DIGEST_LENGTH > SIZE_MAX) {
248 66e6097f 2022-10-27 thomas err = got_error_fmt(GOT_ERR_RANGE,
249 66e6097f 2022-10-27 thomas "mapoff %lld would overflow size_t",
250 66e6097f 2022-10-27 thomas (long long)mapoff + SHA1_DIGEST_LENGTH);
253 142012ed 2022-10-20 thomas memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
254 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
255 142012ed 2022-10-20 thomas obj->crc = crc32(obj->crc, pack->map + mapoff,
256 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
257 b16893ba 2023-02-24 thomas got_hash_update(pack_sha1_ctx, pack->map + mapoff,
258 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
259 142012ed 2022-10-20 thomas mapoff += SHA1_DIGEST_LENGTH;
260 142012ed 2022-10-20 thomas err = got_inflate_to_mem_mmap(NULL, &datalen,
261 142012ed 2022-10-20 thomas &obj->len, &csum, pack->map, mapoff,
262 142012ed 2022-10-20 thomas pack->filesize - mapoff);
266 142012ed 2022-10-20 thomas n = read(pack->fd, obj->delta.ref.ref_id.sha1,
267 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
268 142012ed 2022-10-20 thomas if (n == -1) {
269 142012ed 2022-10-20 thomas err = got_error_from_errno("read");
272 142012ed 2022-10-20 thomas if (n < sizeof(obj->id)) {
273 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_BAD_PACKFILE);
276 142012ed 2022-10-20 thomas obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
277 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
278 b16893ba 2023-02-24 thomas got_hash_update(pack_sha1_ctx,
279 b16893ba 2023-02-24 thomas obj->delta.ref.ref_id.sha1, SHA1_DIGEST_LENGTH);
280 142012ed 2022-10-20 thomas err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
281 142012ed 2022-10-20 thomas &csum, obj->size, pack->fd);
285 142012ed 2022-10-20 thomas obj->len += SHA1_DIGEST_LENGTH;
287 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_OFFSET_DELTA:
288 142012ed 2022-10-20 thomas memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
289 142012ed 2022-10-20 thomas err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
290 142012ed 2022-10-20 thomas &obj->delta.ofs.base_offsetlen, pack, obj->off,
291 142012ed 2022-10-20 thomas obj->tslen);
295 142012ed 2022-10-20 thomas if (pack->map) {
296 6cf83e6a 2022-10-25 thomas if (mapoff + obj->delta.ofs.base_offsetlen >=
297 6cf83e6a 2022-10-25 thomas pack->filesize) {
298 6cf83e6a 2022-10-25 thomas err = got_error(GOT_ERR_BAD_PACKFILE);
302 66e6097f 2022-10-27 thomas if (mapoff + obj->delta.ofs.base_offsetlen >
303 66e6097f 2022-10-27 thomas SIZE_MAX) {
304 66e6097f 2022-10-27 thomas err = got_error_fmt(GOT_ERR_RANGE,
305 66e6097f 2022-10-27 thomas "mapoff %lld would overflow size_t",
306 66e6097f 2022-10-27 thomas (long long)mapoff
307 66e6097f 2022-10-27 thomas + obj->delta.ofs.base_offsetlen);
310 142012ed 2022-10-20 thomas obj->crc = crc32(obj->crc, pack->map + mapoff,
311 142012ed 2022-10-20 thomas obj->delta.ofs.base_offsetlen);
312 b16893ba 2023-02-24 thomas got_hash_update(pack_sha1_ctx, pack->map + mapoff,
313 142012ed 2022-10-20 thomas obj->delta.ofs.base_offsetlen);
314 142012ed 2022-10-20 thomas mapoff += obj->delta.ofs.base_offsetlen;
315 142012ed 2022-10-20 thomas err = got_inflate_to_mem_mmap(NULL, &datalen,
316 142012ed 2022-10-20 thomas &obj->len, &csum, pack->map, mapoff,
317 142012ed 2022-10-20 thomas pack->filesize - mapoff);
322 142012ed 2022-10-20 thomas * XXX Seek back and get CRC and SHA1 of on-disk
323 142012ed 2022-10-20 thomas * offset bytes.
325 142012ed 2022-10-20 thomas if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
327 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
330 142012ed 2022-10-20 thomas err = read_checksum(&obj->crc, pack_sha1_ctx,
331 142012ed 2022-10-20 thomas pack->fd, obj->delta.ofs.base_offsetlen);
335 142012ed 2022-10-20 thomas err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
336 142012ed 2022-10-20 thomas &csum, obj->size, pack->fd);
340 142012ed 2022-10-20 thomas obj->len += obj->delta.ofs.base_offsetlen;
343 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_OBJ_TYPE);
347 142012ed 2022-10-20 thomas return err;
350 3efd8e31 2022-10-23 thomas const struct got_error *
351 b16893ba 2023-02-24 thomas got_pack_hwrite(int fd, void *buf, int len, struct got_hash *ctx)
353 142012ed 2022-10-20 thomas ssize_t w;
355 b16893ba 2023-02-24 thomas got_hash_update(ctx, buf, len);
357 142012ed 2022-10-20 thomas w = write(fd, buf, len);
358 142012ed 2022-10-20 thomas if (w == -1)
359 142012ed 2022-10-20 thomas return got_error_from_errno("write");
360 142012ed 2022-10-20 thomas if (w != len)
361 142012ed 2022-10-20 thomas return got_error(GOT_ERR_IO);
363 142012ed 2022-10-20 thomas return NULL;
366 142012ed 2022-10-20 thomas static const struct got_error *
367 142012ed 2022-10-20 thomas resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
368 142012ed 2022-10-20 thomas struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
369 142012ed 2022-10-20 thomas FILE *delta_accum_file)
371 142012ed 2022-10-20 thomas const struct got_error *err = NULL;
372 142012ed 2022-10-20 thomas struct got_delta_chain deltas;
373 142012ed 2022-10-20 thomas struct got_delta *delta;
374 142012ed 2022-10-20 thomas uint8_t *buf = NULL;
375 142012ed 2022-10-20 thomas size_t len = 0;
376 b16893ba 2023-02-24 thomas struct got_hash ctx;
377 142012ed 2022-10-20 thomas char *header = NULL;
378 142012ed 2022-10-20 thomas size_t headerlen;
379 142012ed 2022-10-20 thomas uint64_t max_size;
380 142012ed 2022-10-20 thomas int base_obj_type;
381 142012ed 2022-10-20 thomas const char *obj_label;
383 142012ed 2022-10-20 thomas deltas.nentries = 0;
384 142012ed 2022-10-20 thomas STAILQ_INIT(&deltas.entries);
386 142012ed 2022-10-20 thomas err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
387 142012ed 2022-10-20 thomas obj->off, obj->tslen, obj->type, obj->size,
388 142012ed 2022-10-20 thomas GOT_DELTA_CHAIN_RECURSION_MAX);
390 142012ed 2022-10-20 thomas goto done;
392 142012ed 2022-10-20 thomas err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
394 142012ed 2022-10-20 thomas goto done;
395 142012ed 2022-10-20 thomas if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
396 142012ed 2022-10-20 thomas rewind(tmpfile);
397 142012ed 2022-10-20 thomas rewind(delta_base_file);
398 142012ed 2022-10-20 thomas rewind(delta_accum_file);
399 142012ed 2022-10-20 thomas err = got_pack_dump_delta_chain_to_file(&len, &deltas,
400 142012ed 2022-10-20 thomas pack, tmpfile, delta_base_file, delta_accum_file);
402 142012ed 2022-10-20 thomas goto done;
404 142012ed 2022-10-20 thomas err = got_pack_dump_delta_chain_to_mem(&buf, &len,
405 142012ed 2022-10-20 thomas &deltas, pack);
408 142012ed 2022-10-20 thomas goto done;
410 142012ed 2022-10-20 thomas err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
412 142012ed 2022-10-20 thomas goto done;
413 6d7eb4f7 2023-04-04 thomas err = got_object_type_label(&obj_label, base_obj_type);
415 142012ed 2022-10-20 thomas goto done;
416 142012ed 2022-10-20 thomas if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
417 142012ed 2022-10-20 thomas err = got_error_from_errno("asprintf");
418 142012ed 2022-10-20 thomas goto done;
420 142012ed 2022-10-20 thomas headerlen = strlen(header) + 1;
421 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
422 b16893ba 2023-02-24 thomas got_hash_update(&ctx, header, headerlen);
423 142012ed 2022-10-20 thomas if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
424 b16893ba 2023-02-24 thomas err = read_file_digest(&ctx, tmpfile, len);
426 142012ed 2022-10-20 thomas goto done;
428 b16893ba 2023-02-24 thomas got_hash_update(&ctx, buf, len);
429 b16893ba 2023-02-24 thomas got_hash_final_object_id(&ctx, &obj->id);
431 142012ed 2022-10-20 thomas free(buf);
432 142012ed 2022-10-20 thomas free(header);
433 142012ed 2022-10-20 thomas while (!STAILQ_EMPTY(&deltas.entries)) {
434 142012ed 2022-10-20 thomas delta = STAILQ_FIRST(&deltas.entries);
435 142012ed 2022-10-20 thomas STAILQ_REMOVE_HEAD(&deltas.entries, entry);
436 142012ed 2022-10-20 thomas free(delta);
438 142012ed 2022-10-20 thomas return err;
441 142012ed 2022-10-20 thomas /* Determine the slot in the pack index a given object ID should use. */
442 142012ed 2022-10-20 thomas static int
443 142012ed 2022-10-20 thomas find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
445 142012ed 2022-10-20 thomas u_int8_t id0 = sha1[0];
446 142012ed 2022-10-20 thomas uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
447 142012ed 2022-10-20 thomas int left = 0, right = nindexed - 1;
448 142012ed 2022-10-20 thomas int cmp = 0, i = 0;
450 142012ed 2022-10-20 thomas if (id0 > 0)
451 142012ed 2022-10-20 thomas left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
453 142012ed 2022-10-20 thomas while (left <= right) {
454 142012ed 2022-10-20 thomas struct got_packidx_object_id *oid;
456 142012ed 2022-10-20 thomas i = ((left + right) / 2);
457 142012ed 2022-10-20 thomas oid = &packidx->hdr.sorted_ids[i];
459 142012ed 2022-10-20 thomas cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
460 142012ed 2022-10-20 thomas if (cmp == 0)
461 142012ed 2022-10-20 thomas return -1; /* object already indexed */
462 142012ed 2022-10-20 thomas else if (cmp > 0)
463 142012ed 2022-10-20 thomas left = i + 1;
464 142012ed 2022-10-20 thomas else if (cmp < 0)
465 142012ed 2022-10-20 thomas right = i - 1;
468 142012ed 2022-10-20 thomas return left;
472 142012ed 2022-10-20 thomas static void
473 142012ed 2022-10-20 thomas print_packidx(struct got_packidx *packidx)
475 142012ed 2022-10-20 thomas uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
478 142012ed 2022-10-20 thomas fprintf(stderr, "object IDs:\n");
479 142012ed 2022-10-20 thomas for (i = 0; i < nindexed; i++) {
480 142012ed 2022-10-20 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
481 142012ed 2022-10-20 thomas got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
482 142012ed 2022-10-20 thomas hex, sizeof(hex));
483 142012ed 2022-10-20 thomas fprintf(stderr, "%s\n", hex);
485 142012ed 2022-10-20 thomas fprintf(stderr, "\n");
487 142012ed 2022-10-20 thomas fprintf(stderr, "object offsets:\n");
488 142012ed 2022-10-20 thomas for (i = 0; i < nindexed; i++) {
489 142012ed 2022-10-20 thomas uint32_t offset = be32toh(packidx->hdr.offsets[i]);
490 142012ed 2022-10-20 thomas if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
491 142012ed 2022-10-20 thomas int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
492 142012ed 2022-10-20 thomas fprintf(stderr, "%u -> %llu\n", offset,
493 142012ed 2022-10-20 thomas be64toh(packidx->hdr.large_offsets[j]));
495 142012ed 2022-10-20 thomas fprintf(stderr, "%u\n", offset);
497 142012ed 2022-10-20 thomas fprintf(stderr, "\n");
499 142012ed 2022-10-20 thomas fprintf(stderr, "fanout table:");
500 142012ed 2022-10-20 thomas for (i = 0; i <= 0xff; i++)
501 142012ed 2022-10-20 thomas fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
502 142012ed 2022-10-20 thomas fprintf(stderr, "\n");
506 142012ed 2022-10-20 thomas static void
507 142012ed 2022-10-20 thomas add_indexed_object(struct got_packidx *packidx, uint32_t idx,
508 142012ed 2022-10-20 thomas struct got_indexed_object *obj)
512 142012ed 2022-10-20 thomas memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
513 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
514 142012ed 2022-10-20 thomas packidx->hdr.crc32[idx] = htobe32(obj->crc);
515 142012ed 2022-10-20 thomas if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
516 142012ed 2022-10-20 thomas packidx->hdr.offsets[idx] = htobe32(obj->off);
518 142012ed 2022-10-20 thomas packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
519 142012ed 2022-10-20 thomas GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
520 142012ed 2022-10-20 thomas packidx->hdr.large_offsets[packidx->nlargeobj] =
521 142012ed 2022-10-20 thomas htobe64(obj->off);
522 142012ed 2022-10-20 thomas packidx->nlargeobj++;
525 142012ed 2022-10-20 thomas for (i = obj->id.sha1[0]; i <= 0xff; i++) {
526 142012ed 2022-10-20 thomas uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
527 142012ed 2022-10-20 thomas packidx->hdr.fanout_table[i] = htobe32(n + 1);
531 142012ed 2022-10-20 thomas static int
532 142012ed 2022-10-20 thomas indexed_obj_cmp(const void *pa, const void *pb)
534 142012ed 2022-10-20 thomas struct got_indexed_object *a, *b;
536 142012ed 2022-10-20 thomas a = (struct got_indexed_object *)pa;
537 142012ed 2022-10-20 thomas b = (struct got_indexed_object *)pb;
538 142012ed 2022-10-20 thomas return got_object_id_cmp(&a->id, &b->id);
541 142012ed 2022-10-20 thomas static void
542 eebe1fbb 2022-10-20 thomas make_packidx(struct got_packidx *packidx, uint32_t nobj,
543 142012ed 2022-10-20 thomas struct got_indexed_object *objects)
545 142012ed 2022-10-20 thomas struct got_indexed_object *obj;
547 142012ed 2022-10-20 thomas uint32_t idx = 0;
549 142012ed 2022-10-20 thomas qsort(objects, nobj, sizeof(struct got_indexed_object),
550 142012ed 2022-10-20 thomas indexed_obj_cmp);
552 142012ed 2022-10-20 thomas memset(packidx->hdr.fanout_table, 0,
553 142012ed 2022-10-20 thomas GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
554 142012ed 2022-10-20 thomas packidx->nlargeobj = 0;
556 142012ed 2022-10-20 thomas for (i = 0; i < nobj; i++) {
557 142012ed 2022-10-20 thomas obj = &objects[i];
558 142012ed 2022-10-20 thomas if (obj->valid)
559 142012ed 2022-10-20 thomas add_indexed_object(packidx, idx++, obj);
563 142012ed 2022-10-20 thomas static void
564 eebe1fbb 2022-10-20 thomas update_packidx(struct got_packidx *packidx, uint32_t nobj,
565 142012ed 2022-10-20 thomas struct got_indexed_object *obj)
568 142012ed 2022-10-20 thomas uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
570 142012ed 2022-10-20 thomas idx = find_object_idx(packidx, obj->id.sha1);
571 aac3e701 2023-02-03 thomas if (idx == -1)
572 142012ed 2022-10-20 thomas return; /* object already indexed */
574 142012ed 2022-10-20 thomas memmove(&packidx->hdr.sorted_ids[idx + 1],
575 142012ed 2022-10-20 thomas &packidx->hdr.sorted_ids[idx],
576 142012ed 2022-10-20 thomas sizeof(struct got_packidx_object_id) * (nindexed - idx));
577 142012ed 2022-10-20 thomas memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
578 142012ed 2022-10-20 thomas sizeof(uint32_t) * (nindexed - idx));
580 142012ed 2022-10-20 thomas add_indexed_object(packidx, idx, obj);
583 142012ed 2022-10-20 thomas static const struct got_error *
584 eebe1fbb 2022-10-20 thomas report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
585 eebe1fbb 2022-10-20 thomas uint32_t nobj_resolved, struct got_ratelimit *rl,
586 142012ed 2022-10-20 thomas got_pack_index_progress_cb progress_cb, void *progress_arg)
588 142012ed 2022-10-20 thomas const struct got_error *err;
589 142012ed 2022-10-20 thomas int elapsed = 0;
591 142012ed 2022-10-20 thomas if (rl) {
592 142012ed 2022-10-20 thomas err = got_ratelimit_check(&elapsed, rl);
593 142012ed 2022-10-20 thomas if (err || !elapsed)
594 142012ed 2022-10-20 thomas return err;
597 142012ed 2022-10-20 thomas return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
598 142012ed 2022-10-20 thomas nobj_resolved);
601 142012ed 2022-10-20 thomas const struct got_error *
602 142012ed 2022-10-20 thomas got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
603 142012ed 2022-10-20 thomas FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
604 aecd2225 2022-10-20 thomas got_pack_index_progress_cb progress_cb, void *progress_arg,
605 aecd2225 2022-10-20 thomas struct got_ratelimit *rl)
607 142012ed 2022-10-20 thomas const struct got_error *err;
608 142012ed 2022-10-20 thomas struct got_packfile_hdr hdr;
609 142012ed 2022-10-20 thomas struct got_packidx packidx;
610 142012ed 2022-10-20 thomas char buf[8];
611 142012ed 2022-10-20 thomas char pack_sha1[SHA1_DIGEST_LENGTH];
612 eebe1fbb 2022-10-20 thomas uint32_t nobj, nvalid, nloose, nresolved = 0, i;
613 142012ed 2022-10-20 thomas struct got_indexed_object *objects = NULL, *obj;
614 b16893ba 2023-02-24 thomas struct got_hash ctx;
615 142012ed 2022-10-20 thomas uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
616 142012ed 2022-10-20 thomas ssize_t r, w;
617 142012ed 2022-10-20 thomas int pass, have_ref_deltas = 0, first_delta_idx = -1;
618 142012ed 2022-10-20 thomas size_t mapoff = 0;
619 142012ed 2022-10-20 thomas int p_indexed = 0, last_p_indexed = -1;
620 142012ed 2022-10-20 thomas int p_resolved = 0, last_p_resolved = -1;
622 142012ed 2022-10-20 thomas /* Require that pack file header and SHA1 trailer are present. */
623 142012ed 2022-10-20 thomas if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
624 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
625 142012ed 2022-10-20 thomas "short pack file");
627 142012ed 2022-10-20 thomas if (pack->map) {
628 142012ed 2022-10-20 thomas memcpy(&hdr, pack->map, sizeof(hdr));
629 142012ed 2022-10-20 thomas mapoff += sizeof(hdr);
631 142012ed 2022-10-20 thomas r = read(pack->fd, &hdr, sizeof(hdr));
632 142012ed 2022-10-20 thomas if (r == -1)
633 142012ed 2022-10-20 thomas return got_error_from_errno("read");
634 142012ed 2022-10-20 thomas if (r < sizeof(hdr))
635 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
636 142012ed 2022-10-20 thomas "short pack file");
639 142012ed 2022-10-20 thomas if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
640 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
641 142012ed 2022-10-20 thomas "bad packfile signature");
642 142012ed 2022-10-20 thomas if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
643 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
644 142012ed 2022-10-20 thomas "bad packfile version");
645 142012ed 2022-10-20 thomas nobj = be32toh(hdr.nobjects);
646 142012ed 2022-10-20 thomas if (nobj == 0)
647 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
648 142012ed 2022-10-20 thomas "bad packfile with zero objects");
650 142012ed 2022-10-20 thomas /* We compute the SHA1 of pack file contents and verify later on. */
651 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
652 b16893ba 2023-02-24 thomas got_hash_update(&ctx, &hdr, sizeof(hdr));
655 142012ed 2022-10-20 thomas * Create an in-memory pack index which will grow as objects
656 142012ed 2022-10-20 thomas * IDs in the pack file are discovered. Only fields used to
657 142012ed 2022-10-20 thomas * read deltified objects will be needed by the pack.c library
658 142012ed 2022-10-20 thomas * code, so setting up just a pack index header is sufficient.
660 142012ed 2022-10-20 thomas memset(&packidx, 0, sizeof(packidx));
661 142012ed 2022-10-20 thomas packidx.hdr.magic = malloc(sizeof(uint32_t));
662 142012ed 2022-10-20 thomas if (packidx.hdr.magic == NULL)
663 142012ed 2022-10-20 thomas return got_error_from_errno("malloc");
664 142012ed 2022-10-20 thomas *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
665 142012ed 2022-10-20 thomas packidx.hdr.version = malloc(sizeof(uint32_t));
666 142012ed 2022-10-20 thomas if (packidx.hdr.version == NULL) {
667 142012ed 2022-10-20 thomas err = got_error_from_errno("malloc");
668 142012ed 2022-10-20 thomas goto done;
670 142012ed 2022-10-20 thomas *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
671 142012ed 2022-10-20 thomas packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
672 142012ed 2022-10-20 thomas sizeof(uint32_t));
673 142012ed 2022-10-20 thomas if (packidx.hdr.fanout_table == NULL) {
674 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
675 142012ed 2022-10-20 thomas goto done;
677 142012ed 2022-10-20 thomas packidx.hdr.sorted_ids = calloc(nobj,
678 142012ed 2022-10-20 thomas sizeof(struct got_packidx_object_id));
679 142012ed 2022-10-20 thomas if (packidx.hdr.sorted_ids == NULL) {
680 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
681 142012ed 2022-10-20 thomas goto done;
683 142012ed 2022-10-20 thomas packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
684 142012ed 2022-10-20 thomas if (packidx.hdr.crc32 == NULL) {
685 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
686 142012ed 2022-10-20 thomas goto done;
688 142012ed 2022-10-20 thomas packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
689 142012ed 2022-10-20 thomas if (packidx.hdr.offsets == NULL) {
690 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
691 142012ed 2022-10-20 thomas goto done;
693 142012ed 2022-10-20 thomas /* Large offsets table is empty for pack files < 2 GB. */
694 142012ed 2022-10-20 thomas if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
695 142012ed 2022-10-20 thomas packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
696 142012ed 2022-10-20 thomas if (packidx.hdr.large_offsets == NULL) {
697 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
698 142012ed 2022-10-20 thomas goto done;
702 142012ed 2022-10-20 thomas nvalid = 0;
703 142012ed 2022-10-20 thomas nloose = 0;
704 142012ed 2022-10-20 thomas objects = calloc(nobj, sizeof(struct got_indexed_object));
705 142012ed 2022-10-20 thomas if (objects == NULL)
706 142012ed 2022-10-20 thomas return got_error_from_errno("calloc");
709 142012ed 2022-10-20 thomas * First pass: locate all objects and identify un-deltified objects.
711 142012ed 2022-10-20 thomas * When this pass has completed we will know offset, type, size, and
712 142012ed 2022-10-20 thomas * CRC information for all objects in this pack file. We won't know
713 142012ed 2022-10-20 thomas * any of the actual object IDs of deltified objects yet since we
714 142012ed 2022-10-20 thomas * will not yet attempt to combine deltas.
716 142012ed 2022-10-20 thomas pass = 1;
717 142012ed 2022-10-20 thomas for (i = 0; i < nobj; i++) {
718 142012ed 2022-10-20 thomas /* Don't send too many progress privsep messages. */
719 142012ed 2022-10-20 thomas p_indexed = ((i + 1) * 100) / nobj;
720 142012ed 2022-10-20 thomas if (p_indexed != last_p_indexed) {
721 142012ed 2022-10-20 thomas err = report_progress(nobj, i + 1, nloose, 0,
722 aecd2225 2022-10-20 thomas rl, progress_cb, progress_arg);
724 142012ed 2022-10-20 thomas goto done;
725 142012ed 2022-10-20 thomas last_p_indexed = p_indexed;
728 142012ed 2022-10-20 thomas obj = &objects[i];
729 142012ed 2022-10-20 thomas obj->crc = crc32(0L, NULL, 0);
731 142012ed 2022-10-20 thomas /* Store offset to type+size information for this object. */
732 142012ed 2022-10-20 thomas if (pack->map) {
733 142012ed 2022-10-20 thomas obj->off = mapoff;
735 142012ed 2022-10-20 thomas obj->off = lseek(pack->fd, 0, SEEK_CUR);
736 142012ed 2022-10-20 thomas if (obj->off == -1) {
737 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
738 142012ed 2022-10-20 thomas goto done;
742 142012ed 2022-10-20 thomas err = read_packed_object(pack, obj, tmpfile, &ctx);
744 142012ed 2022-10-20 thomas goto done;
746 142012ed 2022-10-20 thomas if (pack->map) {
747 142012ed 2022-10-20 thomas mapoff += obj->tslen + obj->len;
749 142012ed 2022-10-20 thomas if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
750 142012ed 2022-10-20 thomas SEEK_SET) == -1) {
751 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
752 142012ed 2022-10-20 thomas goto done;
756 142012ed 2022-10-20 thomas if (obj->type == GOT_OBJ_TYPE_BLOB ||
757 142012ed 2022-10-20 thomas obj->type == GOT_OBJ_TYPE_TREE ||
758 142012ed 2022-10-20 thomas obj->type == GOT_OBJ_TYPE_COMMIT ||
759 142012ed 2022-10-20 thomas obj->type == GOT_OBJ_TYPE_TAG) {
760 142012ed 2022-10-20 thomas obj->valid = 1;
761 142012ed 2022-10-20 thomas nloose++;
763 142012ed 2022-10-20 thomas if (first_delta_idx == -1)
764 142012ed 2022-10-20 thomas first_delta_idx = i;
765 142012ed 2022-10-20 thomas if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
766 142012ed 2022-10-20 thomas have_ref_deltas = 1;
769 142012ed 2022-10-20 thomas nvalid = nloose;
772 142012ed 2022-10-20 thomas * Having done a full pass over the pack file and can now
773 142012ed 2022-10-20 thomas * verify its checksum.
775 b16893ba 2023-02-24 thomas got_hash_final(&ctx, pack_sha1);
777 142012ed 2022-10-20 thomas if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
778 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PACKFILE_CSUM);
779 142012ed 2022-10-20 thomas goto done;
782 142012ed 2022-10-20 thomas /* Verify the SHA1 checksum stored at the end of the pack file. */
783 142012ed 2022-10-20 thomas if (pack->map) {
784 66e6097f 2022-10-27 thomas if (pack->filesize > SIZE_MAX) {
785 66e6097f 2022-10-27 thomas err = got_error_fmt(GOT_ERR_RANGE,
786 66e6097f 2022-10-27 thomas "filesize %lld overflows size_t",
787 66e6097f 2022-10-27 thomas (long long)pack->filesize);
788 66e6097f 2022-10-27 thomas goto done;
791 142012ed 2022-10-20 thomas memcpy(pack_sha1_expected, pack->map +
792 142012ed 2022-10-20 thomas pack->filesize - SHA1_DIGEST_LENGTH,
793 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
795 142012ed 2022-10-20 thomas ssize_t n;
796 142012ed 2022-10-20 thomas if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
797 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
798 142012ed 2022-10-20 thomas goto done;
800 142012ed 2022-10-20 thomas n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
801 142012ed 2022-10-20 thomas if (n == -1) {
802 142012ed 2022-10-20 thomas err = got_error_from_errno("read");
803 142012ed 2022-10-20 thomas goto done;
805 142012ed 2022-10-20 thomas if (n != SHA1_DIGEST_LENGTH) {
806 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_IO);
807 142012ed 2022-10-20 thomas goto done;
810 142012ed 2022-10-20 thomas if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
811 142012ed 2022-10-20 thomas err = got_error_msg(GOT_ERR_BAD_PACKFILE,
812 142012ed 2022-10-20 thomas "bad checksum in pack file trailer");
813 142012ed 2022-10-20 thomas goto done;
816 142012ed 2022-10-20 thomas if (first_delta_idx == -1)
817 142012ed 2022-10-20 thomas first_delta_idx = 0;
819 142012ed 2022-10-20 thomas /* In order to resolve ref deltas we need an in-progress pack index. */
820 142012ed 2022-10-20 thomas if (have_ref_deltas)
821 142012ed 2022-10-20 thomas make_packidx(&packidx, nobj, objects);
824 142012ed 2022-10-20 thomas * Second pass: We can now resolve deltas to compute the IDs of
825 142012ed 2022-10-20 thomas * objects which appear in deltified form. Because deltas can be
826 142012ed 2022-10-20 thomas * chained this pass may require a couple of iterations until all
827 142012ed 2022-10-20 thomas * IDs of deltified objects have been discovered.
830 142012ed 2022-10-20 thomas while (nvalid != nobj) {
831 142012ed 2022-10-20 thomas int n = 0;
833 142012ed 2022-10-20 thomas * This loop will only run once unless the pack file
834 142012ed 2022-10-20 thomas * contains ref deltas which refer to objects located
835 142012ed 2022-10-20 thomas * later in the pack file, which is unusual.
836 142012ed 2022-10-20 thomas * Offset deltas can always be resolved in one pass
837 142012ed 2022-10-20 thomas * unless the packfile is corrupt.
839 142012ed 2022-10-20 thomas for (i = first_delta_idx; i < nobj; i++) {
840 142012ed 2022-10-20 thomas obj = &objects[i];
841 142012ed 2022-10-20 thomas if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
842 142012ed 2022-10-20 thomas obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
843 142012ed 2022-10-20 thomas continue;
845 142012ed 2022-10-20 thomas if (obj->valid)
846 142012ed 2022-10-20 thomas continue;
848 142012ed 2022-10-20 thomas if (pack->map == NULL && lseek(pack->fd,
849 142012ed 2022-10-20 thomas obj->off + obj->tslen, SEEK_SET) == -1) {
850 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
851 142012ed 2022-10-20 thomas goto done;
854 142012ed 2022-10-20 thomas err = resolve_deltified_object(pack, &packidx, obj,
855 142012ed 2022-10-20 thomas tmpfile, delta_base_file, delta_accum_file);
856 142012ed 2022-10-20 thomas if (err) {
857 142012ed 2022-10-20 thomas if (err->code != GOT_ERR_NO_OBJ)
858 142012ed 2022-10-20 thomas goto done;
860 142012ed 2022-10-20 thomas * We cannot resolve this object yet because
861 142012ed 2022-10-20 thomas * a delta base is unknown. Try again later.
863 142012ed 2022-10-20 thomas continue;
866 142012ed 2022-10-20 thomas obj->valid = 1;
868 142012ed 2022-10-20 thomas if (have_ref_deltas)
869 142012ed 2022-10-20 thomas update_packidx(&packidx, nobj, obj);
870 142012ed 2022-10-20 thomas /* Don't send too many progress privsep messages. */
871 142012ed 2022-10-20 thomas p_resolved = ((nresolved + n) * 100) / nobj;
872 142012ed 2022-10-20 thomas if (p_resolved != last_p_resolved) {
873 142012ed 2022-10-20 thomas err = report_progress(nobj, nobj,
874 aecd2225 2022-10-20 thomas nloose, nresolved + n, rl,
875 142012ed 2022-10-20 thomas progress_cb, progress_arg);
877 142012ed 2022-10-20 thomas goto done;
878 142012ed 2022-10-20 thomas last_p_resolved = p_resolved;
882 142012ed 2022-10-20 thomas if (pass++ > 3 && n == 0) {
883 142012ed 2022-10-20 thomas err = got_error_msg(GOT_ERR_BAD_PACKFILE,
884 142012ed 2022-10-20 thomas "could not resolve any of deltas; packfile could "
885 142012ed 2022-10-20 thomas "be corrupt");
886 142012ed 2022-10-20 thomas goto done;
888 142012ed 2022-10-20 thomas nresolved += n;
889 88056de1 2023-01-19 thomas nvalid += n;
892 142012ed 2022-10-20 thomas if (nloose + nresolved != nobj) {
893 142012ed 2022-10-20 thomas static char msg[64];
894 142012ed 2022-10-20 thomas snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
895 142012ed 2022-10-20 thomas nloose + nresolved, nobj);
896 142012ed 2022-10-20 thomas err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
897 142012ed 2022-10-20 thomas goto done;
900 142012ed 2022-10-20 thomas err = report_progress(nobj, nobj, nloose, nresolved, NULL,
901 142012ed 2022-10-20 thomas progress_cb, progress_arg);
903 142012ed 2022-10-20 thomas goto done;
905 142012ed 2022-10-20 thomas make_packidx(&packidx, nobj, objects);
907 142012ed 2022-10-20 thomas free(objects);
908 142012ed 2022-10-20 thomas objects = NULL;
910 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
911 142012ed 2022-10-20 thomas putbe32(buf, GOT_PACKIDX_V2_MAGIC);
912 142012ed 2022-10-20 thomas putbe32(buf + 4, GOT_PACKIDX_VERSION);
913 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, buf, 8, &ctx);
915 142012ed 2022-10-20 thomas goto done;
916 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
917 142012ed 2022-10-20 thomas GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
919 142012ed 2022-10-20 thomas goto done;
920 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
921 142012ed 2022-10-20 thomas nobj * SHA1_DIGEST_LENGTH, &ctx);
923 142012ed 2022-10-20 thomas goto done;
924 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
925 3efd8e31 2022-10-23 thomas nobj * sizeof(uint32_t), &ctx);
927 142012ed 2022-10-20 thomas goto done;
928 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
929 3efd8e31 2022-10-23 thomas nobj * sizeof(uint32_t), &ctx);
931 142012ed 2022-10-20 thomas goto done;
932 142012ed 2022-10-20 thomas if (packidx.nlargeobj > 0) {
933 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
934 142012ed 2022-10-20 thomas packidx.nlargeobj * sizeof(uint64_t), &ctx);
936 142012ed 2022-10-20 thomas goto done;
938 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
940 142012ed 2022-10-20 thomas goto done;
942 b16893ba 2023-02-24 thomas got_hash_final(&ctx, packidx_hash);
943 142012ed 2022-10-20 thomas w = write(idxfd, packidx_hash, sizeof(packidx_hash));
944 142012ed 2022-10-20 thomas if (w == -1) {
945 142012ed 2022-10-20 thomas err = got_error_from_errno("write");
946 142012ed 2022-10-20 thomas goto done;
948 142012ed 2022-10-20 thomas if (w != sizeof(packidx_hash)) {
949 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_IO);
950 142012ed 2022-10-20 thomas goto done;
953 142012ed 2022-10-20 thomas free(objects);
954 142012ed 2022-10-20 thomas free(packidx.hdr.magic);
955 142012ed 2022-10-20 thomas free(packidx.hdr.version);
956 142012ed 2022-10-20 thomas free(packidx.hdr.fanout_table);
957 142012ed 2022-10-20 thomas free(packidx.hdr.sorted_ids);
958 142012ed 2022-10-20 thomas free(packidx.hdr.offsets);
959 142012ed 2022-10-20 thomas free(packidx.hdr.large_offsets);
960 142012ed 2022-10-20 thomas return err;