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 0136599f 2022-10-20 stsp #include <endian.h>
36 0136599f 2022-10-20 stsp #include <fcntl.h>
37 0136599f 2022-10-20 stsp #include <unistd.h>
38 0136599f 2022-10-20 stsp #include <zlib.h>
39 0136599f 2022-10-20 stsp #include <err.h>
40 0136599f 2022-10-20 stsp #include <assert.h>
41 0136599f 2022-10-20 stsp #include <dirent.h>
43 0136599f 2022-10-20 stsp #include "got_error.h"
44 0136599f 2022-10-20 stsp #include "got_object.h"
46 0136599f 2022-10-20 stsp #include "got_lib_sha1.h"
47 0136599f 2022-10-20 stsp #include "got_lib_delta.h"
48 0136599f 2022-10-20 stsp #include "got_lib_inflate.h"
49 0136599f 2022-10-20 stsp #include "got_lib_object.h"
50 0136599f 2022-10-20 stsp #include "got_lib_object_parse.h"
51 0136599f 2022-10-20 stsp #include "got_lib_object_idset.h"
52 0136599f 2022-10-20 stsp #include "got_lib_privsep.h"
53 0136599f 2022-10-20 stsp #include "got_lib_pack.h"
54 0136599f 2022-10-20 stsp #include "got_lib_ratelimit.h"
55 0136599f 2022-10-20 stsp #include "got_lib_pack_index.h"
56 0136599f 2022-10-20 stsp #include "got_lib_delta_cache.h"
58 0136599f 2022-10-20 stsp struct got_indexed_object {
59 0136599f 2022-10-20 stsp struct got_object_id id;
62 0136599f 2022-10-20 stsp * Has this object been fully resolved?
63 0136599f 2022-10-20 stsp * If so, we know its ID, otherwise we don't and 'id' is invalid.
67 0136599f 2022-10-20 stsp /* Offset of type+size field for this object in pack file. */
70 0136599f 2022-10-20 stsp /* Type+size values parsed from pack file. */
71 0136599f 2022-10-20 stsp uint8_t type;
72 0136599f 2022-10-20 stsp uint64_t size;
74 0136599f 2022-10-20 stsp /* Length of on-disk type+size data. */
75 0136599f 2022-10-20 stsp size_t tslen;
77 0136599f 2022-10-20 stsp /* Length of object data following type+size. */
80 0136599f 2022-10-20 stsp uint32_t crc;
84 0136599f 2022-10-20 stsp /* For ref deltas. */
85 0136599f 2022-10-20 stsp struct got_object_id ref_id;
88 0136599f 2022-10-20 stsp /* For offset deltas. */
89 0136599f 2022-10-20 stsp off_t base_offset;
90 0136599f 2022-10-20 stsp size_t base_offsetlen;
96 0136599f 2022-10-20 stsp putbe32(char *b, uint32_t n)
98 0136599f 2022-10-20 stsp b[0] = n >> 24;
99 0136599f 2022-10-20 stsp b[1] = n >> 16;
100 0136599f 2022-10-20 stsp b[2] = n >> 8;
101 0136599f 2022-10-20 stsp b[3] = n >> 0;
104 0136599f 2022-10-20 stsp static const struct got_error *
105 0136599f 2022-10-20 stsp get_obj_type_label(const char **label, int obj_type)
107 0136599f 2022-10-20 stsp const struct got_error *err = NULL;
109 0136599f 2022-10-20 stsp switch (obj_type) {
110 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_BLOB:
111 0136599f 2022-10-20 stsp *label = GOT_OBJ_LABEL_BLOB;
113 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_TREE:
114 0136599f 2022-10-20 stsp *label = GOT_OBJ_LABEL_TREE;
116 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_COMMIT:
117 0136599f 2022-10-20 stsp *label = GOT_OBJ_LABEL_COMMIT;
119 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_TAG:
120 0136599f 2022-10-20 stsp *label = GOT_OBJ_LABEL_TAG;
123 0136599f 2022-10-20 stsp *label = NULL;
124 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_OBJ_TYPE);
128 0136599f 2022-10-20 stsp return err;
131 0136599f 2022-10-20 stsp static const struct got_error *
132 0136599f 2022-10-20 stsp read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
134 0136599f 2022-10-20 stsp uint8_t buf[8192];
138 0136599f 2022-10-20 stsp for (n = len; n > 0; n -= r){
139 0136599f 2022-10-20 stsp r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
140 0136599f 2022-10-20 stsp if (r == -1)
141 0136599f 2022-10-20 stsp return got_error_from_errno("read");
142 0136599f 2022-10-20 stsp if (r == 0)
145 0136599f 2022-10-20 stsp *crc = crc32(*crc, buf, r);
146 0136599f 2022-10-20 stsp if (sha1_ctx)
147 0136599f 2022-10-20 stsp SHA1Update(sha1_ctx, buf, r);
150 0136599f 2022-10-20 stsp return NULL;
153 0136599f 2022-10-20 stsp static const struct got_error *
154 0136599f 2022-10-20 stsp read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
156 0136599f 2022-10-20 stsp uint8_t buf[8192];
157 0136599f 2022-10-20 stsp size_t n, r;
159 0136599f 2022-10-20 stsp for (n = len; n > 0; n -= r) {
160 0136599f 2022-10-20 stsp r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
161 0136599f 2022-10-20 stsp if (r == 0) {
162 0136599f 2022-10-20 stsp if (feof(f))
163 0136599f 2022-10-20 stsp return NULL;
164 0136599f 2022-10-20 stsp return got_ferror(f, GOT_ERR_IO);
166 0136599f 2022-10-20 stsp SHA1Update(ctx, buf, r);
169 0136599f 2022-10-20 stsp return NULL;
172 0136599f 2022-10-20 stsp static const struct got_error *
173 0136599f 2022-10-20 stsp read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
174 0136599f 2022-10-20 stsp FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
176 0136599f 2022-10-20 stsp const struct got_error *err = NULL;
177 0136599f 2022-10-20 stsp SHA1_CTX ctx;
178 0136599f 2022-10-20 stsp uint8_t *data = NULL;
179 0136599f 2022-10-20 stsp size_t datalen = 0;
181 0136599f 2022-10-20 stsp char *header;
182 0136599f 2022-10-20 stsp size_t headerlen;
183 0136599f 2022-10-20 stsp const char *obj_label;
184 0136599f 2022-10-20 stsp size_t mapoff = obj->off;
185 0136599f 2022-10-20 stsp struct got_inflate_checksum csum;
187 0136599f 2022-10-20 stsp memset(&csum, 0, sizeof(csum));
188 0136599f 2022-10-20 stsp csum.input_sha1 = pack_sha1_ctx;
189 0136599f 2022-10-20 stsp csum.input_crc = &obj->crc;
191 0136599f 2022-10-20 stsp err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
192 0136599f 2022-10-20 stsp &obj->tslen, pack, obj->off);
194 0136599f 2022-10-20 stsp return err;
196 0136599f 2022-10-20 stsp if (pack->map) {
197 0136599f 2022-10-20 stsp obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
198 0136599f 2022-10-20 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
199 0136599f 2022-10-20 stsp mapoff += obj->tslen;
201 0136599f 2022-10-20 stsp /* XXX Seek back and get the CRC of on-disk type+size bytes. */
202 0136599f 2022-10-20 stsp if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
203 0136599f 2022-10-20 stsp return got_error_from_errno("lseek");
204 0136599f 2022-10-20 stsp err = read_checksum(&obj->crc, pack_sha1_ctx,
205 0136599f 2022-10-20 stsp pack->fd, obj->tslen);
207 0136599f 2022-10-20 stsp return err;
210 0136599f 2022-10-20 stsp switch (obj->type) {
211 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_BLOB:
212 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_COMMIT:
213 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_TREE:
214 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_TAG:
215 0136599f 2022-10-20 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
216 0136599f 2022-10-20 stsp if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
217 0136599f 2022-10-20 stsp err = got_error_from_errno("fseek");
220 0136599f 2022-10-20 stsp if (pack->map) {
221 0136599f 2022-10-20 stsp err = got_inflate_to_file_mmap(&datalen,
222 0136599f 2022-10-20 stsp &obj->len, &csum, pack->map, mapoff,
223 0136599f 2022-10-20 stsp pack->filesize - mapoff, tmpfile);
225 0136599f 2022-10-20 stsp err = got_inflate_to_file_fd(&datalen,
226 0136599f 2022-10-20 stsp &obj->len, &csum, pack->fd, tmpfile);
229 0136599f 2022-10-20 stsp if (pack->map) {
230 0136599f 2022-10-20 stsp err = got_inflate_to_mem_mmap(&data, &datalen,
231 0136599f 2022-10-20 stsp &obj->len, &csum, pack->map, mapoff,
232 0136599f 2022-10-20 stsp pack->filesize - mapoff);
234 0136599f 2022-10-20 stsp err = got_inflate_to_mem_fd(&data, &datalen,
235 0136599f 2022-10-20 stsp &obj->len, &csum, obj->size, pack->fd);
240 0136599f 2022-10-20 stsp SHA1Init(&ctx);
241 0136599f 2022-10-20 stsp err = get_obj_type_label(&obj_label, obj->type);
243 0136599f 2022-10-20 stsp free(data);
246 0136599f 2022-10-20 stsp if (asprintf(&header, "%s %lld", obj_label,
247 0136599f 2022-10-20 stsp (long long)obj->size) == -1) {
248 0136599f 2022-10-20 stsp err = got_error_from_errno("asprintf");
249 0136599f 2022-10-20 stsp free(data);
252 0136599f 2022-10-20 stsp headerlen = strlen(header) + 1;
253 0136599f 2022-10-20 stsp SHA1Update(&ctx, header, headerlen);
254 0136599f 2022-10-20 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
255 0136599f 2022-10-20 stsp err = read_file_sha1(&ctx, tmpfile, datalen);
257 0136599f 2022-10-20 stsp free(header);
258 0136599f 2022-10-20 stsp free(data);
262 0136599f 2022-10-20 stsp SHA1Update(&ctx, data, datalen);
263 0136599f 2022-10-20 stsp SHA1Final(obj->id.sha1, &ctx);
264 0136599f 2022-10-20 stsp free(header);
265 0136599f 2022-10-20 stsp free(data);
267 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_REF_DELTA:
268 0136599f 2022-10-20 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
269 0136599f 2022-10-20 stsp if (pack->map) {
270 0136599f 2022-10-20 stsp if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
271 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
274 0136599f 2022-10-20 stsp memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
275 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
276 0136599f 2022-10-20 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
277 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
278 0136599f 2022-10-20 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff,
279 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
280 0136599f 2022-10-20 stsp mapoff += SHA1_DIGEST_LENGTH;
281 0136599f 2022-10-20 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
282 0136599f 2022-10-20 stsp &obj->len, &csum, pack->map, mapoff,
283 0136599f 2022-10-20 stsp pack->filesize - mapoff);
287 0136599f 2022-10-20 stsp n = read(pack->fd, obj->delta.ref.ref_id.sha1,
288 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
289 0136599f 2022-10-20 stsp if (n == -1) {
290 0136599f 2022-10-20 stsp err = got_error_from_errno("read");
293 0136599f 2022-10-20 stsp if (n < sizeof(obj->id)) {
294 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
297 0136599f 2022-10-20 stsp obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
298 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
299 0136599f 2022-10-20 stsp SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
300 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
301 0136599f 2022-10-20 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
302 0136599f 2022-10-20 stsp &csum, obj->size, pack->fd);
306 0136599f 2022-10-20 stsp obj->len += SHA1_DIGEST_LENGTH;
308 0136599f 2022-10-20 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
309 0136599f 2022-10-20 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
310 0136599f 2022-10-20 stsp err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
311 0136599f 2022-10-20 stsp &obj->delta.ofs.base_offsetlen, pack, obj->off,
312 0136599f 2022-10-20 stsp obj->tslen);
316 0136599f 2022-10-20 stsp if (pack->map) {
317 5326bd73 2022-10-24 op if (mapoff + obj->delta.ofs.base_offsetlen >=
318 5326bd73 2022-10-24 op pack->filesize) {
319 5326bd73 2022-10-24 op err = got_error(GOT_ERR_BAD_PACKFILE);
323 0136599f 2022-10-20 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
324 0136599f 2022-10-20 stsp obj->delta.ofs.base_offsetlen);
325 0136599f 2022-10-20 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff,
326 0136599f 2022-10-20 stsp obj->delta.ofs.base_offsetlen);
327 0136599f 2022-10-20 stsp mapoff += obj->delta.ofs.base_offsetlen;
328 0136599f 2022-10-20 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
329 0136599f 2022-10-20 stsp &obj->len, &csum, pack->map, mapoff,
330 0136599f 2022-10-20 stsp pack->filesize - mapoff);
335 0136599f 2022-10-20 stsp * XXX Seek back and get CRC and SHA1 of on-disk
336 0136599f 2022-10-20 stsp * offset bytes.
338 0136599f 2022-10-20 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
340 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
343 0136599f 2022-10-20 stsp err = read_checksum(&obj->crc, pack_sha1_ctx,
344 0136599f 2022-10-20 stsp pack->fd, obj->delta.ofs.base_offsetlen);
348 0136599f 2022-10-20 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
349 0136599f 2022-10-20 stsp &csum, obj->size, pack->fd);
353 0136599f 2022-10-20 stsp obj->len += obj->delta.ofs.base_offsetlen;
356 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_OBJ_TYPE);
360 0136599f 2022-10-20 stsp return err;
363 13b2bc37 2022-10-23 stsp const struct got_error *
364 13b2bc37 2022-10-23 stsp got_pack_hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
368 0136599f 2022-10-20 stsp SHA1Update(ctx, buf, len);
370 0136599f 2022-10-20 stsp w = write(fd, buf, len);
371 0136599f 2022-10-20 stsp if (w == -1)
372 0136599f 2022-10-20 stsp return got_error_from_errno("write");
373 0136599f 2022-10-20 stsp if (w != len)
374 0136599f 2022-10-20 stsp return got_error(GOT_ERR_IO);
376 0136599f 2022-10-20 stsp return NULL;
379 0136599f 2022-10-20 stsp static const struct got_error *
380 0136599f 2022-10-20 stsp resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
381 0136599f 2022-10-20 stsp struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
382 0136599f 2022-10-20 stsp FILE *delta_accum_file)
384 0136599f 2022-10-20 stsp const struct got_error *err = NULL;
385 0136599f 2022-10-20 stsp struct got_delta_chain deltas;
386 0136599f 2022-10-20 stsp struct got_delta *delta;
387 0136599f 2022-10-20 stsp uint8_t *buf = NULL;
388 0136599f 2022-10-20 stsp size_t len = 0;
389 0136599f 2022-10-20 stsp SHA1_CTX ctx;
390 0136599f 2022-10-20 stsp char *header = NULL;
391 0136599f 2022-10-20 stsp size_t headerlen;
392 0136599f 2022-10-20 stsp uint64_t max_size;
393 0136599f 2022-10-20 stsp int base_obj_type;
394 0136599f 2022-10-20 stsp const char *obj_label;
396 0136599f 2022-10-20 stsp deltas.nentries = 0;
397 0136599f 2022-10-20 stsp STAILQ_INIT(&deltas.entries);
399 0136599f 2022-10-20 stsp err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
400 0136599f 2022-10-20 stsp obj->off, obj->tslen, obj->type, obj->size,
401 0136599f 2022-10-20 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
405 0136599f 2022-10-20 stsp err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
408 0136599f 2022-10-20 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
409 0136599f 2022-10-20 stsp rewind(tmpfile);
410 0136599f 2022-10-20 stsp rewind(delta_base_file);
411 0136599f 2022-10-20 stsp rewind(delta_accum_file);
412 0136599f 2022-10-20 stsp err = got_pack_dump_delta_chain_to_file(&len, &deltas,
413 0136599f 2022-10-20 stsp pack, tmpfile, delta_base_file, delta_accum_file);
417 0136599f 2022-10-20 stsp err = got_pack_dump_delta_chain_to_mem(&buf, &len,
418 0136599f 2022-10-20 stsp &deltas, pack);
423 0136599f 2022-10-20 stsp err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
426 0136599f 2022-10-20 stsp err = get_obj_type_label(&obj_label, base_obj_type);
429 0136599f 2022-10-20 stsp if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
430 0136599f 2022-10-20 stsp err = got_error_from_errno("asprintf");
433 0136599f 2022-10-20 stsp headerlen = strlen(header) + 1;
434 0136599f 2022-10-20 stsp SHA1Init(&ctx);
435 0136599f 2022-10-20 stsp SHA1Update(&ctx, header, headerlen);
436 0136599f 2022-10-20 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
437 0136599f 2022-10-20 stsp err = read_file_sha1(&ctx, tmpfile, len);
441 0136599f 2022-10-20 stsp SHA1Update(&ctx, buf, len);
442 0136599f 2022-10-20 stsp SHA1Final(obj->id.sha1, &ctx);
445 0136599f 2022-10-20 stsp free(header);
446 0136599f 2022-10-20 stsp while (!STAILQ_EMPTY(&deltas.entries)) {
447 0136599f 2022-10-20 stsp delta = STAILQ_FIRST(&deltas.entries);
448 0136599f 2022-10-20 stsp STAILQ_REMOVE_HEAD(&deltas.entries, entry);
449 0136599f 2022-10-20 stsp free(delta);
451 0136599f 2022-10-20 stsp return err;
454 0136599f 2022-10-20 stsp /* Determine the slot in the pack index a given object ID should use. */
456 0136599f 2022-10-20 stsp find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
458 0136599f 2022-10-20 stsp u_int8_t id0 = sha1[0];
459 0136599f 2022-10-20 stsp uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
460 0136599f 2022-10-20 stsp int left = 0, right = nindexed - 1;
461 0136599f 2022-10-20 stsp int cmp = 0, i = 0;
463 0136599f 2022-10-20 stsp if (id0 > 0)
464 0136599f 2022-10-20 stsp left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
466 0136599f 2022-10-20 stsp while (left <= right) {
467 0136599f 2022-10-20 stsp struct got_packidx_object_id *oid;
469 0136599f 2022-10-20 stsp i = ((left + right) / 2);
470 0136599f 2022-10-20 stsp oid = &packidx->hdr.sorted_ids[i];
472 0136599f 2022-10-20 stsp cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
473 0136599f 2022-10-20 stsp if (cmp == 0)
474 0136599f 2022-10-20 stsp return -1; /* object already indexed */
475 0136599f 2022-10-20 stsp else if (cmp > 0)
476 0136599f 2022-10-20 stsp left = i + 1;
477 0136599f 2022-10-20 stsp else if (cmp < 0)
478 0136599f 2022-10-20 stsp right = i - 1;
481 0136599f 2022-10-20 stsp return left;
485 0136599f 2022-10-20 stsp static void
486 0136599f 2022-10-20 stsp print_packidx(struct got_packidx *packidx)
488 0136599f 2022-10-20 stsp uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
491 0136599f 2022-10-20 stsp fprintf(stderr, "object IDs:\n");
492 0136599f 2022-10-20 stsp for (i = 0; i < nindexed; i++) {
493 0136599f 2022-10-20 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
494 0136599f 2022-10-20 stsp got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
495 0136599f 2022-10-20 stsp hex, sizeof(hex));
496 0136599f 2022-10-20 stsp fprintf(stderr, "%s\n", hex);
498 0136599f 2022-10-20 stsp fprintf(stderr, "\n");
500 0136599f 2022-10-20 stsp fprintf(stderr, "object offsets:\n");
501 0136599f 2022-10-20 stsp for (i = 0; i < nindexed; i++) {
502 0136599f 2022-10-20 stsp uint32_t offset = be32toh(packidx->hdr.offsets[i]);
503 0136599f 2022-10-20 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
504 0136599f 2022-10-20 stsp int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
505 0136599f 2022-10-20 stsp fprintf(stderr, "%u -> %llu\n", offset,
506 0136599f 2022-10-20 stsp be64toh(packidx->hdr.large_offsets[j]));
508 0136599f 2022-10-20 stsp fprintf(stderr, "%u\n", offset);
510 0136599f 2022-10-20 stsp fprintf(stderr, "\n");
512 0136599f 2022-10-20 stsp fprintf(stderr, "fanout table:");
513 0136599f 2022-10-20 stsp for (i = 0; i <= 0xff; i++)
514 0136599f 2022-10-20 stsp fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
515 0136599f 2022-10-20 stsp fprintf(stderr, "\n");
519 0136599f 2022-10-20 stsp static void
520 0136599f 2022-10-20 stsp add_indexed_object(struct got_packidx *packidx, uint32_t idx,
521 0136599f 2022-10-20 stsp struct got_indexed_object *obj)
525 0136599f 2022-10-20 stsp memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
526 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
527 0136599f 2022-10-20 stsp packidx->hdr.crc32[idx] = htobe32(obj->crc);
528 0136599f 2022-10-20 stsp if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
529 0136599f 2022-10-20 stsp packidx->hdr.offsets[idx] = htobe32(obj->off);
531 0136599f 2022-10-20 stsp packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
532 0136599f 2022-10-20 stsp GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
533 0136599f 2022-10-20 stsp packidx->hdr.large_offsets[packidx->nlargeobj] =
534 0136599f 2022-10-20 stsp htobe64(obj->off);
535 0136599f 2022-10-20 stsp packidx->nlargeobj++;
538 0136599f 2022-10-20 stsp for (i = obj->id.sha1[0]; i <= 0xff; i++) {
539 0136599f 2022-10-20 stsp uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
540 0136599f 2022-10-20 stsp packidx->hdr.fanout_table[i] = htobe32(n + 1);
545 0136599f 2022-10-20 stsp indexed_obj_cmp(const void *pa, const void *pb)
547 0136599f 2022-10-20 stsp struct got_indexed_object *a, *b;
549 0136599f 2022-10-20 stsp a = (struct got_indexed_object *)pa;
550 0136599f 2022-10-20 stsp b = (struct got_indexed_object *)pb;
551 0136599f 2022-10-20 stsp return got_object_id_cmp(&a->id, &b->id);
554 0136599f 2022-10-20 stsp static void
555 9316cc27 2022-10-20 stsp make_packidx(struct got_packidx *packidx, uint32_t nobj,
556 0136599f 2022-10-20 stsp struct got_indexed_object *objects)
558 0136599f 2022-10-20 stsp struct got_indexed_object *obj;
560 0136599f 2022-10-20 stsp uint32_t idx = 0;
562 0136599f 2022-10-20 stsp qsort(objects, nobj, sizeof(struct got_indexed_object),
563 0136599f 2022-10-20 stsp indexed_obj_cmp);
565 0136599f 2022-10-20 stsp memset(packidx->hdr.fanout_table, 0,
566 0136599f 2022-10-20 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
567 0136599f 2022-10-20 stsp packidx->nlargeobj = 0;
569 0136599f 2022-10-20 stsp for (i = 0; i < nobj; i++) {
570 0136599f 2022-10-20 stsp obj = &objects[i];
571 0136599f 2022-10-20 stsp if (obj->valid)
572 0136599f 2022-10-20 stsp add_indexed_object(packidx, idx++, obj);
576 0136599f 2022-10-20 stsp static void
577 9316cc27 2022-10-20 stsp update_packidx(struct got_packidx *packidx, uint32_t nobj,
578 0136599f 2022-10-20 stsp struct got_indexed_object *obj)
581 0136599f 2022-10-20 stsp uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
583 0136599f 2022-10-20 stsp idx = find_object_idx(packidx, obj->id.sha1);
584 0136599f 2022-10-20 stsp if (idx == -1) {
585 0136599f 2022-10-20 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
586 0136599f 2022-10-20 stsp got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
587 0136599f 2022-10-20 stsp return; /* object already indexed */
590 0136599f 2022-10-20 stsp memmove(&packidx->hdr.sorted_ids[idx + 1],
591 0136599f 2022-10-20 stsp &packidx->hdr.sorted_ids[idx],
592 0136599f 2022-10-20 stsp sizeof(struct got_packidx_object_id) * (nindexed - idx));
593 0136599f 2022-10-20 stsp memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
594 0136599f 2022-10-20 stsp sizeof(uint32_t) * (nindexed - idx));
596 0136599f 2022-10-20 stsp add_indexed_object(packidx, idx, obj);
599 0136599f 2022-10-20 stsp static const struct got_error *
600 9316cc27 2022-10-20 stsp report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
601 9316cc27 2022-10-20 stsp uint32_t nobj_resolved, struct got_ratelimit *rl,
602 0136599f 2022-10-20 stsp got_pack_index_progress_cb progress_cb, void *progress_arg)
604 0136599f 2022-10-20 stsp const struct got_error *err;
605 0136599f 2022-10-20 stsp int elapsed = 0;
608 0136599f 2022-10-20 stsp err = got_ratelimit_check(&elapsed, rl);
609 0136599f 2022-10-20 stsp if (err || !elapsed)
610 0136599f 2022-10-20 stsp return err;
613 0136599f 2022-10-20 stsp return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
614 0136599f 2022-10-20 stsp nobj_resolved);
617 0136599f 2022-10-20 stsp const struct got_error *
618 0136599f 2022-10-20 stsp got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
619 0136599f 2022-10-20 stsp FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
620 713d6e11 2022-10-20 stsp got_pack_index_progress_cb progress_cb, void *progress_arg,
621 713d6e11 2022-10-20 stsp struct got_ratelimit *rl)
623 0136599f 2022-10-20 stsp const struct got_error *err;
624 0136599f 2022-10-20 stsp struct got_packfile_hdr hdr;
625 0136599f 2022-10-20 stsp struct got_packidx packidx;
626 0136599f 2022-10-20 stsp char buf[8];
627 0136599f 2022-10-20 stsp char pack_sha1[SHA1_DIGEST_LENGTH];
628 9316cc27 2022-10-20 stsp uint32_t nobj, nvalid, nloose, nresolved = 0, i;
629 0136599f 2022-10-20 stsp struct got_indexed_object *objects = NULL, *obj;
630 0136599f 2022-10-20 stsp SHA1_CTX ctx;
631 0136599f 2022-10-20 stsp uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
632 0136599f 2022-10-20 stsp ssize_t r, w;
633 0136599f 2022-10-20 stsp int pass, have_ref_deltas = 0, first_delta_idx = -1;
634 0136599f 2022-10-20 stsp size_t mapoff = 0;
635 0136599f 2022-10-20 stsp int p_indexed = 0, last_p_indexed = -1;
636 0136599f 2022-10-20 stsp int p_resolved = 0, last_p_resolved = -1;
638 0136599f 2022-10-20 stsp /* Require that pack file header and SHA1 trailer are present. */
639 0136599f 2022-10-20 stsp if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
640 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
641 0136599f 2022-10-20 stsp "short pack file");
643 0136599f 2022-10-20 stsp if (pack->map) {
644 0136599f 2022-10-20 stsp memcpy(&hdr, pack->map, sizeof(hdr));
645 0136599f 2022-10-20 stsp mapoff += sizeof(hdr);
647 0136599f 2022-10-20 stsp r = read(pack->fd, &hdr, sizeof(hdr));
648 0136599f 2022-10-20 stsp if (r == -1)
649 0136599f 2022-10-20 stsp return got_error_from_errno("read");
650 0136599f 2022-10-20 stsp if (r < sizeof(hdr))
651 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
652 0136599f 2022-10-20 stsp "short pack file");
655 0136599f 2022-10-20 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
656 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
657 0136599f 2022-10-20 stsp "bad packfile signature");
658 0136599f 2022-10-20 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
659 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
660 0136599f 2022-10-20 stsp "bad packfile version");
661 0136599f 2022-10-20 stsp nobj = be32toh(hdr.nobjects);
662 0136599f 2022-10-20 stsp if (nobj == 0)
663 0136599f 2022-10-20 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
664 0136599f 2022-10-20 stsp "bad packfile with zero objects");
666 0136599f 2022-10-20 stsp /* We compute the SHA1 of pack file contents and verify later on. */
667 0136599f 2022-10-20 stsp SHA1Init(&ctx);
668 0136599f 2022-10-20 stsp SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
671 0136599f 2022-10-20 stsp * Create an in-memory pack index which will grow as objects
672 0136599f 2022-10-20 stsp * IDs in the pack file are discovered. Only fields used to
673 0136599f 2022-10-20 stsp * read deltified objects will be needed by the pack.c library
674 0136599f 2022-10-20 stsp * code, so setting up just a pack index header is sufficient.
676 0136599f 2022-10-20 stsp memset(&packidx, 0, sizeof(packidx));
677 0136599f 2022-10-20 stsp packidx.hdr.magic = malloc(sizeof(uint32_t));
678 0136599f 2022-10-20 stsp if (packidx.hdr.magic == NULL)
679 0136599f 2022-10-20 stsp return got_error_from_errno("malloc");
680 0136599f 2022-10-20 stsp *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
681 0136599f 2022-10-20 stsp packidx.hdr.version = malloc(sizeof(uint32_t));
682 0136599f 2022-10-20 stsp if (packidx.hdr.version == NULL) {
683 0136599f 2022-10-20 stsp err = got_error_from_errno("malloc");
686 0136599f 2022-10-20 stsp *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
687 0136599f 2022-10-20 stsp packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
688 0136599f 2022-10-20 stsp sizeof(uint32_t));
689 0136599f 2022-10-20 stsp if (packidx.hdr.fanout_table == NULL) {
690 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
693 0136599f 2022-10-20 stsp packidx.hdr.sorted_ids = calloc(nobj,
694 0136599f 2022-10-20 stsp sizeof(struct got_packidx_object_id));
695 0136599f 2022-10-20 stsp if (packidx.hdr.sorted_ids == NULL) {
696 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
699 0136599f 2022-10-20 stsp packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
700 0136599f 2022-10-20 stsp if (packidx.hdr.crc32 == NULL) {
701 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
704 0136599f 2022-10-20 stsp packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
705 0136599f 2022-10-20 stsp if (packidx.hdr.offsets == NULL) {
706 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
709 0136599f 2022-10-20 stsp /* Large offsets table is empty for pack files < 2 GB. */
710 0136599f 2022-10-20 stsp if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
711 0136599f 2022-10-20 stsp packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
712 0136599f 2022-10-20 stsp if (packidx.hdr.large_offsets == NULL) {
713 0136599f 2022-10-20 stsp err = got_error_from_errno("calloc");
718 0136599f 2022-10-20 stsp nvalid = 0;
719 0136599f 2022-10-20 stsp nloose = 0;
720 0136599f 2022-10-20 stsp objects = calloc(nobj, sizeof(struct got_indexed_object));
721 0136599f 2022-10-20 stsp if (objects == NULL)
722 0136599f 2022-10-20 stsp return got_error_from_errno("calloc");
725 0136599f 2022-10-20 stsp * First pass: locate all objects and identify un-deltified objects.
727 0136599f 2022-10-20 stsp * When this pass has completed we will know offset, type, size, and
728 0136599f 2022-10-20 stsp * CRC information for all objects in this pack file. We won't know
729 0136599f 2022-10-20 stsp * any of the actual object IDs of deltified objects yet since we
730 0136599f 2022-10-20 stsp * will not yet attempt to combine deltas.
733 0136599f 2022-10-20 stsp for (i = 0; i < nobj; i++) {
734 0136599f 2022-10-20 stsp /* Don't send too many progress privsep messages. */
735 0136599f 2022-10-20 stsp p_indexed = ((i + 1) * 100) / nobj;
736 0136599f 2022-10-20 stsp if (p_indexed != last_p_indexed) {
737 0136599f 2022-10-20 stsp err = report_progress(nobj, i + 1, nloose, 0,
738 713d6e11 2022-10-20 stsp rl, progress_cb, progress_arg);
741 0136599f 2022-10-20 stsp last_p_indexed = p_indexed;
744 0136599f 2022-10-20 stsp obj = &objects[i];
745 0136599f 2022-10-20 stsp obj->crc = crc32(0L, NULL, 0);
747 0136599f 2022-10-20 stsp /* Store offset to type+size information for this object. */
748 0136599f 2022-10-20 stsp if (pack->map) {
749 0136599f 2022-10-20 stsp obj->off = mapoff;
751 0136599f 2022-10-20 stsp obj->off = lseek(pack->fd, 0, SEEK_CUR);
752 0136599f 2022-10-20 stsp if (obj->off == -1) {
753 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
758 0136599f 2022-10-20 stsp err = read_packed_object(pack, obj, tmpfile, &ctx);
762 0136599f 2022-10-20 stsp if (pack->map) {
763 0136599f 2022-10-20 stsp mapoff += obj->tslen + obj->len;
765 0136599f 2022-10-20 stsp if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
766 0136599f 2022-10-20 stsp SEEK_SET) == -1) {
767 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
772 0136599f 2022-10-20 stsp if (obj->type == GOT_OBJ_TYPE_BLOB ||
773 0136599f 2022-10-20 stsp obj->type == GOT_OBJ_TYPE_TREE ||
774 0136599f 2022-10-20 stsp obj->type == GOT_OBJ_TYPE_COMMIT ||
775 0136599f 2022-10-20 stsp obj->type == GOT_OBJ_TYPE_TAG) {
776 0136599f 2022-10-20 stsp obj->valid = 1;
779 0136599f 2022-10-20 stsp if (first_delta_idx == -1)
780 0136599f 2022-10-20 stsp first_delta_idx = i;
781 0136599f 2022-10-20 stsp if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
782 0136599f 2022-10-20 stsp have_ref_deltas = 1;
785 0136599f 2022-10-20 stsp nvalid = nloose;
788 0136599f 2022-10-20 stsp * Having done a full pass over the pack file and can now
789 0136599f 2022-10-20 stsp * verify its checksum.
791 0136599f 2022-10-20 stsp SHA1Final(pack_sha1, &ctx);
793 0136599f 2022-10-20 stsp if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
794 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PACKFILE_CSUM);
798 0136599f 2022-10-20 stsp /* Verify the SHA1 checksum stored at the end of the pack file. */
799 0136599f 2022-10-20 stsp if (pack->map) {
800 0136599f 2022-10-20 stsp memcpy(pack_sha1_expected, pack->map +
801 0136599f 2022-10-20 stsp pack->filesize - SHA1_DIGEST_LENGTH,
802 0136599f 2022-10-20 stsp SHA1_DIGEST_LENGTH);
805 0136599f 2022-10-20 stsp if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
806 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
809 0136599f 2022-10-20 stsp n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
810 0136599f 2022-10-20 stsp if (n == -1) {
811 0136599f 2022-10-20 stsp err = got_error_from_errno("read");
814 0136599f 2022-10-20 stsp if (n != SHA1_DIGEST_LENGTH) {
815 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_IO);
819 0136599f 2022-10-20 stsp if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
820 0136599f 2022-10-20 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
821 0136599f 2022-10-20 stsp "bad checksum in pack file trailer");
825 0136599f 2022-10-20 stsp if (first_delta_idx == -1)
826 0136599f 2022-10-20 stsp first_delta_idx = 0;
828 0136599f 2022-10-20 stsp /* In order to resolve ref deltas we need an in-progress pack index. */
829 0136599f 2022-10-20 stsp if (have_ref_deltas)
830 0136599f 2022-10-20 stsp make_packidx(&packidx, nobj, objects);
833 0136599f 2022-10-20 stsp * Second pass: We can now resolve deltas to compute the IDs of
834 0136599f 2022-10-20 stsp * objects which appear in deltified form. Because deltas can be
835 0136599f 2022-10-20 stsp * chained this pass may require a couple of iterations until all
836 0136599f 2022-10-20 stsp * IDs of deltified objects have been discovered.
839 0136599f 2022-10-20 stsp while (nvalid != nobj) {
842 0136599f 2022-10-20 stsp * This loop will only run once unless the pack file
843 0136599f 2022-10-20 stsp * contains ref deltas which refer to objects located
844 0136599f 2022-10-20 stsp * later in the pack file, which is unusual.
845 0136599f 2022-10-20 stsp * Offset deltas can always be resolved in one pass
846 0136599f 2022-10-20 stsp * unless the packfile is corrupt.
848 0136599f 2022-10-20 stsp for (i = first_delta_idx; i < nobj; i++) {
849 0136599f 2022-10-20 stsp obj = &objects[i];
850 0136599f 2022-10-20 stsp if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
851 0136599f 2022-10-20 stsp obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
854 0136599f 2022-10-20 stsp if (obj->valid)
857 0136599f 2022-10-20 stsp if (pack->map == NULL && lseek(pack->fd,
858 0136599f 2022-10-20 stsp obj->off + obj->tslen, SEEK_SET) == -1) {
859 0136599f 2022-10-20 stsp err = got_error_from_errno("lseek");
863 0136599f 2022-10-20 stsp err = resolve_deltified_object(pack, &packidx, obj,
864 0136599f 2022-10-20 stsp tmpfile, delta_base_file, delta_accum_file);
866 0136599f 2022-10-20 stsp if (err->code != GOT_ERR_NO_OBJ)
869 0136599f 2022-10-20 stsp * We cannot resolve this object yet because
870 0136599f 2022-10-20 stsp * a delta base is unknown. Try again later.
875 0136599f 2022-10-20 stsp obj->valid = 1;
877 0136599f 2022-10-20 stsp if (have_ref_deltas)
878 0136599f 2022-10-20 stsp update_packidx(&packidx, nobj, obj);
879 0136599f 2022-10-20 stsp /* Don't send too many progress privsep messages. */
880 0136599f 2022-10-20 stsp p_resolved = ((nresolved + n) * 100) / nobj;
881 0136599f 2022-10-20 stsp if (p_resolved != last_p_resolved) {
882 0136599f 2022-10-20 stsp err = report_progress(nobj, nobj,
883 713d6e11 2022-10-20 stsp nloose, nresolved + n, rl,
884 0136599f 2022-10-20 stsp progress_cb, progress_arg);
887 0136599f 2022-10-20 stsp last_p_resolved = p_resolved;
891 0136599f 2022-10-20 stsp if (pass++ > 3 && n == 0) {
892 0136599f 2022-10-20 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
893 0136599f 2022-10-20 stsp "could not resolve any of deltas; packfile could "
894 0136599f 2022-10-20 stsp "be corrupt");
897 0136599f 2022-10-20 stsp nresolved += n;
898 0136599f 2022-10-20 stsp nvalid += nresolved;
901 0136599f 2022-10-20 stsp if (nloose + nresolved != nobj) {
902 0136599f 2022-10-20 stsp static char msg[64];
903 0136599f 2022-10-20 stsp snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
904 0136599f 2022-10-20 stsp nloose + nresolved, nobj);
905 0136599f 2022-10-20 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
909 0136599f 2022-10-20 stsp err = report_progress(nobj, nobj, nloose, nresolved, NULL,
910 0136599f 2022-10-20 stsp progress_cb, progress_arg);
914 0136599f 2022-10-20 stsp make_packidx(&packidx, nobj, objects);
916 0136599f 2022-10-20 stsp free(objects);
917 0136599f 2022-10-20 stsp objects = NULL;
919 0136599f 2022-10-20 stsp SHA1Init(&ctx);
920 0136599f 2022-10-20 stsp putbe32(buf, GOT_PACKIDX_V2_MAGIC);
921 0136599f 2022-10-20 stsp putbe32(buf + 4, GOT_PACKIDX_VERSION);
922 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, buf, 8, &ctx);
925 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
926 0136599f 2022-10-20 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
929 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
930 0136599f 2022-10-20 stsp nobj * SHA1_DIGEST_LENGTH, &ctx);
933 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
934 13b2bc37 2022-10-23 stsp nobj * sizeof(uint32_t), &ctx);
937 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
938 13b2bc37 2022-10-23 stsp nobj * sizeof(uint32_t), &ctx);
941 0136599f 2022-10-20 stsp if (packidx.nlargeobj > 0) {
942 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
943 0136599f 2022-10-20 stsp packidx.nlargeobj * sizeof(uint64_t), &ctx);
947 13b2bc37 2022-10-23 stsp err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
951 0136599f 2022-10-20 stsp SHA1Final(packidx_hash, &ctx);
952 0136599f 2022-10-20 stsp w = write(idxfd, packidx_hash, sizeof(packidx_hash));
953 0136599f 2022-10-20 stsp if (w == -1) {
954 0136599f 2022-10-20 stsp err = got_error_from_errno("write");
957 0136599f 2022-10-20 stsp if (w != sizeof(packidx_hash)) {
958 0136599f 2022-10-20 stsp err = got_error(GOT_ERR_IO);
962 0136599f 2022-10-20 stsp free(objects);
963 0136599f 2022-10-20 stsp free(packidx.hdr.magic);
964 0136599f 2022-10-20 stsp free(packidx.hdr.version);
965 0136599f 2022-10-20 stsp free(packidx.hdr.fanout_table);
966 0136599f 2022-10-20 stsp free(packidx.hdr.sorted_ids);
967 0136599f 2022-10-20 stsp free(packidx.hdr.offsets);
968 0136599f 2022-10-20 stsp free(packidx.hdr.large_offsets);
969 0136599f 2022-10-20 stsp return err;