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