Blame


1 142012ed 2022-10-20 thomas /*
2 142012ed 2022-10-20 thomas * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 142012ed 2022-10-20 thomas * Copyright (c) 2020, 2022 Stefan Sperling <stsp@openbsd.org>
4 142012ed 2022-10-20 thomas *
5 142012ed 2022-10-20 thomas * Permission to use, copy, modify, and distribute this software for any
6 142012ed 2022-10-20 thomas * purpose with or without fee is hereby granted, provided that the above
7 142012ed 2022-10-20 thomas * copyright notice and this permission notice appear in all copies.
8 142012ed 2022-10-20 thomas *
9 142012ed 2022-10-20 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 142012ed 2022-10-20 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 142012ed 2022-10-20 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 142012ed 2022-10-20 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 142012ed 2022-10-20 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 142012ed 2022-10-20 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 142012ed 2022-10-20 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 142012ed 2022-10-20 thomas */
17 142012ed 2022-10-20 thomas
18 142012ed 2022-10-20 thomas #include <sys/queue.h>
19 142012ed 2022-10-20 thomas #include <sys/stat.h>
20 142012ed 2022-10-20 thomas #include <sys/time.h>
21 142012ed 2022-10-20 thomas #include <sys/types.h>
22 142012ed 2022-10-20 thomas #include <sys/uio.h>
23 142012ed 2022-10-20 thomas #include <sys/mman.h>
24 142012ed 2022-10-20 thomas
25 142012ed 2022-10-20 thomas #include <stdint.h>
26 142012ed 2022-10-20 thomas #include <errno.h>
27 142012ed 2022-10-20 thomas #include <imsg.h>
28 142012ed 2022-10-20 thomas #include <limits.h>
29 142012ed 2022-10-20 thomas #include <signal.h>
30 142012ed 2022-10-20 thomas #include <stdio.h>
31 142012ed 2022-10-20 thomas #include <stdlib.h>
32 142012ed 2022-10-20 thomas #include <string.h>
33 142012ed 2022-10-20 thomas #include <ctype.h>
34 142012ed 2022-10-20 thomas #include <fcntl.h>
35 142012ed 2022-10-20 thomas #include <unistd.h>
36 142012ed 2022-10-20 thomas #include <zlib.h>
37 142012ed 2022-10-20 thomas #include <err.h>
38 142012ed 2022-10-20 thomas #include <assert.h>
39 142012ed 2022-10-20 thomas #include <dirent.h>
40 142012ed 2022-10-20 thomas
41 142012ed 2022-10-20 thomas #include "got_error.h"
42 142012ed 2022-10-20 thomas #include "got_object.h"
43 142012ed 2022-10-20 thomas
44 142012ed 2022-10-20 thomas #include "got_lib_sha1.h"
45 142012ed 2022-10-20 thomas #include "got_lib_delta.h"
46 142012ed 2022-10-20 thomas #include "got_lib_inflate.h"
47 142012ed 2022-10-20 thomas #include "got_lib_object.h"
48 142012ed 2022-10-20 thomas #include "got_lib_object_parse.h"
49 142012ed 2022-10-20 thomas #include "got_lib_object_idset.h"
50 142012ed 2022-10-20 thomas #include "got_lib_privsep.h"
51 142012ed 2022-10-20 thomas #include "got_lib_pack.h"
52 142012ed 2022-10-20 thomas #include "got_lib_ratelimit.h"
53 142012ed 2022-10-20 thomas #include "got_lib_pack_index.h"
54 142012ed 2022-10-20 thomas #include "got_lib_delta_cache.h"
55 142012ed 2022-10-20 thomas
56 142012ed 2022-10-20 thomas struct got_indexed_object {
57 142012ed 2022-10-20 thomas struct got_object_id id;
58 142012ed 2022-10-20 thomas
59 142012ed 2022-10-20 thomas /*
60 142012ed 2022-10-20 thomas * Has this object been fully resolved?
61 142012ed 2022-10-20 thomas * If so, we know its ID, otherwise we don't and 'id' is invalid.
62 142012ed 2022-10-20 thomas */
63 142012ed 2022-10-20 thomas int valid;
64 142012ed 2022-10-20 thomas
65 142012ed 2022-10-20 thomas /* Offset of type+size field for this object in pack file. */
66 142012ed 2022-10-20 thomas off_t off;
67 142012ed 2022-10-20 thomas
68 142012ed 2022-10-20 thomas /* Type+size values parsed from pack file. */
69 142012ed 2022-10-20 thomas uint8_t type;
70 142012ed 2022-10-20 thomas uint64_t size;
71 142012ed 2022-10-20 thomas
72 142012ed 2022-10-20 thomas /* Length of on-disk type+size data. */
73 142012ed 2022-10-20 thomas size_t tslen;
74 142012ed 2022-10-20 thomas
75 142012ed 2022-10-20 thomas /* Length of object data following type+size. */
76 142012ed 2022-10-20 thomas size_t len;
77 142012ed 2022-10-20 thomas
78 142012ed 2022-10-20 thomas uint32_t crc;
79 142012ed 2022-10-20 thomas
80 142012ed 2022-10-20 thomas union {
81 142012ed 2022-10-20 thomas struct {
82 142012ed 2022-10-20 thomas /* For ref deltas. */
83 142012ed 2022-10-20 thomas struct got_object_id ref_id;
84 142012ed 2022-10-20 thomas } ref;
85 142012ed 2022-10-20 thomas struct {
86 142012ed 2022-10-20 thomas /* For offset deltas. */
87 142012ed 2022-10-20 thomas off_t base_offset;
88 142012ed 2022-10-20 thomas size_t base_offsetlen;
89 142012ed 2022-10-20 thomas } ofs;
90 142012ed 2022-10-20 thomas } delta;
91 142012ed 2022-10-20 thomas };
92 142012ed 2022-10-20 thomas
93 142012ed 2022-10-20 thomas static void
94 142012ed 2022-10-20 thomas putbe32(char *b, uint32_t n)
95 142012ed 2022-10-20 thomas {
96 142012ed 2022-10-20 thomas b[0] = n >> 24;
97 142012ed 2022-10-20 thomas b[1] = n >> 16;
98 142012ed 2022-10-20 thomas b[2] = n >> 8;
99 142012ed 2022-10-20 thomas b[3] = n >> 0;
100 142012ed 2022-10-20 thomas }
101 142012ed 2022-10-20 thomas
102 142012ed 2022-10-20 thomas static const struct got_error *
103 142012ed 2022-10-20 thomas get_obj_type_label(const char **label, int obj_type)
104 142012ed 2022-10-20 thomas {
105 142012ed 2022-10-20 thomas const struct got_error *err = NULL;
106 142012ed 2022-10-20 thomas
107 142012ed 2022-10-20 thomas switch (obj_type) {
108 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_BLOB:
109 142012ed 2022-10-20 thomas *label = GOT_OBJ_LABEL_BLOB;
110 142012ed 2022-10-20 thomas break;
111 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_TREE:
112 142012ed 2022-10-20 thomas *label = GOT_OBJ_LABEL_TREE;
113 142012ed 2022-10-20 thomas break;
114 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_COMMIT:
115 142012ed 2022-10-20 thomas *label = GOT_OBJ_LABEL_COMMIT;
116 142012ed 2022-10-20 thomas break;
117 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_TAG:
118 142012ed 2022-10-20 thomas *label = GOT_OBJ_LABEL_TAG;
119 142012ed 2022-10-20 thomas break;
120 142012ed 2022-10-20 thomas default:
121 142012ed 2022-10-20 thomas *label = NULL;
122 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_OBJ_TYPE);
123 142012ed 2022-10-20 thomas break;
124 142012ed 2022-10-20 thomas }
125 142012ed 2022-10-20 thomas
126 142012ed 2022-10-20 thomas return err;
127 142012ed 2022-10-20 thomas }
128 142012ed 2022-10-20 thomas
129 142012ed 2022-10-20 thomas static const struct got_error *
130 142012ed 2022-10-20 thomas read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
131 142012ed 2022-10-20 thomas {
132 142012ed 2022-10-20 thomas uint8_t buf[8192];
133 142012ed 2022-10-20 thomas size_t n;
134 142012ed 2022-10-20 thomas ssize_t r;
135 142012ed 2022-10-20 thomas
136 142012ed 2022-10-20 thomas for (n = len; n > 0; n -= r){
137 142012ed 2022-10-20 thomas r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
138 142012ed 2022-10-20 thomas if (r == -1)
139 142012ed 2022-10-20 thomas return got_error_from_errno("read");
140 142012ed 2022-10-20 thomas if (r == 0)
141 142012ed 2022-10-20 thomas break;
142 142012ed 2022-10-20 thomas if (crc)
143 142012ed 2022-10-20 thomas *crc = crc32(*crc, buf, r);
144 142012ed 2022-10-20 thomas if (sha1_ctx)
145 142012ed 2022-10-20 thomas SHA1Update(sha1_ctx, buf, r);
146 142012ed 2022-10-20 thomas }
147 142012ed 2022-10-20 thomas
148 142012ed 2022-10-20 thomas return NULL;
149 142012ed 2022-10-20 thomas }
150 142012ed 2022-10-20 thomas
151 142012ed 2022-10-20 thomas static const struct got_error *
152 142012ed 2022-10-20 thomas read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
153 142012ed 2022-10-20 thomas {
154 142012ed 2022-10-20 thomas uint8_t buf[8192];
155 142012ed 2022-10-20 thomas size_t n, r;
156 142012ed 2022-10-20 thomas
157 142012ed 2022-10-20 thomas for (n = len; n > 0; n -= r) {
158 142012ed 2022-10-20 thomas r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
159 142012ed 2022-10-20 thomas if (r == 0) {
160 142012ed 2022-10-20 thomas if (feof(f))
161 142012ed 2022-10-20 thomas return NULL;
162 142012ed 2022-10-20 thomas return got_ferror(f, GOT_ERR_IO);
163 142012ed 2022-10-20 thomas }
164 142012ed 2022-10-20 thomas SHA1Update(ctx, buf, r);
165 142012ed 2022-10-20 thomas }
166 142012ed 2022-10-20 thomas
167 142012ed 2022-10-20 thomas return NULL;
168 142012ed 2022-10-20 thomas }
169 142012ed 2022-10-20 thomas
170 142012ed 2022-10-20 thomas static const struct got_error *
171 142012ed 2022-10-20 thomas read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
172 142012ed 2022-10-20 thomas FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
173 142012ed 2022-10-20 thomas {
174 142012ed 2022-10-20 thomas const struct got_error *err = NULL;
175 142012ed 2022-10-20 thomas SHA1_CTX ctx;
176 142012ed 2022-10-20 thomas uint8_t *data = NULL;
177 142012ed 2022-10-20 thomas size_t datalen = 0;
178 142012ed 2022-10-20 thomas ssize_t n;
179 142012ed 2022-10-20 thomas char *header;
180 142012ed 2022-10-20 thomas size_t headerlen;
181 142012ed 2022-10-20 thomas const char *obj_label;
182 142012ed 2022-10-20 thomas size_t mapoff = obj->off;
183 142012ed 2022-10-20 thomas struct got_inflate_checksum csum;
184 142012ed 2022-10-20 thomas
185 142012ed 2022-10-20 thomas memset(&csum, 0, sizeof(csum));
186 142012ed 2022-10-20 thomas csum.input_sha1 = pack_sha1_ctx;
187 142012ed 2022-10-20 thomas csum.input_crc = &obj->crc;
188 142012ed 2022-10-20 thomas
189 142012ed 2022-10-20 thomas err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
190 142012ed 2022-10-20 thomas &obj->tslen, pack, obj->off);
191 142012ed 2022-10-20 thomas if (err)
192 142012ed 2022-10-20 thomas return err;
193 142012ed 2022-10-20 thomas
194 142012ed 2022-10-20 thomas if (pack->map) {
195 142012ed 2022-10-20 thomas obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
196 142012ed 2022-10-20 thomas SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
197 142012ed 2022-10-20 thomas mapoff += obj->tslen;
198 142012ed 2022-10-20 thomas } else {
199 142012ed 2022-10-20 thomas /* XXX Seek back and get the CRC of on-disk type+size bytes. */
200 142012ed 2022-10-20 thomas if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
201 142012ed 2022-10-20 thomas return got_error_from_errno("lseek");
202 142012ed 2022-10-20 thomas err = read_checksum(&obj->crc, pack_sha1_ctx,
203 142012ed 2022-10-20 thomas pack->fd, obj->tslen);
204 142012ed 2022-10-20 thomas if (err)
205 142012ed 2022-10-20 thomas return err;
206 142012ed 2022-10-20 thomas }
207 142012ed 2022-10-20 thomas
208 142012ed 2022-10-20 thomas switch (obj->type) {
209 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_BLOB:
210 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_COMMIT:
211 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_TREE:
212 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_TAG:
213 142012ed 2022-10-20 thomas if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
214 142012ed 2022-10-20 thomas if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
215 142012ed 2022-10-20 thomas err = got_error_from_errno("fseek");
216 142012ed 2022-10-20 thomas break;
217 142012ed 2022-10-20 thomas }
218 142012ed 2022-10-20 thomas if (pack->map) {
219 142012ed 2022-10-20 thomas err = got_inflate_to_file_mmap(&datalen,
220 142012ed 2022-10-20 thomas &obj->len, &csum, pack->map, mapoff,
221 142012ed 2022-10-20 thomas pack->filesize - mapoff, tmpfile);
222 142012ed 2022-10-20 thomas } else {
223 142012ed 2022-10-20 thomas err = got_inflate_to_file_fd(&datalen,
224 142012ed 2022-10-20 thomas &obj->len, &csum, pack->fd, tmpfile);
225 142012ed 2022-10-20 thomas }
226 142012ed 2022-10-20 thomas } else {
227 142012ed 2022-10-20 thomas if (pack->map) {
228 142012ed 2022-10-20 thomas err = got_inflate_to_mem_mmap(&data, &datalen,
229 142012ed 2022-10-20 thomas &obj->len, &csum, pack->map, mapoff,
230 142012ed 2022-10-20 thomas pack->filesize - mapoff);
231 142012ed 2022-10-20 thomas } else {
232 142012ed 2022-10-20 thomas err = got_inflate_to_mem_fd(&data, &datalen,
233 142012ed 2022-10-20 thomas &obj->len, &csum, obj->size, pack->fd);
234 142012ed 2022-10-20 thomas }
235 142012ed 2022-10-20 thomas }
236 142012ed 2022-10-20 thomas if (err)
237 142012ed 2022-10-20 thomas break;
238 142012ed 2022-10-20 thomas SHA1Init(&ctx);
239 142012ed 2022-10-20 thomas err = get_obj_type_label(&obj_label, obj->type);
240 142012ed 2022-10-20 thomas if (err) {
241 142012ed 2022-10-20 thomas free(data);
242 142012ed 2022-10-20 thomas break;
243 142012ed 2022-10-20 thomas }
244 142012ed 2022-10-20 thomas if (asprintf(&header, "%s %lld", obj_label,
245 142012ed 2022-10-20 thomas (long long)obj->size) == -1) {
246 142012ed 2022-10-20 thomas err = got_error_from_errno("asprintf");
247 142012ed 2022-10-20 thomas free(data);
248 142012ed 2022-10-20 thomas break;
249 142012ed 2022-10-20 thomas }
250 142012ed 2022-10-20 thomas headerlen = strlen(header) + 1;
251 142012ed 2022-10-20 thomas SHA1Update(&ctx, header, headerlen);
252 142012ed 2022-10-20 thomas if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
253 142012ed 2022-10-20 thomas err = read_file_sha1(&ctx, tmpfile, datalen);
254 142012ed 2022-10-20 thomas if (err) {
255 142012ed 2022-10-20 thomas free(header);
256 142012ed 2022-10-20 thomas free(data);
257 142012ed 2022-10-20 thomas break;
258 142012ed 2022-10-20 thomas }
259 142012ed 2022-10-20 thomas } else
260 142012ed 2022-10-20 thomas SHA1Update(&ctx, data, datalen);
261 142012ed 2022-10-20 thomas SHA1Final(obj->id.sha1, &ctx);
262 142012ed 2022-10-20 thomas free(header);
263 142012ed 2022-10-20 thomas free(data);
264 142012ed 2022-10-20 thomas break;
265 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_REF_DELTA:
266 142012ed 2022-10-20 thomas memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
267 142012ed 2022-10-20 thomas if (pack->map) {
268 142012ed 2022-10-20 thomas if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
269 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_BAD_PACKFILE);
270 66e6097f 2022-10-27 thomas break;
271 66e6097f 2022-10-27 thomas }
272 66e6097f 2022-10-27 thomas if (mapoff + SHA1_DIGEST_LENGTH > SIZE_MAX) {
273 66e6097f 2022-10-27 thomas err = got_error_fmt(GOT_ERR_RANGE,
274 66e6097f 2022-10-27 thomas "mapoff %lld would overflow size_t",
275 66e6097f 2022-10-27 thomas (long long)mapoff + SHA1_DIGEST_LENGTH);
276 142012ed 2022-10-20 thomas break;
277 142012ed 2022-10-20 thomas }
278 142012ed 2022-10-20 thomas memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
279 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
280 142012ed 2022-10-20 thomas obj->crc = crc32(obj->crc, pack->map + mapoff,
281 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
282 142012ed 2022-10-20 thomas SHA1Update(pack_sha1_ctx, pack->map + mapoff,
283 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
284 142012ed 2022-10-20 thomas mapoff += SHA1_DIGEST_LENGTH;
285 142012ed 2022-10-20 thomas err = got_inflate_to_mem_mmap(NULL, &datalen,
286 142012ed 2022-10-20 thomas &obj->len, &csum, pack->map, mapoff,
287 142012ed 2022-10-20 thomas pack->filesize - mapoff);
288 142012ed 2022-10-20 thomas if (err)
289 142012ed 2022-10-20 thomas break;
290 142012ed 2022-10-20 thomas } else {
291 142012ed 2022-10-20 thomas n = read(pack->fd, obj->delta.ref.ref_id.sha1,
292 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
293 142012ed 2022-10-20 thomas if (n == -1) {
294 142012ed 2022-10-20 thomas err = got_error_from_errno("read");
295 142012ed 2022-10-20 thomas break;
296 142012ed 2022-10-20 thomas }
297 142012ed 2022-10-20 thomas if (n < sizeof(obj->id)) {
298 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_BAD_PACKFILE);
299 142012ed 2022-10-20 thomas break;
300 142012ed 2022-10-20 thomas }
301 142012ed 2022-10-20 thomas obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
302 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
303 142012ed 2022-10-20 thomas SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
304 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
305 142012ed 2022-10-20 thomas err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
306 142012ed 2022-10-20 thomas &csum, obj->size, pack->fd);
307 142012ed 2022-10-20 thomas if (err)
308 142012ed 2022-10-20 thomas break;
309 142012ed 2022-10-20 thomas }
310 142012ed 2022-10-20 thomas obj->len += SHA1_DIGEST_LENGTH;
311 142012ed 2022-10-20 thomas break;
312 142012ed 2022-10-20 thomas case GOT_OBJ_TYPE_OFFSET_DELTA:
313 142012ed 2022-10-20 thomas memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
314 142012ed 2022-10-20 thomas err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
315 142012ed 2022-10-20 thomas &obj->delta.ofs.base_offsetlen, pack, obj->off,
316 142012ed 2022-10-20 thomas obj->tslen);
317 142012ed 2022-10-20 thomas if (err)
318 142012ed 2022-10-20 thomas break;
319 142012ed 2022-10-20 thomas
320 142012ed 2022-10-20 thomas if (pack->map) {
321 6cf83e6a 2022-10-25 thomas if (mapoff + obj->delta.ofs.base_offsetlen >=
322 6cf83e6a 2022-10-25 thomas pack->filesize) {
323 6cf83e6a 2022-10-25 thomas err = got_error(GOT_ERR_BAD_PACKFILE);
324 6cf83e6a 2022-10-25 thomas break;
325 6cf83e6a 2022-10-25 thomas }
326 6cf83e6a 2022-10-25 thomas
327 66e6097f 2022-10-27 thomas if (mapoff + obj->delta.ofs.base_offsetlen >
328 66e6097f 2022-10-27 thomas SIZE_MAX) {
329 66e6097f 2022-10-27 thomas err = got_error_fmt(GOT_ERR_RANGE,
330 66e6097f 2022-10-27 thomas "mapoff %lld would overflow size_t",
331 66e6097f 2022-10-27 thomas (long long)mapoff
332 66e6097f 2022-10-27 thomas + obj->delta.ofs.base_offsetlen);
333 66e6097f 2022-10-27 thomas }
334 66e6097f 2022-10-27 thomas
335 142012ed 2022-10-20 thomas obj->crc = crc32(obj->crc, pack->map + mapoff,
336 142012ed 2022-10-20 thomas obj->delta.ofs.base_offsetlen);
337 142012ed 2022-10-20 thomas SHA1Update(pack_sha1_ctx, pack->map + mapoff,
338 142012ed 2022-10-20 thomas obj->delta.ofs.base_offsetlen);
339 142012ed 2022-10-20 thomas mapoff += obj->delta.ofs.base_offsetlen;
340 142012ed 2022-10-20 thomas err = got_inflate_to_mem_mmap(NULL, &datalen,
341 142012ed 2022-10-20 thomas &obj->len, &csum, pack->map, mapoff,
342 142012ed 2022-10-20 thomas pack->filesize - mapoff);
343 142012ed 2022-10-20 thomas if (err)
344 142012ed 2022-10-20 thomas break;
345 142012ed 2022-10-20 thomas } else {
346 142012ed 2022-10-20 thomas /*
347 142012ed 2022-10-20 thomas * XXX Seek back and get CRC and SHA1 of on-disk
348 142012ed 2022-10-20 thomas * offset bytes.
349 142012ed 2022-10-20 thomas */
350 142012ed 2022-10-20 thomas if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
351 142012ed 2022-10-20 thomas == -1) {
352 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
353 142012ed 2022-10-20 thomas break;
354 142012ed 2022-10-20 thomas }
355 142012ed 2022-10-20 thomas err = read_checksum(&obj->crc, pack_sha1_ctx,
356 142012ed 2022-10-20 thomas pack->fd, obj->delta.ofs.base_offsetlen);
357 142012ed 2022-10-20 thomas if (err)
358 142012ed 2022-10-20 thomas break;
359 142012ed 2022-10-20 thomas
360 142012ed 2022-10-20 thomas err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
361 142012ed 2022-10-20 thomas &csum, obj->size, pack->fd);
362 142012ed 2022-10-20 thomas if (err)
363 142012ed 2022-10-20 thomas break;
364 142012ed 2022-10-20 thomas }
365 142012ed 2022-10-20 thomas obj->len += obj->delta.ofs.base_offsetlen;
366 142012ed 2022-10-20 thomas break;
367 142012ed 2022-10-20 thomas default:
368 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_OBJ_TYPE);
369 142012ed 2022-10-20 thomas break;
370 142012ed 2022-10-20 thomas }
371 142012ed 2022-10-20 thomas
372 142012ed 2022-10-20 thomas return err;
373 142012ed 2022-10-20 thomas }
374 142012ed 2022-10-20 thomas
375 3efd8e31 2022-10-23 thomas const struct got_error *
376 3efd8e31 2022-10-23 thomas got_pack_hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
377 142012ed 2022-10-20 thomas {
378 142012ed 2022-10-20 thomas ssize_t w;
379 142012ed 2022-10-20 thomas
380 142012ed 2022-10-20 thomas SHA1Update(ctx, buf, len);
381 142012ed 2022-10-20 thomas
382 142012ed 2022-10-20 thomas w = write(fd, buf, len);
383 142012ed 2022-10-20 thomas if (w == -1)
384 142012ed 2022-10-20 thomas return got_error_from_errno("write");
385 142012ed 2022-10-20 thomas if (w != len)
386 142012ed 2022-10-20 thomas return got_error(GOT_ERR_IO);
387 142012ed 2022-10-20 thomas
388 142012ed 2022-10-20 thomas return NULL;
389 142012ed 2022-10-20 thomas }
390 142012ed 2022-10-20 thomas
391 142012ed 2022-10-20 thomas static const struct got_error *
392 142012ed 2022-10-20 thomas resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
393 142012ed 2022-10-20 thomas struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
394 142012ed 2022-10-20 thomas FILE *delta_accum_file)
395 142012ed 2022-10-20 thomas {
396 142012ed 2022-10-20 thomas const struct got_error *err = NULL;
397 142012ed 2022-10-20 thomas struct got_delta_chain deltas;
398 142012ed 2022-10-20 thomas struct got_delta *delta;
399 142012ed 2022-10-20 thomas uint8_t *buf = NULL;
400 142012ed 2022-10-20 thomas size_t len = 0;
401 142012ed 2022-10-20 thomas SHA1_CTX ctx;
402 142012ed 2022-10-20 thomas char *header = NULL;
403 142012ed 2022-10-20 thomas size_t headerlen;
404 142012ed 2022-10-20 thomas uint64_t max_size;
405 142012ed 2022-10-20 thomas int base_obj_type;
406 142012ed 2022-10-20 thomas const char *obj_label;
407 142012ed 2022-10-20 thomas
408 142012ed 2022-10-20 thomas deltas.nentries = 0;
409 142012ed 2022-10-20 thomas STAILQ_INIT(&deltas.entries);
410 142012ed 2022-10-20 thomas
411 142012ed 2022-10-20 thomas err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
412 142012ed 2022-10-20 thomas obj->off, obj->tslen, obj->type, obj->size,
413 142012ed 2022-10-20 thomas GOT_DELTA_CHAIN_RECURSION_MAX);
414 142012ed 2022-10-20 thomas if (err)
415 142012ed 2022-10-20 thomas goto done;
416 142012ed 2022-10-20 thomas
417 142012ed 2022-10-20 thomas err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
418 142012ed 2022-10-20 thomas if (err)
419 142012ed 2022-10-20 thomas goto done;
420 142012ed 2022-10-20 thomas if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
421 142012ed 2022-10-20 thomas rewind(tmpfile);
422 142012ed 2022-10-20 thomas rewind(delta_base_file);
423 142012ed 2022-10-20 thomas rewind(delta_accum_file);
424 142012ed 2022-10-20 thomas err = got_pack_dump_delta_chain_to_file(&len, &deltas,
425 142012ed 2022-10-20 thomas pack, tmpfile, delta_base_file, delta_accum_file);
426 142012ed 2022-10-20 thomas if (err)
427 142012ed 2022-10-20 thomas goto done;
428 142012ed 2022-10-20 thomas } else {
429 142012ed 2022-10-20 thomas err = got_pack_dump_delta_chain_to_mem(&buf, &len,
430 142012ed 2022-10-20 thomas &deltas, pack);
431 142012ed 2022-10-20 thomas }
432 142012ed 2022-10-20 thomas if (err)
433 142012ed 2022-10-20 thomas goto done;
434 142012ed 2022-10-20 thomas
435 142012ed 2022-10-20 thomas err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
436 142012ed 2022-10-20 thomas if (err)
437 142012ed 2022-10-20 thomas goto done;
438 142012ed 2022-10-20 thomas err = get_obj_type_label(&obj_label, base_obj_type);
439 142012ed 2022-10-20 thomas if (err)
440 142012ed 2022-10-20 thomas goto done;
441 142012ed 2022-10-20 thomas if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
442 142012ed 2022-10-20 thomas err = got_error_from_errno("asprintf");
443 142012ed 2022-10-20 thomas goto done;
444 142012ed 2022-10-20 thomas }
445 142012ed 2022-10-20 thomas headerlen = strlen(header) + 1;
446 142012ed 2022-10-20 thomas SHA1Init(&ctx);
447 142012ed 2022-10-20 thomas SHA1Update(&ctx, header, headerlen);
448 142012ed 2022-10-20 thomas if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
449 142012ed 2022-10-20 thomas err = read_file_sha1(&ctx, tmpfile, len);
450 142012ed 2022-10-20 thomas if (err)
451 142012ed 2022-10-20 thomas goto done;
452 142012ed 2022-10-20 thomas } else
453 142012ed 2022-10-20 thomas SHA1Update(&ctx, buf, len);
454 142012ed 2022-10-20 thomas SHA1Final(obj->id.sha1, &ctx);
455 142012ed 2022-10-20 thomas done:
456 142012ed 2022-10-20 thomas free(buf);
457 142012ed 2022-10-20 thomas free(header);
458 142012ed 2022-10-20 thomas while (!STAILQ_EMPTY(&deltas.entries)) {
459 142012ed 2022-10-20 thomas delta = STAILQ_FIRST(&deltas.entries);
460 142012ed 2022-10-20 thomas STAILQ_REMOVE_HEAD(&deltas.entries, entry);
461 142012ed 2022-10-20 thomas free(delta);
462 142012ed 2022-10-20 thomas }
463 142012ed 2022-10-20 thomas return err;
464 142012ed 2022-10-20 thomas }
465 142012ed 2022-10-20 thomas
466 142012ed 2022-10-20 thomas /* Determine the slot in the pack index a given object ID should use. */
467 142012ed 2022-10-20 thomas static int
468 142012ed 2022-10-20 thomas find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
469 142012ed 2022-10-20 thomas {
470 142012ed 2022-10-20 thomas u_int8_t id0 = sha1[0];
471 142012ed 2022-10-20 thomas uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
472 142012ed 2022-10-20 thomas int left = 0, right = nindexed - 1;
473 142012ed 2022-10-20 thomas int cmp = 0, i = 0;
474 142012ed 2022-10-20 thomas
475 142012ed 2022-10-20 thomas if (id0 > 0)
476 142012ed 2022-10-20 thomas left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
477 142012ed 2022-10-20 thomas
478 142012ed 2022-10-20 thomas while (left <= right) {
479 142012ed 2022-10-20 thomas struct got_packidx_object_id *oid;
480 142012ed 2022-10-20 thomas
481 142012ed 2022-10-20 thomas i = ((left + right) / 2);
482 142012ed 2022-10-20 thomas oid = &packidx->hdr.sorted_ids[i];
483 142012ed 2022-10-20 thomas
484 142012ed 2022-10-20 thomas cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
485 142012ed 2022-10-20 thomas if (cmp == 0)
486 142012ed 2022-10-20 thomas return -1; /* object already indexed */
487 142012ed 2022-10-20 thomas else if (cmp > 0)
488 142012ed 2022-10-20 thomas left = i + 1;
489 142012ed 2022-10-20 thomas else if (cmp < 0)
490 142012ed 2022-10-20 thomas right = i - 1;
491 142012ed 2022-10-20 thomas }
492 142012ed 2022-10-20 thomas
493 142012ed 2022-10-20 thomas return left;
494 142012ed 2022-10-20 thomas }
495 142012ed 2022-10-20 thomas
496 142012ed 2022-10-20 thomas #if 0
497 142012ed 2022-10-20 thomas static void
498 142012ed 2022-10-20 thomas print_packidx(struct got_packidx *packidx)
499 142012ed 2022-10-20 thomas {
500 142012ed 2022-10-20 thomas uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
501 142012ed 2022-10-20 thomas int i;
502 142012ed 2022-10-20 thomas
503 142012ed 2022-10-20 thomas fprintf(stderr, "object IDs:\n");
504 142012ed 2022-10-20 thomas for (i = 0; i < nindexed; i++) {
505 142012ed 2022-10-20 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
506 142012ed 2022-10-20 thomas got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
507 142012ed 2022-10-20 thomas hex, sizeof(hex));
508 142012ed 2022-10-20 thomas fprintf(stderr, "%s\n", hex);
509 142012ed 2022-10-20 thomas }
510 142012ed 2022-10-20 thomas fprintf(stderr, "\n");
511 142012ed 2022-10-20 thomas
512 142012ed 2022-10-20 thomas fprintf(stderr, "object offsets:\n");
513 142012ed 2022-10-20 thomas for (i = 0; i < nindexed; i++) {
514 142012ed 2022-10-20 thomas uint32_t offset = be32toh(packidx->hdr.offsets[i]);
515 142012ed 2022-10-20 thomas if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
516 142012ed 2022-10-20 thomas int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
517 142012ed 2022-10-20 thomas fprintf(stderr, "%u -> %llu\n", offset,
518 142012ed 2022-10-20 thomas be64toh(packidx->hdr.large_offsets[j]));
519 142012ed 2022-10-20 thomas } else
520 142012ed 2022-10-20 thomas fprintf(stderr, "%u\n", offset);
521 142012ed 2022-10-20 thomas }
522 142012ed 2022-10-20 thomas fprintf(stderr, "\n");
523 142012ed 2022-10-20 thomas
524 142012ed 2022-10-20 thomas fprintf(stderr, "fanout table:");
525 142012ed 2022-10-20 thomas for (i = 0; i <= 0xff; i++)
526 142012ed 2022-10-20 thomas fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
527 142012ed 2022-10-20 thomas fprintf(stderr, "\n");
528 142012ed 2022-10-20 thomas }
529 142012ed 2022-10-20 thomas #endif
530 142012ed 2022-10-20 thomas
531 142012ed 2022-10-20 thomas static void
532 142012ed 2022-10-20 thomas add_indexed_object(struct got_packidx *packidx, uint32_t idx,
533 142012ed 2022-10-20 thomas struct got_indexed_object *obj)
534 142012ed 2022-10-20 thomas {
535 142012ed 2022-10-20 thomas int i;
536 142012ed 2022-10-20 thomas
537 142012ed 2022-10-20 thomas memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
538 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
539 142012ed 2022-10-20 thomas packidx->hdr.crc32[idx] = htobe32(obj->crc);
540 142012ed 2022-10-20 thomas if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
541 142012ed 2022-10-20 thomas packidx->hdr.offsets[idx] = htobe32(obj->off);
542 142012ed 2022-10-20 thomas else {
543 142012ed 2022-10-20 thomas packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
544 142012ed 2022-10-20 thomas GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
545 142012ed 2022-10-20 thomas packidx->hdr.large_offsets[packidx->nlargeobj] =
546 142012ed 2022-10-20 thomas htobe64(obj->off);
547 142012ed 2022-10-20 thomas packidx->nlargeobj++;
548 142012ed 2022-10-20 thomas }
549 142012ed 2022-10-20 thomas
550 142012ed 2022-10-20 thomas for (i = obj->id.sha1[0]; i <= 0xff; i++) {
551 142012ed 2022-10-20 thomas uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
552 142012ed 2022-10-20 thomas packidx->hdr.fanout_table[i] = htobe32(n + 1);
553 142012ed 2022-10-20 thomas }
554 142012ed 2022-10-20 thomas }
555 142012ed 2022-10-20 thomas
556 142012ed 2022-10-20 thomas static int
557 142012ed 2022-10-20 thomas indexed_obj_cmp(const void *pa, const void *pb)
558 142012ed 2022-10-20 thomas {
559 142012ed 2022-10-20 thomas struct got_indexed_object *a, *b;
560 142012ed 2022-10-20 thomas
561 142012ed 2022-10-20 thomas a = (struct got_indexed_object *)pa;
562 142012ed 2022-10-20 thomas b = (struct got_indexed_object *)pb;
563 142012ed 2022-10-20 thomas return got_object_id_cmp(&a->id, &b->id);
564 142012ed 2022-10-20 thomas }
565 142012ed 2022-10-20 thomas
566 142012ed 2022-10-20 thomas static void
567 eebe1fbb 2022-10-20 thomas make_packidx(struct got_packidx *packidx, uint32_t nobj,
568 142012ed 2022-10-20 thomas struct got_indexed_object *objects)
569 142012ed 2022-10-20 thomas {
570 142012ed 2022-10-20 thomas struct got_indexed_object *obj;
571 142012ed 2022-10-20 thomas int i;
572 142012ed 2022-10-20 thomas uint32_t idx = 0;
573 142012ed 2022-10-20 thomas
574 142012ed 2022-10-20 thomas qsort(objects, nobj, sizeof(struct got_indexed_object),
575 142012ed 2022-10-20 thomas indexed_obj_cmp);
576 142012ed 2022-10-20 thomas
577 142012ed 2022-10-20 thomas memset(packidx->hdr.fanout_table, 0,
578 142012ed 2022-10-20 thomas GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
579 142012ed 2022-10-20 thomas packidx->nlargeobj = 0;
580 142012ed 2022-10-20 thomas
581 142012ed 2022-10-20 thomas for (i = 0; i < nobj; i++) {
582 142012ed 2022-10-20 thomas obj = &objects[i];
583 142012ed 2022-10-20 thomas if (obj->valid)
584 142012ed 2022-10-20 thomas add_indexed_object(packidx, idx++, obj);
585 142012ed 2022-10-20 thomas }
586 142012ed 2022-10-20 thomas }
587 142012ed 2022-10-20 thomas
588 142012ed 2022-10-20 thomas static void
589 eebe1fbb 2022-10-20 thomas update_packidx(struct got_packidx *packidx, uint32_t nobj,
590 142012ed 2022-10-20 thomas struct got_indexed_object *obj)
591 142012ed 2022-10-20 thomas {
592 142012ed 2022-10-20 thomas int idx;
593 142012ed 2022-10-20 thomas uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
594 142012ed 2022-10-20 thomas
595 142012ed 2022-10-20 thomas idx = find_object_idx(packidx, obj->id.sha1);
596 aac3e701 2023-02-03 thomas if (idx == -1)
597 142012ed 2022-10-20 thomas return; /* object already indexed */
598 142012ed 2022-10-20 thomas
599 142012ed 2022-10-20 thomas memmove(&packidx->hdr.sorted_ids[idx + 1],
600 142012ed 2022-10-20 thomas &packidx->hdr.sorted_ids[idx],
601 142012ed 2022-10-20 thomas sizeof(struct got_packidx_object_id) * (nindexed - idx));
602 142012ed 2022-10-20 thomas memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
603 142012ed 2022-10-20 thomas sizeof(uint32_t) * (nindexed - idx));
604 142012ed 2022-10-20 thomas
605 142012ed 2022-10-20 thomas add_indexed_object(packidx, idx, obj);
606 142012ed 2022-10-20 thomas }
607 142012ed 2022-10-20 thomas
608 142012ed 2022-10-20 thomas static const struct got_error *
609 eebe1fbb 2022-10-20 thomas report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
610 eebe1fbb 2022-10-20 thomas uint32_t nobj_resolved, struct got_ratelimit *rl,
611 142012ed 2022-10-20 thomas got_pack_index_progress_cb progress_cb, void *progress_arg)
612 142012ed 2022-10-20 thomas {
613 142012ed 2022-10-20 thomas const struct got_error *err;
614 142012ed 2022-10-20 thomas int elapsed = 0;
615 142012ed 2022-10-20 thomas
616 142012ed 2022-10-20 thomas if (rl) {
617 142012ed 2022-10-20 thomas err = got_ratelimit_check(&elapsed, rl);
618 142012ed 2022-10-20 thomas if (err || !elapsed)
619 142012ed 2022-10-20 thomas return err;
620 142012ed 2022-10-20 thomas }
621 142012ed 2022-10-20 thomas
622 142012ed 2022-10-20 thomas return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
623 142012ed 2022-10-20 thomas nobj_resolved);
624 142012ed 2022-10-20 thomas }
625 142012ed 2022-10-20 thomas
626 142012ed 2022-10-20 thomas const struct got_error *
627 142012ed 2022-10-20 thomas got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
628 142012ed 2022-10-20 thomas FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
629 aecd2225 2022-10-20 thomas got_pack_index_progress_cb progress_cb, void *progress_arg,
630 aecd2225 2022-10-20 thomas struct got_ratelimit *rl)
631 142012ed 2022-10-20 thomas {
632 142012ed 2022-10-20 thomas const struct got_error *err;
633 142012ed 2022-10-20 thomas struct got_packfile_hdr hdr;
634 142012ed 2022-10-20 thomas struct got_packidx packidx;
635 142012ed 2022-10-20 thomas char buf[8];
636 142012ed 2022-10-20 thomas char pack_sha1[SHA1_DIGEST_LENGTH];
637 eebe1fbb 2022-10-20 thomas uint32_t nobj, nvalid, nloose, nresolved = 0, i;
638 142012ed 2022-10-20 thomas struct got_indexed_object *objects = NULL, *obj;
639 142012ed 2022-10-20 thomas SHA1_CTX ctx;
640 142012ed 2022-10-20 thomas uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
641 142012ed 2022-10-20 thomas ssize_t r, w;
642 142012ed 2022-10-20 thomas int pass, have_ref_deltas = 0, first_delta_idx = -1;
643 142012ed 2022-10-20 thomas size_t mapoff = 0;
644 142012ed 2022-10-20 thomas int p_indexed = 0, last_p_indexed = -1;
645 142012ed 2022-10-20 thomas int p_resolved = 0, last_p_resolved = -1;
646 142012ed 2022-10-20 thomas
647 142012ed 2022-10-20 thomas /* Require that pack file header and SHA1 trailer are present. */
648 142012ed 2022-10-20 thomas if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
649 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
650 142012ed 2022-10-20 thomas "short pack file");
651 142012ed 2022-10-20 thomas
652 142012ed 2022-10-20 thomas if (pack->map) {
653 142012ed 2022-10-20 thomas memcpy(&hdr, pack->map, sizeof(hdr));
654 142012ed 2022-10-20 thomas mapoff += sizeof(hdr);
655 142012ed 2022-10-20 thomas } else {
656 142012ed 2022-10-20 thomas r = read(pack->fd, &hdr, sizeof(hdr));
657 142012ed 2022-10-20 thomas if (r == -1)
658 142012ed 2022-10-20 thomas return got_error_from_errno("read");
659 142012ed 2022-10-20 thomas if (r < sizeof(hdr))
660 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
661 142012ed 2022-10-20 thomas "short pack file");
662 142012ed 2022-10-20 thomas }
663 142012ed 2022-10-20 thomas
664 142012ed 2022-10-20 thomas if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
665 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
666 142012ed 2022-10-20 thomas "bad packfile signature");
667 142012ed 2022-10-20 thomas if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
668 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
669 142012ed 2022-10-20 thomas "bad packfile version");
670 142012ed 2022-10-20 thomas nobj = be32toh(hdr.nobjects);
671 142012ed 2022-10-20 thomas if (nobj == 0)
672 142012ed 2022-10-20 thomas return got_error_msg(GOT_ERR_BAD_PACKFILE,
673 142012ed 2022-10-20 thomas "bad packfile with zero objects");
674 142012ed 2022-10-20 thomas
675 142012ed 2022-10-20 thomas /* We compute the SHA1 of pack file contents and verify later on. */
676 142012ed 2022-10-20 thomas SHA1Init(&ctx);
677 142012ed 2022-10-20 thomas SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
678 142012ed 2022-10-20 thomas
679 142012ed 2022-10-20 thomas /*
680 142012ed 2022-10-20 thomas * Create an in-memory pack index which will grow as objects
681 142012ed 2022-10-20 thomas * IDs in the pack file are discovered. Only fields used to
682 142012ed 2022-10-20 thomas * read deltified objects will be needed by the pack.c library
683 142012ed 2022-10-20 thomas * code, so setting up just a pack index header is sufficient.
684 142012ed 2022-10-20 thomas */
685 142012ed 2022-10-20 thomas memset(&packidx, 0, sizeof(packidx));
686 142012ed 2022-10-20 thomas packidx.hdr.magic = malloc(sizeof(uint32_t));
687 142012ed 2022-10-20 thomas if (packidx.hdr.magic == NULL)
688 142012ed 2022-10-20 thomas return got_error_from_errno("malloc");
689 142012ed 2022-10-20 thomas *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
690 142012ed 2022-10-20 thomas packidx.hdr.version = malloc(sizeof(uint32_t));
691 142012ed 2022-10-20 thomas if (packidx.hdr.version == NULL) {
692 142012ed 2022-10-20 thomas err = got_error_from_errno("malloc");
693 142012ed 2022-10-20 thomas goto done;
694 142012ed 2022-10-20 thomas }
695 142012ed 2022-10-20 thomas *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
696 142012ed 2022-10-20 thomas packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
697 142012ed 2022-10-20 thomas sizeof(uint32_t));
698 142012ed 2022-10-20 thomas if (packidx.hdr.fanout_table == NULL) {
699 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
700 142012ed 2022-10-20 thomas goto done;
701 142012ed 2022-10-20 thomas }
702 142012ed 2022-10-20 thomas packidx.hdr.sorted_ids = calloc(nobj,
703 142012ed 2022-10-20 thomas sizeof(struct got_packidx_object_id));
704 142012ed 2022-10-20 thomas if (packidx.hdr.sorted_ids == NULL) {
705 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
706 142012ed 2022-10-20 thomas goto done;
707 142012ed 2022-10-20 thomas }
708 142012ed 2022-10-20 thomas packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
709 142012ed 2022-10-20 thomas if (packidx.hdr.crc32 == NULL) {
710 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
711 142012ed 2022-10-20 thomas goto done;
712 142012ed 2022-10-20 thomas }
713 142012ed 2022-10-20 thomas packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
714 142012ed 2022-10-20 thomas if (packidx.hdr.offsets == NULL) {
715 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
716 142012ed 2022-10-20 thomas goto done;
717 142012ed 2022-10-20 thomas }
718 142012ed 2022-10-20 thomas /* Large offsets table is empty for pack files < 2 GB. */
719 142012ed 2022-10-20 thomas if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
720 142012ed 2022-10-20 thomas packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
721 142012ed 2022-10-20 thomas if (packidx.hdr.large_offsets == NULL) {
722 142012ed 2022-10-20 thomas err = got_error_from_errno("calloc");
723 142012ed 2022-10-20 thomas goto done;
724 142012ed 2022-10-20 thomas }
725 142012ed 2022-10-20 thomas }
726 142012ed 2022-10-20 thomas
727 142012ed 2022-10-20 thomas nvalid = 0;
728 142012ed 2022-10-20 thomas nloose = 0;
729 142012ed 2022-10-20 thomas objects = calloc(nobj, sizeof(struct got_indexed_object));
730 142012ed 2022-10-20 thomas if (objects == NULL)
731 142012ed 2022-10-20 thomas return got_error_from_errno("calloc");
732 142012ed 2022-10-20 thomas
733 142012ed 2022-10-20 thomas /*
734 142012ed 2022-10-20 thomas * First pass: locate all objects and identify un-deltified objects.
735 142012ed 2022-10-20 thomas *
736 142012ed 2022-10-20 thomas * When this pass has completed we will know offset, type, size, and
737 142012ed 2022-10-20 thomas * CRC information for all objects in this pack file. We won't know
738 142012ed 2022-10-20 thomas * any of the actual object IDs of deltified objects yet since we
739 142012ed 2022-10-20 thomas * will not yet attempt to combine deltas.
740 142012ed 2022-10-20 thomas */
741 142012ed 2022-10-20 thomas pass = 1;
742 142012ed 2022-10-20 thomas for (i = 0; i < nobj; i++) {
743 142012ed 2022-10-20 thomas /* Don't send too many progress privsep messages. */
744 142012ed 2022-10-20 thomas p_indexed = ((i + 1) * 100) / nobj;
745 142012ed 2022-10-20 thomas if (p_indexed != last_p_indexed) {
746 142012ed 2022-10-20 thomas err = report_progress(nobj, i + 1, nloose, 0,
747 aecd2225 2022-10-20 thomas rl, progress_cb, progress_arg);
748 142012ed 2022-10-20 thomas if (err)
749 142012ed 2022-10-20 thomas goto done;
750 142012ed 2022-10-20 thomas last_p_indexed = p_indexed;
751 142012ed 2022-10-20 thomas }
752 142012ed 2022-10-20 thomas
753 142012ed 2022-10-20 thomas obj = &objects[i];
754 142012ed 2022-10-20 thomas obj->crc = crc32(0L, NULL, 0);
755 142012ed 2022-10-20 thomas
756 142012ed 2022-10-20 thomas /* Store offset to type+size information for this object. */
757 142012ed 2022-10-20 thomas if (pack->map) {
758 142012ed 2022-10-20 thomas obj->off = mapoff;
759 142012ed 2022-10-20 thomas } else {
760 142012ed 2022-10-20 thomas obj->off = lseek(pack->fd, 0, SEEK_CUR);
761 142012ed 2022-10-20 thomas if (obj->off == -1) {
762 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
763 142012ed 2022-10-20 thomas goto done;
764 142012ed 2022-10-20 thomas }
765 142012ed 2022-10-20 thomas }
766 142012ed 2022-10-20 thomas
767 142012ed 2022-10-20 thomas err = read_packed_object(pack, obj, tmpfile, &ctx);
768 142012ed 2022-10-20 thomas if (err)
769 142012ed 2022-10-20 thomas goto done;
770 142012ed 2022-10-20 thomas
771 142012ed 2022-10-20 thomas if (pack->map) {
772 142012ed 2022-10-20 thomas mapoff += obj->tslen + obj->len;
773 142012ed 2022-10-20 thomas } else {
774 142012ed 2022-10-20 thomas if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
775 142012ed 2022-10-20 thomas SEEK_SET) == -1) {
776 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
777 142012ed 2022-10-20 thomas goto done;
778 142012ed 2022-10-20 thomas }
779 142012ed 2022-10-20 thomas }
780 142012ed 2022-10-20 thomas
781 142012ed 2022-10-20 thomas if (obj->type == GOT_OBJ_TYPE_BLOB ||
782 142012ed 2022-10-20 thomas obj->type == GOT_OBJ_TYPE_TREE ||
783 142012ed 2022-10-20 thomas obj->type == GOT_OBJ_TYPE_COMMIT ||
784 142012ed 2022-10-20 thomas obj->type == GOT_OBJ_TYPE_TAG) {
785 142012ed 2022-10-20 thomas obj->valid = 1;
786 142012ed 2022-10-20 thomas nloose++;
787 142012ed 2022-10-20 thomas } else {
788 142012ed 2022-10-20 thomas if (first_delta_idx == -1)
789 142012ed 2022-10-20 thomas first_delta_idx = i;
790 142012ed 2022-10-20 thomas if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
791 142012ed 2022-10-20 thomas have_ref_deltas = 1;
792 142012ed 2022-10-20 thomas }
793 142012ed 2022-10-20 thomas }
794 142012ed 2022-10-20 thomas nvalid = nloose;
795 142012ed 2022-10-20 thomas
796 142012ed 2022-10-20 thomas /*
797 142012ed 2022-10-20 thomas * Having done a full pass over the pack file and can now
798 142012ed 2022-10-20 thomas * verify its checksum.
799 142012ed 2022-10-20 thomas */
800 142012ed 2022-10-20 thomas SHA1Final(pack_sha1, &ctx);
801 3efd8e31 2022-10-23 thomas
802 142012ed 2022-10-20 thomas if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
803 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PACKFILE_CSUM);
804 142012ed 2022-10-20 thomas goto done;
805 142012ed 2022-10-20 thomas }
806 142012ed 2022-10-20 thomas
807 142012ed 2022-10-20 thomas /* Verify the SHA1 checksum stored at the end of the pack file. */
808 142012ed 2022-10-20 thomas if (pack->map) {
809 66e6097f 2022-10-27 thomas if (pack->filesize > SIZE_MAX) {
810 66e6097f 2022-10-27 thomas err = got_error_fmt(GOT_ERR_RANGE,
811 66e6097f 2022-10-27 thomas "filesize %lld overflows size_t",
812 66e6097f 2022-10-27 thomas (long long)pack->filesize);
813 66e6097f 2022-10-27 thomas goto done;
814 66e6097f 2022-10-27 thomas }
815 66e6097f 2022-10-27 thomas
816 142012ed 2022-10-20 thomas memcpy(pack_sha1_expected, pack->map +
817 142012ed 2022-10-20 thomas pack->filesize - SHA1_DIGEST_LENGTH,
818 142012ed 2022-10-20 thomas SHA1_DIGEST_LENGTH);
819 142012ed 2022-10-20 thomas } else {
820 142012ed 2022-10-20 thomas ssize_t n;
821 142012ed 2022-10-20 thomas if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
822 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
823 142012ed 2022-10-20 thomas goto done;
824 142012ed 2022-10-20 thomas }
825 142012ed 2022-10-20 thomas n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
826 142012ed 2022-10-20 thomas if (n == -1) {
827 142012ed 2022-10-20 thomas err = got_error_from_errno("read");
828 142012ed 2022-10-20 thomas goto done;
829 142012ed 2022-10-20 thomas }
830 142012ed 2022-10-20 thomas if (n != SHA1_DIGEST_LENGTH) {
831 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_IO);
832 142012ed 2022-10-20 thomas goto done;
833 142012ed 2022-10-20 thomas }
834 142012ed 2022-10-20 thomas }
835 142012ed 2022-10-20 thomas if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
836 142012ed 2022-10-20 thomas err = got_error_msg(GOT_ERR_BAD_PACKFILE,
837 142012ed 2022-10-20 thomas "bad checksum in pack file trailer");
838 142012ed 2022-10-20 thomas goto done;
839 142012ed 2022-10-20 thomas }
840 142012ed 2022-10-20 thomas
841 142012ed 2022-10-20 thomas if (first_delta_idx == -1)
842 142012ed 2022-10-20 thomas first_delta_idx = 0;
843 142012ed 2022-10-20 thomas
844 142012ed 2022-10-20 thomas /* In order to resolve ref deltas we need an in-progress pack index. */
845 142012ed 2022-10-20 thomas if (have_ref_deltas)
846 142012ed 2022-10-20 thomas make_packidx(&packidx, nobj, objects);
847 142012ed 2022-10-20 thomas
848 142012ed 2022-10-20 thomas /*
849 142012ed 2022-10-20 thomas * Second pass: We can now resolve deltas to compute the IDs of
850 142012ed 2022-10-20 thomas * objects which appear in deltified form. Because deltas can be
851 142012ed 2022-10-20 thomas * chained this pass may require a couple of iterations until all
852 142012ed 2022-10-20 thomas * IDs of deltified objects have been discovered.
853 142012ed 2022-10-20 thomas */
854 142012ed 2022-10-20 thomas pass++;
855 142012ed 2022-10-20 thomas while (nvalid != nobj) {
856 142012ed 2022-10-20 thomas int n = 0;
857 142012ed 2022-10-20 thomas /*
858 142012ed 2022-10-20 thomas * This loop will only run once unless the pack file
859 142012ed 2022-10-20 thomas * contains ref deltas which refer to objects located
860 142012ed 2022-10-20 thomas * later in the pack file, which is unusual.
861 142012ed 2022-10-20 thomas * Offset deltas can always be resolved in one pass
862 142012ed 2022-10-20 thomas * unless the packfile is corrupt.
863 142012ed 2022-10-20 thomas */
864 142012ed 2022-10-20 thomas for (i = first_delta_idx; i < nobj; i++) {
865 142012ed 2022-10-20 thomas obj = &objects[i];
866 142012ed 2022-10-20 thomas if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
867 142012ed 2022-10-20 thomas obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
868 142012ed 2022-10-20 thomas continue;
869 142012ed 2022-10-20 thomas
870 142012ed 2022-10-20 thomas if (obj->valid)
871 142012ed 2022-10-20 thomas continue;
872 142012ed 2022-10-20 thomas
873 142012ed 2022-10-20 thomas if (pack->map == NULL && lseek(pack->fd,
874 142012ed 2022-10-20 thomas obj->off + obj->tslen, SEEK_SET) == -1) {
875 142012ed 2022-10-20 thomas err = got_error_from_errno("lseek");
876 142012ed 2022-10-20 thomas goto done;
877 142012ed 2022-10-20 thomas }
878 142012ed 2022-10-20 thomas
879 142012ed 2022-10-20 thomas err = resolve_deltified_object(pack, &packidx, obj,
880 142012ed 2022-10-20 thomas tmpfile, delta_base_file, delta_accum_file);
881 142012ed 2022-10-20 thomas if (err) {
882 142012ed 2022-10-20 thomas if (err->code != GOT_ERR_NO_OBJ)
883 142012ed 2022-10-20 thomas goto done;
884 142012ed 2022-10-20 thomas /*
885 142012ed 2022-10-20 thomas * We cannot resolve this object yet because
886 142012ed 2022-10-20 thomas * a delta base is unknown. Try again later.
887 142012ed 2022-10-20 thomas */
888 142012ed 2022-10-20 thomas continue;
889 142012ed 2022-10-20 thomas }
890 142012ed 2022-10-20 thomas
891 142012ed 2022-10-20 thomas obj->valid = 1;
892 142012ed 2022-10-20 thomas n++;
893 142012ed 2022-10-20 thomas if (have_ref_deltas)
894 142012ed 2022-10-20 thomas update_packidx(&packidx, nobj, obj);
895 142012ed 2022-10-20 thomas /* Don't send too many progress privsep messages. */
896 142012ed 2022-10-20 thomas p_resolved = ((nresolved + n) * 100) / nobj;
897 142012ed 2022-10-20 thomas if (p_resolved != last_p_resolved) {
898 142012ed 2022-10-20 thomas err = report_progress(nobj, nobj,
899 aecd2225 2022-10-20 thomas nloose, nresolved + n, rl,
900 142012ed 2022-10-20 thomas progress_cb, progress_arg);
901 142012ed 2022-10-20 thomas if (err)
902 142012ed 2022-10-20 thomas goto done;
903 142012ed 2022-10-20 thomas last_p_resolved = p_resolved;
904 142012ed 2022-10-20 thomas }
905 142012ed 2022-10-20 thomas
906 142012ed 2022-10-20 thomas }
907 142012ed 2022-10-20 thomas if (pass++ > 3 && n == 0) {
908 142012ed 2022-10-20 thomas err = got_error_msg(GOT_ERR_BAD_PACKFILE,
909 142012ed 2022-10-20 thomas "could not resolve any of deltas; packfile could "
910 142012ed 2022-10-20 thomas "be corrupt");
911 142012ed 2022-10-20 thomas goto done;
912 142012ed 2022-10-20 thomas }
913 142012ed 2022-10-20 thomas nresolved += n;
914 88056de1 2023-01-19 thomas nvalid += n;
915 142012ed 2022-10-20 thomas }
916 142012ed 2022-10-20 thomas
917 142012ed 2022-10-20 thomas if (nloose + nresolved != nobj) {
918 142012ed 2022-10-20 thomas static char msg[64];
919 142012ed 2022-10-20 thomas snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
920 142012ed 2022-10-20 thomas nloose + nresolved, nobj);
921 142012ed 2022-10-20 thomas err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
922 142012ed 2022-10-20 thomas goto done;
923 142012ed 2022-10-20 thomas }
924 142012ed 2022-10-20 thomas
925 142012ed 2022-10-20 thomas err = report_progress(nobj, nobj, nloose, nresolved, NULL,
926 142012ed 2022-10-20 thomas progress_cb, progress_arg);
927 142012ed 2022-10-20 thomas if (err)
928 142012ed 2022-10-20 thomas goto done;
929 142012ed 2022-10-20 thomas
930 142012ed 2022-10-20 thomas make_packidx(&packidx, nobj, objects);
931 142012ed 2022-10-20 thomas
932 142012ed 2022-10-20 thomas free(objects);
933 142012ed 2022-10-20 thomas objects = NULL;
934 142012ed 2022-10-20 thomas
935 142012ed 2022-10-20 thomas SHA1Init(&ctx);
936 142012ed 2022-10-20 thomas putbe32(buf, GOT_PACKIDX_V2_MAGIC);
937 142012ed 2022-10-20 thomas putbe32(buf + 4, GOT_PACKIDX_VERSION);
938 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, buf, 8, &ctx);
939 142012ed 2022-10-20 thomas if (err)
940 142012ed 2022-10-20 thomas goto done;
941 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
942 142012ed 2022-10-20 thomas GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
943 142012ed 2022-10-20 thomas if (err)
944 142012ed 2022-10-20 thomas goto done;
945 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
946 142012ed 2022-10-20 thomas nobj * SHA1_DIGEST_LENGTH, &ctx);
947 142012ed 2022-10-20 thomas if (err)
948 142012ed 2022-10-20 thomas goto done;
949 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
950 3efd8e31 2022-10-23 thomas nobj * sizeof(uint32_t), &ctx);
951 142012ed 2022-10-20 thomas if (err)
952 142012ed 2022-10-20 thomas goto done;
953 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
954 3efd8e31 2022-10-23 thomas nobj * sizeof(uint32_t), &ctx);
955 142012ed 2022-10-20 thomas if (err)
956 142012ed 2022-10-20 thomas goto done;
957 142012ed 2022-10-20 thomas if (packidx.nlargeobj > 0) {
958 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
959 142012ed 2022-10-20 thomas packidx.nlargeobj * sizeof(uint64_t), &ctx);
960 142012ed 2022-10-20 thomas if (err)
961 142012ed 2022-10-20 thomas goto done;
962 142012ed 2022-10-20 thomas }
963 3efd8e31 2022-10-23 thomas err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
964 142012ed 2022-10-20 thomas if (err)
965 142012ed 2022-10-20 thomas goto done;
966 142012ed 2022-10-20 thomas
967 142012ed 2022-10-20 thomas SHA1Final(packidx_hash, &ctx);
968 142012ed 2022-10-20 thomas w = write(idxfd, packidx_hash, sizeof(packidx_hash));
969 142012ed 2022-10-20 thomas if (w == -1) {
970 142012ed 2022-10-20 thomas err = got_error_from_errno("write");
971 142012ed 2022-10-20 thomas goto done;
972 142012ed 2022-10-20 thomas }
973 142012ed 2022-10-20 thomas if (w != sizeof(packidx_hash)) {
974 142012ed 2022-10-20 thomas err = got_error(GOT_ERR_IO);
975 142012ed 2022-10-20 thomas goto done;
976 142012ed 2022-10-20 thomas }
977 142012ed 2022-10-20 thomas done:
978 142012ed 2022-10-20 thomas free(objects);
979 142012ed 2022-10-20 thomas free(packidx.hdr.magic);
980 142012ed 2022-10-20 thomas free(packidx.hdr.version);
981 142012ed 2022-10-20 thomas free(packidx.hdr.fanout_table);
982 142012ed 2022-10-20 thomas free(packidx.hdr.sorted_ids);
983 142012ed 2022-10-20 thomas free(packidx.hdr.offsets);
984 142012ed 2022-10-20 thomas free(packidx.hdr.large_offsets);
985 142012ed 2022-10-20 thomas return err;
986 142012ed 2022-10-20 thomas }