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