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