Blame


1 93658fb9 2020-03-18 stsp /*
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>
4 93658fb9 2020-03-18 stsp *
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.
8 93658fb9 2020-03-18 stsp *
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.
16 93658fb9 2020-03-18 stsp */
17 93658fb9 2020-03-18 stsp
18 93658fb9 2020-03-18 stsp #include <sys/stat.h>
19 93658fb9 2020-03-18 stsp #include <sys/time.h>
20 93658fb9 2020-03-18 stsp #include <sys/types.h>
21 93658fb9 2020-03-18 stsp #include <sys/uio.h>
22 2e5a6fad 2020-03-18 stsp #include <sys/mman.h>
23 93658fb9 2020-03-18 stsp
24 93658fb9 2020-03-18 stsp #include <stdint.h>
25 93658fb9 2020-03-18 stsp #include <errno.h>
26 93658fb9 2020-03-18 stsp #include <limits.h>
27 93658fb9 2020-03-18 stsp #include <signal.h>
28 93658fb9 2020-03-18 stsp #include <stdio.h>
29 93658fb9 2020-03-18 stsp #include <stdlib.h>
30 93658fb9 2020-03-18 stsp #include <string.h>
31 93658fb9 2020-03-18 stsp #include <ctype.h>
32 93658fb9 2020-03-18 stsp #include <fcntl.h>
33 81a12da5 2020-09-09 naddy #include <unistd.h>
34 93658fb9 2020-03-18 stsp #include <zlib.h>
35 93658fb9 2020-03-18 stsp #include <err.h>
36 93658fb9 2020-03-18 stsp #include <assert.h>
37 93658fb9 2020-03-18 stsp #include <dirent.h>
38 93658fb9 2020-03-18 stsp
39 93658fb9 2020-03-18 stsp #include "got_error.h"
40 93658fb9 2020-03-18 stsp #include "got_object.h"
41 dd038bc6 2021-09-21 thomas.ad
42 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
43 93658fb9 2020-03-18 stsp
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"
53 87c63bee 2022-02-23 thomas #include "got_lib_ratelimit.h"
54 93658fb9 2020-03-18 stsp
55 d582f26c 2020-03-18 stsp #ifndef nitems
56 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 d582f26c 2020-03-18 stsp #endif
58 d582f26c 2020-03-18 stsp
59 668a20f6 2020-03-18 stsp struct got_indexed_object {
60 668a20f6 2020-03-18 stsp struct got_object_id id;
61 93658fb9 2020-03-18 stsp
62 668a20f6 2020-03-18 stsp /*
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.
65 668a20f6 2020-03-18 stsp */
66 668a20f6 2020-03-18 stsp int valid;
67 93658fb9 2020-03-18 stsp
68 668a20f6 2020-03-18 stsp /* Offset of type+size field for this object in pack file. */
69 668a20f6 2020-03-18 stsp off_t off;
70 93658fb9 2020-03-18 stsp
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;
74 93658fb9 2020-03-18 stsp
75 668a20f6 2020-03-18 stsp /* Length of on-disk type+size data. */
76 668a20f6 2020-03-18 stsp size_t tslen;
77 93658fb9 2020-03-18 stsp
78 668a20f6 2020-03-18 stsp /* Length of object data following type+size. */
79 668a20f6 2020-03-18 stsp size_t len;
80 93658fb9 2020-03-18 stsp
81 668a20f6 2020-03-18 stsp uint32_t crc;
82 93658fb9 2020-03-18 stsp
83 836f2c92 2020-03-18 stsp union {
84 836f2c92 2020-03-18 stsp struct {
85 836f2c92 2020-03-18 stsp /* For ref deltas. */
86 836f2c92 2020-03-18 stsp struct got_object_id ref_id;
87 836f2c92 2020-03-18 stsp } ref;
88 836f2c92 2020-03-18 stsp struct {
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;
92 836f2c92 2020-03-18 stsp } ofs;
93 836f2c92 2020-03-18 stsp } delta;
94 93658fb9 2020-03-18 stsp };
95 93658fb9 2020-03-18 stsp
96 b102634c 2020-03-18 stsp static void
97 b102634c 2020-03-18 stsp putbe32(char *b, uint32_t n)
98 b102634c 2020-03-18 stsp {
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;
103 b102634c 2020-03-18 stsp }
104 93658fb9 2020-03-18 stsp
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)
107 93658fb9 2020-03-18 stsp {
108 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
109 93658fb9 2020-03-18 stsp
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;
113 668a20f6 2020-03-18 stsp break;
114 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
115 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TREE;
116 668a20f6 2020-03-18 stsp break;
117 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
118 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_COMMIT;
119 668a20f6 2020-03-18 stsp break;
120 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
121 668a20f6 2020-03-18 stsp *label = GOT_OBJ_LABEL_TAG;
122 668a20f6 2020-03-18 stsp break;
123 668a20f6 2020-03-18 stsp default:
124 668a20f6 2020-03-18 stsp *label = NULL;
125 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
126 668a20f6 2020-03-18 stsp break;
127 93658fb9 2020-03-18 stsp }
128 93658fb9 2020-03-18 stsp
129 668a20f6 2020-03-18 stsp return err;
130 93658fb9 2020-03-18 stsp }
131 93658fb9 2020-03-18 stsp
132 1e87a3c3 2020-03-18 stsp static const struct got_error *
133 6ad68bce 2020-03-24 stsp read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
134 1e87a3c3 2020-03-18 stsp {
135 1e87a3c3 2020-03-18 stsp uint8_t buf[8192];
136 1e87a3c3 2020-03-18 stsp size_t n;
137 1e87a3c3 2020-03-18 stsp ssize_t r;
138 668a20f6 2020-03-18 stsp
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)
144 1e87a3c3 2020-03-18 stsp break;
145 6ad68bce 2020-03-24 stsp if (crc)
146 6ad68bce 2020-03-24 stsp *crc = crc32(*crc, buf, r);
147 6ad68bce 2020-03-24 stsp if (sha1_ctx)
148 6ad68bce 2020-03-24 stsp SHA1Update(sha1_ctx, buf, r);
149 1e87a3c3 2020-03-18 stsp }
150 1e87a3c3 2020-03-18 stsp
151 1e87a3c3 2020-03-18 stsp return NULL;
152 1e87a3c3 2020-03-18 stsp }
153 1e87a3c3 2020-03-18 stsp
154 668a20f6 2020-03-18 stsp static const struct got_error *
155 d582f26c 2020-03-18 stsp read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
156 93658fb9 2020-03-18 stsp {
157 4788f1ce 2020-03-18 stsp uint8_t buf[8192];
158 d582f26c 2020-03-18 stsp size_t n, r;
159 4788f1ce 2020-03-18 stsp
160 d582f26c 2020-03-18 stsp for (n = len; n > 0; n -= r) {
161 d582f26c 2020-03-18 stsp r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
162 4788f1ce 2020-03-18 stsp if (r == 0) {
163 4788f1ce 2020-03-18 stsp if (feof(f))
164 4788f1ce 2020-03-18 stsp return NULL;
165 4788f1ce 2020-03-18 stsp return got_ferror(f, GOT_ERR_IO);
166 4788f1ce 2020-03-18 stsp }
167 4788f1ce 2020-03-18 stsp SHA1Update(ctx, buf, r);
168 4788f1ce 2020-03-18 stsp }
169 4788f1ce 2020-03-18 stsp
170 4788f1ce 2020-03-18 stsp return NULL;
171 4788f1ce 2020-03-18 stsp }
172 4788f1ce 2020-03-18 stsp
173 4788f1ce 2020-03-18 stsp static const struct got_error *
174 4788f1ce 2020-03-18 stsp read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
175 6ad68bce 2020-03-24 stsp FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
176 4788f1ce 2020-03-18 stsp {
177 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
178 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
179 4788f1ce 2020-03-18 stsp uint8_t *data = NULL;
180 d582f26c 2020-03-18 stsp size_t datalen = 0;
181 668a20f6 2020-03-18 stsp ssize_t n;
182 668a20f6 2020-03-18 stsp char *header;
183 668a20f6 2020-03-18 stsp size_t headerlen;
184 668a20f6 2020-03-18 stsp const char *obj_label;
185 2e5a6fad 2020-03-18 stsp size_t mapoff = obj->off;
186 6ad68bce 2020-03-24 stsp struct got_inflate_checksum csum;
187 93658fb9 2020-03-18 stsp
188 d5c81d44 2021-07-08 stsp memset(&csum, 0, sizeof(csum));
189 6ad68bce 2020-03-24 stsp csum.input_sha1 = pack_sha1_ctx;
190 6ad68bce 2020-03-24 stsp csum.input_crc = &obj->crc;
191 6ad68bce 2020-03-24 stsp
192 cbc66309 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
193 cbc66309 2020-03-18 stsp &obj->tslen, pack, obj->off);
194 668a20f6 2020-03-18 stsp if (err)
195 668a20f6 2020-03-18 stsp return err;
196 93658fb9 2020-03-18 stsp
197 2e5a6fad 2020-03-18 stsp if (pack->map) {
198 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
199 6ad68bce 2020-03-24 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
200 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen;
201 2e5a6fad 2020-03-18 stsp } else {
202 2e5a6fad 2020-03-18 stsp /* XXX Seek back and get the CRC of on-disk type+size bytes. */
203 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
204 2e5a6fad 2020-03-18 stsp return got_error_from_errno("lseek");
205 6ad68bce 2020-03-24 stsp err = read_checksum(&obj->crc, pack_sha1_ctx,
206 6ad68bce 2020-03-24 stsp pack->fd, obj->tslen);
207 2e5a6fad 2020-03-18 stsp if (err)
208 2e5a6fad 2020-03-18 stsp return err;
209 2e5a6fad 2020-03-18 stsp }
210 1e87a3c3 2020-03-18 stsp
211 668a20f6 2020-03-18 stsp switch (obj->type) {
212 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_BLOB:
213 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_COMMIT:
214 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TREE:
215 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_TAG:
216 4788f1ce 2020-03-18 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
217 4788f1ce 2020-03-18 stsp if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
218 4788f1ce 2020-03-18 stsp err = got_error_from_errno("fseek");
219 4788f1ce 2020-03-18 stsp break;
220 4788f1ce 2020-03-18 stsp }
221 4788f1ce 2020-03-18 stsp if (pack->map) {
222 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_mmap(&datalen,
223 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->map, mapoff,
224 4788f1ce 2020-03-18 stsp pack->filesize - mapoff, tmpfile);
225 4788f1ce 2020-03-18 stsp } else {
226 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_fd(&datalen,
227 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->fd, tmpfile);
228 4788f1ce 2020-03-18 stsp }
229 2e5a6fad 2020-03-18 stsp } else {
230 4788f1ce 2020-03-18 stsp if (pack->map) {
231 4788f1ce 2020-03-18 stsp err = got_inflate_to_mem_mmap(&data, &datalen,
232 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->map, mapoff,
233 4788f1ce 2020-03-18 stsp pack->filesize - mapoff);
234 4788f1ce 2020-03-18 stsp } else {
235 4788f1ce 2020-03-18 stsp err = got_inflate_to_mem_fd(&data, &datalen,
236 6ad68bce 2020-03-24 stsp &obj->len, &csum, obj->size, pack->fd);
237 4788f1ce 2020-03-18 stsp }
238 2e5a6fad 2020-03-18 stsp }
239 668a20f6 2020-03-18 stsp if (err)
240 668a20f6 2020-03-18 stsp break;
241 668a20f6 2020-03-18 stsp SHA1Init(&ctx);
242 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, obj->type);
243 7cd14ea0 2020-03-18 stsp if (err) {
244 7cd14ea0 2020-03-18 stsp free(data);
245 668a20f6 2020-03-18 stsp break;
246 7cd14ea0 2020-03-18 stsp }
247 1367695b 2020-09-26 naddy if (asprintf(&header, "%s %lld", obj_label,
248 1367695b 2020-09-26 naddy (long long)obj->size) == -1) {
249 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
250 668a20f6 2020-03-18 stsp free(data);
251 668a20f6 2020-03-18 stsp break;
252 668a20f6 2020-03-18 stsp }
253 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
254 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
255 4788f1ce 2020-03-18 stsp if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
256 d582f26c 2020-03-18 stsp err = read_file_sha1(&ctx, tmpfile, datalen);
257 8c2924d7 2021-10-15 thomas if (err) {
258 8c2924d7 2021-10-15 thomas free(header);
259 8c2924d7 2021-10-15 thomas free(data);
260 4788f1ce 2020-03-18 stsp break;
261 8c2924d7 2021-10-15 thomas }
262 4788f1ce 2020-03-18 stsp } else
263 4788f1ce 2020-03-18 stsp SHA1Update(&ctx, data, datalen);
264 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
265 668a20f6 2020-03-18 stsp free(header);
266 668a20f6 2020-03-18 stsp free(data);
267 668a20f6 2020-03-18 stsp break;
268 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_REF_DELTA:
269 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
270 2e5a6fad 2020-03-18 stsp if (pack->map) {
271 2e5a6fad 2020-03-18 stsp if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
272 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
273 2e5a6fad 2020-03-18 stsp break;
274 2e5a6fad 2020-03-18 stsp }
275 2e5a6fad 2020-03-18 stsp memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
276 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
277 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
278 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
279 6ad68bce 2020-03-24 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff,
280 6ad68bce 2020-03-24 stsp SHA1_DIGEST_LENGTH);
281 2e5a6fad 2020-03-18 stsp mapoff += SHA1_DIGEST_LENGTH;
282 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
283 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->map, mapoff,
284 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
285 2e5a6fad 2020-03-18 stsp if (err)
286 2e5a6fad 2020-03-18 stsp break;
287 2e5a6fad 2020-03-18 stsp } else {
288 2e5a6fad 2020-03-18 stsp n = read(pack->fd, obj->delta.ref.ref_id.sha1,
289 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
290 2e5a6fad 2020-03-18 stsp if (n == -1) {
291 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("read");
292 2e5a6fad 2020-03-18 stsp break;
293 2e5a6fad 2020-03-18 stsp }
294 2e5a6fad 2020-03-18 stsp if (n < sizeof(obj->id)) {
295 2e5a6fad 2020-03-18 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
296 2e5a6fad 2020-03-18 stsp break;
297 2e5a6fad 2020-03-18 stsp }
298 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
299 2e5a6fad 2020-03-18 stsp SHA1_DIGEST_LENGTH);
300 6ad68bce 2020-03-24 stsp SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
301 6ad68bce 2020-03-24 stsp SHA1_DIGEST_LENGTH);
302 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
303 6ad68bce 2020-03-24 stsp &csum, obj->size, pack->fd);
304 2e5a6fad 2020-03-18 stsp if (err)
305 2e5a6fad 2020-03-18 stsp break;
306 668a20f6 2020-03-18 stsp }
307 668a20f6 2020-03-18 stsp obj->len += SHA1_DIGEST_LENGTH;
308 668a20f6 2020-03-18 stsp break;
309 668a20f6 2020-03-18 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
310 668a20f6 2020-03-18 stsp memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
311 836f2c92 2020-03-18 stsp err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
312 836f2c92 2020-03-18 stsp &obj->delta.ofs.base_offsetlen, pack, obj->off,
313 836f2c92 2020-03-18 stsp obj->tslen);
314 668a20f6 2020-03-18 stsp if (err)
315 668a20f6 2020-03-18 stsp break;
316 1e87a3c3 2020-03-18 stsp
317 2e5a6fad 2020-03-18 stsp if (pack->map) {
318 2e5a6fad 2020-03-18 stsp obj->crc = crc32(obj->crc, pack->map + mapoff,
319 2e5a6fad 2020-03-18 stsp obj->delta.ofs.base_offsetlen);
320 6ad68bce 2020-03-24 stsp SHA1Update(pack_sha1_ctx, pack->map + mapoff,
321 6ad68bce 2020-03-24 stsp obj->delta.ofs.base_offsetlen);
322 2e5a6fad 2020-03-18 stsp mapoff += obj->delta.ofs.base_offsetlen;
323 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(NULL, &datalen,
324 6ad68bce 2020-03-24 stsp &obj->len, &csum, pack->map, mapoff,
325 2e5a6fad 2020-03-18 stsp pack->filesize - mapoff);
326 2e5a6fad 2020-03-18 stsp if (err)
327 2e5a6fad 2020-03-18 stsp break;
328 2e5a6fad 2020-03-18 stsp } else {
329 2e5a6fad 2020-03-18 stsp /*
330 6ad68bce 2020-03-24 stsp * XXX Seek back and get CRC and SHA1 of on-disk
331 2e5a6fad 2020-03-18 stsp * offset bytes.
332 2e5a6fad 2020-03-18 stsp */
333 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
334 2e5a6fad 2020-03-18 stsp == -1) {
335 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
336 2e5a6fad 2020-03-18 stsp break;
337 2e5a6fad 2020-03-18 stsp }
338 6ad68bce 2020-03-24 stsp err = read_checksum(&obj->crc, pack_sha1_ctx,
339 6ad68bce 2020-03-24 stsp pack->fd, obj->delta.ofs.base_offsetlen);
340 2e5a6fad 2020-03-18 stsp if (err)
341 2e5a6fad 2020-03-18 stsp break;
342 1e87a3c3 2020-03-18 stsp
343 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
344 6ad68bce 2020-03-24 stsp &csum, obj->size, pack->fd);
345 2e5a6fad 2020-03-18 stsp if (err)
346 2e5a6fad 2020-03-18 stsp break;
347 2e5a6fad 2020-03-18 stsp }
348 836f2c92 2020-03-18 stsp obj->len += obj->delta.ofs.base_offsetlen;
349 668a20f6 2020-03-18 stsp break;
350 668a20f6 2020-03-18 stsp default:
351 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
352 668a20f6 2020-03-18 stsp break;
353 668a20f6 2020-03-18 stsp }
354 668a20f6 2020-03-18 stsp
355 668a20f6 2020-03-18 stsp return err;
356 668a20f6 2020-03-18 stsp }
357 668a20f6 2020-03-18 stsp
358 668a20f6 2020-03-18 stsp static const struct got_error *
359 668a20f6 2020-03-18 stsp hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
360 93658fb9 2020-03-18 stsp {
361 668a20f6 2020-03-18 stsp ssize_t w;
362 668a20f6 2020-03-18 stsp
363 668a20f6 2020-03-18 stsp SHA1Update(ctx, buf, len);
364 668a20f6 2020-03-18 stsp
365 668a20f6 2020-03-18 stsp w = write(fd, buf, len);
366 668a20f6 2020-03-18 stsp if (w == -1)
367 668a20f6 2020-03-18 stsp return got_error_from_errno("write");
368 668a20f6 2020-03-18 stsp if (w != len)
369 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_IO);
370 668a20f6 2020-03-18 stsp
371 668a20f6 2020-03-18 stsp return NULL;
372 668a20f6 2020-03-18 stsp }
373 668a20f6 2020-03-18 stsp
374 668a20f6 2020-03-18 stsp static const struct got_error *
375 668a20f6 2020-03-18 stsp resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
376 d582f26c 2020-03-18 stsp struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
377 d582f26c 2020-03-18 stsp FILE *delta_accum_file)
378 668a20f6 2020-03-18 stsp {
379 668a20f6 2020-03-18 stsp const struct got_error *err = NULL;
380 668a20f6 2020-03-18 stsp struct got_delta_chain deltas;
381 668a20f6 2020-03-18 stsp struct got_delta *delta;
382 668a20f6 2020-03-18 stsp uint8_t *buf = NULL;
383 d582f26c 2020-03-18 stsp size_t len = 0;
384 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
385 4d0fef1d 2020-03-18 stsp char *header = NULL;
386 668a20f6 2020-03-18 stsp size_t headerlen;
387 d582f26c 2020-03-18 stsp uint64_t max_size;
388 668a20f6 2020-03-18 stsp int base_obj_type;
389 668a20f6 2020-03-18 stsp const char *obj_label;
390 668a20f6 2020-03-18 stsp
391 668a20f6 2020-03-18 stsp deltas.nentries = 0;
392 dbdddfee 2021-06-23 naddy STAILQ_INIT(&deltas.entries);
393 668a20f6 2020-03-18 stsp
394 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
395 668a20f6 2020-03-18 stsp obj->off, obj->tslen, obj->type, obj->size,
396 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
397 668a20f6 2020-03-18 stsp if (err)
398 668a20f6 2020-03-18 stsp goto done;
399 668a20f6 2020-03-18 stsp
400 d582f26c 2020-03-18 stsp err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
401 668a20f6 2020-03-18 stsp if (err)
402 668a20f6 2020-03-18 stsp goto done;
403 d582f26c 2020-03-18 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
404 d582f26c 2020-03-18 stsp rewind(tmpfile);
405 d582f26c 2020-03-18 stsp rewind(delta_base_file);
406 d582f26c 2020-03-18 stsp rewind(delta_accum_file);
407 d582f26c 2020-03-18 stsp err = got_pack_dump_delta_chain_to_file(&len, &deltas,
408 d582f26c 2020-03-18 stsp pack, tmpfile, delta_base_file, delta_accum_file);
409 d582f26c 2020-03-18 stsp if (err)
410 d582f26c 2020-03-18 stsp goto done;
411 d582f26c 2020-03-18 stsp } else {
412 d582f26c 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(&buf, &len,
413 d582f26c 2020-03-18 stsp &deltas, pack);
414 d582f26c 2020-03-18 stsp }
415 d582f26c 2020-03-18 stsp if (err)
416 d582f26c 2020-03-18 stsp goto done;
417 668a20f6 2020-03-18 stsp
418 668a20f6 2020-03-18 stsp err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
419 668a20f6 2020-03-18 stsp if (err)
420 668a20f6 2020-03-18 stsp goto done;
421 668a20f6 2020-03-18 stsp err = get_obj_type_label(&obj_label, base_obj_type);
422 668a20f6 2020-03-18 stsp if (err)
423 668a20f6 2020-03-18 stsp goto done;
424 668a20f6 2020-03-18 stsp if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
425 668a20f6 2020-03-18 stsp err = got_error_from_errno("asprintf");
426 668a20f6 2020-03-18 stsp goto done;
427 93658fb9 2020-03-18 stsp }
428 668a20f6 2020-03-18 stsp headerlen = strlen(header) + 1;
429 d582f26c 2020-03-18 stsp SHA1Init(&ctx);
430 668a20f6 2020-03-18 stsp SHA1Update(&ctx, header, headerlen);
431 d582f26c 2020-03-18 stsp if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
432 d582f26c 2020-03-18 stsp err = read_file_sha1(&ctx, tmpfile, len);
433 d582f26c 2020-03-18 stsp if (err)
434 d582f26c 2020-03-18 stsp goto done;
435 d582f26c 2020-03-18 stsp } else
436 d582f26c 2020-03-18 stsp SHA1Update(&ctx, buf, len);
437 668a20f6 2020-03-18 stsp SHA1Final(obj->id.sha1, &ctx);
438 668a20f6 2020-03-18 stsp done:
439 668a20f6 2020-03-18 stsp free(buf);
440 4d0fef1d 2020-03-18 stsp free(header);
441 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&deltas.entries)) {
442 dbdddfee 2021-06-23 naddy delta = STAILQ_FIRST(&deltas.entries);
443 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&deltas.entries, entry);
444 668a20f6 2020-03-18 stsp free(delta);
445 668a20f6 2020-03-18 stsp }
446 668a20f6 2020-03-18 stsp return err;
447 93658fb9 2020-03-18 stsp }
448 93658fb9 2020-03-18 stsp
449 668a20f6 2020-03-18 stsp /* Determine the slot in the pack index a given object ID should use. */
450 668a20f6 2020-03-18 stsp static int
451 668a20f6 2020-03-18 stsp find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
452 93658fb9 2020-03-18 stsp {
453 668a20f6 2020-03-18 stsp u_int8_t id0 = sha1[0];
454 78fb0967 2020-09-09 naddy uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
455 668a20f6 2020-03-18 stsp int left = 0, right = nindexed - 1;
456 668a20f6 2020-03-18 stsp int cmp = 0, i = 0;
457 93658fb9 2020-03-18 stsp
458 668a20f6 2020-03-18 stsp if (id0 > 0)
459 78fb0967 2020-09-09 naddy left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
460 93658fb9 2020-03-18 stsp
461 668a20f6 2020-03-18 stsp while (left <= right) {
462 668a20f6 2020-03-18 stsp struct got_packidx_object_id *oid;
463 93658fb9 2020-03-18 stsp
464 668a20f6 2020-03-18 stsp i = ((left + right) / 2);
465 668a20f6 2020-03-18 stsp oid = &packidx->hdr.sorted_ids[i];
466 93658fb9 2020-03-18 stsp
467 668a20f6 2020-03-18 stsp cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
468 668a20f6 2020-03-18 stsp if (cmp == 0)
469 668a20f6 2020-03-18 stsp return -1; /* object already indexed */
470 668a20f6 2020-03-18 stsp else if (cmp > 0)
471 668a20f6 2020-03-18 stsp left = i + 1;
472 668a20f6 2020-03-18 stsp else if (cmp < 0)
473 668a20f6 2020-03-18 stsp right = i - 1;
474 93658fb9 2020-03-18 stsp }
475 93658fb9 2020-03-18 stsp
476 668a20f6 2020-03-18 stsp return left;
477 93658fb9 2020-03-18 stsp }
478 93658fb9 2020-03-18 stsp
479 668a20f6 2020-03-18 stsp #if 0
480 668a20f6 2020-03-18 stsp static void
481 668a20f6 2020-03-18 stsp print_packidx(struct got_packidx *packidx)
482 93658fb9 2020-03-18 stsp {
483 78fb0967 2020-09-09 naddy uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
484 668a20f6 2020-03-18 stsp int i;
485 93658fb9 2020-03-18 stsp
486 668a20f6 2020-03-18 stsp fprintf(stderr, "object IDs:\n");
487 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
488 668a20f6 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
489 668a20f6 2020-03-18 stsp got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
490 668a20f6 2020-03-18 stsp hex, sizeof(hex));
491 668a20f6 2020-03-18 stsp fprintf(stderr, "%s\n", hex);
492 93658fb9 2020-03-18 stsp }
493 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
494 93658fb9 2020-03-18 stsp
495 668a20f6 2020-03-18 stsp fprintf(stderr, "object offsets:\n");
496 668a20f6 2020-03-18 stsp for (i = 0; i < nindexed; i++) {
497 668a20f6 2020-03-18 stsp uint32_t offset = be32toh(packidx->hdr.offsets[i]);
498 668a20f6 2020-03-18 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
499 668a20f6 2020-03-18 stsp int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
500 668a20f6 2020-03-18 stsp fprintf(stderr, "%u -> %llu\n", offset,
501 668a20f6 2020-03-18 stsp be64toh(packidx->hdr.large_offsets[j]));
502 668a20f6 2020-03-18 stsp } else
503 668a20f6 2020-03-18 stsp fprintf(stderr, "%u\n", offset);
504 93658fb9 2020-03-18 stsp }
505 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
506 93658fb9 2020-03-18 stsp
507 668a20f6 2020-03-18 stsp fprintf(stderr, "fanout table:");
508 668a20f6 2020-03-18 stsp for (i = 0; i <= 0xff; i++)
509 668a20f6 2020-03-18 stsp fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
510 668a20f6 2020-03-18 stsp fprintf(stderr, "\n");
511 93658fb9 2020-03-18 stsp }
512 668a20f6 2020-03-18 stsp #endif
513 93658fb9 2020-03-18 stsp
514 668a20f6 2020-03-18 stsp static void
515 950de2cd 2020-03-18 stsp add_indexed_object(struct got_packidx *packidx, uint32_t idx,
516 668a20f6 2020-03-18 stsp struct got_indexed_object *obj)
517 93658fb9 2020-03-18 stsp {
518 950de2cd 2020-03-18 stsp int i;
519 93658fb9 2020-03-18 stsp
520 668a20f6 2020-03-18 stsp memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
521 668a20f6 2020-03-18 stsp SHA1_DIGEST_LENGTH);
522 97684601 2020-03-18 stsp packidx->hdr.crc32[idx] = htobe32(obj->crc);
523 668a20f6 2020-03-18 stsp if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
524 668a20f6 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(obj->off);
525 668a20f6 2020-03-18 stsp else {
526 7bad1537 2020-03-18 stsp packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
527 668a20f6 2020-03-18 stsp GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
528 7bad1537 2020-03-18 stsp packidx->hdr.large_offsets[packidx->nlargeobj] =
529 7bad1537 2020-03-18 stsp htobe64(obj->off);
530 7bad1537 2020-03-18 stsp packidx->nlargeobj++;
531 93658fb9 2020-03-18 stsp }
532 93658fb9 2020-03-18 stsp
533 668a20f6 2020-03-18 stsp for (i = obj->id.sha1[0]; i <= 0xff; i++) {
534 950de2cd 2020-03-18 stsp uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
535 668a20f6 2020-03-18 stsp packidx->hdr.fanout_table[i] = htobe32(n + 1);
536 93658fb9 2020-03-18 stsp }
537 668a20f6 2020-03-18 stsp }
538 93658fb9 2020-03-18 stsp
539 8bb2b40c 2020-03-18 stsp static int
540 8bb2b40c 2020-03-18 stsp indexed_obj_cmp(const void *pa, const void *pb)
541 8bb2b40c 2020-03-18 stsp {
542 8bb2b40c 2020-03-18 stsp struct got_indexed_object *a, *b;
543 8bb2b40c 2020-03-18 stsp
544 262c582a 2020-03-18 stsp a = (struct got_indexed_object *)pa;
545 262c582a 2020-03-18 stsp b = (struct got_indexed_object *)pb;
546 8bb2b40c 2020-03-18 stsp return got_object_id_cmp(&a->id, &b->id);
547 8bb2b40c 2020-03-18 stsp }
548 8bb2b40c 2020-03-18 stsp
549 950de2cd 2020-03-18 stsp static void
550 0fd91daf 2020-03-18 stsp make_packidx(struct got_packidx *packidx, int nobj,
551 262c582a 2020-03-18 stsp struct got_indexed_object *objects)
552 950de2cd 2020-03-18 stsp {
553 950de2cd 2020-03-18 stsp struct got_indexed_object *obj;
554 950de2cd 2020-03-18 stsp int i;
555 950de2cd 2020-03-18 stsp uint32_t idx = 0;
556 950de2cd 2020-03-18 stsp
557 93bba072 2020-03-18 stsp qsort(objects, nobj, sizeof(struct got_indexed_object),
558 950de2cd 2020-03-18 stsp indexed_obj_cmp);
559 950de2cd 2020-03-18 stsp
560 0fd91daf 2020-03-18 stsp memset(packidx->hdr.fanout_table, 0,
561 0fd91daf 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
562 0fd91daf 2020-03-18 stsp packidx->nlargeobj = 0;
563 0fd91daf 2020-03-18 stsp
564 950de2cd 2020-03-18 stsp for (i = 0; i < nobj; i++) {
565 262c582a 2020-03-18 stsp obj = &objects[i];
566 0fd91daf 2020-03-18 stsp if (obj->valid)
567 0fd91daf 2020-03-18 stsp add_indexed_object(packidx, idx++, obj);
568 950de2cd 2020-03-18 stsp }
569 950de2cd 2020-03-18 stsp }
570 950de2cd 2020-03-18 stsp
571 950de2cd 2020-03-18 stsp static void
572 950de2cd 2020-03-18 stsp update_packidx(struct got_packidx *packidx, int nobj,
573 950de2cd 2020-03-18 stsp struct got_indexed_object *obj)
574 950de2cd 2020-03-18 stsp {
575 85f4e1e7 2020-12-17 stsp int idx;
576 78fb0967 2020-09-09 naddy uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
577 950de2cd 2020-03-18 stsp
578 950de2cd 2020-03-18 stsp idx = find_object_idx(packidx, obj->id.sha1);
579 950de2cd 2020-03-18 stsp if (idx == -1) {
580 950de2cd 2020-03-18 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
581 950de2cd 2020-03-18 stsp got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
582 950de2cd 2020-03-18 stsp return; /* object already indexed */
583 950de2cd 2020-03-18 stsp }
584 950de2cd 2020-03-18 stsp
585 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.sorted_ids[idx + 1],
586 950de2cd 2020-03-18 stsp &packidx->hdr.sorted_ids[idx],
587 950de2cd 2020-03-18 stsp sizeof(struct got_packidx_object_id) * (nindexed - idx));
588 950de2cd 2020-03-18 stsp memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
589 950de2cd 2020-03-18 stsp sizeof(uint32_t) * (nindexed - idx));
590 950de2cd 2020-03-18 stsp
591 950de2cd 2020-03-18 stsp add_indexed_object(packidx, idx, obj);
592 e70bf110 2020-03-22 stsp }
593 e70bf110 2020-03-22 stsp
594 e70bf110 2020-03-22 stsp static const struct got_error *
595 e70bf110 2020-03-22 stsp send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
596 87c63bee 2022-02-23 thomas int nobj_indexed, int nobj_loose, int nobj_resolved,
597 87c63bee 2022-02-23 thomas struct got_ratelimit *rl)
598 e70bf110 2020-03-22 stsp {
599 87c63bee 2022-02-23 thomas const struct got_error *err;
600 e70bf110 2020-03-22 stsp struct got_imsg_index_pack_progress iprogress;
601 87c63bee 2022-02-23 thomas int elapsed = 0;
602 87c63bee 2022-02-23 thomas
603 87c63bee 2022-02-23 thomas if (rl) {
604 87c63bee 2022-02-23 thomas err = got_ratelimit_check(&elapsed, rl);
605 87c63bee 2022-02-23 thomas if (err || !elapsed)
606 87c63bee 2022-02-23 thomas return err;
607 87c63bee 2022-02-23 thomas }
608 e70bf110 2020-03-22 stsp
609 e70bf110 2020-03-22 stsp iprogress.nobj_total = nobj_total;
610 e70bf110 2020-03-22 stsp iprogress.nobj_indexed = nobj_indexed;
611 e70bf110 2020-03-22 stsp iprogress.nobj_loose = nobj_loose;
612 e70bf110 2020-03-22 stsp iprogress.nobj_resolved = nobj_resolved;
613 e70bf110 2020-03-22 stsp
614 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
615 e70bf110 2020-03-22 stsp &iprogress, sizeof(iprogress)) == -1)
616 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
617 e70bf110 2020-03-22 stsp
618 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
619 e70bf110 2020-03-22 stsp }
620 e70bf110 2020-03-22 stsp
621 e70bf110 2020-03-22 stsp static const struct got_error *
622 e70bf110 2020-03-22 stsp send_index_pack_done(struct imsgbuf *ibuf)
623 e70bf110 2020-03-22 stsp {
624 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
625 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
626 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
627 950de2cd 2020-03-18 stsp }
628 950de2cd 2020-03-18 stsp
629 e70bf110 2020-03-22 stsp
630 668a20f6 2020-03-18 stsp static const struct got_error *
631 4788f1ce 2020-03-18 stsp index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
632 3abebdc2 2020-03-24 stsp FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
633 d582f26c 2020-03-18 stsp struct imsgbuf *ibuf)
634 668a20f6 2020-03-18 stsp {
635 668a20f6 2020-03-18 stsp const struct got_error *err;
636 668a20f6 2020-03-18 stsp struct got_packfile_hdr hdr;
637 668a20f6 2020-03-18 stsp struct got_packidx packidx;
638 668a20f6 2020-03-18 stsp char buf[8];
639 6ad68bce 2020-03-24 stsp char pack_sha1[SHA1_DIGEST_LENGTH];
640 7bad1537 2020-03-18 stsp int nobj, nvalid, nloose, nresolved = 0, i;
641 262c582a 2020-03-18 stsp struct got_indexed_object *objects = NULL, *obj;
642 668a20f6 2020-03-18 stsp SHA1_CTX ctx;
643 668a20f6 2020-03-18 stsp uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
644 668a20f6 2020-03-18 stsp ssize_t r, w;
645 160bbe2e 2020-03-18 stsp int pass, have_ref_deltas = 0, first_delta_idx = -1;
646 2e5a6fad 2020-03-18 stsp size_t mapoff = 0;
647 5672d305 2020-03-18 stsp int p_indexed = 0, last_p_indexed = -1;
648 5672d305 2020-03-18 stsp int p_resolved = 0, last_p_resolved = -1;
649 87c63bee 2022-02-23 thomas struct got_ratelimit rl;
650 93658fb9 2020-03-18 stsp
651 6ad68bce 2020-03-24 stsp /* Require that pack file header and SHA1 trailer are present. */
652 6ad68bce 2020-03-24 stsp if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
653 6ad68bce 2020-03-24 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
654 6ad68bce 2020-03-24 stsp "short pack file");
655 6ad68bce 2020-03-24 stsp
656 2e5a6fad 2020-03-18 stsp if (pack->map) {
657 2e5a6fad 2020-03-18 stsp memcpy(&hdr, pack->map, sizeof(hdr));
658 2e5a6fad 2020-03-18 stsp mapoff += sizeof(hdr);
659 2e5a6fad 2020-03-18 stsp } else {
660 2e5a6fad 2020-03-18 stsp r = read(pack->fd, &hdr, sizeof(hdr));
661 2e5a6fad 2020-03-18 stsp if (r == -1)
662 2e5a6fad 2020-03-18 stsp return got_error_from_errno("read");
663 2e5a6fad 2020-03-18 stsp if (r < sizeof(hdr))
664 2e5a6fad 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
665 6ad68bce 2020-03-24 stsp "short pack file");
666 2e5a6fad 2020-03-18 stsp }
667 668a20f6 2020-03-18 stsp
668 668a20f6 2020-03-18 stsp if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
669 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
670 668a20f6 2020-03-18 stsp "bad packfile signature");
671 668a20f6 2020-03-18 stsp if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
672 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
673 668a20f6 2020-03-18 stsp "bad packfile version");
674 78fb0967 2020-09-09 naddy nobj = be32toh(hdr.nobjects);
675 668a20f6 2020-03-18 stsp if (nobj == 0)
676 668a20f6 2020-03-18 stsp return got_error_msg(GOT_ERR_BAD_PACKFILE,
677 668a20f6 2020-03-18 stsp "bad packfile with zero objects");
678 668a20f6 2020-03-18 stsp
679 6ad68bce 2020-03-24 stsp /* We compute the SHA1 of pack file contents and verify later on. */
680 6ad68bce 2020-03-24 stsp SHA1Init(&ctx);
681 6ad68bce 2020-03-24 stsp SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
682 6ad68bce 2020-03-24 stsp
683 93658fb9 2020-03-18 stsp /*
684 668a20f6 2020-03-18 stsp * Create an in-memory pack index which will grow as objects
685 668a20f6 2020-03-18 stsp * IDs in the pack file are discovered. Only fields used to
686 668a20f6 2020-03-18 stsp * read deltified objects will be needed by the pack.c library
687 668a20f6 2020-03-18 stsp * code, so setting up just a pack index header is sufficient.
688 93658fb9 2020-03-18 stsp */
689 668a20f6 2020-03-18 stsp memset(&packidx, 0, sizeof(packidx));
690 668a20f6 2020-03-18 stsp packidx.hdr.magic = malloc(sizeof(uint32_t));
691 668a20f6 2020-03-18 stsp if (packidx.hdr.magic == NULL)
692 812c6838 2021-10-15 thomas return got_error_from_errno("malloc");
693 668a20f6 2020-03-18 stsp *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
694 668a20f6 2020-03-18 stsp packidx.hdr.version = malloc(sizeof(uint32_t));
695 668a20f6 2020-03-18 stsp if (packidx.hdr.version == NULL) {
696 668a20f6 2020-03-18 stsp err = got_error_from_errno("malloc");
697 668a20f6 2020-03-18 stsp goto done;
698 93658fb9 2020-03-18 stsp }
699 668a20f6 2020-03-18 stsp *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
700 668a20f6 2020-03-18 stsp packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
701 668a20f6 2020-03-18 stsp sizeof(uint32_t));
702 668a20f6 2020-03-18 stsp if (packidx.hdr.fanout_table == NULL) {
703 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
704 668a20f6 2020-03-18 stsp goto done;
705 93658fb9 2020-03-18 stsp }
706 668a20f6 2020-03-18 stsp packidx.hdr.sorted_ids = calloc(nobj,
707 668a20f6 2020-03-18 stsp sizeof(struct got_packidx_object_id));
708 668a20f6 2020-03-18 stsp if (packidx.hdr.sorted_ids == NULL) {
709 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
710 668a20f6 2020-03-18 stsp goto done;
711 93658fb9 2020-03-18 stsp }
712 97684601 2020-03-18 stsp packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
713 97684601 2020-03-18 stsp if (packidx.hdr.crc32 == NULL) {
714 97684601 2020-03-18 stsp err = got_error_from_errno("calloc");
715 97684601 2020-03-18 stsp goto done;
716 97684601 2020-03-18 stsp }
717 668a20f6 2020-03-18 stsp packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
718 668a20f6 2020-03-18 stsp if (packidx.hdr.offsets == NULL) {
719 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
720 668a20f6 2020-03-18 stsp goto done;
721 93658fb9 2020-03-18 stsp }
722 668a20f6 2020-03-18 stsp /* Large offsets table is empty for pack files < 2 GB. */
723 668a20f6 2020-03-18 stsp if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
724 668a20f6 2020-03-18 stsp packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
725 668a20f6 2020-03-18 stsp if (packidx.hdr.large_offsets == NULL) {
726 668a20f6 2020-03-18 stsp err = got_error_from_errno("calloc");
727 668a20f6 2020-03-18 stsp goto done;
728 668a20f6 2020-03-18 stsp }
729 93658fb9 2020-03-18 stsp }
730 93658fb9 2020-03-18 stsp
731 668a20f6 2020-03-18 stsp nvalid = 0;
732 668a20f6 2020-03-18 stsp nloose = 0;
733 262c582a 2020-03-18 stsp objects = calloc(nobj, sizeof(struct got_indexed_object));
734 668a20f6 2020-03-18 stsp if (objects == NULL)
735 668a20f6 2020-03-18 stsp return got_error_from_errno("calloc");
736 93658fb9 2020-03-18 stsp
737 87c63bee 2022-02-23 thomas got_ratelimit_init(&rl, 0, 500);
738 87c63bee 2022-02-23 thomas
739 668a20f6 2020-03-18 stsp /*
740 668a20f6 2020-03-18 stsp * First pass: locate all objects and identify un-deltified objects.
741 668a20f6 2020-03-18 stsp *
742 668a20f6 2020-03-18 stsp * When this pass has completed we will know offset, type, size, and
743 668a20f6 2020-03-18 stsp * CRC information for all objects in this pack file. We won't know
744 668a20f6 2020-03-18 stsp * any of the actual object IDs of deltified objects yet since we
745 668a20f6 2020-03-18 stsp * will not yet attempt to combine deltas.
746 668a20f6 2020-03-18 stsp */
747 668a20f6 2020-03-18 stsp pass = 1;
748 668a20f6 2020-03-18 stsp for (i = 0; i < nobj; i++) {
749 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
750 5672d305 2020-03-18 stsp p_indexed = ((i + 1) * 100) / nobj;
751 5672d305 2020-03-18 stsp if (p_indexed != last_p_indexed) {
752 e70bf110 2020-03-22 stsp err = send_index_pack_progress(ibuf, nobj, i + 1,
753 87c63bee 2022-02-23 thomas nloose, 0, &rl);
754 5672d305 2020-03-18 stsp if (err)
755 5672d305 2020-03-18 stsp goto done;
756 5672d305 2020-03-18 stsp last_p_indexed = p_indexed;
757 5672d305 2020-03-18 stsp }
758 93658fb9 2020-03-18 stsp
759 262c582a 2020-03-18 stsp obj = &objects[i];
760 1e87a3c3 2020-03-18 stsp obj->crc = crc32(0L, NULL, 0);
761 93658fb9 2020-03-18 stsp
762 668a20f6 2020-03-18 stsp /* Store offset to type+size information for this object. */
763 2e5a6fad 2020-03-18 stsp if (pack->map) {
764 2e5a6fad 2020-03-18 stsp obj->off = mapoff;
765 2e5a6fad 2020-03-18 stsp } else {
766 2e5a6fad 2020-03-18 stsp obj->off = lseek(pack->fd, 0, SEEK_CUR);
767 2e5a6fad 2020-03-18 stsp if (obj->off == -1) {
768 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
769 2e5a6fad 2020-03-18 stsp goto done;
770 2e5a6fad 2020-03-18 stsp }
771 668a20f6 2020-03-18 stsp }
772 93658fb9 2020-03-18 stsp
773 6ad68bce 2020-03-24 stsp err = read_packed_object(pack, obj, tmpfile, &ctx);
774 668a20f6 2020-03-18 stsp if (err)
775 668a20f6 2020-03-18 stsp goto done;
776 668a20f6 2020-03-18 stsp
777 2e5a6fad 2020-03-18 stsp if (pack->map) {
778 2e5a6fad 2020-03-18 stsp mapoff += obj->tslen + obj->len;
779 2e5a6fad 2020-03-18 stsp } else {
780 2e5a6fad 2020-03-18 stsp if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
781 2e5a6fad 2020-03-18 stsp SEEK_SET) == -1) {
782 2e5a6fad 2020-03-18 stsp err = got_error_from_errno("lseek");
783 2e5a6fad 2020-03-18 stsp goto done;
784 2e5a6fad 2020-03-18 stsp }
785 1e87a3c3 2020-03-18 stsp }
786 93658fb9 2020-03-18 stsp
787 668a20f6 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_BLOB ||
788 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TREE ||
789 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_COMMIT ||
790 668a20f6 2020-03-18 stsp obj->type == GOT_OBJ_TYPE_TAG) {
791 262c582a 2020-03-18 stsp obj->valid = 1;
792 668a20f6 2020-03-18 stsp nloose++;
793 160bbe2e 2020-03-18 stsp } else {
794 160bbe2e 2020-03-18 stsp if (first_delta_idx == -1)
795 160bbe2e 2020-03-18 stsp first_delta_idx = i;
796 160bbe2e 2020-03-18 stsp if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
797 160bbe2e 2020-03-18 stsp have_ref_deltas = 1;
798 160bbe2e 2020-03-18 stsp }
799 93658fb9 2020-03-18 stsp }
800 668a20f6 2020-03-18 stsp nvalid = nloose;
801 93658fb9 2020-03-18 stsp
802 6ad68bce 2020-03-24 stsp /*
803 6ad68bce 2020-03-24 stsp * Having done a full pass over the pack file and can now
804 6ad68bce 2020-03-24 stsp * verify its checksum.
805 6ad68bce 2020-03-24 stsp */
806 6ad68bce 2020-03-24 stsp SHA1Final(pack_sha1, &ctx);
807 3abebdc2 2020-03-24 stsp if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
808 3abebdc2 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
809 3abebdc2 2020-03-24 stsp "pack file checksum mismatch");
810 3abebdc2 2020-03-24 stsp goto done;
811 3abebdc2 2020-03-24 stsp }
812 3abebdc2 2020-03-24 stsp
813 3abebdc2 2020-03-24 stsp /* Verify the SHA1 checksum stored at the end of the pack file. */
814 6ad68bce 2020-03-24 stsp if (pack->map) {
815 6ad68bce 2020-03-24 stsp memcpy(pack_sha1_expected, pack->map +
816 6ad68bce 2020-03-24 stsp pack->filesize - SHA1_DIGEST_LENGTH,
817 6ad68bce 2020-03-24 stsp SHA1_DIGEST_LENGTH);
818 6ad68bce 2020-03-24 stsp } else {
819 6ad68bce 2020-03-24 stsp ssize_t n;
820 6ad68bce 2020-03-24 stsp if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
821 6ad68bce 2020-03-24 stsp err = got_error_from_errno("lseek");
822 6ad68bce 2020-03-24 stsp goto done;
823 6ad68bce 2020-03-24 stsp }
824 6ad68bce 2020-03-24 stsp n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
825 6ad68bce 2020-03-24 stsp if (n == -1) {
826 6ad68bce 2020-03-24 stsp err = got_error_from_errno("read");
827 6ad68bce 2020-03-24 stsp goto done;
828 6ad68bce 2020-03-24 stsp }
829 6ad68bce 2020-03-24 stsp if (n != SHA1_DIGEST_LENGTH) {
830 6ad68bce 2020-03-24 stsp err = got_error(GOT_ERR_IO);
831 6ad68bce 2020-03-24 stsp goto done;
832 6ad68bce 2020-03-24 stsp }
833 6ad68bce 2020-03-24 stsp }
834 6ad68bce 2020-03-24 stsp if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
835 6ad68bce 2020-03-24 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE,
836 3abebdc2 2020-03-24 stsp "bad checksum in pack file trailer");
837 6ad68bce 2020-03-24 stsp goto done;
838 6ad68bce 2020-03-24 stsp }
839 6ad68bce 2020-03-24 stsp
840 160bbe2e 2020-03-18 stsp if (first_delta_idx == -1)
841 160bbe2e 2020-03-18 stsp first_delta_idx = 0;
842 160bbe2e 2020-03-18 stsp
843 0fd91daf 2020-03-18 stsp /* In order to resolve ref deltas we need an in-progress pack index. */
844 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
845 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
846 950de2cd 2020-03-18 stsp
847 668a20f6 2020-03-18 stsp /*
848 668a20f6 2020-03-18 stsp * Second pass: We can now resolve deltas to compute the IDs of
849 668a20f6 2020-03-18 stsp * objects which appear in deltified form. Because deltas can be
850 668a20f6 2020-03-18 stsp * chained this pass may require a couple of iterations until all
851 668a20f6 2020-03-18 stsp * IDs of deltified objects have been discovered.
852 668a20f6 2020-03-18 stsp */
853 668a20f6 2020-03-18 stsp pass++;
854 668a20f6 2020-03-18 stsp while (nvalid != nobj) {
855 668a20f6 2020-03-18 stsp int n = 0;
856 e6c1173d 2020-03-18 stsp /*
857 e6c1173d 2020-03-18 stsp * This loop will only run once unless the pack file
858 e6c1173d 2020-03-18 stsp * contains ref deltas which refer to objects located
859 e6c1173d 2020-03-18 stsp * later in the pack file, which is unusual.
860 e6c1173d 2020-03-18 stsp * Offset deltas can always be resolved in one pass
861 e6c1173d 2020-03-18 stsp * unless the packfile is corrupt.
862 e6c1173d 2020-03-18 stsp */
863 160bbe2e 2020-03-18 stsp for (i = first_delta_idx; i < nobj; i++) {
864 262c582a 2020-03-18 stsp obj = &objects[i];
865 262c582a 2020-03-18 stsp if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
866 262c582a 2020-03-18 stsp obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
867 668a20f6 2020-03-18 stsp continue;
868 93658fb9 2020-03-18 stsp
869 262c582a 2020-03-18 stsp if (obj->valid)
870 668a20f6 2020-03-18 stsp continue;
871 93658fb9 2020-03-18 stsp
872 b3e1118b 2020-03-18 stsp if (pack->map == NULL && lseek(pack->fd,
873 b3e1118b 2020-03-18 stsp obj->off + obj->tslen, SEEK_SET) == -1) {
874 b3e1118b 2020-03-18 stsp err = got_error_from_errno("lseek");
875 b3e1118b 2020-03-18 stsp goto done;
876 668a20f6 2020-03-18 stsp }
877 93658fb9 2020-03-18 stsp
878 d582f26c 2020-03-18 stsp err = resolve_deltified_object(pack, &packidx, obj,
879 d582f26c 2020-03-18 stsp tmpfile, delta_base_file, delta_accum_file);
880 668a20f6 2020-03-18 stsp if (err) {
881 668a20f6 2020-03-18 stsp if (err->code != GOT_ERR_NO_OBJ)
882 668a20f6 2020-03-18 stsp goto done;
883 668a20f6 2020-03-18 stsp /*
884 668a20f6 2020-03-18 stsp * We cannot resolve this object yet because
885 668a20f6 2020-03-18 stsp * a delta base is unknown. Try again later.
886 668a20f6 2020-03-18 stsp */
887 668a20f6 2020-03-18 stsp continue;
888 668a20f6 2020-03-18 stsp }
889 93658fb9 2020-03-18 stsp
890 262c582a 2020-03-18 stsp obj->valid = 1;
891 668a20f6 2020-03-18 stsp n++;
892 0fd91daf 2020-03-18 stsp if (have_ref_deltas)
893 0fd91daf 2020-03-18 stsp update_packidx(&packidx, nobj, obj);
894 5672d305 2020-03-18 stsp /* Don't send too many progress privsep messages. */
895 5672d305 2020-03-18 stsp p_resolved = ((nresolved + n) * 100) / nobj;
896 5672d305 2020-03-18 stsp if (p_resolved != last_p_resolved) {
897 e70bf110 2020-03-22 stsp err = send_index_pack_progress(ibuf, nobj,
898 87c63bee 2022-02-23 thomas nobj, nloose, nresolved + n, &rl);
899 5672d305 2020-03-18 stsp if (err)
900 5672d305 2020-03-18 stsp goto done;
901 5672d305 2020-03-18 stsp last_p_resolved = p_resolved;
902 5672d305 2020-03-18 stsp }
903 93658fb9 2020-03-18 stsp
904 668a20f6 2020-03-18 stsp }
905 668a20f6 2020-03-18 stsp if (pass++ > 3 && n == 0) {
906 668a20f6 2020-03-18 stsp static char msg[64];
907 668a20f6 2020-03-18 stsp snprintf(msg, sizeof(msg), "could not resolve "
908 668a20f6 2020-03-18 stsp "any of deltas; packfile could be corrupt");
909 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
910 668a20f6 2020-03-18 stsp goto done;
911 3168e5da 2020-09-10 stsp
912 668a20f6 2020-03-18 stsp }
913 668a20f6 2020-03-18 stsp nresolved += n;
914 668a20f6 2020-03-18 stsp nvalid += nresolved;
915 93658fb9 2020-03-18 stsp }
916 93658fb9 2020-03-18 stsp
917 668a20f6 2020-03-18 stsp if (nloose + nresolved != nobj) {
918 668a20f6 2020-03-18 stsp static char msg[64];
919 ec92f929 2020-03-18 stsp snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
920 cbc66309 2020-03-18 stsp nloose + nresolved, nobj);
921 668a20f6 2020-03-18 stsp err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
922 668a20f6 2020-03-18 stsp goto done;
923 93658fb9 2020-03-18 stsp }
924 93658fb9 2020-03-18 stsp
925 87c63bee 2022-02-23 thomas err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved,
926 87c63bee 2022-02-23 thomas NULL);
927 021b0c6f 2020-03-18 stsp if (err)
928 021b0c6f 2020-03-18 stsp goto done;
929 021b0c6f 2020-03-18 stsp
930 0fd91daf 2020-03-18 stsp make_packidx(&packidx, nobj, objects);
931 97684601 2020-03-18 stsp
932 97684601 2020-03-18 stsp free(objects);
933 97684601 2020-03-18 stsp objects = NULL;
934 93658fb9 2020-03-18 stsp
935 93658fb9 2020-03-18 stsp SHA1Init(&ctx);
936 b102634c 2020-03-18 stsp putbe32(buf, GOT_PACKIDX_V2_MAGIC);
937 b102634c 2020-03-18 stsp putbe32(buf + 4, GOT_PACKIDX_VERSION);
938 b102634c 2020-03-18 stsp err = hwrite(idxfd, buf, 8, &ctx);
939 668a20f6 2020-03-18 stsp if (err)
940 668a20f6 2020-03-18 stsp goto done;
941 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.fanout_table,
942 668a20f6 2020-03-18 stsp GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
943 668a20f6 2020-03-18 stsp if (err)
944 668a20f6 2020-03-18 stsp goto done;
945 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.sorted_ids,
946 668a20f6 2020-03-18 stsp nobj * SHA1_DIGEST_LENGTH, &ctx);
947 668a20f6 2020-03-18 stsp if (err)
948 668a20f6 2020-03-18 stsp goto done;
949 97684601 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
950 97684601 2020-03-18 stsp if (err)
951 97684601 2020-03-18 stsp goto done;
952 cbc66309 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
953 cbc66309 2020-03-18 stsp &ctx);
954 668a20f6 2020-03-18 stsp if (err)
955 668a20f6 2020-03-18 stsp goto done;
956 7bad1537 2020-03-18 stsp if (packidx.nlargeobj > 0) {
957 668a20f6 2020-03-18 stsp err = hwrite(idxfd, packidx.hdr.large_offsets,
958 7bad1537 2020-03-18 stsp packidx.nlargeobj * sizeof(uint64_t), &ctx);
959 668a20f6 2020-03-18 stsp if (err)
960 668a20f6 2020-03-18 stsp goto done;
961 668a20f6 2020-03-18 stsp }
962 3abebdc2 2020-03-24 stsp err = hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
963 668a20f6 2020-03-18 stsp if (err)
964 668a20f6 2020-03-18 stsp goto done;
965 93658fb9 2020-03-18 stsp
966 668a20f6 2020-03-18 stsp SHA1Final(packidx_hash, &ctx);
967 668a20f6 2020-03-18 stsp w = write(idxfd, packidx_hash, sizeof(packidx_hash));
968 668a20f6 2020-03-18 stsp if (w == -1) {
969 668a20f6 2020-03-18 stsp err = got_error_from_errno("write");
970 668a20f6 2020-03-18 stsp goto done;
971 93658fb9 2020-03-18 stsp }
972 668a20f6 2020-03-18 stsp if (w != sizeof(packidx_hash)) {
973 668a20f6 2020-03-18 stsp err = got_error(GOT_ERR_IO);
974 668a20f6 2020-03-18 stsp goto done;
975 93658fb9 2020-03-18 stsp }
976 668a20f6 2020-03-18 stsp done:
977 37ebab0a 2020-03-18 stsp free(objects);
978 668a20f6 2020-03-18 stsp free(packidx.hdr.magic);
979 668a20f6 2020-03-18 stsp free(packidx.hdr.version);
980 668a20f6 2020-03-18 stsp free(packidx.hdr.fanout_table);
981 668a20f6 2020-03-18 stsp free(packidx.hdr.sorted_ids);
982 668a20f6 2020-03-18 stsp free(packidx.hdr.offsets);
983 668a20f6 2020-03-18 stsp free(packidx.hdr.large_offsets);
984 668a20f6 2020-03-18 stsp return err;
985 93658fb9 2020-03-18 stsp }
986 93658fb9 2020-03-18 stsp
987 93658fb9 2020-03-18 stsp int
988 93658fb9 2020-03-18 stsp main(int argc, char **argv)
989 93658fb9 2020-03-18 stsp {
990 668a20f6 2020-03-18 stsp const struct got_error *err = NULL, *close_err;
991 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
992 93658fb9 2020-03-18 stsp struct imsg imsg;
993 6059809a 2020-12-17 stsp size_t i;
994 6059809a 2020-12-17 stsp int idxfd = -1, tmpfd = -1;
995 d582f26c 2020-03-18 stsp FILE *tmpfiles[3];
996 668a20f6 2020-03-18 stsp struct got_pack pack;
997 668a20f6 2020-03-18 stsp uint8_t pack_hash[SHA1_DIGEST_LENGTH];
998 668a20f6 2020-03-18 stsp off_t packfile_size;
999 668a20f6 2020-03-18 stsp #if 0
1000 668a20f6 2020-03-18 stsp static int attached;
1001 668a20f6 2020-03-18 stsp while (!attached)
1002 668a20f6 2020-03-18 stsp sleep(1);
1003 668a20f6 2020-03-18 stsp #endif
1004 93658fb9 2020-03-18 stsp
1005 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++)
1006 d582f26c 2020-03-18 stsp tmpfiles[i] = NULL;
1007 d582f26c 2020-03-18 stsp
1008 668a20f6 2020-03-18 stsp memset(&pack, 0, sizeof(pack));
1009 668a20f6 2020-03-18 stsp pack.fd = -1;
1010 18d4da03 2020-03-18 stsp pack.delta_cache = got_delta_cache_alloc(500,
1011 668a20f6 2020-03-18 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1012 668a20f6 2020-03-18 stsp if (pack.delta_cache == NULL) {
1013 668a20f6 2020-03-18 stsp err = got_error_from_errno("got_delta_cache_alloc");
1014 93658fb9 2020-03-18 stsp goto done;
1015 93658fb9 2020-03-18 stsp }
1016 668a20f6 2020-03-18 stsp
1017 668a20f6 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1018 861f3006 2020-03-18 stsp #ifndef PROFILE
1019 861f3006 2020-03-18 stsp /* revoke access to most system calls */
1020 861f3006 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
1021 861f3006 2020-03-18 stsp err = got_error_from_errno("pledge");
1022 861f3006 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1023 861f3006 2020-03-18 stsp return 1;
1024 861f3006 2020-03-18 stsp }
1025 97799ccd 2022-02-06 thomas
1026 97799ccd 2022-02-06 thomas /* revoke fs access */
1027 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
1028 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
1029 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
1030 97799ccd 2022-02-06 thomas return 1;
1031 97799ccd 2022-02-06 thomas }
1032 861f3006 2020-03-18 stsp #endif
1033 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1034 668a20f6 2020-03-18 stsp if (err)
1035 668a20f6 2020-03-18 stsp goto done;
1036 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1037 93658fb9 2020-03-18 stsp goto done;
1038 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
1039 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1040 93658fb9 2020-03-18 stsp goto done;
1041 93658fb9 2020-03-18 stsp }
1042 668a20f6 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
1043 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1044 93658fb9 2020-03-18 stsp goto done;
1045 93658fb9 2020-03-18 stsp }
1046 668a20f6 2020-03-18 stsp memcpy(pack_hash, imsg.data, sizeof(pack_hash));
1047 668a20f6 2020-03-18 stsp pack.fd = imsg.fd;
1048 93658fb9 2020-03-18 stsp
1049 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1050 668a20f6 2020-03-18 stsp if (err)
1051 93658fb9 2020-03-18 stsp goto done;
1052 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1053 93658fb9 2020-03-18 stsp goto done;
1054 73ab1060 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
1055 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1056 93658fb9 2020-03-18 stsp goto done;
1057 93658fb9 2020-03-18 stsp }
1058 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1059 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1060 93658fb9 2020-03-18 stsp goto done;
1061 93658fb9 2020-03-18 stsp }
1062 93658fb9 2020-03-18 stsp idxfd = imsg.fd;
1063 4788f1ce 2020-03-18 stsp
1064 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
1065 d582f26c 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1066 d582f26c 2020-03-18 stsp if (err)
1067 d582f26c 2020-03-18 stsp goto done;
1068 d582f26c 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1069 d582f26c 2020-03-18 stsp goto done;
1070 d582f26c 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
1071 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1072 d582f26c 2020-03-18 stsp goto done;
1073 d582f26c 2020-03-18 stsp }
1074 d582f26c 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1075 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1076 d582f26c 2020-03-18 stsp goto done;
1077 d582f26c 2020-03-18 stsp }
1078 d582f26c 2020-03-18 stsp tmpfd = imsg.fd;
1079 d582f26c 2020-03-18 stsp tmpfiles[i] = fdopen(tmpfd, "w+");
1080 d582f26c 2020-03-18 stsp if (tmpfiles[i] == NULL) {
1081 d582f26c 2020-03-18 stsp err = got_error_from_errno("fdopen");
1082 d582f26c 2020-03-18 stsp goto done;
1083 d582f26c 2020-03-18 stsp }
1084 d582f26c 2020-03-18 stsp tmpfd = -1;
1085 4788f1ce 2020-03-18 stsp }
1086 93658fb9 2020-03-18 stsp
1087 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_END) == -1) {
1088 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
1089 668a20f6 2020-03-18 stsp goto done;
1090 668a20f6 2020-03-18 stsp }
1091 668a20f6 2020-03-18 stsp packfile_size = lseek(pack.fd, 0, SEEK_CUR);
1092 668a20f6 2020-03-18 stsp if (packfile_size == -1) {
1093 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
1094 668a20f6 2020-03-18 stsp goto done;
1095 668a20f6 2020-03-18 stsp }
1096 668a20f6 2020-03-18 stsp pack.filesize = packfile_size; /* XXX off_t vs size_t */
1097 668a20f6 2020-03-18 stsp
1098 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_SET) == -1) {
1099 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
1100 668a20f6 2020-03-18 stsp goto done;
1101 668a20f6 2020-03-18 stsp }
1102 668a20f6 2020-03-18 stsp
1103 2e5a6fad 2020-03-18 stsp #ifndef GOT_PACK_NO_MMAP
1104 2e5a6fad 2020-03-18 stsp pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
1105 2e5a6fad 2020-03-18 stsp pack.fd, 0);
1106 2e5a6fad 2020-03-18 stsp if (pack.map == MAP_FAILED)
1107 2e5a6fad 2020-03-18 stsp pack.map = NULL; /* fall back to read(2) */
1108 2e5a6fad 2020-03-18 stsp #endif
1109 d582f26c 2020-03-18 stsp err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1110 d582f26c 2020-03-18 stsp pack_hash, &ibuf);
1111 93658fb9 2020-03-18 stsp done:
1112 668a20f6 2020-03-18 stsp close_err = got_pack_close(&pack);
1113 668a20f6 2020-03-18 stsp if (close_err && err == NULL)
1114 668a20f6 2020-03-18 stsp err = close_err;
1115 668a20f6 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1116 668a20f6 2020-03-18 stsp err = got_error_from_errno("close");
1117 4788f1ce 2020-03-18 stsp if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1118 4788f1ce 2020-03-18 stsp err = got_error_from_errno("close");
1119 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
1120 d582f26c 2020-03-18 stsp if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1121 d582f26c 2020-03-18 stsp err == NULL)
1122 812c6838 2021-10-15 thomas err = got_error_from_errno("fclose");
1123 d582f26c 2020-03-18 stsp }
1124 668a20f6 2020-03-18 stsp
1125 668a20f6 2020-03-18 stsp if (err == NULL)
1126 e70bf110 2020-03-22 stsp err = send_index_pack_done(&ibuf);
1127 668a20f6 2020-03-18 stsp if (err) {
1128 668a20f6 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1129 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1130 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
1131 668a20f6 2020-03-18 stsp exit(1);
1132 93658fb9 2020-03-18 stsp }
1133 93658fb9 2020-03-18 stsp
1134 93658fb9 2020-03-18 stsp exit(0);
1135 93658fb9 2020-03-18 stsp }