2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 668a20f6 2020-03-18 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
5 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
6 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
7 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
9 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 93658fb9 2020-03-18 stsp #include <sys/queue.h>
19 93658fb9 2020-03-18 stsp #include <sys/stat.h>
20 93658fb9 2020-03-18 stsp #include <sys/syslimits.h>
21 93658fb9 2020-03-18 stsp #include <sys/time.h>
22 93658fb9 2020-03-18 stsp #include <sys/types.h>
23 93658fb9 2020-03-18 stsp #include <sys/uio.h>
24 2e5a6fad 2020-03-18 stsp #include <sys/mman.h>
26 93658fb9 2020-03-18 stsp #include <stdint.h>
27 93658fb9 2020-03-18 stsp #include <errno.h>
28 93658fb9 2020-03-18 stsp #include <imsg.h>
29 93658fb9 2020-03-18 stsp #include <limits.h>
30 93658fb9 2020-03-18 stsp #include <signal.h>
31 93658fb9 2020-03-18 stsp #include <stdio.h>
32 93658fb9 2020-03-18 stsp #include <stdlib.h>
33 93658fb9 2020-03-18 stsp #include <string.h>
34 93658fb9 2020-03-18 stsp #include <ctype.h>
35 93658fb9 2020-03-18 stsp #include <sha1.h>
36 93658fb9 2020-03-18 stsp #include <fcntl.h>
37 93658fb9 2020-03-18 stsp #include <zlib.h>
38 93658fb9 2020-03-18 stsp #include <err.h>
39 93658fb9 2020-03-18 stsp #include <assert.h>
40 93658fb9 2020-03-18 stsp #include <dirent.h>
42 93658fb9 2020-03-18 stsp #include "got_error.h"
43 93658fb9 2020-03-18 stsp #include "got_object.h"
45 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
50 93658fb9 2020-03-18 stsp #include "got_lib_object_idset.h"
51 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
52 668a20f6 2020-03-18 stsp #include "got_lib_pack.h"
53 668a20f6 2020-03-18 stsp #include "got_lib_delta_cache.h"
55 668a20f6 2020-03-18 stsp struct got_indexed_object {
56 668a20f6 2020-03-18 stsp struct got_object_id id;
59 668a20f6 2020-03-18 stsp * Has this object been fully resolved?
60 668a20f6 2020-03-18 stsp * If so, we know its ID, otherwise we don't and 'id' is invalid.
64 668a20f6 2020-03-18 stsp /* Offset of type+size field for this object in pack file. */
67 668a20f6 2020-03-18 stsp /* Type+size values parsed from pack file. */
68 668a20f6 2020-03-18 stsp uint8_t type;
69 668a20f6 2020-03-18 stsp uint64_t size;
71 668a20f6 2020-03-18 stsp /* Length of on-disk type+size data. */
72 668a20f6 2020-03-18 stsp size_t tslen;
74 668a20f6 2020-03-18 stsp /* Length of object data following type+size. */
77 668a20f6 2020-03-18 stsp uint32_t crc;
81 836f2c92 2020-03-18 stsp /* For ref deltas. */
82 836f2c92 2020-03-18 stsp struct got_object_id ref_id;
85 836f2c92 2020-03-18 stsp /* For offset deltas. */
86 836f2c92 2020-03-18 stsp off_t base_offset;
87 836f2c92 2020-03-18 stsp size_t base_offsetlen;
93 b102634c 2020-03-18 stsp putbe32(char *b, uint32_t n)
95 b102634c 2020-03-18 stsp b[0] = n >> 24;
96 b102634c 2020-03-18 stsp b[1] = n >> 16;
97 b102634c 2020-03-18 stsp b[2] = n >> 8;
98 b102634c 2020-03-18 stsp b[3] = n >> 0;
101 668a20f6 2020-03-18 stsp static const struct got_error *
102 668a20f6 2020-03-18 stsp get_obj_type_label(const char **label, int obj_type)
104 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
106 668a20f6 2020-03-18 stsp switch (obj_type) {
107 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
108 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_BLOB;
110 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
111 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TREE;
113 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
114 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_COMMIT;
116 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
117 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TAG;
120 668a20f6 2020-03-18 stsp *label = NULL;
121 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
125 668a20f6 2020-03-18 stsp return err;
128 1e87a3c3 2020-03-18 stsp static const struct got_error *
129 1e87a3c3 2020-03-18 stsp read_crc(uint32_t *crc, int fd, size_t len)
131 1e87a3c3 2020-03-18 stsp uint8_t buf[8192];
135 1e87a3c3 2020-03-18 stsp for (n = len; n > 0; n -= r){
136 1e87a3c3 2020-03-18 stsp r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
137 1e87a3c3 2020-03-18 stsp if (r == -1)
138 1e87a3c3 2020-03-18 stsp return got_error_from_errno("read");
139 1e87a3c3 2020-03-18 stsp if (r == 0)
141 1e87a3c3 2020-03-18 stsp *crc = crc32(*crc, buf, r);
144 1e87a3c3 2020-03-18 stsp return NULL;
147 668a20f6 2020-03-18 stsp static const struct got_error *
148 4788f1ce 2020-03-18 stsp read_file_sha1(SHA1_CTX *ctx, FILE *f)
150 4788f1ce 2020-03-18 stsp uint8_t buf[8192];
154 4788f1ce 2020-03-18 stsp r = fread(buf, 1, sizeof(buf), f);
155 4788f1ce 2020-03-18 stsp if (r == 0) {
156 4788f1ce 2020-03-18 stsp if (feof(f))
157 4788f1ce 2020-03-18 stsp return NULL;
158 4788f1ce 2020-03-18 stsp return got_ferror(f, GOT_ERR_IO);
160 4788f1ce 2020-03-18 stsp SHA1Update(ctx, buf, r);
163 4788f1ce 2020-03-18 stsp return NULL;
166 4788f1ce 2020-03-18 stsp static const struct got_error *
167 4788f1ce 2020-03-18 stsp read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
168 4788f1ce 2020-03-18 stsp FILE *tmpfile)
170 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
171 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
172 4788f1ce 2020-03-18 stsp uint8_t *data = NULL;
173 668a20f6 2020-03-18 stsp size_t datalen;
175 668a20f6 2020-03-18 stsp char *header;
176 668a20f6 2020-03-18 stsp size_t headerlen;
177 668a20f6 2020-03-18 stsp const char *obj_label;
178 2e5a6fad 2020-03-18 stsp size_t mapoff = obj->off;
180 cbc66309 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
181 cbc66309 2020-03-18 stsp &obj->tslen, pack, obj->off);
183 668a20f6 2020-03-18 stsp return err;
185 2e5a6fad 2020-03-18 stsp if (pack->map) {
186 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
187 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen;
189 2e5a6fad 2020-03-18 stsp /* XXX Seek back and get the CRC of on-disk type+size bytes. */
190 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
191 2e5a6fad 2020-03-18 stsp return got_error_from_errno("lseek");
192 2e5a6fad 2020-03-18 stsp err = read_crc(&obj->crc, pack->fd, obj->tslen);
194 2e5a6fad 2020-03-18 stsp return err;
197 668a20f6 2020-03-18 stsp switch (obj->type) {
198 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
199 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
200 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
201 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
202 4788f1ce 2020-03-18 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
203 4788f1ce 2020-03-18 stsp if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
204 4788f1ce 2020-03-18 stsp err = got_error_from_errno("fseek");
207 4788f1ce 2020-03-18 stsp if (pack->map) {
208 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_mmap(&datalen,
209 4788f1ce 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
210 4788f1ce 2020-03-18 stsp pack->filesize - mapoff, tmpfile);
212 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_fd(&datalen,
213 4788f1ce 2020-03-18 stsp &obj->len, &obj->crc, pack->fd, tmpfile);
216 4788f1ce 2020-03-18 stsp if (pack->map) {
217 4788f1ce 2020-03-18 stsp err = got_inflate_to_mem_mmap(&data, &datalen,
218 4788f1ce 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
219 4788f1ce 2020-03-18 stsp pack->filesize - mapoff);
221 4788f1ce 2020-03-18 stsp err = got_inflate_to_mem_fd(&data, &datalen,
222 4788f1ce 2020-03-18 stsp &obj->len, &obj->crc, obj->size, pack->fd);
227 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
228 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, obj->type);
230 7cd14ea0 2020-03-18 stsp free(data);
233 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
234 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
235 668a20f6 2020-03-18 stsp free(data);
238 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
239 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
240 4788f1ce 2020-03-18 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
241 4788f1ce 2020-03-18 stsp err = read_file_sha1(&ctx, tmpfile);
245 4788f1ce 2020-03-18 stsp SHA1Update(&ctx, data, datalen);
246 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
247 668a20f6 2020-03-18 stsp free(header);
248 668a20f6 2020-03-18 stsp free(data);
250 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_REF_DELTA:
251 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
252 2e5a6fad 2020-03-18 stsp if (pack->map) {
253 2e5a6fad 2020-03-18 stsp if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
254 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
257 2e5a6fad 2020-03-18 stsp memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
258 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
259 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
260 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
261 2e5a6fad 2020-03-18 stsp mapoff += SHA1_DIGEST_LENGTH;
262 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
263 2e5a6fad 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
264 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
268 2e5a6fad 2020-03-18 stsp n = read(pack->fd, obj->delta.ref.ref_id.sha1,
269 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
270 2e5a6fad 2020-03-18 stsp if (n == -1) {
271 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("read");
274 2e5a6fad 2020-03-18 stsp if (n < sizeof(obj->id)) {
275 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
278 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
279 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
280 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
281 2e5a6fad 2020-03-18 stsp &obj->crc, obj->size, pack->fd);
285 668a20f6 2020-03-18 stsp obj->len += SHA1_DIGEST_LENGTH;
287 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
288 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
289 836f2c92 2020-03-18 stsp err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
290 836f2c92 2020-03-18 stsp &obj->delta.ofs.base_offsetlen, pack, obj->off,
291 836f2c92 2020-03-18 stsp obj->tslen);
295 2e5a6fad 2020-03-18 stsp if (pack->map) {
296 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
297 2e5a6fad 2020-03-18 stsp obj->delta.ofs.base_offsetlen);
298 2e5a6fad 2020-03-18 stsp mapoff += obj->delta.ofs.base_offsetlen;
299 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
300 2e5a6fad 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
301 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
306 2e5a6fad 2020-03-18 stsp * XXX Seek back and get the CRC of on-disk
307 2e5a6fad 2020-03-18 stsp * offset bytes.
309 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
311 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
314 2e5a6fad 2020-03-18 stsp err = read_crc(&obj->crc, pack->fd,
315 2e5a6fad 2020-03-18 stsp obj->delta.ofs.base_offsetlen);
319 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
320 2e5a6fad 2020-03-18 stsp &obj->crc, obj->size, pack->fd);
324 836f2c92 2020-03-18 stsp obj->len += obj->delta.ofs.base_offsetlen;
327 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
331 668a20f6 2020-03-18 stsp return err;
334 668a20f6 2020-03-18 stsp static const struct got_error *
335 668a20f6 2020-03-18 stsp hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
339 668a20f6 2020-03-18 stsp SHA1Update(ctx, buf, len);
341 668a20f6 2020-03-18 stsp w = write(fd, buf, len);
342 668a20f6 2020-03-18 stsp if (w == -1)
343 668a20f6 2020-03-18 stsp return got_error_from_errno("write");
344 668a20f6 2020-03-18 stsp if (w != len)
345 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_IO);
347 668a20f6 2020-03-18 stsp return NULL;
350 668a20f6 2020-03-18 stsp static const struct got_error *
351 668a20f6 2020-03-18 stsp resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
352 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
354 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
355 668a20f6 2020-03-18 stsp struct got_delta_chain deltas;
356 668a20f6 2020-03-18 stsp struct got_delta *delta;
357 668a20f6 2020-03-18 stsp uint8_t *buf = NULL;
358 668a20f6 2020-03-18 stsp size_t len;
359 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
360 4d0fef1d 2020-03-18 stsp char *header = NULL;
361 668a20f6 2020-03-18 stsp size_t headerlen;
362 668a20f6 2020-03-18 stsp int base_obj_type;
363 668a20f6 2020-03-18 stsp const char *obj_label;
365 668a20f6 2020-03-18 stsp deltas.nentries = 0;
366 668a20f6 2020-03-18 stsp SIMPLEQ_INIT(&deltas.entries);
368 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
369 668a20f6 2020-03-18 stsp obj->off, obj->tslen, obj->type, obj->size,
370 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
374 668a20f6 2020-03-18 stsp /* XXX TODO reading large objects into memory is bad! */
375 668a20f6 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
379 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
381 668a20f6 2020-03-18 stsp err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
384 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, base_obj_type);
387 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
388 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
391 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
392 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
393 668a20f6 2020-03-18 stsp SHA1Update(&ctx, buf, len);
394 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
397 4d0fef1d 2020-03-18 stsp free(header);
398 668a20f6 2020-03-18 stsp while (!SIMPLEQ_EMPTY(&deltas.entries)) {
399 668a20f6 2020-03-18 stsp delta = SIMPLEQ_FIRST(&deltas.entries);
400 668a20f6 2020-03-18 stsp SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
401 668a20f6 2020-03-18 stsp free(delta);
403 668a20f6 2020-03-18 stsp return err;
406 668a20f6 2020-03-18 stsp /* Determine the slot in the pack index a given object ID should use. */
408 668a20f6 2020-03-18 stsp find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
410 668a20f6 2020-03-18 stsp u_int8_t id0 = sha1[0];
411 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
412 668a20f6 2020-03-18 stsp int left = 0, right = nindexed - 1;
413 668a20f6 2020-03-18 stsp int cmp = 0, i = 0;
415 668a20f6 2020-03-18 stsp if (id0 > 0)
416 668a20f6 2020-03-18 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
418 668a20f6 2020-03-18 stsp while (left <= right) {
419 668a20f6 2020-03-18 stsp struct got_packidx_object_id *oid;
421 668a20f6 2020-03-18 stsp i = ((left + right) / 2);
422 668a20f6 2020-03-18 stsp oid = &packidx->hdr.sorted_ids[i];
424 668a20f6 2020-03-18 stsp cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
425 668a20f6 2020-03-18 stsp if (cmp == 0)
426 668a20f6 2020-03-18 stsp return -1; /* object already indexed */
427 668a20f6 2020-03-18 stsp else if (cmp > 0)
428 668a20f6 2020-03-18 stsp left = i + 1;
429 668a20f6 2020-03-18 stsp else if (cmp < 0)
430 668a20f6 2020-03-18 stsp right = i - 1;
433 668a20f6 2020-03-18 stsp return left;
437 668a20f6 2020-03-18 stsp static void
438 668a20f6 2020-03-18 stsp print_packidx(struct got_packidx *packidx)
440 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
443 668a20f6 2020-03-18 stsp fprintf(stderr, "object IDs:\n");
444 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
445 668a20f6 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
446 668a20f6 2020-03-18 stsp got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
447 668a20f6 2020-03-18 stsp hex, sizeof(hex));
448 668a20f6 2020-03-18 stsp fprintf(stderr, "%s\n", hex);
450 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
452 668a20f6 2020-03-18 stsp fprintf(stderr, "object offsets:\n");
453 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
454 668a20f6 2020-03-18 stsp uint32_t offset = be32toh(packidx->hdr.offsets[i]);
455 668a20f6 2020-03-18 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
456 668a20f6 2020-03-18 stsp int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
457 668a20f6 2020-03-18 stsp fprintf(stderr, "%u -> %llu\n", offset,
458 668a20f6 2020-03-18 stsp be64toh(packidx->hdr.large_offsets[j]));
460 668a20f6 2020-03-18 stsp fprintf(stderr, "%u\n", offset);
462 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
464 668a20f6 2020-03-18 stsp fprintf(stderr, "fanout table:");
465 668a20f6 2020-03-18 stsp for (i = 0; i <= 0xff; i++)
466 668a20f6 2020-03-18 stsp fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
467 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
471 668a20f6 2020-03-18 stsp static void
472 950de2cd 2020-03-18 stsp add_indexed_object(struct got_packidx *packidx, uint32_t idx,
473 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
477 668a20f6 2020-03-18 stsp memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
478 668a20f6 2020-03-18 stsp SHA1_DIGEST_LENGTH);
479 97684601 2020-03-18 stsp packidx->hdr.crc32[idx] = htobe32(obj->crc);
480 668a20f6 2020-03-18 stsp if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
481 668a20f6 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(obj->off);
483 7bad1537 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
484 668a20f6 2020-03-18 stsp GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
485 7bad1537 2020-03-18 stsp packidx->hdr.large_offsets[packidx->nlargeobj] =
486 7bad1537 2020-03-18 stsp htobe64(obj->off);
487 7bad1537 2020-03-18 stsp packidx->nlargeobj++;
490 668a20f6 2020-03-18 stsp for (i = obj->id.sha1[0]; i <= 0xff; i++) {
491 950de2cd 2020-03-18 stsp uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
492 668a20f6 2020-03-18 stsp packidx->hdr.fanout_table[i] = htobe32(n + 1);
497 8bb2b40c 2020-03-18 stsp indexed_obj_cmp(const void *pa, const void *pb)
499 8bb2b40c 2020-03-18 stsp struct got_indexed_object *a, *b;
501 262c582a 2020-03-18 stsp a = (struct got_indexed_object *)pa;
502 262c582a 2020-03-18 stsp b = (struct got_indexed_object *)pb;
503 8bb2b40c 2020-03-18 stsp return got_object_id_cmp(&a->id, &b->id);
506 950de2cd 2020-03-18 stsp static void
507 0fd91daf 2020-03-18 stsp make_packidx(struct got_packidx *packidx, int nobj,
508 262c582a 2020-03-18 stsp struct got_indexed_object *objects)
510 950de2cd 2020-03-18 stsp struct got_indexed_object *obj;
512 950de2cd 2020-03-18 stsp uint32_t idx = 0;
514 93bba072 2020-03-18 stsp qsort(objects, nobj, sizeof(struct got_indexed_object),
515 950de2cd 2020-03-18 stsp indexed_obj_cmp);
517 0fd91daf 2020-03-18 stsp memset(packidx->hdr.fanout_table, 0,
518 0fd91daf 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
519 0fd91daf 2020-03-18 stsp packidx->nlargeobj = 0;
521 950de2cd 2020-03-18 stsp for (i = 0; i < nobj; i++) {
522 262c582a 2020-03-18 stsp obj = &objects[i];
523 0fd91daf 2020-03-18 stsp if (obj->valid)
524 0fd91daf 2020-03-18 stsp add_indexed_object(packidx, idx++, obj);
528 950de2cd 2020-03-18 stsp static void
529 950de2cd 2020-03-18 stsp update_packidx(struct got_packidx *packidx, int nobj,
530 950de2cd 2020-03-18 stsp struct got_indexed_object *obj)
532 950de2cd 2020-03-18 stsp uint32_t idx;
533 950de2cd 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
535 950de2cd 2020-03-18 stsp idx = find_object_idx(packidx, obj->id.sha1);
536 950de2cd 2020-03-18 stsp if (idx == -1) {
537 950de2cd 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
538 950de2cd 2020-03-18 stsp got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
539 950de2cd 2020-03-18 stsp return; /* object already indexed */
542 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.sorted_ids[idx + 1],
543 950de2cd 2020-03-18 stsp &packidx->hdr.sorted_ids[idx],
544 950de2cd 2020-03-18 stsp sizeof(struct got_packidx_object_id) * (nindexed - idx));
545 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
546 950de2cd 2020-03-18 stsp sizeof(uint32_t) * (nindexed - idx));
548 950de2cd 2020-03-18 stsp add_indexed_object(packidx, idx, obj);
551 668a20f6 2020-03-18 stsp static const struct got_error *
552 4788f1ce 2020-03-18 stsp index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
553 4788f1ce 2020-03-18 stsp uint8_t *pack_hash, struct imsgbuf *ibuf)
555 668a20f6 2020-03-18 stsp const struct got_error *err;
556 668a20f6 2020-03-18 stsp struct got_packfile_hdr hdr;
557 668a20f6 2020-03-18 stsp struct got_packidx packidx;
558 668a20f6 2020-03-18 stsp char buf[8];
559 7bad1537 2020-03-18 stsp int nobj, nvalid, nloose, nresolved = 0, i;
560 262c582a 2020-03-18 stsp struct got_indexed_object *objects = NULL, *obj;
561 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
562 668a20f6 2020-03-18 stsp uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
563 668a20f6 2020-03-18 stsp ssize_t r, w;
564 160bbe2e 2020-03-18 stsp int pass, have_ref_deltas = 0, first_delta_idx = -1;
565 2e5a6fad 2020-03-18 stsp size_t mapoff = 0;
566 5672d305 2020-03-18 stsp int p_indexed = 0, last_p_indexed = -1;
567 5672d305 2020-03-18 stsp int p_resolved = 0, last_p_resolved = -1;
569 668a20f6 2020-03-18 stsp /* Check pack file header. */
570 2e5a6fad 2020-03-18 stsp if (pack->map) {
571 2e5a6fad 2020-03-18 stsp if (pack->filesize < sizeof(hdr))
572 2e5a6fad 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
573 2e5a6fad 2020-03-18 stsp "short packfile header");
574 2e5a6fad 2020-03-18 stsp memcpy(&hdr, pack->map, sizeof(hdr));
575 2e5a6fad 2020-03-18 stsp mapoff += sizeof(hdr);
577 2e5a6fad 2020-03-18 stsp r = read(pack->fd, &hdr, sizeof(hdr));
578 2e5a6fad 2020-03-18 stsp if (r == -1)
579 2e5a6fad 2020-03-18 stsp return got_error_from_errno("read");
580 2e5a6fad 2020-03-18 stsp if (r < sizeof(hdr))
581 2e5a6fad 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
582 2e5a6fad 2020-03-18 stsp "short packfile header");
585 668a20f6 2020-03-18 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
586 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
587 668a20f6 2020-03-18 stsp "bad packfile signature");
588 668a20f6 2020-03-18 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
589 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
590 668a20f6 2020-03-18 stsp "bad packfile version");
591 668a20f6 2020-03-18 stsp nobj = betoh32(hdr.nobjects);
592 668a20f6 2020-03-18 stsp if (nobj == 0)
593 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
594 668a20f6 2020-03-18 stsp "bad packfile with zero objects");
597 668a20f6 2020-03-18 stsp * Create an in-memory pack index which will grow as objects
598 668a20f6 2020-03-18 stsp * IDs in the pack file are discovered. Only fields used to
599 668a20f6 2020-03-18 stsp * read deltified objects will be needed by the pack.c library
600 668a20f6 2020-03-18 stsp * code, so setting up just a pack index header is sufficient.
602 668a20f6 2020-03-18 stsp memset(&packidx, 0, sizeof(packidx));
603 668a20f6 2020-03-18 stsp packidx.hdr.magic = malloc(sizeof(uint32_t));
604 668a20f6 2020-03-18 stsp if (packidx.hdr.magic == NULL)
605 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
606 668a20f6 2020-03-18 stsp *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
607 668a20f6 2020-03-18 stsp packidx.hdr.version = malloc(sizeof(uint32_t));
608 668a20f6 2020-03-18 stsp if (packidx.hdr.version == NULL) {
609 668a20f6 2020-03-18 stsp err = got_error_from_errno("malloc");
612 668a20f6 2020-03-18 stsp *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
613 668a20f6 2020-03-18 stsp packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
614 668a20f6 2020-03-18 stsp sizeof(uint32_t));
615 668a20f6 2020-03-18 stsp if (packidx.hdr.fanout_table == NULL) {
616 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
619 668a20f6 2020-03-18 stsp packidx.hdr.sorted_ids = calloc(nobj,
620 668a20f6 2020-03-18 stsp sizeof(struct got_packidx_object_id));
621 668a20f6 2020-03-18 stsp if (packidx.hdr.sorted_ids == NULL) {
622 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
625 97684601 2020-03-18 stsp packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
626 97684601 2020-03-18 stsp if (packidx.hdr.crc32 == NULL) {
627 97684601 2020-03-18 stsp err = got_error_from_errno("calloc");
630 668a20f6 2020-03-18 stsp packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
631 668a20f6 2020-03-18 stsp if (packidx.hdr.offsets == NULL) {
632 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
635 668a20f6 2020-03-18 stsp /* Large offsets table is empty for pack files < 2 GB. */
636 668a20f6 2020-03-18 stsp if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
637 668a20f6 2020-03-18 stsp packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
638 668a20f6 2020-03-18 stsp if (packidx.hdr.large_offsets == NULL) {
639 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
644 668a20f6 2020-03-18 stsp nvalid = 0;
645 668a20f6 2020-03-18 stsp nloose = 0;
646 262c582a 2020-03-18 stsp objects = calloc(nobj, sizeof(struct got_indexed_object));
647 668a20f6 2020-03-18 stsp if (objects == NULL)
648 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
651 668a20f6 2020-03-18 stsp * First pass: locate all objects and identify un-deltified objects.
653 668a20f6 2020-03-18 stsp * When this pass has completed we will know offset, type, size, and
654 668a20f6 2020-03-18 stsp * CRC information for all objects in this pack file. We won't know
655 668a20f6 2020-03-18 stsp * any of the actual object IDs of deltified objects yet since we
656 668a20f6 2020-03-18 stsp * will not yet attempt to combine deltas.
659 668a20f6 2020-03-18 stsp for (i = 0; i < nobj; i++) {
660 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
661 5672d305 2020-03-18 stsp p_indexed = ((i + 1) * 100) / nobj;
662 5672d305 2020-03-18 stsp if (p_indexed != last_p_indexed) {
663 5672d305 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf,
664 5672d305 2020-03-18 stsp nobj, i + 1, nloose, 0);
667 5672d305 2020-03-18 stsp last_p_indexed = p_indexed;
670 262c582a 2020-03-18 stsp obj = &objects[i];
671 1e87a3c3 2020-03-18 stsp obj->crc = crc32(0L, NULL, 0);
673 668a20f6 2020-03-18 stsp /* Store offset to type+size information for this object. */
674 2e5a6fad 2020-03-18 stsp if (pack->map) {
675 2e5a6fad 2020-03-18 stsp obj->off = mapoff;
677 2e5a6fad 2020-03-18 stsp obj->off = lseek(pack->fd, 0, SEEK_CUR);
678 2e5a6fad 2020-03-18 stsp if (obj->off == -1) {
679 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
684 4788f1ce 2020-03-18 stsp err = read_packed_object(pack, obj, tmpfile);
688 2e5a6fad 2020-03-18 stsp if (pack->map) {
689 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen + obj->len;
691 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
692 2e5a6fad 2020-03-18 stsp SEEK_SET) == -1) {
693 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
698 668a20f6 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_BLOB ||
699 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TREE ||
700 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_COMMIT ||
701 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TAG) {
702 262c582a 2020-03-18 stsp obj->valid = 1;
705 160bbe2e 2020-03-18 stsp if (first_delta_idx == -1)
706 160bbe2e 2020-03-18 stsp first_delta_idx = i;
707 160bbe2e 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
708 160bbe2e 2020-03-18 stsp have_ref_deltas = 1;
711 668a20f6 2020-03-18 stsp nvalid = nloose;
713 160bbe2e 2020-03-18 stsp if (first_delta_idx == -1)
714 160bbe2e 2020-03-18 stsp first_delta_idx = 0;
716 0fd91daf 2020-03-18 stsp /* In order to resolve ref deltas we need an in-progress pack index. */
717 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
718 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
721 668a20f6 2020-03-18 stsp * Second pass: We can now resolve deltas to compute the IDs of
722 668a20f6 2020-03-18 stsp * objects which appear in deltified form. Because deltas can be
723 668a20f6 2020-03-18 stsp * chained this pass may require a couple of iterations until all
724 668a20f6 2020-03-18 stsp * IDs of deltified objects have been discovered.
727 668a20f6 2020-03-18 stsp while (nvalid != nobj) {
730 e6c1173d 2020-03-18 stsp * This loop will only run once unless the pack file
731 e6c1173d 2020-03-18 stsp * contains ref deltas which refer to objects located
732 e6c1173d 2020-03-18 stsp * later in the pack file, which is unusual.
733 e6c1173d 2020-03-18 stsp * Offset deltas can always be resolved in one pass
734 e6c1173d 2020-03-18 stsp * unless the packfile is corrupt.
736 160bbe2e 2020-03-18 stsp for (i = first_delta_idx; i < nobj; i++) {
737 262c582a 2020-03-18 stsp obj = &objects[i];
738 262c582a 2020-03-18 stsp if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
739 262c582a 2020-03-18 stsp obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
742 262c582a 2020-03-18 stsp if (obj->valid)
745 b3e1118b 2020-03-18 stsp if (pack->map == NULL && lseek(pack->fd,
746 b3e1118b 2020-03-18 stsp obj->off + obj->tslen, SEEK_SET) == -1) {
747 b3e1118b 2020-03-18 stsp err = got_error_from_errno("lseek");
751 668a20f6 2020-03-18 stsp err = resolve_deltified_object(pack, &packidx, obj);
753 668a20f6 2020-03-18 stsp if (err->code != GOT_ERR_NO_OBJ)
756 668a20f6 2020-03-18 stsp * We cannot resolve this object yet because
757 668a20f6 2020-03-18 stsp * a delta base is unknown. Try again later.
762 262c582a 2020-03-18 stsp obj->valid = 1;
764 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
765 0fd91daf 2020-03-18 stsp update_packidx(&packidx, nobj, obj);
766 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
767 5672d305 2020-03-18 stsp p_resolved = ((nresolved + n) * 100) / nobj;
768 5672d305 2020-03-18 stsp if (p_resolved != last_p_resolved) {
769 5672d305 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf,
770 5672d305 2020-03-18 stsp nobj, nobj, nloose, nresolved + n);
773 5672d305 2020-03-18 stsp last_p_resolved = p_resolved;
777 668a20f6 2020-03-18 stsp if (pass++ > 3 && n == 0) {
778 668a20f6 2020-03-18 stsp static char msg[64];
779 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg), "could not resolve "
780 668a20f6 2020-03-18 stsp "any of deltas; packfile could be corrupt");
781 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
785 668a20f6 2020-03-18 stsp nresolved += n;
786 668a20f6 2020-03-18 stsp nvalid += nresolved;
789 668a20f6 2020-03-18 stsp if (nloose + nresolved != nobj) {
790 668a20f6 2020-03-18 stsp static char msg[64];
791 ec92f929 2020-03-18 stsp snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
792 cbc66309 2020-03-18 stsp nloose + nresolved, nobj);
793 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
797 021b0c6f 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
798 021b0c6f 2020-03-18 stsp nloose, nresolved);
802 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
804 97684601 2020-03-18 stsp free(objects);
805 97684601 2020-03-18 stsp objects = NULL;
807 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
808 b102634c 2020-03-18 stsp putbe32(buf, GOT_PACKIDX_V2_MAGIC);
809 b102634c 2020-03-18 stsp putbe32(buf + 4, GOT_PACKIDX_VERSION);
810 b102634c 2020-03-18 stsp err = hwrite(idxfd, buf, 8, &ctx);
813 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.fanout_table,
814 668a20f6 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
817 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.sorted_ids,
818 668a20f6 2020-03-18 stsp nobj * SHA1_DIGEST_LENGTH, &ctx);
821 97684601 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
824 cbc66309 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
828 7bad1537 2020-03-18 stsp if (packidx.nlargeobj > 0) {
829 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.large_offsets,
830 7bad1537 2020-03-18 stsp packidx.nlargeobj * sizeof(uint64_t), &ctx);
834 668a20f6 2020-03-18 stsp err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
838 668a20f6 2020-03-18 stsp SHA1Final(packidx_hash, &ctx);
839 668a20f6 2020-03-18 stsp w = write(idxfd, packidx_hash, sizeof(packidx_hash));
840 668a20f6 2020-03-18 stsp if (w == -1) {
841 668a20f6 2020-03-18 stsp err = got_error_from_errno("write");
844 668a20f6 2020-03-18 stsp if (w != sizeof(packidx_hash)) {
845 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_IO);
849 37ebab0a 2020-03-18 stsp free(objects);
850 668a20f6 2020-03-18 stsp free(packidx.hdr.magic);
851 668a20f6 2020-03-18 stsp free(packidx.hdr.version);
852 668a20f6 2020-03-18 stsp free(packidx.hdr.fanout_table);
853 668a20f6 2020-03-18 stsp free(packidx.hdr.sorted_ids);
854 668a20f6 2020-03-18 stsp free(packidx.hdr.offsets);
855 668a20f6 2020-03-18 stsp free(packidx.hdr.large_offsets);
856 668a20f6 2020-03-18 stsp return err;
860 93658fb9 2020-03-18 stsp main(int argc, char **argv)
862 668a20f6 2020-03-18 stsp const struct got_error *err = NULL, *close_err;
863 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
864 93658fb9 2020-03-18 stsp struct imsg imsg;
865 4788f1ce 2020-03-18 stsp int idxfd = -1, tmpfd = -1;
866 4788f1ce 2020-03-18 stsp FILE *tmpfile = NULL;
867 668a20f6 2020-03-18 stsp struct got_pack pack;
868 668a20f6 2020-03-18 stsp uint8_t pack_hash[SHA1_DIGEST_LENGTH];
869 668a20f6 2020-03-18 stsp off_t packfile_size;
871 668a20f6 2020-03-18 stsp static int attached;
872 668a20f6 2020-03-18 stsp while (!attached)
876 668a20f6 2020-03-18 stsp memset(&pack, 0, sizeof(pack));
877 668a20f6 2020-03-18 stsp pack.fd = -1;
878 18d4da03 2020-03-18 stsp pack.delta_cache = got_delta_cache_alloc(500,
879 668a20f6 2020-03-18 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
880 668a20f6 2020-03-18 stsp if (pack.delta_cache == NULL) {
881 668a20f6 2020-03-18 stsp err = got_error_from_errno("got_delta_cache_alloc");
885 668a20f6 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
886 861f3006 2020-03-18 stsp #ifndef PROFILE
887 861f3006 2020-03-18 stsp /* revoke access to most system calls */
888 861f3006 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
889 861f3006 2020-03-18 stsp err = got_error_from_errno("pledge");
890 861f3006 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
894 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
897 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
899 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
900 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
903 668a20f6 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
904 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
907 668a20f6 2020-03-18 stsp memcpy(pack_hash, imsg.data, sizeof(pack_hash));
908 668a20f6 2020-03-18 stsp pack.fd = imsg.fd;
910 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
913 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
915 73ab1060 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
916 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
919 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
920 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
923 93658fb9 2020-03-18 stsp idxfd = imsg.fd;
925 4788f1ce 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
928 4788f1ce 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
930 4788f1ce 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
931 4788f1ce 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
934 4788f1ce 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
935 4788f1ce 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
938 4788f1ce 2020-03-18 stsp tmpfd = imsg.fd;
939 4788f1ce 2020-03-18 stsp tmpfile = fdopen(tmpfd, "w+");
940 4788f1ce 2020-03-18 stsp if (tmpfile == NULL) {
941 4788f1ce 2020-03-18 stsp err = got_error_from_errno("fdopen");
944 4788f1ce 2020-03-18 stsp tmpfd = -1;
946 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_END) == -1) {
947 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
950 668a20f6 2020-03-18 stsp packfile_size = lseek(pack.fd, 0, SEEK_CUR);
951 668a20f6 2020-03-18 stsp if (packfile_size == -1) {
952 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
955 668a20f6 2020-03-18 stsp pack.filesize = packfile_size; /* XXX off_t vs size_t */
957 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_SET) == -1) {
958 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
962 2e5a6fad 2020-03-18 stsp #ifndef GOT_PACK_NO_MMAP
963 2e5a6fad 2020-03-18 stsp pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
964 2e5a6fad 2020-03-18 stsp pack.fd, 0);
965 2e5a6fad 2020-03-18 stsp if (pack.map == MAP_FAILED)
966 2e5a6fad 2020-03-18 stsp pack.map = NULL; /* fall back to read(2) */
968 4788f1ce 2020-03-18 stsp err = index_pack(&pack, idxfd, tmpfile, pack_hash, &ibuf);
970 668a20f6 2020-03-18 stsp close_err = got_pack_close(&pack);
971 668a20f6 2020-03-18 stsp if (close_err && err == NULL)
972 668a20f6 2020-03-18 stsp err = close_err;
973 668a20f6 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
974 668a20f6 2020-03-18 stsp err = got_error_from_errno("close");
975 4788f1ce 2020-03-18 stsp if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
976 4788f1ce 2020-03-18 stsp err = got_error_from_errno("close");
978 668a20f6 2020-03-18 stsp if (err == NULL)
979 93658fb9 2020-03-18 stsp err = got_privsep_send_index_pack_done(&ibuf);
981 668a20f6 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
982 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
983 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);