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