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>
25 93658fb9 2020-03-18 stsp #include <stdint.h>
26 93658fb9 2020-03-18 stsp #include <errno.h>
27 93658fb9 2020-03-18 stsp #include <imsg.h>
28 93658fb9 2020-03-18 stsp #include <limits.h>
29 93658fb9 2020-03-18 stsp #include <signal.h>
30 93658fb9 2020-03-18 stsp #include <stdio.h>
31 93658fb9 2020-03-18 stsp #include <stdlib.h>
32 93658fb9 2020-03-18 stsp #include <string.h>
33 93658fb9 2020-03-18 stsp #include <ctype.h>
34 93658fb9 2020-03-18 stsp #include <sha1.h>
35 93658fb9 2020-03-18 stsp #include <fcntl.h>
36 93658fb9 2020-03-18 stsp #include <zlib.h>
37 93658fb9 2020-03-18 stsp #include <err.h>
38 93658fb9 2020-03-18 stsp #include <assert.h>
39 93658fb9 2020-03-18 stsp #include <dirent.h>
41 93658fb9 2020-03-18 stsp #include "got_error.h"
42 93658fb9 2020-03-18 stsp #include "got_object.h"
44 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
45 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
46 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
47 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
48 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_object_idset.h"
50 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
51 668a20f6 2020-03-18 stsp #include "got_lib_pack.h"
52 668a20f6 2020-03-18 stsp #include "got_lib_delta_cache.h"
54 668a20f6 2020-03-18 stsp struct got_indexed_object {
55 668a20f6 2020-03-18 stsp struct got_object_id id;
58 668a20f6 2020-03-18 stsp * Has this object been fully resolved?
59 668a20f6 2020-03-18 stsp * If so, we know its ID, otherwise we don't and 'id' is invalid.
63 668a20f6 2020-03-18 stsp /* Offset of type+size field for this object in pack file. */
66 668a20f6 2020-03-18 stsp /* Type+size values parsed from pack file. */
67 668a20f6 2020-03-18 stsp uint8_t type;
68 668a20f6 2020-03-18 stsp uint64_t size;
70 668a20f6 2020-03-18 stsp /* Length of on-disk type+size data. */
71 668a20f6 2020-03-18 stsp size_t tslen;
73 668a20f6 2020-03-18 stsp /* Length of object data following type+size. */
76 668a20f6 2020-03-18 stsp uint32_t crc;
78 668a20f6 2020-03-18 stsp /* For ref deltas. */
79 668a20f6 2020-03-18 stsp struct got_object_id ref_id;
81 668a20f6 2020-03-18 stsp /* For offset deltas. */
82 668a20f6 2020-03-18 stsp off_t base_offset;
83 668a20f6 2020-03-18 stsp size_t base_offsetlen;
86 93658fb9 2020-03-18 stsp #define PUTBE32(b, n)\
88 93658fb9 2020-03-18 stsp (b)[0] = (n) >> 24; \
89 93658fb9 2020-03-18 stsp (b)[1] = (n) >> 16; \
90 93658fb9 2020-03-18 stsp (b)[2] = (n) >> 8; \
91 93658fb9 2020-03-18 stsp (b)[3] = (n) >> 0; \
94 93658fb9 2020-03-18 stsp #define PUTBE64(b, n)\
96 93658fb9 2020-03-18 stsp (b)[0] = (n) >> 56; \
97 93658fb9 2020-03-18 stsp (b)[1] = (n) >> 48; \
98 93658fb9 2020-03-18 stsp (b)[2] = (n) >> 40; \
99 93658fb9 2020-03-18 stsp (b)[3] = (n) >> 32; \
100 93658fb9 2020-03-18 stsp (b)[4] = (n) >> 24; \
101 93658fb9 2020-03-18 stsp (b)[5] = (n) >> 16; \
102 93658fb9 2020-03-18 stsp (b)[6] = (n) >> 8; \
103 93658fb9 2020-03-18 stsp (b)[7] = (n) >> 0; \
106 668a20f6 2020-03-18 stsp static const struct got_error *
107 668a20f6 2020-03-18 stsp get_obj_type_label(const char **label, int obj_type)
109 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
111 668a20f6 2020-03-18 stsp switch (obj_type) {
112 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
113 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_BLOB;
115 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
116 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TREE;
118 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
119 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_COMMIT;
121 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
122 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TAG;
125 668a20f6 2020-03-18 stsp *label = NULL;
126 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
130 668a20f6 2020-03-18 stsp return err;
133 1e87a3c3 2020-03-18 stsp static const struct got_error *
134 1e87a3c3 2020-03-18 stsp read_crc(uint32_t *crc, int fd, size_t len)
136 1e87a3c3 2020-03-18 stsp uint8_t buf[8192];
140 1e87a3c3 2020-03-18 stsp for (n = len; n > 0; n -= r){
141 1e87a3c3 2020-03-18 stsp r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
142 1e87a3c3 2020-03-18 stsp if (r == -1)
143 1e87a3c3 2020-03-18 stsp return got_error_from_errno("read");
144 1e87a3c3 2020-03-18 stsp if (r == 0)
146 1e87a3c3 2020-03-18 stsp *crc = crc32(*crc, buf, r);
149 1e87a3c3 2020-03-18 stsp return NULL;
152 668a20f6 2020-03-18 stsp static const struct got_error *
153 668a20f6 2020-03-18 stsp read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
155 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
156 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
157 668a20f6 2020-03-18 stsp uint8_t *data;
158 668a20f6 2020-03-18 stsp size_t datalen;
160 668a20f6 2020-03-18 stsp char *header;
161 668a20f6 2020-03-18 stsp size_t headerlen;
162 668a20f6 2020-03-18 stsp const char *obj_label;
164 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&obj->type, &obj->size, &obj->tslen,
165 668a20f6 2020-03-18 stsp pack, obj->off);
167 668a20f6 2020-03-18 stsp return err;
169 1e87a3c3 2020-03-18 stsp /* XXX Seek back and get the CRC of on-disk type+size bytes. */
170 1e87a3c3 2020-03-18 stsp if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
171 1e87a3c3 2020-03-18 stsp return got_error_from_errno("lseek");
172 1e87a3c3 2020-03-18 stsp err = read_crc(&obj->crc, pack->fd, obj->tslen);
174 1e87a3c3 2020-03-18 stsp return err;
176 668a20f6 2020-03-18 stsp switch (obj->type) {
177 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
178 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
179 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
180 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
181 668a20f6 2020-03-18 stsp /* XXX TODO reading large objects into memory is bad! */
182 1e87a3c3 2020-03-18 stsp err = got_inflate_to_mem_fd(&data, &datalen, &obj->len,
183 1e87a3c3 2020-03-18 stsp &obj->crc, pack->fd);
186 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
187 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, obj->type);
190 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
191 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
192 668a20f6 2020-03-18 stsp free(data);
195 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
196 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
197 668a20f6 2020-03-18 stsp SHA1Update(&ctx, data, datalen);
198 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
199 668a20f6 2020-03-18 stsp free(header);
200 668a20f6 2020-03-18 stsp free(data);
202 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_REF_DELTA:
203 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
204 668a20f6 2020-03-18 stsp n = read(pack->fd, &obj->ref_id.sha1, SHA1_DIGEST_LENGTH);
205 668a20f6 2020-03-18 stsp if (n == -1) {
206 668a20f6 2020-03-18 stsp err = got_error_from_errno("read");
209 668a20f6 2020-03-18 stsp if (n < sizeof(obj->id)) {
210 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
213 1e87a3c3 2020-03-18 stsp obj->crc = crc32(obj->crc, obj->ref_id.sha1,
214 1e87a3c3 2020-03-18 stsp SHA1_DIGEST_LENGTH);
215 1e87a3c3 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
216 1e87a3c3 2020-03-18 stsp &obj->crc, pack->fd);
219 668a20f6 2020-03-18 stsp obj->len += SHA1_DIGEST_LENGTH;
221 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
222 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
223 668a20f6 2020-03-18 stsp err = got_pack_parse_offset_delta(&obj->base_offset,
224 668a20f6 2020-03-18 stsp &obj->base_offsetlen, pack, obj->off, obj->tslen);
228 1e87a3c3 2020-03-18 stsp /* XXX Seek back and get the CRC of on-disk offset bytes. */
229 1e87a3c3 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
230 1e87a3c3 2020-03-18 stsp err = got_error_from_errno("lseek");
233 1e87a3c3 2020-03-18 stsp err = read_crc(&obj->crc, pack->fd, obj->base_offsetlen);
237 1e87a3c3 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
238 1e87a3c3 2020-03-18 stsp &obj->crc, pack->fd);
241 668a20f6 2020-03-18 stsp obj->len += obj->base_offsetlen;
244 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
248 668a20f6 2020-03-18 stsp return err;
251 668a20f6 2020-03-18 stsp static const struct got_error *
252 668a20f6 2020-03-18 stsp hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
256 668a20f6 2020-03-18 stsp SHA1Update(ctx, buf, len);
258 668a20f6 2020-03-18 stsp w = write(fd, buf, len);
259 668a20f6 2020-03-18 stsp if (w == -1)
260 668a20f6 2020-03-18 stsp return got_error_from_errno("write");
261 668a20f6 2020-03-18 stsp if (w != len)
262 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_IO);
264 668a20f6 2020-03-18 stsp return NULL;
267 668a20f6 2020-03-18 stsp static const struct got_error *
268 668a20f6 2020-03-18 stsp resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
269 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
271 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
272 668a20f6 2020-03-18 stsp struct got_delta_chain deltas;
273 668a20f6 2020-03-18 stsp struct got_delta *delta;
274 668a20f6 2020-03-18 stsp uint8_t *buf = NULL;
275 668a20f6 2020-03-18 stsp size_t len;
276 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
277 668a20f6 2020-03-18 stsp char *header;
278 668a20f6 2020-03-18 stsp size_t headerlen;
279 668a20f6 2020-03-18 stsp int base_obj_type;
280 668a20f6 2020-03-18 stsp const char *obj_label;
282 668a20f6 2020-03-18 stsp deltas.nentries = 0;
283 668a20f6 2020-03-18 stsp SIMPLEQ_INIT(&deltas.entries);
285 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
286 668a20f6 2020-03-18 stsp obj->off, obj->tslen, obj->type, obj->size,
287 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
291 668a20f6 2020-03-18 stsp /* XXX TODO reading large objects into memory is bad! */
292 668a20f6 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
296 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
298 668a20f6 2020-03-18 stsp err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
301 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, base_obj_type);
304 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
305 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
308 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
309 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
310 668a20f6 2020-03-18 stsp SHA1Update(&ctx, buf, len);
311 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
314 668a20f6 2020-03-18 stsp while (!SIMPLEQ_EMPTY(&deltas.entries)) {
315 668a20f6 2020-03-18 stsp delta = SIMPLEQ_FIRST(&deltas.entries);
316 668a20f6 2020-03-18 stsp SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
317 668a20f6 2020-03-18 stsp free(delta);
319 668a20f6 2020-03-18 stsp return err;
322 668a20f6 2020-03-18 stsp /* Determine the slot in the pack index a given object ID should use. */
324 668a20f6 2020-03-18 stsp find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
326 668a20f6 2020-03-18 stsp u_int8_t id0 = sha1[0];
327 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
328 668a20f6 2020-03-18 stsp int left = 0, right = nindexed - 1;
329 668a20f6 2020-03-18 stsp int cmp = 0, i = 0;
331 668a20f6 2020-03-18 stsp if (id0 > 0)
332 668a20f6 2020-03-18 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
334 668a20f6 2020-03-18 stsp while (left <= right) {
335 668a20f6 2020-03-18 stsp struct got_packidx_object_id *oid;
337 668a20f6 2020-03-18 stsp i = ((left + right) / 2);
338 668a20f6 2020-03-18 stsp oid = &packidx->hdr.sorted_ids[i];
340 668a20f6 2020-03-18 stsp cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
341 668a20f6 2020-03-18 stsp if (cmp == 0)
342 668a20f6 2020-03-18 stsp return -1; /* object already indexed */
343 668a20f6 2020-03-18 stsp else if (cmp > 0)
344 668a20f6 2020-03-18 stsp left = i + 1;
345 668a20f6 2020-03-18 stsp else if (cmp < 0)
346 668a20f6 2020-03-18 stsp right = i - 1;
349 668a20f6 2020-03-18 stsp return left;
353 668a20f6 2020-03-18 stsp static void
354 668a20f6 2020-03-18 stsp print_packidx(struct got_packidx *packidx)
356 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
359 668a20f6 2020-03-18 stsp fprintf(stderr, "object IDs:\n");
360 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
361 668a20f6 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
362 668a20f6 2020-03-18 stsp got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
363 668a20f6 2020-03-18 stsp hex, sizeof(hex));
364 668a20f6 2020-03-18 stsp fprintf(stderr, "%s\n", hex);
366 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
368 668a20f6 2020-03-18 stsp fprintf(stderr, "object offsets:\n");
369 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
370 668a20f6 2020-03-18 stsp uint32_t offset = be32toh(packidx->hdr.offsets[i]);
371 668a20f6 2020-03-18 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
372 668a20f6 2020-03-18 stsp int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
373 668a20f6 2020-03-18 stsp fprintf(stderr, "%u -> %llu\n", offset,
374 668a20f6 2020-03-18 stsp be64toh(packidx->hdr.large_offsets[j]));
376 668a20f6 2020-03-18 stsp fprintf(stderr, "%u\n", offset);
378 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
380 668a20f6 2020-03-18 stsp fprintf(stderr, "fanout table:");
381 668a20f6 2020-03-18 stsp for (i = 0; i <= 0xff; i++)
382 668a20f6 2020-03-18 stsp fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
383 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
387 668a20f6 2020-03-18 stsp static void
388 7bad1537 2020-03-18 stsp update_packidx(struct got_packidx *packidx, int nobj,
389 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
391 668a20f6 2020-03-18 stsp int i, n, idx;
392 668a20f6 2020-03-18 stsp uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
394 668a20f6 2020-03-18 stsp idx = find_object_idx(packidx, obj->id.sha1);
395 668a20f6 2020-03-18 stsp if (idx == -1) {
396 668a20f6 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
397 668a20f6 2020-03-18 stsp got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
398 668a20f6 2020-03-18 stsp return; /* object already indexed */
401 668a20f6 2020-03-18 stsp memmove(&packidx->hdr.sorted_ids[idx + 1],
402 668a20f6 2020-03-18 stsp &packidx->hdr.sorted_ids[idx],
403 668a20f6 2020-03-18 stsp sizeof(struct got_packidx_object_id) * (nindexed - idx));
404 668a20f6 2020-03-18 stsp memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
405 668a20f6 2020-03-18 stsp sizeof(uint32_t) * (nindexed - idx));
407 668a20f6 2020-03-18 stsp memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
408 668a20f6 2020-03-18 stsp SHA1_DIGEST_LENGTH);
409 668a20f6 2020-03-18 stsp if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
410 668a20f6 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(obj->off);
412 7bad1537 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
413 668a20f6 2020-03-18 stsp GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
414 7bad1537 2020-03-18 stsp packidx->hdr.large_offsets[packidx->nlargeobj] =
415 7bad1537 2020-03-18 stsp htobe64(obj->off);
416 7bad1537 2020-03-18 stsp packidx->nlargeobj++;
419 668a20f6 2020-03-18 stsp for (i = obj->id.sha1[0]; i <= 0xff; i++) {
420 668a20f6 2020-03-18 stsp n = be32toh(packidx->hdr.fanout_table[i]);
421 668a20f6 2020-03-18 stsp packidx->hdr.fanout_table[i] = htobe32(n + 1);
426 8bb2b40c 2020-03-18 stsp indexed_obj_cmp(const void *pa, const void *pb)
428 8bb2b40c 2020-03-18 stsp struct got_indexed_object *a, *b;
430 8bb2b40c 2020-03-18 stsp a = *(struct got_indexed_object **)pa;
431 8bb2b40c 2020-03-18 stsp b = *(struct got_indexed_object **)pb;
432 8bb2b40c 2020-03-18 stsp return got_object_id_cmp(&a->id, &b->id);
435 668a20f6 2020-03-18 stsp static const struct got_error *
436 668a20f6 2020-03-18 stsp index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
437 668a20f6 2020-03-18 stsp struct imsgbuf *ibuf)
439 668a20f6 2020-03-18 stsp const struct got_error *err;
440 668a20f6 2020-03-18 stsp struct got_packfile_hdr hdr;
441 668a20f6 2020-03-18 stsp struct got_packidx packidx;
442 668a20f6 2020-03-18 stsp char buf[8];
443 7bad1537 2020-03-18 stsp int nobj, nvalid, nloose, nresolved = 0, i;
444 668a20f6 2020-03-18 stsp struct got_indexed_object **objects = NULL, *obj;
445 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
446 668a20f6 2020-03-18 stsp uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
447 668a20f6 2020-03-18 stsp ssize_t r, w;
450 668a20f6 2020-03-18 stsp /* Check pack file header. */
451 668a20f6 2020-03-18 stsp r = read(pack->fd, &hdr, sizeof(hdr));
452 668a20f6 2020-03-18 stsp if (r == -1)
453 668a20f6 2020-03-18 stsp return got_error_from_errno("read");
454 668a20f6 2020-03-18 stsp if (r < sizeof(hdr))
455 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
456 668a20f6 2020-03-18 stsp "short packfile header");
458 668a20f6 2020-03-18 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
459 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
460 668a20f6 2020-03-18 stsp "bad packfile signature");
461 668a20f6 2020-03-18 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
462 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
463 668a20f6 2020-03-18 stsp "bad packfile version");
464 668a20f6 2020-03-18 stsp nobj = betoh32(hdr.nobjects);
465 668a20f6 2020-03-18 stsp if (nobj == 0)
466 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
467 668a20f6 2020-03-18 stsp "bad packfile with zero objects");
470 668a20f6 2020-03-18 stsp * Create an in-memory pack index which will grow as objects
471 668a20f6 2020-03-18 stsp * IDs in the pack file are discovered. Only fields used to
472 668a20f6 2020-03-18 stsp * read deltified objects will be needed by the pack.c library
473 668a20f6 2020-03-18 stsp * code, so setting up just a pack index header is sufficient.
475 668a20f6 2020-03-18 stsp memset(&packidx, 0, sizeof(packidx));
476 668a20f6 2020-03-18 stsp packidx.hdr.magic = malloc(sizeof(uint32_t));
477 668a20f6 2020-03-18 stsp if (packidx.hdr.magic == NULL)
478 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
479 668a20f6 2020-03-18 stsp *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
480 668a20f6 2020-03-18 stsp packidx.hdr.version = malloc(sizeof(uint32_t));
481 668a20f6 2020-03-18 stsp if (packidx.hdr.version == NULL) {
482 668a20f6 2020-03-18 stsp err = got_error_from_errno("malloc");
485 668a20f6 2020-03-18 stsp *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
486 668a20f6 2020-03-18 stsp packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
487 668a20f6 2020-03-18 stsp sizeof(uint32_t));
488 668a20f6 2020-03-18 stsp if (packidx.hdr.fanout_table == NULL) {
489 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
492 668a20f6 2020-03-18 stsp packidx.hdr.sorted_ids = calloc(nobj,
493 668a20f6 2020-03-18 stsp sizeof(struct got_packidx_object_id));
494 668a20f6 2020-03-18 stsp if (packidx.hdr.sorted_ids == NULL) {
495 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
498 668a20f6 2020-03-18 stsp packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
499 668a20f6 2020-03-18 stsp if (packidx.hdr.offsets == NULL) {
500 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
503 668a20f6 2020-03-18 stsp /* Large offsets table is empty for pack files < 2 GB. */
504 668a20f6 2020-03-18 stsp if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
505 668a20f6 2020-03-18 stsp packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
506 668a20f6 2020-03-18 stsp if (packidx.hdr.large_offsets == NULL) {
507 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
512 668a20f6 2020-03-18 stsp nvalid = 0;
513 668a20f6 2020-03-18 stsp nloose = 0;
514 668a20f6 2020-03-18 stsp objects = calloc(nobj, sizeof(struct got_indexed_object *));
515 668a20f6 2020-03-18 stsp if (objects == NULL)
516 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
519 668a20f6 2020-03-18 stsp * First pass: locate all objects and identify un-deltified objects.
521 668a20f6 2020-03-18 stsp * When this pass has completed we will know offset, type, size, and
522 668a20f6 2020-03-18 stsp * CRC information for all objects in this pack file. We won't know
523 668a20f6 2020-03-18 stsp * any of the actual object IDs of deltified objects yet since we
524 668a20f6 2020-03-18 stsp * will not yet attempt to combine deltas.
527 668a20f6 2020-03-18 stsp for (i = 0; i < nobj; i++) {
528 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
529 668a20f6 2020-03-18 stsp nloose, 0);
533 668a20f6 2020-03-18 stsp obj = calloc(1, sizeof(*obj));
534 668a20f6 2020-03-18 stsp if (obj == NULL) {
535 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
538 1e87a3c3 2020-03-18 stsp obj->crc = crc32(0L, NULL, 0);
540 668a20f6 2020-03-18 stsp /* Store offset to type+size information for this object. */
541 668a20f6 2020-03-18 stsp obj->off = lseek(pack->fd, 0, SEEK_CUR);
542 668a20f6 2020-03-18 stsp if (obj->off == -1) {
543 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
547 668a20f6 2020-03-18 stsp err = read_packed_object(pack, obj);
551 668a20f6 2020-03-18 stsp objects[i] = obj;
553 1e87a3c3 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
554 1e87a3c3 2020-03-18 stsp SEEK_SET) == -1) {
555 1e87a3c3 2020-03-18 stsp err = got_error_from_errno("lseek");
559 668a20f6 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_BLOB ||
560 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TREE ||
561 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_COMMIT ||
562 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TAG) {
563 668a20f6 2020-03-18 stsp objects[i]->valid = 1;
565 7bad1537 2020-03-18 stsp update_packidx(&packidx, nobj, obj);
568 668a20f6 2020-03-18 stsp nvalid = nloose;
571 668a20f6 2020-03-18 stsp * Second pass: We can now resolve deltas to compute the IDs of
572 668a20f6 2020-03-18 stsp * objects which appear in deltified form. Because deltas can be
573 668a20f6 2020-03-18 stsp * chained this pass may require a couple of iterations until all
574 668a20f6 2020-03-18 stsp * IDs of deltified objects have been discovered.
577 668a20f6 2020-03-18 stsp while (nvalid != nobj) {
579 668a20f6 2020-03-18 stsp for (i = 0; i < nobj; i++) {
580 668a20f6 2020-03-18 stsp if (objects[i]->type != GOT_OBJ_TYPE_REF_DELTA &&
581 668a20f6 2020-03-18 stsp objects[i]->type != GOT_OBJ_TYPE_OFFSET_DELTA)
584 668a20f6 2020-03-18 stsp if (objects[i]->valid)
587 668a20f6 2020-03-18 stsp obj = objects[i];
588 668a20f6 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
589 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
593 668a20f6 2020-03-18 stsp err = resolve_deltified_object(pack, &packidx, obj);
595 668a20f6 2020-03-18 stsp if (err->code != GOT_ERR_NO_OBJ)
598 668a20f6 2020-03-18 stsp * We cannot resolve this object yet because
599 668a20f6 2020-03-18 stsp * a delta base is unknown. Try again later.
604 668a20f6 2020-03-18 stsp objects[i]->valid = 1;
606 7bad1537 2020-03-18 stsp update_packidx(&packidx, nobj, obj);
607 668a20f6 2020-03-18 stsp err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
608 668a20f6 2020-03-18 stsp nloose, nresolved + n);
613 668a20f6 2020-03-18 stsp if (pass++ > 3 && n == 0) {
614 668a20f6 2020-03-18 stsp static char msg[64];
615 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg), "could not resolve "
616 668a20f6 2020-03-18 stsp "any of deltas; packfile could be corrupt");
617 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
621 668a20f6 2020-03-18 stsp if (nloose + nresolved == nobj) {
622 668a20f6 2020-03-18 stsp static char msg[64];
623 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg),
624 668a20f6 2020-03-18 stsp "fix point reached too early: %d/%d/%d", nvalid, nresolved, nobj);
625 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
628 668a20f6 2020-03-18 stsp nresolved += n;
629 668a20f6 2020-03-18 stsp nvalid += nresolved;
632 668a20f6 2020-03-18 stsp if (nloose + nresolved != nobj) {
633 668a20f6 2020-03-18 stsp static char msg[64];
634 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg),
635 668a20f6 2020-03-18 stsp "discovered only %d of %d objects", nloose + nresolved, nobj);
636 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
640 668a20f6 2020-03-18 stsp /* We may have seen duplicates. Update our total object count. */
641 668a20f6 2020-03-18 stsp nobj = betoh32(packidx.hdr.fanout_table[0xff]);
643 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
644 668a20f6 2020-03-18 stsp err = hwrite(idxfd, "\xfftOc\x00\x00\x00\x02", 8, &ctx);
647 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.fanout_table,
648 668a20f6 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
651 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.sorted_ids,
652 668a20f6 2020-03-18 stsp nobj * SHA1_DIGEST_LENGTH, &ctx);
655 8bb2b40c 2020-03-18 stsp mergesort(objects, nobj, sizeof(struct got_indexed_object *),
656 8bb2b40c 2020-03-18 stsp indexed_obj_cmp);
657 93658fb9 2020-03-18 stsp for(i = 0; i < nobj; i++){
658 93658fb9 2020-03-18 stsp PUTBE32(buf, objects[i]->crc);
659 668a20f6 2020-03-18 stsp err = hwrite(idxfd, buf, 4, &ctx);
663 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t), &ctx);
666 7bad1537 2020-03-18 stsp if (packidx.nlargeobj > 0) {
667 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.large_offsets,
668 7bad1537 2020-03-18 stsp packidx.nlargeobj * sizeof(uint64_t), &ctx);
672 668a20f6 2020-03-18 stsp err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
676 668a20f6 2020-03-18 stsp SHA1Final(packidx_hash, &ctx);
677 668a20f6 2020-03-18 stsp w = write(idxfd, packidx_hash, sizeof(packidx_hash));
678 668a20f6 2020-03-18 stsp if (w == -1) {
679 668a20f6 2020-03-18 stsp err = got_error_from_errno("write");
682 668a20f6 2020-03-18 stsp if (w != sizeof(packidx_hash)) {
683 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_IO);
687 668a20f6 2020-03-18 stsp free(packidx.hdr.magic);
688 668a20f6 2020-03-18 stsp free(packidx.hdr.version);
689 668a20f6 2020-03-18 stsp free(packidx.hdr.fanout_table);
690 668a20f6 2020-03-18 stsp free(packidx.hdr.sorted_ids);
691 668a20f6 2020-03-18 stsp free(packidx.hdr.offsets);
692 668a20f6 2020-03-18 stsp free(packidx.hdr.large_offsets);
693 668a20f6 2020-03-18 stsp return err;
697 93658fb9 2020-03-18 stsp main(int argc, char **argv)
699 668a20f6 2020-03-18 stsp const struct got_error *err = NULL, *close_err;
700 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
701 93658fb9 2020-03-18 stsp struct imsg imsg;
702 668a20f6 2020-03-18 stsp int idxfd = -1;
703 668a20f6 2020-03-18 stsp struct got_pack pack;
704 668a20f6 2020-03-18 stsp uint8_t pack_hash[SHA1_DIGEST_LENGTH];
705 668a20f6 2020-03-18 stsp off_t packfile_size;
707 668a20f6 2020-03-18 stsp static int attached;
708 668a20f6 2020-03-18 stsp while (!attached)
712 668a20f6 2020-03-18 stsp memset(&pack, 0, sizeof(pack));
713 668a20f6 2020-03-18 stsp pack.fd = -1;
714 668a20f6 2020-03-18 stsp pack.delta_cache = got_delta_cache_alloc(100,
715 668a20f6 2020-03-18 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
716 668a20f6 2020-03-18 stsp if (pack.delta_cache == NULL) {
717 668a20f6 2020-03-18 stsp err = got_error_from_errno("got_delta_cache_alloc");
721 668a20f6 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
723 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
726 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
728 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
729 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
732 668a20f6 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
733 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
736 668a20f6 2020-03-18 stsp memcpy(pack_hash, imsg.data, sizeof(pack_hash));
737 668a20f6 2020-03-18 stsp pack.fd = imsg.fd;
739 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
742 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
744 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
745 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
748 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
749 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
752 93658fb9 2020-03-18 stsp idxfd = imsg.fd;
754 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_END) == -1) {
755 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
758 668a20f6 2020-03-18 stsp packfile_size = lseek(pack.fd, 0, SEEK_CUR);
759 668a20f6 2020-03-18 stsp if (packfile_size == -1) {
760 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
763 668a20f6 2020-03-18 stsp pack.filesize = packfile_size; /* XXX off_t vs size_t */
765 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_SET) == -1) {
766 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
770 668a20f6 2020-03-18 stsp err = index_pack(&pack, idxfd, pack_hash, &ibuf);
772 668a20f6 2020-03-18 stsp close_err = got_pack_close(&pack);
773 668a20f6 2020-03-18 stsp if (close_err && err == NULL)
774 668a20f6 2020-03-18 stsp err = close_err;
775 668a20f6 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
776 668a20f6 2020-03-18 stsp err = got_error_from_errno("close");
778 668a20f6 2020-03-18 stsp if (err == NULL)
779 93658fb9 2020-03-18 stsp err = got_privsep_send_index_pack_done(&ibuf);
781 668a20f6 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
782 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
783 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);