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 d582f26c 2020-03-18 stsp #ifndef nitems
56 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 668a20f6 2020-03-18 stsp struct got_indexed_object {
60 668a20f6 2020-03-18 stsp struct got_object_id id;
63 668a20f6 2020-03-18 stsp * Has this object been fully resolved?
64 668a20f6 2020-03-18 stsp * If so, we know its ID, otherwise we don't and 'id' is invalid.
68 668a20f6 2020-03-18 stsp /* Offset of type+size field for this object in pack file. */
71 668a20f6 2020-03-18 stsp /* Type+size values parsed from pack file. */
72 668a20f6 2020-03-18 stsp uint8_t type;
73 668a20f6 2020-03-18 stsp uint64_t size;
75 668a20f6 2020-03-18 stsp /* Length of on-disk type+size data. */
76 668a20f6 2020-03-18 stsp size_t tslen;
78 668a20f6 2020-03-18 stsp /* Length of object data following type+size. */
81 668a20f6 2020-03-18 stsp uint32_t crc;
85 836f2c92 2020-03-18 stsp /* For ref deltas. */
86 836f2c92 2020-03-18 stsp struct got_object_id ref_id;
89 836f2c92 2020-03-18 stsp /* For offset deltas. */
90 836f2c92 2020-03-18 stsp off_t base_offset;
91 836f2c92 2020-03-18 stsp size_t base_offsetlen;
97 b102634c 2020-03-18 stsp putbe32(char *b, uint32_t n)
99 b102634c 2020-03-18 stsp b[0] = n >> 24;
100 b102634c 2020-03-18 stsp b[1] = n >> 16;
101 b102634c 2020-03-18 stsp b[2] = n >> 8;
102 b102634c 2020-03-18 stsp b[3] = n >> 0;
105 668a20f6 2020-03-18 stsp static const struct got_error *
106 668a20f6 2020-03-18 stsp get_obj_type_label(const char **label, int obj_type)
108 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
110 668a20f6 2020-03-18 stsp switch (obj_type) {
111 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
112 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_BLOB;
114 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
115 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TREE;
117 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
118 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_COMMIT;
120 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
121 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TAG;
124 668a20f6 2020-03-18 stsp *label = NULL;
125 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
129 668a20f6 2020-03-18 stsp return err;
132 1e87a3c3 2020-03-18 stsp static const struct got_error *
133 1e87a3c3 2020-03-18 stsp read_crc(uint32_t *crc, int fd, size_t len)
135 1e87a3c3 2020-03-18 stsp uint8_t buf[8192];
139 1e87a3c3 2020-03-18 stsp for (n = len; n > 0; n -= r){
140 1e87a3c3 2020-03-18 stsp r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
141 1e87a3c3 2020-03-18 stsp if (r == -1)
142 1e87a3c3 2020-03-18 stsp return got_error_from_errno("read");
143 1e87a3c3 2020-03-18 stsp if (r == 0)
145 1e87a3c3 2020-03-18 stsp *crc = crc32(*crc, buf, r);
148 1e87a3c3 2020-03-18 stsp return NULL;
151 668a20f6 2020-03-18 stsp static const struct got_error *
152 d582f26c 2020-03-18 stsp read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
154 4788f1ce 2020-03-18 stsp uint8_t buf[8192];
155 d582f26c 2020-03-18 stsp size_t n, r;
157 d582f26c 2020-03-18 stsp for (n = len; n > 0; n -= r) {
158 d582f26c 2020-03-18 stsp r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
159 4788f1ce 2020-03-18 stsp if (r == 0) {
160 4788f1ce 2020-03-18 stsp if (feof(f))
161 4788f1ce 2020-03-18 stsp return NULL;
162 4788f1ce 2020-03-18 stsp return got_ferror(f, GOT_ERR_IO);
164 4788f1ce 2020-03-18 stsp SHA1Update(ctx, buf, r);
167 4788f1ce 2020-03-18 stsp return NULL;
170 4788f1ce 2020-03-18 stsp static const struct got_error *
171 4788f1ce 2020-03-18 stsp read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
172 4788f1ce 2020-03-18 stsp FILE *tmpfile)
174 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
175 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
176 4788f1ce 2020-03-18 stsp uint8_t *data = NULL;
177 d582f26c 2020-03-18 stsp size_t datalen = 0;
179 668a20f6 2020-03-18 stsp char *header;
180 668a20f6 2020-03-18 stsp size_t headerlen;
181 668a20f6 2020-03-18 stsp const char *obj_label;
182 2e5a6fad 2020-03-18 stsp size_t mapoff = obj->off;
184 cbc66309 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
185 cbc66309 2020-03-18 stsp &obj->tslen, pack, obj->off);
187 668a20f6 2020-03-18 stsp return err;
189 2e5a6fad 2020-03-18 stsp if (pack->map) {
190 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
191 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen;
193 2e5a6fad 2020-03-18 stsp /* XXX Seek back and get the CRC of on-disk type+size bytes. */
194 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
195 2e5a6fad 2020-03-18 stsp return got_error_from_errno("lseek");
196 2e5a6fad 2020-03-18 stsp err = read_crc(&obj->crc, pack->fd, obj->tslen);
198 2e5a6fad 2020-03-18 stsp return err;
201 668a20f6 2020-03-18 stsp switch (obj->type) {
202 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
203 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
204 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
205 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
206 4788f1ce 2020-03-18 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
207 4788f1ce 2020-03-18 stsp if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
208 4788f1ce 2020-03-18 stsp err = got_error_from_errno("fseek");
211 4788f1ce 2020-03-18 stsp if (pack->map) {
212 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_mmap(&datalen,
213 4788f1ce 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
214 4788f1ce 2020-03-18 stsp pack->filesize - mapoff, tmpfile);
216 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_fd(&datalen,
217 4788f1ce 2020-03-18 stsp &obj->len, &obj->crc, pack->fd, tmpfile);
220 4788f1ce 2020-03-18 stsp if (pack->map) {
221 4788f1ce 2020-03-18 stsp err = got_inflate_to_mem_mmap(&data, &datalen,
222 4788f1ce 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
223 4788f1ce 2020-03-18 stsp pack->filesize - mapoff);
225 4788f1ce 2020-03-18 stsp err = got_inflate_to_mem_fd(&data, &datalen,
226 4788f1ce 2020-03-18 stsp &obj->len, &obj->crc, obj->size, pack->fd);
231 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
232 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, obj->type);
234 7cd14ea0 2020-03-18 stsp free(data);
237 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
238 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
239 668a20f6 2020-03-18 stsp free(data);
242 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
243 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
244 4788f1ce 2020-03-18 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
245 d582f26c 2020-03-18 stsp err = read_file_sha1(&ctx, tmpfile, datalen);
249 4788f1ce 2020-03-18 stsp SHA1Update(&ctx, data, datalen);
250 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
251 668a20f6 2020-03-18 stsp free(header);
252 668a20f6 2020-03-18 stsp free(data);
254 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_REF_DELTA:
255 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
256 2e5a6fad 2020-03-18 stsp if (pack->map) {
257 2e5a6fad 2020-03-18 stsp if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
258 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
261 2e5a6fad 2020-03-18 stsp memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
262 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
263 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
264 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
265 2e5a6fad 2020-03-18 stsp mapoff += SHA1_DIGEST_LENGTH;
266 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
267 2e5a6fad 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
268 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
272 2e5a6fad 2020-03-18 stsp n = read(pack->fd, obj->delta.ref.ref_id.sha1,
273 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
274 2e5a6fad 2020-03-18 stsp if (n == -1) {
275 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("read");
278 2e5a6fad 2020-03-18 stsp if (n < sizeof(obj->id)) {
279 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
282 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
283 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
284 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
285 2e5a6fad 2020-03-18 stsp &obj->crc, obj->size, pack->fd);
289 668a20f6 2020-03-18 stsp obj->len += SHA1_DIGEST_LENGTH;
291 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
292 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
293 836f2c92 2020-03-18 stsp err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
294 836f2c92 2020-03-18 stsp &obj->delta.ofs.base_offsetlen, pack, obj->off,
295 836f2c92 2020-03-18 stsp obj->tslen);
299 2e5a6fad 2020-03-18 stsp if (pack->map) {
300 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
301 2e5a6fad 2020-03-18 stsp obj->delta.ofs.base_offsetlen);
302 2e5a6fad 2020-03-18 stsp mapoff += obj->delta.ofs.base_offsetlen;
303 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
304 2e5a6fad 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
305 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
310 2e5a6fad 2020-03-18 stsp * XXX Seek back and get the CRC of on-disk
311 2e5a6fad 2020-03-18 stsp * offset bytes.
313 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
315 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
318 2e5a6fad 2020-03-18 stsp err = read_crc(&obj->crc, pack->fd,
319 2e5a6fad 2020-03-18 stsp obj->delta.ofs.base_offsetlen);
323 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
324 2e5a6fad 2020-03-18 stsp &obj->crc, obj->size, pack->fd);
328 836f2c92 2020-03-18 stsp obj->len += obj->delta.ofs.base_offsetlen;
331 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
335 668a20f6 2020-03-18 stsp return err;
338 668a20f6 2020-03-18 stsp static const struct got_error *
339 668a20f6 2020-03-18 stsp hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
343 668a20f6 2020-03-18 stsp SHA1Update(ctx, buf, len);
345 668a20f6 2020-03-18 stsp w = write(fd, buf, len);
346 668a20f6 2020-03-18 stsp if (w == -1)
347 668a20f6 2020-03-18 stsp return got_error_from_errno("write");
348 668a20f6 2020-03-18 stsp if (w != len)
349 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_IO);
351 668a20f6 2020-03-18 stsp return NULL;
354 668a20f6 2020-03-18 stsp static const struct got_error *
355 668a20f6 2020-03-18 stsp resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
356 d582f26c 2020-03-18 stsp struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
357 d582f26c 2020-03-18 stsp FILE *delta_accum_file)
359 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
360 668a20f6 2020-03-18 stsp struct got_delta_chain deltas;
361 668a20f6 2020-03-18 stsp struct got_delta *delta;
362 668a20f6 2020-03-18 stsp uint8_t *buf = NULL;
363 d582f26c 2020-03-18 stsp size_t len = 0;
364 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
365 4d0fef1d 2020-03-18 stsp char *header = NULL;
366 668a20f6 2020-03-18 stsp size_t headerlen;
367 d582f26c 2020-03-18 stsp uint64_t max_size;
368 668a20f6 2020-03-18 stsp int base_obj_type;
369 668a20f6 2020-03-18 stsp const char *obj_label;
371 668a20f6 2020-03-18 stsp deltas.nentries = 0;
372 668a20f6 2020-03-18 stsp SIMPLEQ_INIT(&deltas.entries);
374 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
375 668a20f6 2020-03-18 stsp obj->off, obj->tslen, obj->type, obj->size,
376 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
380 d582f26c 2020-03-18 stsp err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
383 d582f26c 2020-03-18 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
384 d582f26c 2020-03-18 stsp rewind(tmpfile);
385 d582f26c 2020-03-18 stsp rewind(delta_base_file);
386 d582f26c 2020-03-18 stsp rewind(delta_accum_file);
387 d582f26c 2020-03-18 stsp err = got_pack_dump_delta_chain_to_file(&len, &deltas,
388 d582f26c 2020-03-18 stsp pack, tmpfile, delta_base_file, delta_accum_file);
392 d582f26c 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(&buf, &len,
393 d582f26c 2020-03-18 stsp &deltas, pack);
398 668a20f6 2020-03-18 stsp err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
401 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, base_obj_type);
404 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
405 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
408 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
409 d582f26c 2020-03-18 stsp SHA1Init(&ctx);
410 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
411 d582f26c 2020-03-18 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
412 d582f26c 2020-03-18 stsp err = read_file_sha1(&ctx, tmpfile, len);
416 d582f26c 2020-03-18 stsp SHA1Update(&ctx, buf, len);
417 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
420 4d0fef1d 2020-03-18 stsp free(header);
421 668a20f6 2020-03-18 stsp while (!SIMPLEQ_EMPTY(&deltas.entries)) {
422 668a20f6 2020-03-18 stsp delta = SIMPLEQ_FIRST(&deltas.entries);
423 668a20f6 2020-03-18 stsp SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
424 668a20f6 2020-03-18 stsp free(delta);
426 668a20f6 2020-03-18 stsp return err;
429 668a20f6 2020-03-18 stsp /* Determine the slot in the pack index a given object ID should use. */
431 668a20f6 2020-03-18 stsp find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
433 668a20f6 2020-03-18 stsp u_int8_t id0 = sha1[0];
434 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
435 668a20f6 2020-03-18 stsp int left = 0, right = nindexed - 1;
436 668a20f6 2020-03-18 stsp int cmp = 0, i = 0;
438 668a20f6 2020-03-18 stsp if (id0 > 0)
439 668a20f6 2020-03-18 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
441 668a20f6 2020-03-18 stsp while (left <= right) {
442 668a20f6 2020-03-18 stsp struct got_packidx_object_id *oid;
444 668a20f6 2020-03-18 stsp i = ((left + right) / 2);
445 668a20f6 2020-03-18 stsp oid = &packidx->hdr.sorted_ids[i];
447 668a20f6 2020-03-18 stsp cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
448 668a20f6 2020-03-18 stsp if (cmp == 0)
449 668a20f6 2020-03-18 stsp return -1; /* object already indexed */
450 668a20f6 2020-03-18 stsp else if (cmp > 0)
451 668a20f6 2020-03-18 stsp left = i + 1;
452 668a20f6 2020-03-18 stsp else if (cmp < 0)
453 668a20f6 2020-03-18 stsp right = i - 1;
456 668a20f6 2020-03-18 stsp return left;
460 668a20f6 2020-03-18 stsp static void
461 668a20f6 2020-03-18 stsp print_packidx(struct got_packidx *packidx)
463 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
466 668a20f6 2020-03-18 stsp fprintf(stderr, "object IDs:\n");
467 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
468 668a20f6 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
469 668a20f6 2020-03-18 stsp got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
470 668a20f6 2020-03-18 stsp hex, sizeof(hex));
471 668a20f6 2020-03-18 stsp fprintf(stderr, "%s\n", hex);
473 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
475 668a20f6 2020-03-18 stsp fprintf(stderr, "object offsets:\n");
476 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
477 668a20f6 2020-03-18 stsp uint32_t offset = be32toh(packidx->hdr.offsets[i]);
478 668a20f6 2020-03-18 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
479 668a20f6 2020-03-18 stsp int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
480 668a20f6 2020-03-18 stsp fprintf(stderr, "%u -> %llu\n", offset,
481 668a20f6 2020-03-18 stsp be64toh(packidx->hdr.large_offsets[j]));
483 668a20f6 2020-03-18 stsp fprintf(stderr, "%u\n", offset);
485 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
487 668a20f6 2020-03-18 stsp fprintf(stderr, "fanout table:");
488 668a20f6 2020-03-18 stsp for (i = 0; i <= 0xff; i++)
489 668a20f6 2020-03-18 stsp fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
490 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
494 668a20f6 2020-03-18 stsp static void
495 950de2cd 2020-03-18 stsp add_indexed_object(struct got_packidx *packidx, uint32_t idx,
496 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
500 668a20f6 2020-03-18 stsp memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
501 668a20f6 2020-03-18 stsp SHA1_DIGEST_LENGTH);
502 97684601 2020-03-18 stsp packidx->hdr.crc32[idx] = htobe32(obj->crc);
503 668a20f6 2020-03-18 stsp if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
504 668a20f6 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(obj->off);
506 7bad1537 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
507 668a20f6 2020-03-18 stsp GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
508 7bad1537 2020-03-18 stsp packidx->hdr.large_offsets[packidx->nlargeobj] =
509 7bad1537 2020-03-18 stsp htobe64(obj->off);
510 7bad1537 2020-03-18 stsp packidx->nlargeobj++;
513 668a20f6 2020-03-18 stsp for (i = obj->id.sha1[0]; i <= 0xff; i++) {
514 950de2cd 2020-03-18 stsp uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
515 668a20f6 2020-03-18 stsp packidx->hdr.fanout_table[i] = htobe32(n + 1);
520 8bb2b40c 2020-03-18 stsp indexed_obj_cmp(const void *pa, const void *pb)
522 8bb2b40c 2020-03-18 stsp struct got_indexed_object *a, *b;
524 262c582a 2020-03-18 stsp a = (struct got_indexed_object *)pa;
525 262c582a 2020-03-18 stsp b = (struct got_indexed_object *)pb;
526 8bb2b40c 2020-03-18 stsp return got_object_id_cmp(&a->id, &b->id);
529 950de2cd 2020-03-18 stsp static void
530 0fd91daf 2020-03-18 stsp make_packidx(struct got_packidx *packidx, int nobj,
531 262c582a 2020-03-18 stsp struct got_indexed_object *objects)
533 950de2cd 2020-03-18 stsp struct got_indexed_object *obj;
535 950de2cd 2020-03-18 stsp uint32_t idx = 0;
537 93bba072 2020-03-18 stsp qsort(objects, nobj, sizeof(struct got_indexed_object),
538 950de2cd 2020-03-18 stsp indexed_obj_cmp);
540 0fd91daf 2020-03-18 stsp memset(packidx->hdr.fanout_table, 0,
541 0fd91daf 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
542 0fd91daf 2020-03-18 stsp packidx->nlargeobj = 0;
544 950de2cd 2020-03-18 stsp for (i = 0; i < nobj; i++) {
545 262c582a 2020-03-18 stsp obj = &objects[i];
546 0fd91daf 2020-03-18 stsp if (obj->valid)
547 0fd91daf 2020-03-18 stsp add_indexed_object(packidx, idx++, obj);
551 950de2cd 2020-03-18 stsp static void
552 950de2cd 2020-03-18 stsp update_packidx(struct got_packidx *packidx, int nobj,
553 950de2cd 2020-03-18 stsp struct got_indexed_object *obj)
555 950de2cd 2020-03-18 stsp uint32_t idx;
556 950de2cd 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
558 950de2cd 2020-03-18 stsp idx = find_object_idx(packidx, obj->id.sha1);
559 950de2cd 2020-03-18 stsp if (idx == -1) {
560 950de2cd 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
561 950de2cd 2020-03-18 stsp got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
562 950de2cd 2020-03-18 stsp return; /* object already indexed */
565 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.sorted_ids[idx + 1],
566 950de2cd 2020-03-18 stsp &packidx->hdr.sorted_ids[idx],
567 950de2cd 2020-03-18 stsp sizeof(struct got_packidx_object_id) * (nindexed - idx));
568 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
569 950de2cd 2020-03-18 stsp sizeof(uint32_t) * (nindexed - idx));
571 950de2cd 2020-03-18 stsp add_indexed_object(packidx, idx, obj);
574 668a20f6 2020-03-18 stsp static const struct got_error *
575 4788f1ce 2020-03-18 stsp index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
576 d582f26c 2020-03-18 stsp FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_hash,
577 d582f26c 2020-03-18 stsp struct imsgbuf *ibuf)
579 668a20f6 2020-03-18 stsp const struct got_error *err;
580 668a20f6 2020-03-18 stsp struct got_packfile_hdr hdr;
581 668a20f6 2020-03-18 stsp struct got_packidx packidx;
582 668a20f6 2020-03-18 stsp char buf[8];
583 7bad1537 2020-03-18 stsp int nobj, nvalid, nloose, nresolved = 0, i;
584 262c582a 2020-03-18 stsp struct got_indexed_object *objects = NULL, *obj;
585 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
586 668a20f6 2020-03-18 stsp uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
587 668a20f6 2020-03-18 stsp ssize_t r, w;
588 160bbe2e 2020-03-18 stsp int pass, have_ref_deltas = 0, first_delta_idx = -1;
589 2e5a6fad 2020-03-18 stsp size_t mapoff = 0;
590 5672d305 2020-03-18 stsp int p_indexed = 0, last_p_indexed = -1;
591 5672d305 2020-03-18 stsp int p_resolved = 0, last_p_resolved = -1;
593 668a20f6 2020-03-18 stsp /* Check pack file header. */
594 2e5a6fad 2020-03-18 stsp if (pack->map) {
595 2e5a6fad 2020-03-18 stsp if (pack->filesize < sizeof(hdr))
596 2e5a6fad 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
597 2e5a6fad 2020-03-18 stsp "short packfile header");
598 2e5a6fad 2020-03-18 stsp memcpy(&hdr, pack->map, sizeof(hdr));
599 2e5a6fad 2020-03-18 stsp mapoff += sizeof(hdr);
601 2e5a6fad 2020-03-18 stsp r = read(pack->fd, &hdr, sizeof(hdr));
602 2e5a6fad 2020-03-18 stsp if (r == -1)
603 2e5a6fad 2020-03-18 stsp return got_error_from_errno("read");
604 2e5a6fad 2020-03-18 stsp if (r < sizeof(hdr))
605 2e5a6fad 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
606 2e5a6fad 2020-03-18 stsp "short packfile header");
609 668a20f6 2020-03-18 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
610 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
611 668a20f6 2020-03-18 stsp "bad packfile signature");
612 668a20f6 2020-03-18 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
613 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
614 668a20f6 2020-03-18 stsp "bad packfile version");
615 668a20f6 2020-03-18 stsp nobj = betoh32(hdr.nobjects);
616 668a20f6 2020-03-18 stsp if (nobj == 0)
617 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
618 668a20f6 2020-03-18 stsp "bad packfile with zero objects");
621 668a20f6 2020-03-18 stsp * Create an in-memory pack index which will grow as objects
622 668a20f6 2020-03-18 stsp * IDs in the pack file are discovered. Only fields used to
623 668a20f6 2020-03-18 stsp * read deltified objects will be needed by the pack.c library
624 668a20f6 2020-03-18 stsp * code, so setting up just a pack index header is sufficient.
626 668a20f6 2020-03-18 stsp memset(&packidx, 0, sizeof(packidx));
627 668a20f6 2020-03-18 stsp packidx.hdr.magic = malloc(sizeof(uint32_t));
628 668a20f6 2020-03-18 stsp if (packidx.hdr.magic == NULL)
629 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
630 668a20f6 2020-03-18 stsp *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
631 668a20f6 2020-03-18 stsp packidx.hdr.version = malloc(sizeof(uint32_t));
632 668a20f6 2020-03-18 stsp if (packidx.hdr.version == NULL) {
633 668a20f6 2020-03-18 stsp err = got_error_from_errno("malloc");
636 668a20f6 2020-03-18 stsp *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
637 668a20f6 2020-03-18 stsp packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
638 668a20f6 2020-03-18 stsp sizeof(uint32_t));
639 668a20f6 2020-03-18 stsp if (packidx.hdr.fanout_table == NULL) {
640 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
643 668a20f6 2020-03-18 stsp packidx.hdr.sorted_ids = calloc(nobj,
644 668a20f6 2020-03-18 stsp sizeof(struct got_packidx_object_id));
645 668a20f6 2020-03-18 stsp if (packidx.hdr.sorted_ids == NULL) {
646 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
649 97684601 2020-03-18 stsp packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
650 97684601 2020-03-18 stsp if (packidx.hdr.crc32 == NULL) {
651 97684601 2020-03-18 stsp err = got_error_from_errno("calloc");
654 668a20f6 2020-03-18 stsp packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
655 668a20f6 2020-03-18 stsp if (packidx.hdr.offsets == NULL) {
656 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
659 668a20f6 2020-03-18 stsp /* Large offsets table is empty for pack files < 2 GB. */
660 668a20f6 2020-03-18 stsp if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
661 668a20f6 2020-03-18 stsp packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
662 668a20f6 2020-03-18 stsp if (packidx.hdr.large_offsets == NULL) {
663 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
668 668a20f6 2020-03-18 stsp nvalid = 0;
669 668a20f6 2020-03-18 stsp nloose = 0;
670 262c582a 2020-03-18 stsp objects = calloc(nobj, sizeof(struct got_indexed_object));
671 668a20f6 2020-03-18 stsp if (objects == NULL)
672 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
675 668a20f6 2020-03-18 stsp * First pass: locate all objects and identify un-deltified objects.
677 668a20f6 2020-03-18 stsp * When this pass has completed we will know offset, type, size, and
678 668a20f6 2020-03-18 stsp * CRC information for all objects in this pack file. We won't know
679 668a20f6 2020-03-18 stsp * any of the actual object IDs of deltified objects yet since we
680 668a20f6 2020-03-18 stsp * will not yet attempt to combine deltas.
683 668a20f6 2020-03-18 stsp for (i = 0; i < nobj; i++) {
684 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
685 5672d305 2020-03-18 stsp p_indexed = ((i + 1) * 100) / nobj;
686 5672d305 2020-03-18 stsp if (p_indexed != last_p_indexed) {
687 5672d305 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf,
688 5672d305 2020-03-18 stsp nobj, i + 1, nloose, 0);
691 5672d305 2020-03-18 stsp last_p_indexed = p_indexed;
694 262c582a 2020-03-18 stsp obj = &objects[i];
695 1e87a3c3 2020-03-18 stsp obj->crc = crc32(0L, NULL, 0);
697 668a20f6 2020-03-18 stsp /* Store offset to type+size information for this object. */
698 2e5a6fad 2020-03-18 stsp if (pack->map) {
699 2e5a6fad 2020-03-18 stsp obj->off = mapoff;
701 2e5a6fad 2020-03-18 stsp obj->off = lseek(pack->fd, 0, SEEK_CUR);
702 2e5a6fad 2020-03-18 stsp if (obj->off == -1) {
703 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
708 4788f1ce 2020-03-18 stsp err = read_packed_object(pack, obj, tmpfile);
712 2e5a6fad 2020-03-18 stsp if (pack->map) {
713 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen + obj->len;
715 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
716 2e5a6fad 2020-03-18 stsp SEEK_SET) == -1) {
717 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
722 668a20f6 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_BLOB ||
723 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TREE ||
724 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_COMMIT ||
725 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TAG) {
726 262c582a 2020-03-18 stsp obj->valid = 1;
729 160bbe2e 2020-03-18 stsp if (first_delta_idx == -1)
730 160bbe2e 2020-03-18 stsp first_delta_idx = i;
731 160bbe2e 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
732 160bbe2e 2020-03-18 stsp have_ref_deltas = 1;
735 668a20f6 2020-03-18 stsp nvalid = nloose;
737 160bbe2e 2020-03-18 stsp if (first_delta_idx == -1)
738 160bbe2e 2020-03-18 stsp first_delta_idx = 0;
740 0fd91daf 2020-03-18 stsp /* In order to resolve ref deltas we need an in-progress pack index. */
741 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
742 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
745 668a20f6 2020-03-18 stsp * Second pass: We can now resolve deltas to compute the IDs of
746 668a20f6 2020-03-18 stsp * objects which appear in deltified form. Because deltas can be
747 668a20f6 2020-03-18 stsp * chained this pass may require a couple of iterations until all
748 668a20f6 2020-03-18 stsp * IDs of deltified objects have been discovered.
751 668a20f6 2020-03-18 stsp while (nvalid != nobj) {
754 e6c1173d 2020-03-18 stsp * This loop will only run once unless the pack file
755 e6c1173d 2020-03-18 stsp * contains ref deltas which refer to objects located
756 e6c1173d 2020-03-18 stsp * later in the pack file, which is unusual.
757 e6c1173d 2020-03-18 stsp * Offset deltas can always be resolved in one pass
758 e6c1173d 2020-03-18 stsp * unless the packfile is corrupt.
760 160bbe2e 2020-03-18 stsp for (i = first_delta_idx; i < nobj; i++) {
761 262c582a 2020-03-18 stsp obj = &objects[i];
762 262c582a 2020-03-18 stsp if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
763 262c582a 2020-03-18 stsp obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
766 262c582a 2020-03-18 stsp if (obj->valid)
769 b3e1118b 2020-03-18 stsp if (pack->map == NULL && lseek(pack->fd,
770 b3e1118b 2020-03-18 stsp obj->off + obj->tslen, SEEK_SET) == -1) {
771 b3e1118b 2020-03-18 stsp err = got_error_from_errno("lseek");
775 d582f26c 2020-03-18 stsp err = resolve_deltified_object(pack, &packidx, obj,
776 d582f26c 2020-03-18 stsp tmpfile, delta_base_file, delta_accum_file);
778 668a20f6 2020-03-18 stsp if (err->code != GOT_ERR_NO_OBJ)
781 668a20f6 2020-03-18 stsp * We cannot resolve this object yet because
782 668a20f6 2020-03-18 stsp * a delta base is unknown. Try again later.
787 262c582a 2020-03-18 stsp obj->valid = 1;
789 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
790 0fd91daf 2020-03-18 stsp update_packidx(&packidx, nobj, obj);
791 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
792 5672d305 2020-03-18 stsp p_resolved = ((nresolved + n) * 100) / nobj;
793 5672d305 2020-03-18 stsp if (p_resolved != last_p_resolved) {
794 5672d305 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf,
795 5672d305 2020-03-18 stsp nobj, nobj, nloose, nresolved + n);
798 5672d305 2020-03-18 stsp last_p_resolved = p_resolved;
802 668a20f6 2020-03-18 stsp if (pass++ > 3 && n == 0) {
803 668a20f6 2020-03-18 stsp static char msg[64];
804 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg), "could not resolve "
805 668a20f6 2020-03-18 stsp "any of deltas; packfile could be corrupt");
806 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
810 668a20f6 2020-03-18 stsp nresolved += n;
811 668a20f6 2020-03-18 stsp nvalid += nresolved;
814 668a20f6 2020-03-18 stsp if (nloose + nresolved != nobj) {
815 668a20f6 2020-03-18 stsp static char msg[64];
816 ec92f929 2020-03-18 stsp snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
817 cbc66309 2020-03-18 stsp nloose + nresolved, nobj);
818 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
822 021b0c6f 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
823 021b0c6f 2020-03-18 stsp nloose, nresolved);
827 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
829 97684601 2020-03-18 stsp free(objects);
830 97684601 2020-03-18 stsp objects = NULL;
832 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
833 b102634c 2020-03-18 stsp putbe32(buf, GOT_PACKIDX_V2_MAGIC);
834 b102634c 2020-03-18 stsp putbe32(buf + 4, GOT_PACKIDX_VERSION);
835 b102634c 2020-03-18 stsp err = hwrite(idxfd, buf, 8, &ctx);
838 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.fanout_table,
839 668a20f6 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
842 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.sorted_ids,
843 668a20f6 2020-03-18 stsp nobj * SHA1_DIGEST_LENGTH, &ctx);
846 97684601 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
849 cbc66309 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
853 7bad1537 2020-03-18 stsp if (packidx.nlargeobj > 0) {
854 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.large_offsets,
855 7bad1537 2020-03-18 stsp packidx.nlargeobj * sizeof(uint64_t), &ctx);
859 668a20f6 2020-03-18 stsp err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
863 668a20f6 2020-03-18 stsp SHA1Final(packidx_hash, &ctx);
864 668a20f6 2020-03-18 stsp w = write(idxfd, packidx_hash, sizeof(packidx_hash));
865 668a20f6 2020-03-18 stsp if (w == -1) {
866 668a20f6 2020-03-18 stsp err = got_error_from_errno("write");
869 668a20f6 2020-03-18 stsp if (w != sizeof(packidx_hash)) {
870 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_IO);
874 37ebab0a 2020-03-18 stsp free(objects);
875 668a20f6 2020-03-18 stsp free(packidx.hdr.magic);
876 668a20f6 2020-03-18 stsp free(packidx.hdr.version);
877 668a20f6 2020-03-18 stsp free(packidx.hdr.fanout_table);
878 668a20f6 2020-03-18 stsp free(packidx.hdr.sorted_ids);
879 668a20f6 2020-03-18 stsp free(packidx.hdr.offsets);
880 668a20f6 2020-03-18 stsp free(packidx.hdr.large_offsets);
881 668a20f6 2020-03-18 stsp return err;
885 93658fb9 2020-03-18 stsp main(int argc, char **argv)
887 668a20f6 2020-03-18 stsp const struct got_error *err = NULL, *close_err;
888 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
889 93658fb9 2020-03-18 stsp struct imsg imsg;
890 d582f26c 2020-03-18 stsp int idxfd = -1, tmpfd = -1, i;
891 d582f26c 2020-03-18 stsp FILE *tmpfiles[3];
892 668a20f6 2020-03-18 stsp struct got_pack pack;
893 668a20f6 2020-03-18 stsp uint8_t pack_hash[SHA1_DIGEST_LENGTH];
894 668a20f6 2020-03-18 stsp off_t packfile_size;
896 668a20f6 2020-03-18 stsp static int attached;
897 668a20f6 2020-03-18 stsp while (!attached)
901 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++)
902 d582f26c 2020-03-18 stsp tmpfiles[i] = NULL;
904 668a20f6 2020-03-18 stsp memset(&pack, 0, sizeof(pack));
905 668a20f6 2020-03-18 stsp pack.fd = -1;
906 18d4da03 2020-03-18 stsp pack.delta_cache = got_delta_cache_alloc(500,
907 668a20f6 2020-03-18 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
908 668a20f6 2020-03-18 stsp if (pack.delta_cache == NULL) {
909 668a20f6 2020-03-18 stsp err = got_error_from_errno("got_delta_cache_alloc");
913 668a20f6 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
914 861f3006 2020-03-18 stsp #ifndef PROFILE
915 861f3006 2020-03-18 stsp /* revoke access to most system calls */
916 861f3006 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
917 861f3006 2020-03-18 stsp err = got_error_from_errno("pledge");
918 861f3006 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
922 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
925 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
927 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
928 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
931 668a20f6 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
932 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
935 668a20f6 2020-03-18 stsp memcpy(pack_hash, imsg.data, sizeof(pack_hash));
936 668a20f6 2020-03-18 stsp pack.fd = imsg.fd;
938 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
941 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
943 73ab1060 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
944 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
947 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
948 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
951 93658fb9 2020-03-18 stsp idxfd = imsg.fd;
953 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
954 d582f26c 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
957 d582f26c 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
959 d582f26c 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
960 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
963 d582f26c 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
964 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
967 d582f26c 2020-03-18 stsp tmpfd = imsg.fd;
968 d582f26c 2020-03-18 stsp tmpfiles[i] = fdopen(tmpfd, "w+");
969 d582f26c 2020-03-18 stsp if (tmpfiles[i] == NULL) {
970 d582f26c 2020-03-18 stsp err = got_error_from_errno("fdopen");
973 d582f26c 2020-03-18 stsp tmpfd = -1;
976 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_END) == -1) {
977 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
980 668a20f6 2020-03-18 stsp packfile_size = lseek(pack.fd, 0, SEEK_CUR);
981 668a20f6 2020-03-18 stsp if (packfile_size == -1) {
982 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
985 668a20f6 2020-03-18 stsp pack.filesize = packfile_size; /* XXX off_t vs size_t */
987 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_SET) == -1) {
988 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
992 2e5a6fad 2020-03-18 stsp #ifndef GOT_PACK_NO_MMAP
993 2e5a6fad 2020-03-18 stsp pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
994 2e5a6fad 2020-03-18 stsp pack.fd, 0);
995 2e5a6fad 2020-03-18 stsp if (pack.map == MAP_FAILED)
996 2e5a6fad 2020-03-18 stsp pack.map = NULL; /* fall back to read(2) */
998 d582f26c 2020-03-18 stsp err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
999 d582f26c 2020-03-18 stsp pack_hash, &ibuf);
1001 668a20f6 2020-03-18 stsp close_err = got_pack_close(&pack);
1002 668a20f6 2020-03-18 stsp if (close_err && err == NULL)
1003 668a20f6 2020-03-18 stsp err = close_err;
1004 668a20f6 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1005 668a20f6 2020-03-18 stsp err = got_error_from_errno("close");
1006 4788f1ce 2020-03-18 stsp if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1007 4788f1ce 2020-03-18 stsp err = got_error_from_errno("close");
1008 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
1009 d582f26c 2020-03-18 stsp if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1010 d582f26c 2020-03-18 stsp err == NULL)
1011 d582f26c 2020-03-18 stsp err = got_error_from_errno("close");
1014 668a20f6 2020-03-18 stsp if (err == NULL)
1015 93658fb9 2020-03-18 stsp err = got_privsep_send_index_pack_done(&ibuf);
1016 668a20f6 2020-03-18 stsp if (err) {
1017 668a20f6 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1018 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1019 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);