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