Blame


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