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