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