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 668a20f6 2020-03-18 stsp read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
150 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
151 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
152 668a20f6 2020-03-18 stsp uint8_t *data;
153 668a20f6 2020-03-18 stsp size_t datalen;
155 668a20f6 2020-03-18 stsp char *header;
156 668a20f6 2020-03-18 stsp size_t headerlen;
157 668a20f6 2020-03-18 stsp const char *obj_label;
158 2e5a6fad 2020-03-18 stsp size_t mapoff = obj->off;
160 cbc66309 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
161 cbc66309 2020-03-18 stsp &obj->tslen, pack, obj->off);
163 668a20f6 2020-03-18 stsp return err;
165 2e5a6fad 2020-03-18 stsp if (pack->map) {
166 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
167 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen;
169 2e5a6fad 2020-03-18 stsp /* XXX Seek back and get the CRC of on-disk type+size bytes. */
170 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
171 2e5a6fad 2020-03-18 stsp return got_error_from_errno("lseek");
172 2e5a6fad 2020-03-18 stsp err = read_crc(&obj->crc, pack->fd, obj->tslen);
174 2e5a6fad 2020-03-18 stsp return err;
177 668a20f6 2020-03-18 stsp switch (obj->type) {
178 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
179 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
180 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
181 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
182 668a20f6 2020-03-18 stsp /* XXX TODO reading large objects into memory is bad! */
183 2e5a6fad 2020-03-18 stsp if (pack->map) {
184 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(&data, &datalen,
185 2e5a6fad 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
186 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
188 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(&data, &datalen,
189 2e5a6fad 2020-03-18 stsp &obj->len, &obj->crc, obj->size, pack->fd);
193 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
194 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, obj->type);
196 7cd14ea0 2020-03-18 stsp free(data);
199 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
200 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
201 668a20f6 2020-03-18 stsp free(data);
204 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
205 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
206 668a20f6 2020-03-18 stsp SHA1Update(&ctx, data, datalen);
207 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
208 668a20f6 2020-03-18 stsp free(header);
209 668a20f6 2020-03-18 stsp free(data);
211 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_REF_DELTA:
212 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
213 2e5a6fad 2020-03-18 stsp if (pack->map) {
214 2e5a6fad 2020-03-18 stsp if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
215 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
218 2e5a6fad 2020-03-18 stsp memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
219 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
220 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
221 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
222 2e5a6fad 2020-03-18 stsp mapoff += SHA1_DIGEST_LENGTH;
223 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
224 2e5a6fad 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
225 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
229 2e5a6fad 2020-03-18 stsp n = read(pack->fd, obj->delta.ref.ref_id.sha1,
230 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
231 2e5a6fad 2020-03-18 stsp if (n == -1) {
232 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("read");
235 2e5a6fad 2020-03-18 stsp if (n < sizeof(obj->id)) {
236 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
239 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
240 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
241 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
242 2e5a6fad 2020-03-18 stsp &obj->crc, obj->size, pack->fd);
246 668a20f6 2020-03-18 stsp obj->len += SHA1_DIGEST_LENGTH;
248 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
249 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
250 836f2c92 2020-03-18 stsp err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
251 836f2c92 2020-03-18 stsp &obj->delta.ofs.base_offsetlen, pack, obj->off,
252 836f2c92 2020-03-18 stsp obj->tslen);
256 2e5a6fad 2020-03-18 stsp if (pack->map) {
257 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
258 2e5a6fad 2020-03-18 stsp obj->delta.ofs.base_offsetlen);
259 2e5a6fad 2020-03-18 stsp mapoff += obj->delta.ofs.base_offsetlen;
260 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
261 2e5a6fad 2020-03-18 stsp &obj->len, &obj->crc, pack->map, mapoff,
262 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
267 2e5a6fad 2020-03-18 stsp * XXX Seek back and get the CRC of on-disk
268 2e5a6fad 2020-03-18 stsp * offset bytes.
270 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
272 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
275 2e5a6fad 2020-03-18 stsp err = read_crc(&obj->crc, pack->fd,
276 2e5a6fad 2020-03-18 stsp obj->delta.ofs.base_offsetlen);
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 836f2c92 2020-03-18 stsp obj->len += obj->delta.ofs.base_offsetlen;
288 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
292 668a20f6 2020-03-18 stsp return err;
295 668a20f6 2020-03-18 stsp static const struct got_error *
296 668a20f6 2020-03-18 stsp hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
300 668a20f6 2020-03-18 stsp SHA1Update(ctx, buf, len);
302 668a20f6 2020-03-18 stsp w = write(fd, buf, len);
303 668a20f6 2020-03-18 stsp if (w == -1)
304 668a20f6 2020-03-18 stsp return got_error_from_errno("write");
305 668a20f6 2020-03-18 stsp if (w != len)
306 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_IO);
308 668a20f6 2020-03-18 stsp return NULL;
311 668a20f6 2020-03-18 stsp static const struct got_error *
312 668a20f6 2020-03-18 stsp resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
313 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
315 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
316 668a20f6 2020-03-18 stsp struct got_delta_chain deltas;
317 668a20f6 2020-03-18 stsp struct got_delta *delta;
318 668a20f6 2020-03-18 stsp uint8_t *buf = NULL;
319 668a20f6 2020-03-18 stsp size_t len;
320 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
321 4d0fef1d 2020-03-18 stsp char *header = NULL;
322 668a20f6 2020-03-18 stsp size_t headerlen;
323 668a20f6 2020-03-18 stsp int base_obj_type;
324 668a20f6 2020-03-18 stsp const char *obj_label;
326 668a20f6 2020-03-18 stsp deltas.nentries = 0;
327 668a20f6 2020-03-18 stsp SIMPLEQ_INIT(&deltas.entries);
329 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
330 668a20f6 2020-03-18 stsp obj->off, obj->tslen, obj->type, obj->size,
331 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
335 668a20f6 2020-03-18 stsp /* XXX TODO reading large objects into memory is bad! */
336 668a20f6 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
340 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
342 668a20f6 2020-03-18 stsp err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
345 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, base_obj_type);
348 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
349 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
352 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
353 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
354 668a20f6 2020-03-18 stsp SHA1Update(&ctx, buf, len);
355 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
358 4d0fef1d 2020-03-18 stsp free(header);
359 668a20f6 2020-03-18 stsp while (!SIMPLEQ_EMPTY(&deltas.entries)) {
360 668a20f6 2020-03-18 stsp delta = SIMPLEQ_FIRST(&deltas.entries);
361 668a20f6 2020-03-18 stsp SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
362 668a20f6 2020-03-18 stsp free(delta);
364 668a20f6 2020-03-18 stsp return err;
367 668a20f6 2020-03-18 stsp /* Determine the slot in the pack index a given object ID should use. */
369 668a20f6 2020-03-18 stsp find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
371 668a20f6 2020-03-18 stsp u_int8_t id0 = sha1[0];
372 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
373 668a20f6 2020-03-18 stsp int left = 0, right = nindexed - 1;
374 668a20f6 2020-03-18 stsp int cmp = 0, i = 0;
376 668a20f6 2020-03-18 stsp if (id0 > 0)
377 668a20f6 2020-03-18 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
379 668a20f6 2020-03-18 stsp while (left <= right) {
380 668a20f6 2020-03-18 stsp struct got_packidx_object_id *oid;
382 668a20f6 2020-03-18 stsp i = ((left + right) / 2);
383 668a20f6 2020-03-18 stsp oid = &packidx->hdr.sorted_ids[i];
385 668a20f6 2020-03-18 stsp cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
386 668a20f6 2020-03-18 stsp if (cmp == 0)
387 668a20f6 2020-03-18 stsp return -1; /* object already indexed */
388 668a20f6 2020-03-18 stsp else if (cmp > 0)
389 668a20f6 2020-03-18 stsp left = i + 1;
390 668a20f6 2020-03-18 stsp else if (cmp < 0)
391 668a20f6 2020-03-18 stsp right = i - 1;
394 668a20f6 2020-03-18 stsp return left;
398 668a20f6 2020-03-18 stsp static void
399 668a20f6 2020-03-18 stsp print_packidx(struct got_packidx *packidx)
401 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
404 668a20f6 2020-03-18 stsp fprintf(stderr, "object IDs:\n");
405 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
406 668a20f6 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
407 668a20f6 2020-03-18 stsp got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
408 668a20f6 2020-03-18 stsp hex, sizeof(hex));
409 668a20f6 2020-03-18 stsp fprintf(stderr, "%s\n", hex);
411 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
413 668a20f6 2020-03-18 stsp fprintf(stderr, "object offsets:\n");
414 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
415 668a20f6 2020-03-18 stsp uint32_t offset = be32toh(packidx->hdr.offsets[i]);
416 668a20f6 2020-03-18 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
417 668a20f6 2020-03-18 stsp int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
418 668a20f6 2020-03-18 stsp fprintf(stderr, "%u -> %llu\n", offset,
419 668a20f6 2020-03-18 stsp be64toh(packidx->hdr.large_offsets[j]));
421 668a20f6 2020-03-18 stsp fprintf(stderr, "%u\n", offset);
423 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
425 668a20f6 2020-03-18 stsp fprintf(stderr, "fanout table:");
426 668a20f6 2020-03-18 stsp for (i = 0; i <= 0xff; i++)
427 668a20f6 2020-03-18 stsp fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
428 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
432 668a20f6 2020-03-18 stsp static void
433 950de2cd 2020-03-18 stsp add_indexed_object(struct got_packidx *packidx, uint32_t idx,
434 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
438 668a20f6 2020-03-18 stsp memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
439 668a20f6 2020-03-18 stsp SHA1_DIGEST_LENGTH);
440 668a20f6 2020-03-18 stsp if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
441 668a20f6 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(obj->off);
443 7bad1537 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
444 668a20f6 2020-03-18 stsp GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
445 7bad1537 2020-03-18 stsp packidx->hdr.large_offsets[packidx->nlargeobj] =
446 7bad1537 2020-03-18 stsp htobe64(obj->off);
447 7bad1537 2020-03-18 stsp packidx->nlargeobj++;
450 668a20f6 2020-03-18 stsp for (i = obj->id.sha1[0]; i <= 0xff; i++) {
451 950de2cd 2020-03-18 stsp uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
452 668a20f6 2020-03-18 stsp packidx->hdr.fanout_table[i] = htobe32(n + 1);
457 8bb2b40c 2020-03-18 stsp indexed_obj_cmp(const void *pa, const void *pb)
459 8bb2b40c 2020-03-18 stsp struct got_indexed_object *a, *b;
461 262c582a 2020-03-18 stsp a = (struct got_indexed_object *)pa;
462 262c582a 2020-03-18 stsp b = (struct got_indexed_object *)pb;
463 8bb2b40c 2020-03-18 stsp return got_object_id_cmp(&a->id, &b->id);
466 950de2cd 2020-03-18 stsp static void
467 0fd91daf 2020-03-18 stsp make_packidx(struct got_packidx *packidx, int nobj,
468 262c582a 2020-03-18 stsp struct got_indexed_object *objects)
470 950de2cd 2020-03-18 stsp struct got_indexed_object *obj;
472 950de2cd 2020-03-18 stsp uint32_t idx = 0;
474 262c582a 2020-03-18 stsp mergesort(objects, nobj, sizeof(struct got_indexed_object),
475 950de2cd 2020-03-18 stsp indexed_obj_cmp);
477 0fd91daf 2020-03-18 stsp memset(packidx->hdr.fanout_table, 0,
478 0fd91daf 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
479 0fd91daf 2020-03-18 stsp packidx->nlargeobj = 0;
481 950de2cd 2020-03-18 stsp for (i = 0; i < nobj; i++) {
482 262c582a 2020-03-18 stsp obj = &objects[i];
483 0fd91daf 2020-03-18 stsp if (obj->valid)
484 0fd91daf 2020-03-18 stsp add_indexed_object(packidx, idx++, obj);
488 950de2cd 2020-03-18 stsp static void
489 950de2cd 2020-03-18 stsp update_packidx(struct got_packidx *packidx, int nobj,
490 950de2cd 2020-03-18 stsp struct got_indexed_object *obj)
492 950de2cd 2020-03-18 stsp uint32_t idx;
493 950de2cd 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
495 950de2cd 2020-03-18 stsp idx = find_object_idx(packidx, obj->id.sha1);
496 950de2cd 2020-03-18 stsp if (idx == -1) {
497 950de2cd 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
498 950de2cd 2020-03-18 stsp got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
499 950de2cd 2020-03-18 stsp return; /* object already indexed */
502 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.sorted_ids[idx + 1],
503 950de2cd 2020-03-18 stsp &packidx->hdr.sorted_ids[idx],
504 950de2cd 2020-03-18 stsp sizeof(struct got_packidx_object_id) * (nindexed - idx));
505 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
506 950de2cd 2020-03-18 stsp sizeof(uint32_t) * (nindexed - idx));
508 950de2cd 2020-03-18 stsp add_indexed_object(packidx, idx, obj);
511 668a20f6 2020-03-18 stsp static const struct got_error *
512 668a20f6 2020-03-18 stsp index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
513 668a20f6 2020-03-18 stsp struct imsgbuf *ibuf)
515 668a20f6 2020-03-18 stsp const struct got_error *err;
516 668a20f6 2020-03-18 stsp struct got_packfile_hdr hdr;
517 668a20f6 2020-03-18 stsp struct got_packidx packidx;
518 668a20f6 2020-03-18 stsp char buf[8];
519 7bad1537 2020-03-18 stsp int nobj, nvalid, nloose, nresolved = 0, i;
520 262c582a 2020-03-18 stsp struct got_indexed_object *objects = NULL, *obj;
521 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
522 668a20f6 2020-03-18 stsp uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
523 668a20f6 2020-03-18 stsp ssize_t r, w;
524 0fd91daf 2020-03-18 stsp int pass, have_ref_deltas = 0;
525 2e5a6fad 2020-03-18 stsp size_t mapoff = 0;
527 668a20f6 2020-03-18 stsp /* Check pack file header. */
528 2e5a6fad 2020-03-18 stsp if (pack->map) {
529 2e5a6fad 2020-03-18 stsp if (pack->filesize < sizeof(hdr))
530 2e5a6fad 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
531 2e5a6fad 2020-03-18 stsp "short packfile header");
532 2e5a6fad 2020-03-18 stsp memcpy(&hdr, pack->map, sizeof(hdr));
533 2e5a6fad 2020-03-18 stsp mapoff += sizeof(hdr);
535 2e5a6fad 2020-03-18 stsp r = read(pack->fd, &hdr, sizeof(hdr));
536 2e5a6fad 2020-03-18 stsp if (r == -1)
537 2e5a6fad 2020-03-18 stsp return got_error_from_errno("read");
538 2e5a6fad 2020-03-18 stsp if (r < sizeof(hdr))
539 2e5a6fad 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
540 2e5a6fad 2020-03-18 stsp "short packfile header");
543 668a20f6 2020-03-18 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
544 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
545 668a20f6 2020-03-18 stsp "bad packfile signature");
546 668a20f6 2020-03-18 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
547 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
548 668a20f6 2020-03-18 stsp "bad packfile version");
549 668a20f6 2020-03-18 stsp nobj = betoh32(hdr.nobjects);
550 668a20f6 2020-03-18 stsp if (nobj == 0)
551 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
552 668a20f6 2020-03-18 stsp "bad packfile with zero objects");
555 668a20f6 2020-03-18 stsp * Create an in-memory pack index which will grow as objects
556 668a20f6 2020-03-18 stsp * IDs in the pack file are discovered. Only fields used to
557 668a20f6 2020-03-18 stsp * read deltified objects will be needed by the pack.c library
558 668a20f6 2020-03-18 stsp * code, so setting up just a pack index header is sufficient.
560 668a20f6 2020-03-18 stsp memset(&packidx, 0, sizeof(packidx));
561 668a20f6 2020-03-18 stsp packidx.hdr.magic = malloc(sizeof(uint32_t));
562 668a20f6 2020-03-18 stsp if (packidx.hdr.magic == NULL)
563 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
564 668a20f6 2020-03-18 stsp *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
565 668a20f6 2020-03-18 stsp packidx.hdr.version = malloc(sizeof(uint32_t));
566 668a20f6 2020-03-18 stsp if (packidx.hdr.version == NULL) {
567 668a20f6 2020-03-18 stsp err = got_error_from_errno("malloc");
570 668a20f6 2020-03-18 stsp *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
571 668a20f6 2020-03-18 stsp packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
572 668a20f6 2020-03-18 stsp sizeof(uint32_t));
573 668a20f6 2020-03-18 stsp if (packidx.hdr.fanout_table == NULL) {
574 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
577 668a20f6 2020-03-18 stsp packidx.hdr.sorted_ids = calloc(nobj,
578 668a20f6 2020-03-18 stsp sizeof(struct got_packidx_object_id));
579 668a20f6 2020-03-18 stsp if (packidx.hdr.sorted_ids == NULL) {
580 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
583 668a20f6 2020-03-18 stsp packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
584 668a20f6 2020-03-18 stsp if (packidx.hdr.offsets == NULL) {
585 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
588 668a20f6 2020-03-18 stsp /* Large offsets table is empty for pack files < 2 GB. */
589 668a20f6 2020-03-18 stsp if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
590 668a20f6 2020-03-18 stsp packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
591 668a20f6 2020-03-18 stsp if (packidx.hdr.large_offsets == NULL) {
592 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
597 668a20f6 2020-03-18 stsp nvalid = 0;
598 668a20f6 2020-03-18 stsp nloose = 0;
599 262c582a 2020-03-18 stsp objects = calloc(nobj, sizeof(struct got_indexed_object));
600 668a20f6 2020-03-18 stsp if (objects == NULL)
601 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
604 668a20f6 2020-03-18 stsp * First pass: locate all objects and identify un-deltified objects.
606 668a20f6 2020-03-18 stsp * When this pass has completed we will know offset, type, size, and
607 668a20f6 2020-03-18 stsp * CRC information for all objects in this pack file. We won't know
608 668a20f6 2020-03-18 stsp * any of the actual object IDs of deltified objects yet since we
609 668a20f6 2020-03-18 stsp * will not yet attempt to combine deltas.
612 668a20f6 2020-03-18 stsp for (i = 0; i < nobj; i++) {
613 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
614 668a20f6 2020-03-18 stsp nloose, 0);
618 262c582a 2020-03-18 stsp obj = &objects[i];
619 1e87a3c3 2020-03-18 stsp obj->crc = crc32(0L, NULL, 0);
621 668a20f6 2020-03-18 stsp /* Store offset to type+size information for this object. */
622 2e5a6fad 2020-03-18 stsp if (pack->map) {
623 2e5a6fad 2020-03-18 stsp obj->off = mapoff;
625 2e5a6fad 2020-03-18 stsp obj->off = lseek(pack->fd, 0, SEEK_CUR);
626 2e5a6fad 2020-03-18 stsp if (obj->off == -1) {
627 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
632 668a20f6 2020-03-18 stsp err = read_packed_object(pack, obj);
636 2e5a6fad 2020-03-18 stsp if (pack->map) {
637 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen + obj->len;
639 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
640 2e5a6fad 2020-03-18 stsp SEEK_SET) == -1) {
641 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
646 668a20f6 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_BLOB ||
647 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TREE ||
648 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_COMMIT ||
649 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TAG) {
650 262c582a 2020-03-18 stsp obj->valid = 1;
652 0fd91daf 2020-03-18 stsp } else if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
653 0fd91daf 2020-03-18 stsp have_ref_deltas = 1;
655 668a20f6 2020-03-18 stsp nvalid = nloose;
657 0fd91daf 2020-03-18 stsp /* In order to resolve ref deltas we need an in-progress pack index. */
658 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
659 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
662 668a20f6 2020-03-18 stsp * Second pass: We can now resolve deltas to compute the IDs of
663 668a20f6 2020-03-18 stsp * objects which appear in deltified form. Because deltas can be
664 668a20f6 2020-03-18 stsp * chained this pass may require a couple of iterations until all
665 668a20f6 2020-03-18 stsp * IDs of deltified objects have been discovered.
668 668a20f6 2020-03-18 stsp while (nvalid != nobj) {
670 668a20f6 2020-03-18 stsp for (i = 0; i < nobj; i++) {
671 262c582a 2020-03-18 stsp obj = &objects[i];
672 262c582a 2020-03-18 stsp if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
673 262c582a 2020-03-18 stsp obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
676 262c582a 2020-03-18 stsp if (obj->valid)
679 2e5a6fad 2020-03-18 stsp if (pack->map == NULL &&
680 2e5a6fad 2020-03-18 stsp lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
682 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
686 668a20f6 2020-03-18 stsp err = resolve_deltified_object(pack, &packidx, obj);
688 668a20f6 2020-03-18 stsp if (err->code != GOT_ERR_NO_OBJ)
691 668a20f6 2020-03-18 stsp * We cannot resolve this object yet because
692 668a20f6 2020-03-18 stsp * a delta base is unknown. Try again later.
697 262c582a 2020-03-18 stsp obj->valid = 1;
699 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
700 0fd91daf 2020-03-18 stsp update_packidx(&packidx, nobj, obj);
701 cbc66309 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf,
702 cbc66309 2020-03-18 stsp nobj, nobj, nloose, nresolved + n);
707 668a20f6 2020-03-18 stsp if (pass++ > 3 && n == 0) {
708 668a20f6 2020-03-18 stsp static char msg[64];
709 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg), "could not resolve "
710 668a20f6 2020-03-18 stsp "any of deltas; packfile could be corrupt");
711 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
715 668a20f6 2020-03-18 stsp if (nloose + nresolved == nobj) {
716 668a20f6 2020-03-18 stsp static char msg[64];
717 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg),
718 cbc66309 2020-03-18 stsp "fix point reached too early: %d/%d/%d",
719 cbc66309 2020-03-18 stsp nvalid, nresolved, nobj);
720 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
723 668a20f6 2020-03-18 stsp nresolved += n;
724 668a20f6 2020-03-18 stsp nvalid += nresolved;
727 668a20f6 2020-03-18 stsp if (nloose + nresolved != nobj) {
728 668a20f6 2020-03-18 stsp static char msg[64];
729 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg),
730 cbc66309 2020-03-18 stsp "discovered only %d of %d objects",
731 cbc66309 2020-03-18 stsp nloose + nresolved, nobj);
732 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
736 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
738 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
739 b102634c 2020-03-18 stsp putbe32(buf, GOT_PACKIDX_V2_MAGIC);
740 b102634c 2020-03-18 stsp putbe32(buf + 4, GOT_PACKIDX_VERSION);
741 b102634c 2020-03-18 stsp err = hwrite(idxfd, buf, 8, &ctx);
744 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.fanout_table,
745 668a20f6 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
748 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.sorted_ids,
749 668a20f6 2020-03-18 stsp nobj * SHA1_DIGEST_LENGTH, &ctx);
752 93658fb9 2020-03-18 stsp for(i = 0; i < nobj; i++){
753 b102634c 2020-03-18 stsp putbe32(buf, objects[i].crc);
754 668a20f6 2020-03-18 stsp err = hwrite(idxfd, buf, 4, &ctx);
758 cbc66309 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
762 7bad1537 2020-03-18 stsp if (packidx.nlargeobj > 0) {
763 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.large_offsets,
764 7bad1537 2020-03-18 stsp packidx.nlargeobj * sizeof(uint64_t), &ctx);
768 668a20f6 2020-03-18 stsp err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
772 668a20f6 2020-03-18 stsp SHA1Final(packidx_hash, &ctx);
773 668a20f6 2020-03-18 stsp w = write(idxfd, packidx_hash, sizeof(packidx_hash));
774 668a20f6 2020-03-18 stsp if (w == -1) {
775 668a20f6 2020-03-18 stsp err = got_error_from_errno("write");
778 668a20f6 2020-03-18 stsp if (w != sizeof(packidx_hash)) {
779 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_IO);
783 37ebab0a 2020-03-18 stsp free(objects);
784 668a20f6 2020-03-18 stsp free(packidx.hdr.magic);
785 668a20f6 2020-03-18 stsp free(packidx.hdr.version);
786 668a20f6 2020-03-18 stsp free(packidx.hdr.fanout_table);
787 668a20f6 2020-03-18 stsp free(packidx.hdr.sorted_ids);
788 668a20f6 2020-03-18 stsp free(packidx.hdr.offsets);
789 668a20f6 2020-03-18 stsp free(packidx.hdr.large_offsets);
790 668a20f6 2020-03-18 stsp return err;
794 93658fb9 2020-03-18 stsp main(int argc, char **argv)
796 668a20f6 2020-03-18 stsp const struct got_error *err = NULL, *close_err;
797 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
798 93658fb9 2020-03-18 stsp struct imsg imsg;
799 668a20f6 2020-03-18 stsp int idxfd = -1;
800 668a20f6 2020-03-18 stsp struct got_pack pack;
801 668a20f6 2020-03-18 stsp uint8_t pack_hash[SHA1_DIGEST_LENGTH];
802 668a20f6 2020-03-18 stsp off_t packfile_size;
804 668a20f6 2020-03-18 stsp static int attached;
805 668a20f6 2020-03-18 stsp while (!attached)
809 668a20f6 2020-03-18 stsp memset(&pack, 0, sizeof(pack));
810 668a20f6 2020-03-18 stsp pack.fd = -1;
811 668a20f6 2020-03-18 stsp pack.delta_cache = got_delta_cache_alloc(100,
812 668a20f6 2020-03-18 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
813 668a20f6 2020-03-18 stsp if (pack.delta_cache == NULL) {
814 668a20f6 2020-03-18 stsp err = got_error_from_errno("got_delta_cache_alloc");
818 668a20f6 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
819 861f3006 2020-03-18 stsp #ifndef PROFILE
820 861f3006 2020-03-18 stsp /* revoke access to most system calls */
821 861f3006 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
822 861f3006 2020-03-18 stsp err = got_error_from_errno("pledge");
823 861f3006 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
827 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
830 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
832 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
833 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
836 668a20f6 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
837 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
840 668a20f6 2020-03-18 stsp memcpy(pack_hash, imsg.data, sizeof(pack_hash));
841 668a20f6 2020-03-18 stsp pack.fd = imsg.fd;
843 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
846 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
848 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
849 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
852 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
853 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
856 93658fb9 2020-03-18 stsp idxfd = imsg.fd;
858 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_END) == -1) {
859 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
862 668a20f6 2020-03-18 stsp packfile_size = lseek(pack.fd, 0, SEEK_CUR);
863 668a20f6 2020-03-18 stsp if (packfile_size == -1) {
864 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
867 668a20f6 2020-03-18 stsp pack.filesize = packfile_size; /* XXX off_t vs size_t */
869 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_SET) == -1) {
870 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
874 2e5a6fad 2020-03-18 stsp #ifndef GOT_PACK_NO_MMAP
875 2e5a6fad 2020-03-18 stsp pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
876 2e5a6fad 2020-03-18 stsp pack.fd, 0);
877 2e5a6fad 2020-03-18 stsp if (pack.map == MAP_FAILED)
878 2e5a6fad 2020-03-18 stsp pack.map = NULL; /* fall back to read(2) */
880 668a20f6 2020-03-18 stsp err = index_pack(&pack, idxfd, pack_hash, &ibuf);
882 668a20f6 2020-03-18 stsp close_err = got_pack_close(&pack);
883 668a20f6 2020-03-18 stsp if (close_err && err == NULL)
884 668a20f6 2020-03-18 stsp err = close_err;
885 668a20f6 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
886 668a20f6 2020-03-18 stsp err = got_error_from_errno("close");
888 668a20f6 2020-03-18 stsp if (err == NULL)
889 93658fb9 2020-03-18 stsp err = got_privsep_send_index_pack_done(&ibuf);
891 668a20f6 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
892 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
893 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);