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