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