Blame


1 0a0a3048 2018-01-10 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 0a0a3048 2018-01-10 stsp *
4 0a0a3048 2018-01-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 0a0a3048 2018-01-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 0a0a3048 2018-01-10 stsp * copyright notice and this permission notice appear in all copies.
7 0a0a3048 2018-01-10 stsp *
8 0a0a3048 2018-01-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0a0a3048 2018-01-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0a0a3048 2018-01-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0a0a3048 2018-01-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0a0a3048 2018-01-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0a0a3048 2018-01-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0a0a3048 2018-01-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 0a0a3048 2018-01-10 stsp */
16 0a0a3048 2018-01-10 stsp
17 a1fd68d8 2018-01-12 stsp #include <sys/types.h>
18 0a0a3048 2018-01-10 stsp #include <sys/stat.h>
19 a1fd68d8 2018-01-12 stsp #include <sys/queue.h>
20 876c234b 2018-09-10 stsp #include <sys/uio.h>
21 57b35b75 2018-06-22 stsp #include <sys/mman.h>
22 3d589bee 2022-06-25 stsp #include <sys/resource.h>
23 3d589bee 2022-06-25 stsp #include <sys/socket.h>
24 0a0a3048 2018-01-10 stsp
25 7e656b93 2018-03-17 stsp #include <fcntl.h>
26 a1fd68d8 2018-01-12 stsp #include <errno.h>
27 0a0a3048 2018-01-10 stsp #include <stdio.h>
28 a1fd68d8 2018-01-12 stsp #include <stdint.h>
29 0a0a3048 2018-01-10 stsp #include <stdlib.h>
30 0a0a3048 2018-01-10 stsp #include <string.h>
31 0a0a3048 2018-01-10 stsp #include <limits.h>
32 0a0a3048 2018-01-10 stsp #include <sha1.h>
33 0a0a3048 2018-01-10 stsp #include <endian.h>
34 81a12da5 2020-09-09 naddy #include <unistd.h>
35 a1fd68d8 2018-01-12 stsp #include <zlib.h>
36 876c234b 2018-09-10 stsp #include <imsg.h>
37 0a0a3048 2018-01-10 stsp
38 0a0a3048 2018-01-10 stsp #include "got_error.h"
39 a1fd68d8 2018-01-12 stsp #include "got_object.h"
40 324d37e7 2019-05-11 stsp #include "got_path.h"
41 0a0a3048 2018-01-10 stsp
42 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
45 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
47 dd88155e 2019-06-29 stsp #include "got_lib_object_parse.h"
48 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
49 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
50 79b11c62 2018-03-09 stsp
51 79b11c62 2018-03-09 stsp #ifndef nitems
52 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 79b11c62 2018-03-09 stsp #endif
54 1411938b 2018-02-12 stsp
55 a1fd68d8 2018-01-12 stsp #ifndef MIN
56 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 a1fd68d8 2018-01-12 stsp #endif
58 a1fd68d8 2018-01-12 stsp
59 0a0a3048 2018-01-10 stsp static const struct got_error *
60 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
61 0a0a3048 2018-01-10 stsp {
62 0a0a3048 2018-01-10 stsp int i;
63 0a0a3048 2018-01-10 stsp
64 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
65 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
66 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
67 0a0a3048 2018-01-10 stsp }
68 0a0a3048 2018-01-10 stsp
69 0a0a3048 2018-01-10 stsp return NULL;
70 0a0a3048 2018-01-10 stsp }
71 0a0a3048 2018-01-10 stsp
72 1510f469 2018-09-09 stsp const struct got_error *
73 c3564dfa 2021-07-15 stsp got_packidx_init_hdr(struct got_packidx *p, int verify, off_t packfile_size)
74 0a0a3048 2018-01-10 stsp {
75 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
76 817c5a18 2018-09-09 stsp struct got_packidx_v2_hdr *h;
77 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
78 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
79 817c5a18 2018-09-09 stsp size_t nobj, len_fanout, len_ids, offset, remain;
80 817c5a18 2018-09-09 stsp ssize_t n;
81 5e6be232 2019-11-08 stsp int i;
82 0a0a3048 2018-01-10 stsp
83 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
84 0ebaf008 2018-01-10 stsp
85 57b35b75 2018-06-22 stsp h = &p->hdr;
86 57b35b75 2018-06-22 stsp offset = 0;
87 57b35b75 2018-06-22 stsp remain = p->len;
88 0a0a3048 2018-01-10 stsp
89 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
90 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
91 0a0a3048 2018-01-10 stsp goto done;
92 0a0a3048 2018-01-10 stsp }
93 fc79a48d 2018-07-09 stsp if (p->map)
94 fc79a48d 2018-07-09 stsp h->magic = (uint32_t *)(p->map + offset);
95 fc79a48d 2018-07-09 stsp else {
96 fc79a48d 2018-07-09 stsp h->magic = malloc(sizeof(*h->magic));
97 fc79a48d 2018-07-09 stsp if (h->magic == NULL) {
98 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
99 fc79a48d 2018-07-09 stsp goto done;
100 fc79a48d 2018-07-09 stsp }
101 fc79a48d 2018-07-09 stsp n = read(p->fd, h->magic, sizeof(*h->magic));
102 b1317e77 2019-09-22 stsp if (n < 0) {
103 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
104 b1317e77 2019-09-22 stsp goto done;
105 b1317e77 2019-09-22 stsp } else if (n != sizeof(*h->magic)) {
106 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
107 fc79a48d 2018-07-09 stsp goto done;
108 fc79a48d 2018-07-09 stsp }
109 fc79a48d 2018-07-09 stsp }
110 ac62b712 2021-03-30 stsp if (*h->magic != htobe32(GOT_PACKIDX_V2_MAGIC)) {
111 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
112 57b35b75 2018-06-22 stsp goto done;
113 57b35b75 2018-06-22 stsp }
114 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
115 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
116 0a0a3048 2018-01-10 stsp
117 0cb74cf4 2018-07-08 stsp if (verify)
118 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
119 0ebaf008 2018-01-10 stsp
120 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
121 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
122 0a0a3048 2018-01-10 stsp goto done;
123 0a0a3048 2018-01-10 stsp }
124 fc79a48d 2018-07-09 stsp if (p->map)
125 fc79a48d 2018-07-09 stsp h->version = (uint32_t *)(p->map + offset);
126 fc79a48d 2018-07-09 stsp else {
127 fc79a48d 2018-07-09 stsp h->version = malloc(sizeof(*h->version));
128 fc79a48d 2018-07-09 stsp if (h->version == NULL) {
129 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
130 fc79a48d 2018-07-09 stsp goto done;
131 fc79a48d 2018-07-09 stsp }
132 fc79a48d 2018-07-09 stsp n = read(p->fd, h->version, sizeof(*h->version));
133 c6368c2e 2019-10-11 stsp if (n < 0) {
134 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
135 c6368c2e 2019-10-11 stsp goto done;
136 c6368c2e 2019-10-11 stsp } else if (n != sizeof(*h->version)) {
137 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
138 fc79a48d 2018-07-09 stsp goto done;
139 fc79a48d 2018-07-09 stsp }
140 fc79a48d 2018-07-09 stsp }
141 ac62b712 2021-03-30 stsp if (*h->version != htobe32(GOT_PACKIDX_VERSION)) {
142 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
143 0a0a3048 2018-01-10 stsp goto done;
144 0a0a3048 2018-01-10 stsp }
145 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
146 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
147 0a0a3048 2018-01-10 stsp
148 0cb74cf4 2018-07-08 stsp if (verify)
149 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
150 0ebaf008 2018-01-10 stsp
151 57b35b75 2018-06-22 stsp len_fanout =
152 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
153 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
154 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
155 0a0a3048 2018-01-10 stsp goto done;
156 0a0a3048 2018-01-10 stsp }
157 fc79a48d 2018-07-09 stsp if (p->map)
158 fc79a48d 2018-07-09 stsp h->fanout_table = (uint32_t *)(p->map + offset);
159 fc79a48d 2018-07-09 stsp else {
160 fc79a48d 2018-07-09 stsp h->fanout_table = malloc(len_fanout);
161 fc79a48d 2018-07-09 stsp if (h->fanout_table == NULL) {
162 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
163 fc79a48d 2018-07-09 stsp goto done;
164 fc79a48d 2018-07-09 stsp }
165 fc79a48d 2018-07-09 stsp n = read(p->fd, h->fanout_table, len_fanout);
166 c6368c2e 2019-10-11 stsp if (n < 0) {
167 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
168 c6368c2e 2019-10-11 stsp goto done;
169 c6368c2e 2019-10-11 stsp } else if (n != len_fanout) {
170 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
171 fc79a48d 2018-07-09 stsp goto done;
172 fc79a48d 2018-07-09 stsp }
173 fc79a48d 2018-07-09 stsp }
174 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
175 0a0a3048 2018-01-10 stsp if (err)
176 0a0a3048 2018-01-10 stsp goto done;
177 0cb74cf4 2018-07-08 stsp if (verify)
178 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
179 57b35b75 2018-06-22 stsp offset += len_fanout;
180 57b35b75 2018-06-22 stsp remain -= len_fanout;
181 0a0a3048 2018-01-10 stsp
182 78fb0967 2020-09-09 naddy nobj = be32toh(h->fanout_table[0xff]);
183 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
184 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
185 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
186 0a0a3048 2018-01-10 stsp goto done;
187 0a0a3048 2018-01-10 stsp }
188 fc79a48d 2018-07-09 stsp if (p->map)
189 fc79a48d 2018-07-09 stsp h->sorted_ids =
190 fc79a48d 2018-07-09 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
191 fc79a48d 2018-07-09 stsp else {
192 fc79a48d 2018-07-09 stsp h->sorted_ids = malloc(len_ids);
193 fc79a48d 2018-07-09 stsp if (h->sorted_ids == NULL) {
194 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
195 fc79a48d 2018-07-09 stsp goto done;
196 fc79a48d 2018-07-09 stsp }
197 fc79a48d 2018-07-09 stsp n = read(p->fd, h->sorted_ids, len_ids);
198 faaa1c0f 2018-09-15 stsp if (n < 0)
199 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
200 faaa1c0f 2018-09-15 stsp else if (n != len_ids) {
201 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
202 fc79a48d 2018-07-09 stsp goto done;
203 fc79a48d 2018-07-09 stsp }
204 fc79a48d 2018-07-09 stsp }
205 0cb74cf4 2018-07-08 stsp if (verify)
206 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
207 57b35b75 2018-06-22 stsp offset += len_ids;
208 57b35b75 2018-06-22 stsp remain -= len_ids;
209 0a0a3048 2018-01-10 stsp
210 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
211 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
212 0a0a3048 2018-01-10 stsp goto done;
213 0a0a3048 2018-01-10 stsp }
214 fc79a48d 2018-07-09 stsp if (p->map)
215 fc79a48d 2018-07-09 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
216 fc79a48d 2018-07-09 stsp else {
217 fc79a48d 2018-07-09 stsp h->crc32 = malloc(nobj * sizeof(*h->crc32));
218 fc79a48d 2018-07-09 stsp if (h->crc32 == NULL) {
219 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
220 fc79a48d 2018-07-09 stsp goto done;
221 fc79a48d 2018-07-09 stsp }
222 fc79a48d 2018-07-09 stsp n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
223 faaa1c0f 2018-09-15 stsp if (n < 0)
224 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
225 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->crc32)) {
226 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
227 fc79a48d 2018-07-09 stsp goto done;
228 fc79a48d 2018-07-09 stsp }
229 fc79a48d 2018-07-09 stsp }
230 0cb74cf4 2018-07-08 stsp if (verify)
231 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
232 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->crc32);
233 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->crc32);
234 0ebaf008 2018-01-10 stsp
235 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->offsets)) {
236 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
237 0a0a3048 2018-01-10 stsp goto done;
238 0a0a3048 2018-01-10 stsp }
239 fc79a48d 2018-07-09 stsp if (p->map)
240 fc79a48d 2018-07-09 stsp h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
241 fc79a48d 2018-07-09 stsp else {
242 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->offsets));
243 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
244 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
245 fc79a48d 2018-07-09 stsp goto done;
246 fc79a48d 2018-07-09 stsp }
247 fc79a48d 2018-07-09 stsp n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
248 faaa1c0f 2018-09-15 stsp if (n < 0)
249 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
250 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->offsets)) {
251 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
252 fc79a48d 2018-07-09 stsp goto done;
253 fc79a48d 2018-07-09 stsp }
254 fc79a48d 2018-07-09 stsp }
255 0cb74cf4 2018-07-08 stsp if (verify)
256 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
257 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
258 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
259 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
260 0ebaf008 2018-01-10 stsp
261 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
262 c3564dfa 2021-07-15 stsp if (verify || packfile_size > 0x7fffffff) {
263 c3564dfa 2021-07-15 stsp for (i = 0; i < nobj; i++) {
264 c3564dfa 2021-07-15 stsp uint32_t o = h->offsets[i];
265 c3564dfa 2021-07-15 stsp if (o & htobe32(GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX))
266 c3564dfa 2021-07-15 stsp p->nlargeobj++;
267 c3564dfa 2021-07-15 stsp }
268 5e6be232 2019-11-08 stsp }
269 5e6be232 2019-11-08 stsp if (p->nlargeobj == 0)
270 0a0a3048 2018-01-10 stsp goto checksum;
271 c3564dfa 2021-07-15 stsp else if (packfile_size <= 0x7fffffff) {
272 c3564dfa 2021-07-15 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
273 c3564dfa 2021-07-15 stsp goto done;
274 c3564dfa 2021-07-15 stsp }
275 0a0a3048 2018-01-10 stsp
276 5e6be232 2019-11-08 stsp if (remain < p->nlargeobj * sizeof(*h->large_offsets)) {
277 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
278 0a0a3048 2018-01-10 stsp goto done;
279 0a0a3048 2018-01-10 stsp }
280 fc79a48d 2018-07-09 stsp if (p->map)
281 fc79a48d 2018-07-09 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
282 fc79a48d 2018-07-09 stsp else {
283 5e6be232 2019-11-08 stsp h->large_offsets = malloc(p->nlargeobj *
284 5e6be232 2019-11-08 stsp sizeof(*h->large_offsets));
285 de30857e 2019-08-23 stsp if (h->large_offsets == NULL) {
286 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
287 fc79a48d 2018-07-09 stsp goto done;
288 fc79a48d 2018-07-09 stsp }
289 fc79a48d 2018-07-09 stsp n = read(p->fd, h->large_offsets,
290 5e6be232 2019-11-08 stsp p->nlargeobj * sizeof(*h->large_offsets));
291 faaa1c0f 2018-09-15 stsp if (n < 0)
292 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
293 5e6be232 2019-11-08 stsp else if (n != p->nlargeobj * sizeof(*h->large_offsets)) {
294 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
295 fc79a48d 2018-07-09 stsp goto done;
296 fc79a48d 2018-07-09 stsp }
297 fc79a48d 2018-07-09 stsp }
298 0cb74cf4 2018-07-08 stsp if (verify)
299 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
300 5e6be232 2019-11-08 stsp p->nlargeobj * sizeof(*h->large_offsets));
301 5e6be232 2019-11-08 stsp remain -= p->nlargeobj * sizeof(*h->large_offsets);
302 5e6be232 2019-11-08 stsp offset += p->nlargeobj * sizeof(*h->large_offsets);
303 0ebaf008 2018-01-10 stsp
304 0a0a3048 2018-01-10 stsp checksum:
305 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
306 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
307 0a0a3048 2018-01-10 stsp goto done;
308 0a0a3048 2018-01-10 stsp }
309 fc79a48d 2018-07-09 stsp if (p->map)
310 fc79a48d 2018-07-09 stsp h->trailer =
311 fc79a48d 2018-07-09 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
312 fc79a48d 2018-07-09 stsp else {
313 fc79a48d 2018-07-09 stsp h->trailer = malloc(sizeof(*h->trailer));
314 fc79a48d 2018-07-09 stsp if (h->trailer == NULL) {
315 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
316 fc79a48d 2018-07-09 stsp goto done;
317 fc79a48d 2018-07-09 stsp }
318 fc79a48d 2018-07-09 stsp n = read(p->fd, h->trailer, sizeof(*h->trailer));
319 faaa1c0f 2018-09-15 stsp if (n < 0)
320 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
321 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->trailer)) {
322 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
323 fc79a48d 2018-07-09 stsp goto done;
324 fc79a48d 2018-07-09 stsp }
325 fc79a48d 2018-07-09 stsp }
326 0cb74cf4 2018-07-08 stsp if (verify) {
327 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
328 0cb74cf4 2018-07-08 stsp SHA1Final(sha1, &ctx);
329 0cb74cf4 2018-07-08 stsp if (memcmp(h->trailer->packidx_sha1, sha1,
330 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
331 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
332 0cb74cf4 2018-07-08 stsp }
333 0a0a3048 2018-01-10 stsp done:
334 817c5a18 2018-09-09 stsp return err;
335 817c5a18 2018-09-09 stsp }
336 817c5a18 2018-09-09 stsp
337 817c5a18 2018-09-09 stsp const struct got_error *
338 6d5a9006 2020-12-16 yzhong got_packidx_open(struct got_packidx **packidx,
339 6d5a9006 2020-12-16 yzhong int dir_fd, const char *relpath, int verify)
340 817c5a18 2018-09-09 stsp {
341 817c5a18 2018-09-09 stsp const struct got_error *err = NULL;
342 1124fe40 2021-07-07 stsp struct got_packidx *p = NULL;
343 1124fe40 2021-07-07 stsp char *pack_relpath;
344 c3564dfa 2021-07-15 stsp struct stat idx_sb, pack_sb;
345 817c5a18 2018-09-09 stsp
346 817c5a18 2018-09-09 stsp *packidx = NULL;
347 1124fe40 2021-07-07 stsp
348 1124fe40 2021-07-07 stsp err = got_packidx_get_packfile_path(&pack_relpath, relpath);
349 1124fe40 2021-07-07 stsp if (err)
350 1124fe40 2021-07-07 stsp return err;
351 1124fe40 2021-07-07 stsp
352 1124fe40 2021-07-07 stsp /*
353 1124fe40 2021-07-07 stsp * Ensure that a corresponding pack file exists.
354 1124fe40 2021-07-07 stsp * Some Git repositories have this problem. Git seems to ignore
355 1124fe40 2021-07-07 stsp * the existence of lonely pack index files but we do not.
356 1124fe40 2021-07-07 stsp */
357 c3564dfa 2021-07-15 stsp if (fstatat(dir_fd, pack_relpath, &pack_sb, 0) == -1) {
358 1124fe40 2021-07-07 stsp if (errno == ENOENT) {
359 1124fe40 2021-07-07 stsp err = got_error_fmt(GOT_ERR_LONELY_PACKIDX,
360 1124fe40 2021-07-07 stsp "%s", relpath);
361 1124fe40 2021-07-07 stsp } else
362 1124fe40 2021-07-07 stsp err = got_error_from_errno2("fstatat", pack_relpath);
363 1124fe40 2021-07-07 stsp goto done;
364 1124fe40 2021-07-07 stsp }
365 817c5a18 2018-09-09 stsp
366 817c5a18 2018-09-09 stsp p = calloc(1, sizeof(*p));
367 1124fe40 2021-07-07 stsp if (p == NULL) {
368 1124fe40 2021-07-07 stsp err = got_error_from_errno("calloc");
369 1124fe40 2021-07-07 stsp goto done;
370 1124fe40 2021-07-07 stsp }
371 817c5a18 2018-09-09 stsp
372 e7ae0baf 2021-12-31 stsp p->fd = openat(dir_fd, relpath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
373 6772cf22 2019-08-28 hiltjo if (p->fd == -1) {
374 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("openat", relpath);
375 1124fe40 2021-07-07 stsp goto done;
376 6772cf22 2019-08-28 hiltjo }
377 817c5a18 2018-09-09 stsp
378 c3564dfa 2021-07-15 stsp if (fstat(p->fd, &idx_sb) != 0) {
379 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("fstat", relpath);
380 1124fe40 2021-07-07 stsp goto done;
381 817c5a18 2018-09-09 stsp }
382 c3564dfa 2021-07-15 stsp p->len = idx_sb.st_size;
383 817c5a18 2018-09-09 stsp if (p->len < sizeof(p->hdr)) {
384 817c5a18 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
385 1124fe40 2021-07-07 stsp goto done;
386 817c5a18 2018-09-09 stsp }
387 817c5a18 2018-09-09 stsp
388 6d5a9006 2020-12-16 yzhong p->path_packidx = strdup(relpath);
389 817c5a18 2018-09-09 stsp if (p->path_packidx == NULL) {
390 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
391 817c5a18 2018-09-09 stsp goto done;
392 817c5a18 2018-09-09 stsp }
393 817c5a18 2018-09-09 stsp
394 817c5a18 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
395 1c28a361 2022-10-25 op if (p->len > 0 && p->len <= SIZE_MAX) {
396 1c28a361 2022-10-25 op p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
397 1c28a361 2022-10-25 op if (p->map == MAP_FAILED) {
398 1c28a361 2022-10-25 op if (errno != ENOMEM) {
399 1c28a361 2022-10-25 op err = got_error_from_errno("mmap");
400 1c28a361 2022-10-25 op goto done;
401 1c28a361 2022-10-25 op }
402 1c28a361 2022-10-25 op p->map = NULL; /* fall back to read(2) */
403 3a11398b 2019-02-21 stsp }
404 3a11398b 2019-02-21 stsp }
405 817c5a18 2018-09-09 stsp #endif
406 817c5a18 2018-09-09 stsp
407 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, verify, pack_sb.st_size);
408 817c5a18 2018-09-09 stsp done:
409 1124fe40 2021-07-07 stsp if (err) {
410 1124fe40 2021-07-07 stsp if (p)
411 1124fe40 2021-07-07 stsp got_packidx_close(p);
412 1124fe40 2021-07-07 stsp } else
413 0a0a3048 2018-01-10 stsp *packidx = p;
414 1124fe40 2021-07-07 stsp free(pack_relpath);
415 0a0a3048 2018-01-10 stsp return err;
416 0a0a3048 2018-01-10 stsp }
417 0a0a3048 2018-01-10 stsp
418 57b35b75 2018-06-22 stsp const struct got_error *
419 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
420 0a0a3048 2018-01-10 stsp {
421 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
422 57b35b75 2018-06-22 stsp
423 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
424 fc79a48d 2018-07-09 stsp if (packidx->map) {
425 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
426 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
427 fc79a48d 2018-07-09 stsp } else {
428 fc79a48d 2018-07-09 stsp free(packidx->hdr.magic);
429 fc79a48d 2018-07-09 stsp free(packidx->hdr.version);
430 fc79a48d 2018-07-09 stsp free(packidx->hdr.fanout_table);
431 fc79a48d 2018-07-09 stsp free(packidx->hdr.sorted_ids);
432 fc79a48d 2018-07-09 stsp free(packidx->hdr.crc32);
433 fc79a48d 2018-07-09 stsp free(packidx->hdr.offsets);
434 fc79a48d 2018-07-09 stsp free(packidx->hdr.large_offsets);
435 fc79a48d 2018-07-09 stsp free(packidx->hdr.trailer);
436 57b35b75 2018-06-22 stsp }
437 08578a35 2021-01-22 stsp if (close(packidx->fd) == -1 && err == NULL)
438 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
439 67fd6849 2022-02-13 stsp free(packidx->sorted_offsets);
440 67fd6849 2022-02-13 stsp free(packidx->sorted_large_offsets);
441 0a0a3048 2018-01-10 stsp free(packidx);
442 57b35b75 2018-06-22 stsp
443 57b35b75 2018-06-22 stsp return err;
444 a1fd68d8 2018-01-12 stsp }
445 509c9973 2021-04-10 stsp
446 509c9973 2021-04-10 stsp const struct got_error *
447 aea75d87 2021-07-06 stsp got_packidx_get_packfile_path(char **path_packfile, const char *path_packidx)
448 509c9973 2021-04-10 stsp {
449 509c9973 2021-04-10 stsp size_t size;
450 509c9973 2021-04-10 stsp
451 509c9973 2021-04-10 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
452 aea75d87 2021-07-06 stsp size = strlen(path_packidx) + 2;
453 509c9973 2021-04-10 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
454 aea75d87 2021-07-06 stsp return got_error_path(path_packidx, GOT_ERR_BAD_PATH);
455 a1fd68d8 2018-01-12 stsp
456 509c9973 2021-04-10 stsp *path_packfile = malloc(size);
457 509c9973 2021-04-10 stsp if (*path_packfile == NULL)
458 509c9973 2021-04-10 stsp return got_error_from_errno("malloc");
459 509c9973 2021-04-10 stsp
460 509c9973 2021-04-10 stsp /* Copy up to and excluding ".idx". */
461 aea75d87 2021-07-06 stsp if (strlcpy(*path_packfile, path_packidx,
462 509c9973 2021-04-10 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
463 509c9973 2021-04-10 stsp return got_error(GOT_ERR_NO_SPACE);
464 509c9973 2021-04-10 stsp
465 509c9973 2021-04-10 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
466 509c9973 2021-04-10 stsp return got_error(GOT_ERR_NO_SPACE);
467 509c9973 2021-04-10 stsp
468 509c9973 2021-04-10 stsp return NULL;
469 509c9973 2021-04-10 stsp }
470 509c9973 2021-04-10 stsp
471 02828bfd 2021-06-22 stsp off_t
472 02828bfd 2021-06-22 stsp got_packidx_get_object_offset(struct got_packidx *packidx, int idx)
473 a1fd68d8 2018-01-12 stsp {
474 78fb0967 2020-09-09 naddy uint32_t offset = be32toh(packidx->hdr.offsets[idx]);
475 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
476 a1fd68d8 2018-01-12 stsp uint64_t loffset;
477 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
478 5e6be232 2019-11-08 stsp if (idx < 0 || idx >= packidx->nlargeobj ||
479 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
480 a1fd68d8 2018-01-12 stsp return -1;
481 78fb0967 2020-09-09 naddy loffset = be64toh(packidx->hdr.large_offsets[idx]);
482 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
483 a1fd68d8 2018-01-12 stsp }
484 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
485 a1fd68d8 2018-01-12 stsp }
486 a1fd68d8 2018-01-12 stsp
487 1510f469 2018-09-09 stsp int
488 c0df5966 2021-12-31 stsp got_packidx_get_object_idx(struct got_packidx *packidx,
489 c0df5966 2021-12-31 stsp struct got_object_id *id)
490 a1fd68d8 2018-01-12 stsp {
491 00927983 2020-04-19 stsp u_int8_t id0 = id->sha1[0];
492 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
493 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
494 a1fd68d8 2018-01-12 stsp
495 a1fd68d8 2018-01-12 stsp if (id0 > 0)
496 78fb0967 2020-09-09 naddy left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
497 a1fd68d8 2018-01-12 stsp
498 40aeb19c 2018-06-22 stsp while (left <= right) {
499 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
500 40aeb19c 2018-06-22 stsp int i, cmp;
501 a1fd68d8 2018-01-12 stsp
502 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
503 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
504 00927983 2020-04-19 stsp cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
505 16dcbf91 2018-04-01 stsp if (cmp == 0)
506 6c00b545 2018-01-17 stsp return i;
507 40aeb19c 2018-06-22 stsp else if (cmp > 0)
508 40aeb19c 2018-06-22 stsp left = i + 1;
509 40aeb19c 2018-06-22 stsp else if (cmp < 0)
510 40aeb19c 2018-06-22 stsp right = i - 1;
511 a1fd68d8 2018-01-12 stsp }
512 a1fd68d8 2018-01-12 stsp
513 a1fd68d8 2018-01-12 stsp return -1;
514 67fd6849 2022-02-13 stsp }
515 67fd6849 2022-02-13 stsp
516 67fd6849 2022-02-13 stsp static int
517 67fd6849 2022-02-13 stsp offset_cmp(const void *pa, const void *pb)
518 67fd6849 2022-02-13 stsp {
519 67fd6849 2022-02-13 stsp const struct got_pack_offset_index *a, *b;
520 67fd6849 2022-02-13 stsp
521 67fd6849 2022-02-13 stsp a = (const struct got_pack_offset_index *)pa;
522 67fd6849 2022-02-13 stsp b = (const struct got_pack_offset_index *)pb;
523 67fd6849 2022-02-13 stsp
524 67fd6849 2022-02-13 stsp if (a->offset < b->offset)
525 67fd6849 2022-02-13 stsp return -1;
526 67fd6849 2022-02-13 stsp else if (a->offset > b->offset)
527 67fd6849 2022-02-13 stsp return 1;
528 67fd6849 2022-02-13 stsp
529 67fd6849 2022-02-13 stsp return 0;
530 876c234b 2018-09-10 stsp }
531 876c234b 2018-09-10 stsp
532 67fd6849 2022-02-13 stsp static int
533 67fd6849 2022-02-13 stsp large_offset_cmp(const void *pa, const void *pb)
534 67fd6849 2022-02-13 stsp {
535 67fd6849 2022-02-13 stsp const struct got_pack_large_offset_index *a, *b;
536 67fd6849 2022-02-13 stsp
537 67fd6849 2022-02-13 stsp a = (const struct got_pack_large_offset_index *)pa;
538 67fd6849 2022-02-13 stsp b = (const struct got_pack_large_offset_index *)pb;
539 67fd6849 2022-02-13 stsp
540 67fd6849 2022-02-13 stsp if (a->offset < b->offset)
541 67fd6849 2022-02-13 stsp return -1;
542 67fd6849 2022-02-13 stsp else if (a->offset > b->offset)
543 67fd6849 2022-02-13 stsp return 1;
544 67fd6849 2022-02-13 stsp
545 67fd6849 2022-02-13 stsp return 0;
546 67fd6849 2022-02-13 stsp }
547 67fd6849 2022-02-13 stsp
548 67fd6849 2022-02-13 stsp static const struct got_error *
549 67fd6849 2022-02-13 stsp build_offset_index(struct got_packidx *p)
550 67fd6849 2022-02-13 stsp {
551 67fd6849 2022-02-13 stsp uint32_t nobj = be32toh(p->hdr.fanout_table[0xff]);
552 67fd6849 2022-02-13 stsp unsigned int i, j, k;
553 67fd6849 2022-02-13 stsp
554 67fd6849 2022-02-13 stsp p->sorted_offsets = calloc(nobj - p->nlargeobj,
555 67fd6849 2022-02-13 stsp sizeof(p->sorted_offsets[0]));
556 67fd6849 2022-02-13 stsp if (p->sorted_offsets == NULL)
557 67fd6849 2022-02-13 stsp return got_error_from_errno("calloc");
558 67fd6849 2022-02-13 stsp
559 67fd6849 2022-02-13 stsp if (p->nlargeobj > 0) {
560 67fd6849 2022-02-13 stsp p->sorted_large_offsets = calloc(p->nlargeobj,
561 67fd6849 2022-02-13 stsp sizeof(p->sorted_large_offsets[0]));
562 67fd6849 2022-02-13 stsp if (p->sorted_large_offsets == NULL)
563 67fd6849 2022-02-13 stsp return got_error_from_errno("calloc");
564 67fd6849 2022-02-13 stsp }
565 67fd6849 2022-02-13 stsp
566 67fd6849 2022-02-13 stsp j = 0;
567 67fd6849 2022-02-13 stsp k = 0;
568 67fd6849 2022-02-13 stsp for (i = 0; i < nobj; i++) {
569 67fd6849 2022-02-13 stsp uint32_t offset = be32toh(p->hdr.offsets[i]);
570 67fd6849 2022-02-13 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
571 67fd6849 2022-02-13 stsp uint64_t loffset;
572 67fd6849 2022-02-13 stsp uint32_t idx;
573 67fd6849 2022-02-13 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
574 67fd6849 2022-02-13 stsp if (idx >= p->nlargeobj ||
575 67fd6849 2022-02-13 stsp p->nlargeobj == 0 ||
576 67fd6849 2022-02-13 stsp p->hdr.large_offsets == NULL)
577 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
578 67fd6849 2022-02-13 stsp loffset = be64toh(p->hdr.large_offsets[idx]);
579 67fd6849 2022-02-13 stsp p->sorted_large_offsets[j].offset = loffset;
580 67fd6849 2022-02-13 stsp p->sorted_large_offsets[j].idx = i;
581 67fd6849 2022-02-13 stsp j++;
582 67fd6849 2022-02-13 stsp } else {
583 67fd6849 2022-02-13 stsp p->sorted_offsets[k].offset = offset;
584 67fd6849 2022-02-13 stsp p->sorted_offsets[k].idx = i;
585 67fd6849 2022-02-13 stsp k++;
586 67fd6849 2022-02-13 stsp }
587 67fd6849 2022-02-13 stsp }
588 67fd6849 2022-02-13 stsp if (j != p->nlargeobj || k != nobj - p->nlargeobj)
589 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
590 67fd6849 2022-02-13 stsp
591 67fd6849 2022-02-13 stsp qsort(p->sorted_offsets, nobj - p->nlargeobj,
592 67fd6849 2022-02-13 stsp sizeof(p->sorted_offsets[0]), offset_cmp);
593 67fd6849 2022-02-13 stsp
594 67fd6849 2022-02-13 stsp if (p->sorted_large_offsets)
595 67fd6849 2022-02-13 stsp qsort(p->sorted_large_offsets, p->nlargeobj,
596 67fd6849 2022-02-13 stsp sizeof(p->sorted_large_offsets[0]), large_offset_cmp);
597 67fd6849 2022-02-13 stsp
598 67fd6849 2022-02-13 stsp return NULL;
599 67fd6849 2022-02-13 stsp }
600 67fd6849 2022-02-13 stsp
601 876c234b 2018-09-10 stsp const struct got_error *
602 67fd6849 2022-02-13 stsp got_packidx_get_offset_idx(int *idx, struct got_packidx *packidx, off_t offset)
603 67fd6849 2022-02-13 stsp {
604 67fd6849 2022-02-13 stsp const struct got_error *err;
605 67fd6849 2022-02-13 stsp uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
606 67fd6849 2022-02-13 stsp int i, left, right;
607 67fd6849 2022-02-13 stsp
608 67fd6849 2022-02-13 stsp *idx = -1;
609 67fd6849 2022-02-13 stsp
610 67fd6849 2022-02-13 stsp if (packidx->sorted_offsets == NULL) {
611 67fd6849 2022-02-13 stsp err = build_offset_index(packidx);
612 67fd6849 2022-02-13 stsp if (err)
613 67fd6849 2022-02-13 stsp return err;
614 67fd6849 2022-02-13 stsp }
615 67fd6849 2022-02-13 stsp
616 67fd6849 2022-02-13 stsp if (offset >= 0x7fffffff) {
617 67fd6849 2022-02-13 stsp uint64_t lo;
618 67fd6849 2022-02-13 stsp left = 0, right = packidx->nlargeobj - 1;
619 67fd6849 2022-02-13 stsp while (left <= right) {
620 67fd6849 2022-02-13 stsp i = ((left + right) / 2);
621 67fd6849 2022-02-13 stsp lo = packidx->sorted_large_offsets[i].offset;
622 67fd6849 2022-02-13 stsp if (lo == offset) {
623 67fd6849 2022-02-13 stsp *idx = packidx->sorted_large_offsets[i].idx;
624 67fd6849 2022-02-13 stsp break;
625 67fd6849 2022-02-13 stsp } else if (offset > lo)
626 67fd6849 2022-02-13 stsp left = i + 1;
627 67fd6849 2022-02-13 stsp else if (offset < lo)
628 67fd6849 2022-02-13 stsp right = i - 1;
629 67fd6849 2022-02-13 stsp }
630 67fd6849 2022-02-13 stsp } else {
631 67fd6849 2022-02-13 stsp uint32_t o;
632 67fd6849 2022-02-13 stsp left = 0, right = totobj - packidx->nlargeobj - 1;
633 67fd6849 2022-02-13 stsp while (left <= right) {
634 67fd6849 2022-02-13 stsp i = ((left + right) / 2);
635 67fd6849 2022-02-13 stsp o = packidx->sorted_offsets[i].offset;
636 67fd6849 2022-02-13 stsp if (o == offset) {
637 67fd6849 2022-02-13 stsp *idx = packidx->sorted_offsets[i].idx;
638 67fd6849 2022-02-13 stsp break;
639 67fd6849 2022-02-13 stsp } else if (offset > o)
640 67fd6849 2022-02-13 stsp left = i + 1;
641 67fd6849 2022-02-13 stsp else if (offset < o)
642 67fd6849 2022-02-13 stsp right = i - 1;
643 67fd6849 2022-02-13 stsp }
644 67fd6849 2022-02-13 stsp }
645 67fd6849 2022-02-13 stsp
646 67fd6849 2022-02-13 stsp return NULL;
647 67fd6849 2022-02-13 stsp }
648 67fd6849 2022-02-13 stsp
649 67fd6849 2022-02-13 stsp const struct got_error *
650 67fd6849 2022-02-13 stsp got_packidx_get_object_id(struct got_object_id *id,
651 67fd6849 2022-02-13 stsp struct got_packidx *packidx, int idx)
652 67fd6849 2022-02-13 stsp {
653 67fd6849 2022-02-13 stsp uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
654 67fd6849 2022-02-13 stsp struct got_packidx_object_id *oid;
655 67fd6849 2022-02-13 stsp
656 67fd6849 2022-02-13 stsp if (idx < 0 || idx >= totobj)
657 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_NO_OBJ);
658 67fd6849 2022-02-13 stsp
659 67fd6849 2022-02-13 stsp oid = &packidx->hdr.sorted_ids[idx];
660 67fd6849 2022-02-13 stsp memcpy(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
661 67fd6849 2022-02-13 stsp return NULL;
662 67fd6849 2022-02-13 stsp }
663 67fd6849 2022-02-13 stsp
664 67fd6849 2022-02-13 stsp const struct got_error *
665 dd88155e 2019-06-29 stsp got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
666 4277420a 2019-06-29 stsp struct got_packidx *packidx, const char *id_str_prefix)
667 e09a504c 2019-06-28 stsp {
668 dd88155e 2019-06-29 stsp const struct got_error *err = NULL;
669 4277420a 2019-06-29 stsp u_int8_t id0;
670 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
671 4277420a 2019-06-29 stsp char hex[3];
672 4277420a 2019-06-29 stsp size_t prefix_len = strlen(id_str_prefix);
673 e09a504c 2019-06-28 stsp struct got_packidx_object_id *oid;
674 404bde06 2022-01-03 stsp uint32_t i = 0;
675 e09a504c 2019-06-28 stsp
676 dbdddfee 2021-06-23 naddy STAILQ_INIT(matched_ids);
677 4277420a 2019-06-29 stsp
678 4277420a 2019-06-29 stsp if (prefix_len < 2)
679 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
680 4277420a 2019-06-29 stsp
681 4277420a 2019-06-29 stsp hex[0] = id_str_prefix[0];
682 4277420a 2019-06-29 stsp hex[1] = id_str_prefix[1];
683 4277420a 2019-06-29 stsp hex[2] = '\0';
684 4277420a 2019-06-29 stsp if (!got_parse_xdigit(&id0, hex))
685 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
686 4277420a 2019-06-29 stsp
687 404bde06 2022-01-03 stsp if (id0 > 0)
688 404bde06 2022-01-03 stsp i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
689 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[i];
690 4277420a 2019-06-29 stsp while (i < totobj && oid->sha1[0] == id0) {
691 4277420a 2019-06-29 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
692 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
693 4277420a 2019-06-29 stsp int cmp;
694 4277420a 2019-06-29 stsp
695 4277420a 2019-06-29 stsp if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
696 4277420a 2019-06-29 stsp return got_error(GOT_ERR_NO_SPACE);
697 4277420a 2019-06-29 stsp
698 4277420a 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, prefix_len);
699 4277420a 2019-06-29 stsp if (cmp < 0) {
700 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
701 4277420a 2019-06-29 stsp continue;
702 4277420a 2019-06-29 stsp } else if (cmp > 0)
703 e09a504c 2019-06-28 stsp break;
704 4277420a 2019-06-29 stsp
705 dd88155e 2019-06-29 stsp err = got_object_qid_alloc_partial(&qid);
706 dd88155e 2019-06-29 stsp if (err)
707 dd88155e 2019-06-29 stsp break;
708 d7b5a0e8 2022-04-20 stsp memcpy(qid->id.sha1, oid->sha1, SHA1_DIGEST_LENGTH);
709 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(matched_ids, qid, entry);
710 4277420a 2019-06-29 stsp
711 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
712 e09a504c 2019-06-28 stsp }
713 e09a504c 2019-06-28 stsp
714 0adc7bcc 2019-06-29 stsp if (err)
715 0adc7bcc 2019-06-29 stsp got_object_id_queue_free(matched_ids);
716 3d589bee 2022-06-25 stsp return err;
717 3d589bee 2022-06-25 stsp }
718 3d589bee 2022-06-25 stsp
719 3d589bee 2022-06-25 stsp static void
720 3d589bee 2022-06-25 stsp set_max_datasize(void)
721 3d589bee 2022-06-25 stsp {
722 3d589bee 2022-06-25 stsp struct rlimit rl;
723 3d589bee 2022-06-25 stsp
724 3d589bee 2022-06-25 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
725 3d589bee 2022-06-25 stsp return;
726 3d589bee 2022-06-25 stsp
727 3d589bee 2022-06-25 stsp rl.rlim_cur = rl.rlim_max;
728 3d589bee 2022-06-25 stsp setrlimit(RLIMIT_DATA, &rl);
729 3d589bee 2022-06-25 stsp }
730 3d589bee 2022-06-25 stsp
731 3d589bee 2022-06-25 stsp const struct got_error *
732 3d589bee 2022-06-25 stsp got_pack_start_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
733 3d589bee 2022-06-25 stsp {
734 3d589bee 2022-06-25 stsp const struct got_error *err = NULL;
735 3d589bee 2022-06-25 stsp int imsg_fds[2];
736 3d589bee 2022-06-25 stsp pid_t pid;
737 3d589bee 2022-06-25 stsp struct imsgbuf *ibuf;
738 3d589bee 2022-06-25 stsp
739 3d589bee 2022-06-25 stsp ibuf = calloc(1, sizeof(*ibuf));
740 3d589bee 2022-06-25 stsp if (ibuf == NULL)
741 3d589bee 2022-06-25 stsp return got_error_from_errno("calloc");
742 3d589bee 2022-06-25 stsp
743 3d589bee 2022-06-25 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
744 3d589bee 2022-06-25 stsp if (pack->privsep_child == NULL) {
745 3d589bee 2022-06-25 stsp err = got_error_from_errno("calloc");
746 3d589bee 2022-06-25 stsp free(ibuf);
747 3d589bee 2022-06-25 stsp return err;
748 3d589bee 2022-06-25 stsp }
749 3d589bee 2022-06-25 stsp pack->child_has_tempfiles = 0;
750 3d589bee 2022-06-25 stsp pack->child_has_delta_outfd = 0;
751 3d589bee 2022-06-25 stsp
752 3d589bee 2022-06-25 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
753 3d589bee 2022-06-25 stsp err = got_error_from_errno("socketpair");
754 3d589bee 2022-06-25 stsp goto done;
755 3d589bee 2022-06-25 stsp }
756 3d589bee 2022-06-25 stsp
757 3d589bee 2022-06-25 stsp pid = fork();
758 3d589bee 2022-06-25 stsp if (pid == -1) {
759 3d589bee 2022-06-25 stsp err = got_error_from_errno("fork");
760 3d589bee 2022-06-25 stsp goto done;
761 3d589bee 2022-06-25 stsp } else if (pid == 0) {
762 3d589bee 2022-06-25 stsp set_max_datasize();
763 3d589bee 2022-06-25 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
764 3d589bee 2022-06-25 stsp pack->path_packfile);
765 3d589bee 2022-06-25 stsp /* not reached */
766 3d589bee 2022-06-25 stsp }
767 3d589bee 2022-06-25 stsp
768 3d589bee 2022-06-25 stsp if (close(imsg_fds[1]) == -1)
769 3d589bee 2022-06-25 stsp return got_error_from_errno("close");
770 3d589bee 2022-06-25 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
771 3d589bee 2022-06-25 stsp pack->privsep_child->pid = pid;
772 3d589bee 2022-06-25 stsp imsg_init(ibuf, imsg_fds[0]);
773 3d589bee 2022-06-25 stsp pack->privsep_child->ibuf = ibuf;
774 3d589bee 2022-06-25 stsp
775 3d589bee 2022-06-25 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
776 3d589bee 2022-06-25 stsp if (err) {
777 3d589bee 2022-06-25 stsp const struct got_error *child_err;
778 3d589bee 2022-06-25 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
779 3d589bee 2022-06-25 stsp child_err = got_privsep_wait_for_child(
780 3d589bee 2022-06-25 stsp pack->privsep_child->pid);
781 3d589bee 2022-06-25 stsp if (child_err && err == NULL)
782 3d589bee 2022-06-25 stsp err = child_err;
783 3d589bee 2022-06-25 stsp }
784 3d589bee 2022-06-25 stsp done:
785 3d589bee 2022-06-25 stsp if (err) {
786 3d589bee 2022-06-25 stsp free(ibuf);
787 3d589bee 2022-06-25 stsp free(pack->privsep_child);
788 3d589bee 2022-06-25 stsp pack->privsep_child = NULL;
789 3d589bee 2022-06-25 stsp }
790 dd88155e 2019-06-29 stsp return err;
791 e09a504c 2019-06-28 stsp }
792 e09a504c 2019-06-28 stsp
793 b4f37570 2021-06-19 stsp static const struct got_error *
794 b4f37570 2021-06-19 stsp pack_stop_privsep_child(struct got_pack *pack)
795 876c234b 2018-09-10 stsp {
796 74ef8aae 2022-10-24 stsp const struct got_error *err = NULL, *close_err = NULL;
797 876c234b 2018-09-10 stsp
798 876c234b 2018-09-10 stsp if (pack->privsep_child == NULL)
799 876c234b 2018-09-10 stsp return NULL;
800 876c234b 2018-09-10 stsp
801 876c234b 2018-09-10 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
802 96732e0b 2018-11-11 stsp if (err)
803 96732e0b 2018-11-11 stsp return err;
804 74ef8aae 2022-10-24 stsp if (close(pack->privsep_child->imsg_fd) == -1)
805 74ef8aae 2022-10-24 stsp close_err = got_error_from_errno("close");
806 96732e0b 2018-11-11 stsp err = got_privsep_wait_for_child(pack->privsep_child->pid);
807 74ef8aae 2022-10-24 stsp if (close_err && err == NULL)
808 74ef8aae 2022-10-24 stsp err = close_err;
809 74ef8aae 2022-10-24 stsp imsg_clear(pack->privsep_child->ibuf);
810 cc2a8ef4 2021-06-19 tracey free(pack->privsep_child->ibuf);
811 876c234b 2018-09-10 stsp free(pack->privsep_child);
812 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
813 876c234b 2018-09-10 stsp return err;
814 7e656b93 2018-03-17 stsp }
815 7e656b93 2018-03-17 stsp
816 d7464085 2018-07-09 stsp const struct got_error *
817 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
818 7e656b93 2018-03-17 stsp {
819 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
820 d7464085 2018-07-09 stsp
821 b4f37570 2021-06-19 stsp err = pack_stop_privsep_child(pack);
822 876c234b 2018-09-10 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
823 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
824 08578a35 2021-01-22 stsp if (pack->fd != -1 && close(pack->fd) == -1 && err == NULL)
825 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
826 8b2180d4 2018-04-26 stsp pack->fd = -1;
827 4589e373 2018-03-17 stsp free(pack->path_packfile);
828 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
829 4589e373 2018-03-17 stsp pack->filesize = 0;
830 ab2f42e7 2019-11-10 stsp if (pack->delta_cache) {
831 ab2f42e7 2019-11-10 stsp got_delta_cache_free(pack->delta_cache);
832 ab2f42e7 2019-11-10 stsp pack->delta_cache = NULL;
833 ab2f42e7 2019-11-10 stsp }
834 7e656b93 2018-03-17 stsp
835 57160834 2022-05-31 stsp /*
836 57160834 2022-05-31 stsp * Leave accumfd and basefd alone. They are managed by the
837 57160834 2022-05-31 stsp * repository layer and can be reused.
838 57160834 2022-05-31 stsp */
839 57160834 2022-05-31 stsp
840 c7fe698a 2018-01-23 stsp return err;
841 c7fe698a 2018-01-23 stsp }
842 c7fe698a 2018-01-23 stsp
843 668a20f6 2020-03-18 stsp const struct got_error *
844 668a20f6 2020-03-18 stsp got_pack_parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
845 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
846 a487c1d0 2018-01-14 stsp {
847 a487c1d0 2018-01-14 stsp uint8_t t = 0;
848 a487c1d0 2018-01-14 stsp uint64_t s = 0;
849 a487c1d0 2018-01-14 stsp uint8_t sizeN;
850 d7464085 2018-07-09 stsp size_t mapoff = 0;
851 a487c1d0 2018-01-14 stsp int i = 0;
852 d7464085 2018-07-09 stsp
853 d7464085 2018-07-09 stsp *len = 0;
854 d7464085 2018-07-09 stsp
855 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
856 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
857 d7464085 2018-07-09 stsp
858 d7464085 2018-07-09 stsp if (pack->map) {
859 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
860 d7464085 2018-07-09 stsp } else {
861 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
862 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
863 d7464085 2018-07-09 stsp }
864 a487c1d0 2018-01-14 stsp
865 a1fd68d8 2018-01-12 stsp do {
866 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
867 a487c1d0 2018-01-14 stsp if (i > 9)
868 09ee8ded 2022-10-20 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
869 e082ed67 2022-10-24 naddy "packfile offset %lld", (long long)offset);
870 a1fd68d8 2018-01-12 stsp
871 d7464085 2018-07-09 stsp if (pack->map) {
872 3976db15 2022-01-10 stsp if (mapoff + sizeof(sizeN) >= pack->filesize)
873 3976db15 2022-01-10 stsp return got_error(GOT_ERR_BAD_PACKFILE);
874 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
875 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
876 d7464085 2018-07-09 stsp } else {
877 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
878 d7464085 2018-07-09 stsp if (n < 0)
879 638f9024 2019-05-13 stsp return got_error_from_errno("read");
880 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
881 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
882 d7464085 2018-07-09 stsp }
883 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
884 8251fdbc 2018-01-12 stsp
885 a1fd68d8 2018-01-12 stsp if (i == 0) {
886 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
887 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
888 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
889 a1fd68d8 2018-01-12 stsp } else {
890 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
891 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
892 a1fd68d8 2018-01-12 stsp }
893 a1fd68d8 2018-01-12 stsp i++;
894 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
895 a1fd68d8 2018-01-12 stsp
896 a487c1d0 2018-01-14 stsp *type = t;
897 a487c1d0 2018-01-14 stsp *size = s;
898 a487c1d0 2018-01-14 stsp return NULL;
899 0a0a3048 2018-01-10 stsp }
900 c54542a0 2018-01-13 stsp
901 a1fd68d8 2018-01-12 stsp static const struct got_error *
902 5f25cc85 2019-11-26 stsp open_plain_object(struct got_object **obj, struct got_object_id *id,
903 5f25cc85 2019-11-26 stsp uint8_t type, off_t offset, size_t size, int idx)
904 6ccb713b 2018-01-19 stsp {
905 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
906 6ccb713b 2018-01-19 stsp if (*obj == NULL)
907 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
908 6ccb713b 2018-01-19 stsp
909 6ccb713b 2018-01-19 stsp (*obj)->type = type;
910 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
911 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
912 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
913 6ccb713b 2018-01-19 stsp (*obj)->size = size;
914 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
915 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
916 b107e67f 2018-01-19 stsp
917 b107e67f 2018-01-19 stsp return NULL;
918 b107e67f 2018-01-19 stsp }
919 b107e67f 2018-01-19 stsp
920 b107e67f 2018-01-19 stsp static const struct got_error *
921 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
922 d7464085 2018-07-09 stsp off_t delta_offset)
923 b107e67f 2018-01-19 stsp {
924 b107e67f 2018-01-19 stsp int64_t o = 0;
925 b107e67f 2018-01-19 stsp uint8_t offN;
926 b107e67f 2018-01-19 stsp int i = 0;
927 b107e67f 2018-01-19 stsp
928 a0de39f3 2019-08-09 stsp *offset = 0;
929 d7464085 2018-07-09 stsp *len = 0;
930 d7464085 2018-07-09 stsp
931 b107e67f 2018-01-19 stsp do {
932 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
933 b107e67f 2018-01-19 stsp if (i > 8)
934 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
935 b107e67f 2018-01-19 stsp
936 d7464085 2018-07-09 stsp if (pack->map) {
937 d7464085 2018-07-09 stsp size_t mapoff;
938 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
939 3976db15 2022-01-10 stsp if (mapoff + sizeof(offN) >= pack->filesize)
940 3976db15 2022-01-10 stsp return got_error(GOT_ERR_PACK_OFFSET);
941 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
942 d7464085 2018-07-09 stsp } else {
943 d7464085 2018-07-09 stsp ssize_t n;
944 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
945 d7464085 2018-07-09 stsp if (n < 0)
946 638f9024 2019-05-13 stsp return got_error_from_errno("read");
947 d7464085 2018-07-09 stsp if (n != sizeof(offN))
948 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
949 d7464085 2018-07-09 stsp }
950 d7464085 2018-07-09 stsp *len += sizeof(offN);
951 b107e67f 2018-01-19 stsp
952 b107e67f 2018-01-19 stsp if (i == 0)
953 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
954 b107e67f 2018-01-19 stsp else {
955 cecc778e 2018-01-23 stsp o++;
956 b107e67f 2018-01-19 stsp o <<= 7;
957 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
958 b107e67f 2018-01-19 stsp }
959 b107e67f 2018-01-19 stsp i++;
960 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
961 6ccb713b 2018-01-19 stsp
962 b107e67f 2018-01-19 stsp *offset = o;
963 6ccb713b 2018-01-19 stsp return NULL;
964 6ccb713b 2018-01-19 stsp }
965 6ccb713b 2018-01-19 stsp
966 668a20f6 2020-03-18 stsp const struct got_error *
967 c0df5966 2021-12-31 stsp got_pack_parse_offset_delta(off_t *base_offset, size_t *len,
968 24d916d2 2022-10-24 op struct got_pack *pack, off_t offset, size_t tslen)
969 b107e67f 2018-01-19 stsp {
970 96f5e8b3 2018-01-23 stsp const struct got_error *err;
971 b107e67f 2018-01-19 stsp int64_t negoffset;
972 b107e67f 2018-01-19 stsp size_t negofflen;
973 b107e67f 2018-01-19 stsp
974 d7464085 2018-07-09 stsp *len = 0;
975 d7464085 2018-07-09 stsp
976 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
977 d7464085 2018-07-09 stsp offset + tslen);
978 b107e67f 2018-01-19 stsp if (err)
979 b107e67f 2018-01-19 stsp return err;
980 b107e67f 2018-01-19 stsp
981 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
982 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
983 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
984 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
985 b107e67f 2018-01-19 stsp
986 d7464085 2018-07-09 stsp *len = negofflen;
987 96f5e8b3 2018-01-23 stsp return NULL;
988 96f5e8b3 2018-01-23 stsp }
989 96f5e8b3 2018-01-23 stsp
990 0e22967e 2018-02-11 stsp static const struct got_error *
991 42c69117 2019-11-10 stsp read_delta_data(uint8_t **delta_buf, size_t *delta_len,
992 2d9e6abf 2022-05-04 stsp size_t *delta_compressed_len, size_t delta_data_offset,
993 2d9e6abf 2022-05-04 stsp struct got_pack *pack)
994 42c69117 2019-11-10 stsp {
995 42c69117 2019-11-10 stsp const struct got_error *err = NULL;
996 2d9e6abf 2022-05-04 stsp size_t consumed = 0;
997 42c69117 2019-11-10 stsp
998 42c69117 2019-11-10 stsp if (pack->map) {
999 42c69117 2019-11-10 stsp if (delta_data_offset >= pack->filesize)
1000 42c69117 2019-11-10 stsp return got_error(GOT_ERR_PACK_OFFSET);
1001 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(delta_buf, delta_len,
1002 2d9e6abf 2022-05-04 stsp &consumed, NULL, pack->map, delta_data_offset,
1003 2e5a6fad 2020-03-18 stsp pack->filesize - delta_data_offset);
1004 2d9e6abf 2022-05-04 stsp if (err)
1005 2d9e6abf 2022-05-04 stsp return err;
1006 42c69117 2019-11-10 stsp } else {
1007 42c69117 2019-11-10 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
1008 42c69117 2019-11-10 stsp return got_error_from_errno("lseek");
1009 2d9e6abf 2022-05-04 stsp err = got_inflate_to_mem_fd(delta_buf, delta_len,
1010 2d9e6abf 2022-05-04 stsp &consumed, NULL, 0, pack->fd);
1011 2d9e6abf 2022-05-04 stsp if (err)
1012 2d9e6abf 2022-05-04 stsp return err;
1013 42c69117 2019-11-10 stsp }
1014 2d9e6abf 2022-05-04 stsp
1015 2d9e6abf 2022-05-04 stsp if (delta_compressed_len)
1016 2d9e6abf 2022-05-04 stsp *delta_compressed_len = consumed;
1017 2d9e6abf 2022-05-04 stsp
1018 2d9e6abf 2022-05-04 stsp return NULL;
1019 42c69117 2019-11-10 stsp }
1020 a3500804 2018-01-23 stsp
1021 96f5e8b3 2018-01-23 stsp static const struct got_error *
1022 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
1023 42c69117 2019-11-10 stsp int delta_type, size_t delta_size, size_t delta_data_offset)
1024 bdd2fbb3 2018-02-11 stsp {
1025 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
1026 bdd2fbb3 2018-02-11 stsp
1027 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
1028 42c69117 2019-11-10 stsp delta_data_offset);
1029 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
1030 638f9024 2019-05-13 stsp return got_error_from_errno("got_delta_open");
1031 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
1032 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
1033 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&deltas->entries, delta, entry);
1034 bdd2fbb3 2018-02-11 stsp return NULL;
1035 bdd2fbb3 2018-02-11 stsp }
1036 bdd2fbb3 2018-02-11 stsp
1037 bdd2fbb3 2018-02-11 stsp static const struct got_error *
1038 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
1039 c8ecd499 2018-09-09 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1040 c8ecd499 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1041 a3500804 2018-01-23 stsp {
1042 a3500804 2018-01-23 stsp const struct got_error *err;
1043 c3703302 2018-01-23 stsp off_t base_offset;
1044 c3703302 2018-01-23 stsp uint8_t base_type;
1045 c3703302 2018-01-23 stsp uint64_t base_size;
1046 c3703302 2018-01-23 stsp size_t base_tslen;
1047 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
1048 42c69117 2019-11-10 stsp size_t consumed;
1049 a3500804 2018-01-23 stsp
1050 668a20f6 2020-03-18 stsp err = got_pack_parse_offset_delta(&base_offset, &consumed, pack,
1051 d7464085 2018-07-09 stsp delta_offset, tslen);
1052 a3500804 2018-01-23 stsp if (err)
1053 a3500804 2018-01-23 stsp return err;
1054 a3500804 2018-01-23 stsp
1055 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
1056 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
1057 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1058 a53d2f13 2018-03-17 stsp
1059 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1060 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1061 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
1062 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1063 d7464085 2018-07-09 stsp }
1064 bdd2fbb3 2018-02-11 stsp
1065 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1066 42c69117 2019-11-10 stsp delta_data_offset);
1067 bdd2fbb3 2018-02-11 stsp if (err)
1068 1a35c1bc 2019-05-22 stsp return err;
1069 bdd2fbb3 2018-02-11 stsp
1070 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
1071 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
1072 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
1073 b107e67f 2018-01-19 stsp
1074 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1075 668a20f6 2020-03-18 stsp &base_tslen, pack, base_offset);
1076 b107e67f 2018-01-19 stsp if (err)
1077 1a35c1bc 2019-05-22 stsp return err;
1078 b107e67f 2018-01-19 stsp
1079 668a20f6 2020-03-18 stsp return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1080 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
1081 c3703302 2018-01-23 stsp }
1082 c4330eff 2021-06-22 stsp
1083 c4330eff 2021-06-22 stsp const struct got_error *
1084 c4330eff 2021-06-22 stsp got_pack_parse_ref_delta(struct got_object_id *id,
1085 c4330eff 2021-06-22 stsp struct got_pack *pack, off_t delta_offset, int tslen)
1086 c4330eff 2021-06-22 stsp {
1087 c4330eff 2021-06-22 stsp if (pack->map) {
1088 c4330eff 2021-06-22 stsp size_t mapoff = delta_offset + tslen;
1089 3976db15 2022-01-10 stsp if (mapoff + sizeof(*id) >= pack->filesize)
1090 3976db15 2022-01-10 stsp return got_error(GOT_ERR_PACK_OFFSET);
1091 c4330eff 2021-06-22 stsp memcpy(id, pack->map + mapoff, sizeof(*id));
1092 c4330eff 2021-06-22 stsp } else {
1093 c4330eff 2021-06-22 stsp ssize_t n;
1094 c4330eff 2021-06-22 stsp n = read(pack->fd, id, sizeof(*id));
1095 c4330eff 2021-06-22 stsp if (n < 0)
1096 c4330eff 2021-06-22 stsp return got_error_from_errno("read");
1097 c4330eff 2021-06-22 stsp if (n != sizeof(*id))
1098 c4330eff 2021-06-22 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1099 c4330eff 2021-06-22 stsp }
1100 c3703302 2018-01-23 stsp
1101 c4330eff 2021-06-22 stsp return NULL;
1102 c4330eff 2021-06-22 stsp }
1103 c4330eff 2021-06-22 stsp
1104 c3703302 2018-01-23 stsp static const struct got_error *
1105 c8ecd499 2018-09-09 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
1106 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1107 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
1108 c3703302 2018-01-23 stsp {
1109 6b9c9673 2018-01-23 stsp const struct got_error *err;
1110 6b9c9673 2018-01-23 stsp struct got_object_id id;
1111 6b9c9673 2018-01-23 stsp int idx;
1112 6b9c9673 2018-01-23 stsp off_t base_offset;
1113 6b9c9673 2018-01-23 stsp uint8_t base_type;
1114 6b9c9673 2018-01-23 stsp uint64_t base_size;
1115 6b9c9673 2018-01-23 stsp size_t base_tslen;
1116 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
1117 6b9c9673 2018-01-23 stsp
1118 42c69117 2019-11-10 stsp if (delta_offset + tslen >= pack->filesize)
1119 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1120 a53d2f13 2018-03-17 stsp
1121 c4330eff 2021-06-22 stsp err = got_pack_parse_ref_delta(&id, pack, delta_offset, tslen);
1122 c4330eff 2021-06-22 stsp if (err)
1123 c4330eff 2021-06-22 stsp return err;
1124 d7464085 2018-07-09 stsp if (pack->map) {
1125 c4330eff 2021-06-22 stsp delta_data_offset = delta_offset + tslen + sizeof(id);
1126 d7464085 2018-07-09 stsp } else {
1127 e40b19ed 2020-01-06 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1128 e40b19ed 2020-01-06 stsp if (delta_data_offset == -1)
1129 e40b19ed 2020-01-06 stsp return got_error_from_errno("lseek");
1130 d7464085 2018-07-09 stsp }
1131 d7464085 2018-07-09 stsp
1132 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1133 42c69117 2019-11-10 stsp delta_data_offset);
1134 bdd2fbb3 2018-02-11 stsp if (err)
1135 1a35c1bc 2019-05-22 stsp return err;
1136 bdd2fbb3 2018-02-11 stsp
1137 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
1138 1510f469 2018-09-09 stsp idx = got_packidx_get_object_idx(packidx, &id);
1139 1a35c1bc 2019-05-22 stsp if (idx == -1)
1140 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_NO_OBJ);
1141 6b9c9673 2018-01-23 stsp
1142 02828bfd 2021-06-22 stsp base_offset = got_packidx_get_object_offset(packidx, idx);
1143 04aed155 2022-07-24 naddy if (base_offset == -1)
1144 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1145 6b9c9673 2018-01-23 stsp
1146 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
1147 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
1148 6b9c9673 2018-01-23 stsp
1149 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1150 668a20f6 2020-03-18 stsp &base_tslen, pack, base_offset);
1151 6b9c9673 2018-01-23 stsp if (err)
1152 1a35c1bc 2019-05-22 stsp return err;
1153 6b9c9673 2018-01-23 stsp
1154 668a20f6 2020-03-18 stsp return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1155 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
1156 6b9c9673 2018-01-23 stsp }
1157 6b9c9673 2018-01-23 stsp
1158 668a20f6 2020-03-18 stsp const struct got_error *
1159 668a20f6 2020-03-18 stsp got_pack_resolve_delta_chain(struct got_delta_chain *deltas,
1160 668a20f6 2020-03-18 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1161 668a20f6 2020-03-18 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1162 6b9c9673 2018-01-23 stsp {
1163 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
1164 c3703302 2018-01-23 stsp
1165 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
1166 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
1167 5b7e13a7 2018-04-02 stsp
1168 c3703302 2018-01-23 stsp switch (delta_type) {
1169 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
1170 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
1171 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
1172 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
1173 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
1174 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
1175 42c69117 2019-11-10 stsp delta_size, 0);
1176 4e8cda55 2018-01-19 stsp break;
1177 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1178 c8ecd499 2018-09-09 stsp err = resolve_offset_delta(deltas, packidx, pack,
1179 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1180 96f5e8b3 2018-01-23 stsp break;
1181 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
1182 c8ecd499 2018-09-09 stsp err = resolve_ref_delta(deltas, packidx, pack,
1183 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1184 6b9c9673 2018-01-23 stsp break;
1185 4e8cda55 2018-01-19 stsp default:
1186 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
1187 4e8cda55 2018-01-19 stsp }
1188 96f5e8b3 2018-01-23 stsp
1189 96f5e8b3 2018-01-23 stsp return err;
1190 96f5e8b3 2018-01-23 stsp }
1191 96f5e8b3 2018-01-23 stsp
1192 96f5e8b3 2018-01-23 stsp static const struct got_error *
1193 a98c62b1 2018-09-09 stsp open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1194 a98c62b1 2018-09-09 stsp struct got_pack *pack, struct got_object_id *id, off_t offset,
1195 c59b3346 2018-09-11 stsp size_t tslen, int delta_type, size_t delta_size, int idx)
1196 96f5e8b3 2018-01-23 stsp {
1197 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
1198 96f5e8b3 2018-01-23 stsp int resolved_type;
1199 4e8cda55 2018-01-19 stsp
1200 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
1201 b107e67f 2018-01-19 stsp if (*obj == NULL)
1202 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1203 b107e67f 2018-01-19 stsp
1204 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
1205 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
1206 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1207 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1208 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
1209 1a35c1bc 2019-05-22 stsp
1210 dbdddfee 2021-06-23 naddy STAILQ_INIT(&(*obj)->deltas.entries);
1211 1a35c1bc 2019-05-22 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1212 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1213 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
1214 96f5e8b3 2018-01-23 stsp
1215 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&(*obj)->deltas, packidx, pack,
1216 668a20f6 2020-03-18 stsp offset, tslen, delta_type, delta_size,
1217 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
1218 96f5e8b3 2018-01-23 stsp if (err)
1219 96f5e8b3 2018-01-23 stsp goto done;
1220 96f5e8b3 2018-01-23 stsp
1221 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1222 96f5e8b3 2018-01-23 stsp if (err)
1223 96f5e8b3 2018-01-23 stsp goto done;
1224 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
1225 96f5e8b3 2018-01-23 stsp done:
1226 96f5e8b3 2018-01-23 stsp if (err) {
1227 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
1228 96f5e8b3 2018-01-23 stsp *obj = NULL;
1229 96f5e8b3 2018-01-23 stsp }
1230 96f5e8b3 2018-01-23 stsp return err;
1231 b107e67f 2018-01-19 stsp }
1232 b107e67f 2018-01-19 stsp
1233 2090a03d 2018-09-09 stsp const struct got_error *
1234 2090a03d 2018-09-09 stsp got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
1235 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1236 a1fd68d8 2018-01-12 stsp {
1237 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1238 a1fd68d8 2018-01-12 stsp off_t offset;
1239 6c00b545 2018-01-17 stsp uint8_t type;
1240 6c00b545 2018-01-17 stsp uint64_t size;
1241 3ee5fc21 2018-01-17 stsp size_t tslen;
1242 a1fd68d8 2018-01-12 stsp
1243 6c00b545 2018-01-17 stsp *obj = NULL;
1244 a1fd68d8 2018-01-12 stsp
1245 02828bfd 2021-06-22 stsp offset = got_packidx_get_object_offset(packidx, idx);
1246 04aed155 2022-07-24 naddy if (offset == -1)
1247 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1248 a1fd68d8 2018-01-12 stsp
1249 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1250 668a20f6 2020-03-18 stsp pack, offset);
1251 a1fd68d8 2018-01-12 stsp if (err)
1252 35c73765 2018-09-09 stsp return err;
1253 a1fd68d8 2018-01-12 stsp
1254 6c00b545 2018-01-17 stsp switch (type) {
1255 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1256 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1257 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1258 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1259 5f25cc85 2019-11-26 stsp err = open_plain_object(obj, id, type, offset + tslen,
1260 5f25cc85 2019-11-26 stsp size, idx);
1261 6c00b545 2018-01-17 stsp break;
1262 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1263 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1264 a98c62b1 2018-09-09 stsp err = open_delta_object(obj, packidx, pack, id, offset,
1265 c59b3346 2018-09-11 stsp tslen, type, size, idx);
1266 b107e67f 2018-01-19 stsp break;
1267 6c00b545 2018-01-17 stsp default:
1268 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1269 35c73765 2018-09-09 stsp break;
1270 35c73765 2018-09-09 stsp }
1271 35c73765 2018-09-09 stsp
1272 3ee5fc21 2018-01-17 stsp return err;
1273 3ee5fc21 2018-01-17 stsp }
1274 efd2a263 2018-01-19 stsp
1275 d582f26c 2020-03-18 stsp const struct got_error *
1276 c0df5966 2021-12-31 stsp got_pack_get_delta_chain_max_size(uint64_t *max_size,
1277 c0df5966 2021-12-31 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1278 22484865 2018-03-13 stsp {
1279 22484865 2018-03-13 stsp struct got_delta *delta;
1280 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1281 22484865 2018-03-13 stsp
1282 22484865 2018-03-13 stsp *max_size = 0;
1283 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1284 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1285 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1286 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1287 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1288 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1289 22484865 2018-03-13 stsp const struct got_error *err;
1290 aabb25f8 2022-10-17 stsp uint8_t *delta_buf = NULL;
1291 42c69117 2019-11-10 stsp size_t delta_len;
1292 ab2f42e7 2019-11-10 stsp int cached = 1;
1293 42c69117 2019-11-10 stsp
1294 aabb25f8 2022-10-17 stsp if (pack->delta_cache) {
1295 aabb25f8 2022-10-17 stsp got_delta_cache_get(&delta_buf, &delta_len,
1296 aabb25f8 2022-10-17 stsp pack->delta_cache, delta->data_offset);
1297 aabb25f8 2022-10-17 stsp }
1298 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1299 ab2f42e7 2019-11-10 stsp cached = 0;
1300 ab2f42e7 2019-11-10 stsp err = read_delta_data(&delta_buf, &delta_len,
1301 2d9e6abf 2022-05-04 stsp NULL, delta->data_offset, pack);
1302 ab2f42e7 2019-11-10 stsp if (err)
1303 ab2f42e7 2019-11-10 stsp return err;
1304 aabb25f8 2022-10-17 stsp }
1305 aabb25f8 2022-10-17 stsp if (pack->delta_cache && !cached) {
1306 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1307 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1308 ab2f42e7 2019-11-10 stsp if (err == NULL)
1309 ab2f42e7 2019-11-10 stsp cached = 1;
1310 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1311 ab2f42e7 2019-11-10 stsp free(delta_buf);
1312 ab2f42e7 2019-11-10 stsp return err;
1313 ab2f42e7 2019-11-10 stsp }
1314 ab2f42e7 2019-11-10 stsp }
1315 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1316 42c69117 2019-11-10 stsp delta_buf, delta_len);
1317 ab2f42e7 2019-11-10 stsp if (!cached)
1318 ab2f42e7 2019-11-10 stsp free(delta_buf);
1319 22484865 2018-03-13 stsp if (err)
1320 22484865 2018-03-13 stsp return err;
1321 22484865 2018-03-13 stsp } else
1322 22484865 2018-03-13 stsp base_size = delta->size;
1323 22484865 2018-03-13 stsp if (base_size > *max_size)
1324 22484865 2018-03-13 stsp *max_size = base_size;
1325 22484865 2018-03-13 stsp if (result_size > *max_size)
1326 22484865 2018-03-13 stsp *max_size = result_size;
1327 22484865 2018-03-13 stsp }
1328 bd1223b9 2018-03-14 stsp
1329 9feb4ff2 2018-03-14 stsp return NULL;
1330 ac544f8c 2019-01-13 stsp }
1331 ac544f8c 2019-01-13 stsp
1332 ac544f8c 2019-01-13 stsp const struct got_error *
1333 42c69117 2019-11-10 stsp got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
1334 42c69117 2019-11-10 stsp struct got_pack *pack)
1335 ac544f8c 2019-01-13 stsp {
1336 85a703fa 2019-01-13 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1337 85a703fa 2019-01-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1338 85a703fa 2019-01-13 stsp
1339 d582f26c 2020-03-18 stsp return got_pack_get_delta_chain_max_size(size, &obj->deltas, pack);
1340 22484865 2018-03-13 stsp }
1341 22484865 2018-03-13 stsp
1342 668a20f6 2020-03-18 stsp const struct got_error *
1343 4788f1ce 2020-03-18 stsp got_pack_dump_delta_chain_to_file(size_t *result_size,
1344 4788f1ce 2020-03-18 stsp struct got_delta_chain *deltas, struct got_pack *pack, FILE *outfile,
1345 4788f1ce 2020-03-18 stsp FILE *base_file, FILE *accum_file)
1346 efd2a263 2018-01-19 stsp {
1347 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1348 6691714a 2018-01-23 stsp struct got_delta *delta;
1349 aabb25f8 2022-10-17 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1350 6395114c 2022-05-31 stsp size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0, delta_len;
1351 6395114c 2022-05-31 stsp /* We process small enough files entirely in memory for speed. */
1352 6395114c 2022-05-31 stsp const size_t max_bufsize = GOT_DELTA_RESULT_SIZE_CACHED_MAX;
1353 6395114c 2022-05-31 stsp uint64_t max_size = 0;
1354 6691714a 2018-01-23 stsp int n = 0;
1355 b29656e2 2018-03-16 stsp
1356 b29656e2 2018-03-16 stsp *result_size = 0;
1357 3ee5fc21 2018-01-17 stsp
1358 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&deltas->entries))
1359 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1360 efd2a263 2018-01-19 stsp
1361 6395114c 2022-05-31 stsp if (fseeko(base_file, 0L, SEEK_SET) == -1)
1362 6395114c 2022-05-31 stsp return got_error_from_errno("fseeko");
1363 6395114c 2022-05-31 stsp if (fseeko(accum_file, 0L, SEEK_SET) == -1)
1364 6395114c 2022-05-31 stsp return got_error_from_errno("fseeko");
1365 efd2a263 2018-01-19 stsp
1366 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1367 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1368 aabb25f8 2022-10-17 stsp uint8_t *delta_buf = NULL;
1369 6395114c 2022-05-31 stsp uint64_t base_size, result_size = 0;
1370 ab2f42e7 2019-11-10 stsp int cached = 1;
1371 a6b158cc 2018-02-11 stsp if (n == 0) {
1372 34fca9c3 2018-11-11 stsp size_t mapoff;
1373 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1374 9db65d41 2018-03-14 stsp
1375 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1376 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1377 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1378 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1379 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1380 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1381 72eb3431 2018-04-01 stsp goto done;
1382 72eb3431 2018-04-01 stsp }
1383 72eb3431 2018-04-01 stsp
1384 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1385 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1386 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1387 0c048b15 2018-04-27 stsp goto done;
1388 0c048b15 2018-04-27 stsp }
1389 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1390 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1391 d7464085 2018-07-09 stsp == -1) {
1392 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1393 d7464085 2018-07-09 stsp goto done;
1394 d7464085 2018-07-09 stsp }
1395 bdd2fbb3 2018-02-11 stsp }
1396 6395114c 2022-05-31 stsp if (delta->size > max_size)
1397 6395114c 2022-05-31 stsp max_size = delta->size;
1398 6395114c 2022-05-31 stsp if (max_size > max_bufsize) {
1399 d7464085 2018-07-09 stsp if (pack->map) {
1400 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1401 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1402 4788f1ce 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->map,
1403 4788f1ce 2020-03-18 stsp mapoff, pack->filesize - mapoff,
1404 4788f1ce 2020-03-18 stsp base_file);
1405 d7464085 2018-07-09 stsp } else
1406 34fca9c3 2018-11-11 stsp err = got_inflate_to_file_fd(
1407 4788f1ce 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->fd,
1408 4788f1ce 2020-03-18 stsp base_file);
1409 d7464085 2018-07-09 stsp } else {
1410 6395114c 2022-05-31 stsp accum_buf = malloc(max_size);
1411 6395114c 2022-05-31 stsp if (accum_buf == NULL) {
1412 6395114c 2022-05-31 stsp err = got_error_from_errno("malloc");
1413 6395114c 2022-05-31 stsp goto done;
1414 6395114c 2022-05-31 stsp }
1415 6395114c 2022-05-31 stsp accum_bufsz = max_size;
1416 d7464085 2018-07-09 stsp if (pack->map) {
1417 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1418 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1419 2e5a6fad 2020-03-18 stsp &base_bufsz, NULL, NULL,
1420 2e5a6fad 2020-03-18 stsp pack->map, mapoff,
1421 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1422 d7464085 2018-07-09 stsp } else
1423 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1424 55fdd257 2020-03-18 stsp &base_bufsz, NULL, NULL, max_size,
1425 55fdd257 2020-03-18 stsp pack->fd);
1426 8628c62d 2018-03-15 stsp }
1427 a6b158cc 2018-02-11 stsp if (err)
1428 a6b158cc 2018-02-11 stsp goto done;
1429 a6b158cc 2018-02-11 stsp n++;
1430 6395114c 2022-05-31 stsp if (base_buf == NULL)
1431 8628c62d 2018-03-15 stsp rewind(base_file);
1432 a6b158cc 2018-02-11 stsp continue;
1433 9db65d41 2018-03-14 stsp }
1434 885d3e02 2018-01-27 stsp
1435 aabb25f8 2022-10-17 stsp if (pack->delta_cache) {
1436 aabb25f8 2022-10-17 stsp got_delta_cache_get(&delta_buf, &delta_len,
1437 aabb25f8 2022-10-17 stsp pack->delta_cache, delta->data_offset);
1438 aabb25f8 2022-10-17 stsp }
1439 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1440 ab2f42e7 2019-11-10 stsp cached = 0;
1441 2d9e6abf 2022-05-04 stsp err = read_delta_data(&delta_buf, &delta_len, NULL,
1442 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1443 ab2f42e7 2019-11-10 stsp if (err)
1444 ab2f42e7 2019-11-10 stsp goto done;
1445 aabb25f8 2022-10-17 stsp }
1446 aabb25f8 2022-10-17 stsp if (pack->delta_cache && !cached) {
1447 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1448 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1449 ab2f42e7 2019-11-10 stsp if (err == NULL)
1450 ab2f42e7 2019-11-10 stsp cached = 1;
1451 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1452 ab2f42e7 2019-11-10 stsp free(delta_buf);
1453 ab2f42e7 2019-11-10 stsp goto done;
1454 ab2f42e7 2019-11-10 stsp }
1455 ab2f42e7 2019-11-10 stsp }
1456 6395114c 2022-05-31 stsp
1457 6395114c 2022-05-31 stsp err = got_delta_get_sizes(&base_size, &result_size,
1458 6395114c 2022-05-31 stsp delta_buf, delta_len);
1459 6395114c 2022-05-31 stsp if (err)
1460 6395114c 2022-05-31 stsp goto done;
1461 6395114c 2022-05-31 stsp if (base_size > max_size)
1462 6395114c 2022-05-31 stsp max_size = base_size;
1463 6395114c 2022-05-31 stsp if (result_size > max_size)
1464 6395114c 2022-05-31 stsp max_size = result_size;
1465 6395114c 2022-05-31 stsp
1466 6395114c 2022-05-31 stsp if (base_buf && max_size > max_bufsize) {
1467 6395114c 2022-05-31 stsp /* Switch from buffers to temporary files. */
1468 6395114c 2022-05-31 stsp size_t w = fwrite(base_buf, 1, base_bufsz,
1469 6395114c 2022-05-31 stsp base_file);
1470 6395114c 2022-05-31 stsp if (w != base_bufsz) {
1471 6395114c 2022-05-31 stsp err = got_ferror(outfile, GOT_ERR_IO);
1472 6395114c 2022-05-31 stsp goto done;
1473 6395114c 2022-05-31 stsp }
1474 6395114c 2022-05-31 stsp free(base_buf);
1475 6395114c 2022-05-31 stsp base_buf = NULL;
1476 6395114c 2022-05-31 stsp free(accum_buf);
1477 6395114c 2022-05-31 stsp accum_buf = NULL;
1478 6395114c 2022-05-31 stsp }
1479 6395114c 2022-05-31 stsp
1480 6395114c 2022-05-31 stsp if (base_buf && max_size > base_bufsz) {
1481 6395114c 2022-05-31 stsp uint8_t *p = realloc(base_buf, max_size);
1482 6395114c 2022-05-31 stsp if (p == NULL) {
1483 6395114c 2022-05-31 stsp err = got_error_from_errno("realloc");
1484 6395114c 2022-05-31 stsp goto done;
1485 6395114c 2022-05-31 stsp }
1486 6395114c 2022-05-31 stsp base_buf = p;
1487 6395114c 2022-05-31 stsp base_bufsz = max_size;
1488 6395114c 2022-05-31 stsp }
1489 6395114c 2022-05-31 stsp
1490 6395114c 2022-05-31 stsp if (accum_buf && max_size > accum_bufsz) {
1491 6395114c 2022-05-31 stsp uint8_t *p = realloc(accum_buf, max_size);
1492 6395114c 2022-05-31 stsp if (p == NULL) {
1493 6395114c 2022-05-31 stsp err = got_error_from_errno("realloc");
1494 6395114c 2022-05-31 stsp goto done;
1495 6395114c 2022-05-31 stsp }
1496 6395114c 2022-05-31 stsp accum_buf = p;
1497 6395114c 2022-05-31 stsp accum_bufsz = max_size;
1498 6395114c 2022-05-31 stsp }
1499 6395114c 2022-05-31 stsp
1500 8628c62d 2018-03-15 stsp if (base_buf) {
1501 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1502 42c69117 2019-11-10 stsp delta_buf, delta_len, accum_buf,
1503 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1504 8628c62d 2018-03-15 stsp n++;
1505 8628c62d 2018-03-15 stsp } else {
1506 42c69117 2019-11-10 stsp err = got_delta_apply(base_file, delta_buf,
1507 42c69117 2019-11-10 stsp delta_len,
1508 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1509 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1510 b29656e2 2018-03-16 stsp &accum_size);
1511 8628c62d 2018-03-15 stsp }
1512 ab2f42e7 2019-11-10 stsp if (!cached)
1513 ab2f42e7 2019-11-10 stsp free(delta_buf);
1514 6691714a 2018-01-23 stsp if (err)
1515 6691714a 2018-01-23 stsp goto done;
1516 6691714a 2018-01-23 stsp
1517 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1518 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1519 8628c62d 2018-03-15 stsp if (base_buf) {
1520 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1521 6395114c 2022-05-31 stsp size_t tmp_size = accum_bufsz;
1522 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1523 6395114c 2022-05-31 stsp accum_bufsz = base_bufsz;
1524 8628c62d 2018-03-15 stsp base_buf = tmp;
1525 6395114c 2022-05-31 stsp base_bufsz = tmp_size;
1526 8628c62d 2018-03-15 stsp } else {
1527 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1528 8628c62d 2018-03-15 stsp accum_file = base_file;
1529 8628c62d 2018-03-15 stsp base_file = tmp;
1530 8628c62d 2018-03-15 stsp rewind(base_file);
1531 8628c62d 2018-03-15 stsp rewind(accum_file);
1532 8628c62d 2018-03-15 stsp }
1533 6691714a 2018-01-23 stsp }
1534 6691714a 2018-01-23 stsp }
1535 6691714a 2018-01-23 stsp
1536 6691714a 2018-01-23 stsp done:
1537 8628c62d 2018-03-15 stsp free(base_buf);
1538 8628c62d 2018-03-15 stsp if (accum_buf) {
1539 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1540 8628c62d 2018-03-15 stsp free(accum_buf);
1541 8628c62d 2018-03-15 stsp if (len != accum_size)
1542 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1543 8628c62d 2018-03-15 stsp }
1544 6691714a 2018-01-23 stsp rewind(outfile);
1545 b29656e2 2018-03-16 stsp if (err == NULL)
1546 b29656e2 2018-03-16 stsp *result_size = accum_size;
1547 e0ab43e7 2018-03-16 stsp return err;
1548 e0ab43e7 2018-03-16 stsp }
1549 e0ab43e7 2018-03-16 stsp
1550 668a20f6 2020-03-18 stsp const struct got_error *
1551 668a20f6 2020-03-18 stsp got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1552 48095039 2018-09-09 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1553 e0ab43e7 2018-03-16 stsp {
1554 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1555 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1556 aabb25f8 2022-10-17 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1557 f18c433a 2022-05-31 stsp size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0, delta_len;
1558 f18c433a 2022-05-31 stsp uint64_t max_size = 0;
1559 e0ab43e7 2018-03-16 stsp int n = 0;
1560 e0ab43e7 2018-03-16 stsp
1561 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1562 e0ab43e7 2018-03-16 stsp *outlen = 0;
1563 e0ab43e7 2018-03-16 stsp
1564 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&deltas->entries))
1565 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1566 e0ab43e7 2018-03-16 stsp
1567 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1568 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1569 aabb25f8 2022-10-17 stsp uint8_t *delta_buf = NULL;
1570 f18c433a 2022-05-31 stsp uint64_t base_size, result_size = 0;
1571 ab2f42e7 2019-11-10 stsp int cached = 1;
1572 e0ab43e7 2018-03-16 stsp if (n == 0) {
1573 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1574 e0ab43e7 2018-03-16 stsp
1575 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1576 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1577 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1578 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1579 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1580 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1581 72eb3431 2018-04-01 stsp goto done;
1582 72eb3431 2018-04-01 stsp }
1583 72eb3431 2018-04-01 stsp
1584 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1585 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1586 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1587 0c048b15 2018-04-27 stsp goto done;
1588 0c048b15 2018-04-27 stsp }
1589 f18c433a 2022-05-31 stsp
1590 f18c433a 2022-05-31 stsp if (delta->size > max_size)
1591 f18c433a 2022-05-31 stsp max_size = delta->size;
1592 f18c433a 2022-05-31 stsp
1593 d7464085 2018-07-09 stsp if (pack->map) {
1594 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1595 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1596 2e5a6fad 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->map,
1597 2e5a6fad 2020-03-18 stsp mapoff, pack->filesize - mapoff);
1598 d7464085 2018-07-09 stsp } else {
1599 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1600 d7464085 2018-07-09 stsp == -1) {
1601 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1602 d7464085 2018-07-09 stsp goto done;
1603 d7464085 2018-07-09 stsp }
1604 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1605 55fdd257 2020-03-18 stsp &base_bufsz, NULL, NULL, max_size,
1606 55fdd257 2020-03-18 stsp pack->fd);
1607 e0ab43e7 2018-03-16 stsp }
1608 d7464085 2018-07-09 stsp if (err)
1609 d7464085 2018-07-09 stsp goto done;
1610 e0ab43e7 2018-03-16 stsp n++;
1611 e0ab43e7 2018-03-16 stsp continue;
1612 e0ab43e7 2018-03-16 stsp }
1613 e0ab43e7 2018-03-16 stsp
1614 aabb25f8 2022-10-17 stsp if (pack->delta_cache) {
1615 aabb25f8 2022-10-17 stsp got_delta_cache_get(&delta_buf, &delta_len,
1616 aabb25f8 2022-10-17 stsp pack->delta_cache, delta->data_offset);
1617 aabb25f8 2022-10-17 stsp }
1618 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1619 ab2f42e7 2019-11-10 stsp cached = 0;
1620 2d9e6abf 2022-05-04 stsp err = read_delta_data(&delta_buf, &delta_len, NULL,
1621 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1622 ab2f42e7 2019-11-10 stsp if (err)
1623 ab2f42e7 2019-11-10 stsp goto done;
1624 aabb25f8 2022-10-17 stsp }
1625 aabb25f8 2022-10-17 stsp if (pack->delta_cache && !cached) {
1626 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1627 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1628 ab2f42e7 2019-11-10 stsp if (err == NULL)
1629 ab2f42e7 2019-11-10 stsp cached = 1;
1630 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1631 ab2f42e7 2019-11-10 stsp free(delta_buf);
1632 ab2f42e7 2019-11-10 stsp goto done;
1633 ab2f42e7 2019-11-10 stsp }
1634 ab2f42e7 2019-11-10 stsp }
1635 f18c433a 2022-05-31 stsp
1636 f18c433a 2022-05-31 stsp err = got_delta_get_sizes(&base_size, &result_size,
1637 f18c433a 2022-05-31 stsp delta_buf, delta_len);
1638 f18c433a 2022-05-31 stsp if (err)
1639 f18c433a 2022-05-31 stsp goto done;
1640 f18c433a 2022-05-31 stsp if (base_size > max_size)
1641 f18c433a 2022-05-31 stsp max_size = base_size;
1642 f18c433a 2022-05-31 stsp if (result_size > max_size)
1643 f18c433a 2022-05-31 stsp max_size = result_size;
1644 f18c433a 2022-05-31 stsp
1645 f18c433a 2022-05-31 stsp if (max_size > base_bufsz) {
1646 f18c433a 2022-05-31 stsp uint8_t *p = realloc(base_buf, max_size);
1647 f18c433a 2022-05-31 stsp if (p == NULL) {
1648 f18c433a 2022-05-31 stsp err = got_error_from_errno("realloc");
1649 f18c433a 2022-05-31 stsp goto done;
1650 f18c433a 2022-05-31 stsp }
1651 f18c433a 2022-05-31 stsp base_buf = p;
1652 f18c433a 2022-05-31 stsp base_bufsz = max_size;
1653 f18c433a 2022-05-31 stsp }
1654 f18c433a 2022-05-31 stsp
1655 f18c433a 2022-05-31 stsp if (max_size > accum_bufsz) {
1656 f18c433a 2022-05-31 stsp uint8_t *p = realloc(accum_buf, max_size);
1657 f18c433a 2022-05-31 stsp if (p == NULL) {
1658 f18c433a 2022-05-31 stsp err = got_error_from_errno("realloc");
1659 f18c433a 2022-05-31 stsp goto done;
1660 f18c433a 2022-05-31 stsp }
1661 f18c433a 2022-05-31 stsp accum_buf = p;
1662 f18c433a 2022-05-31 stsp accum_bufsz = max_size;
1663 f18c433a 2022-05-31 stsp }
1664 f18c433a 2022-05-31 stsp
1665 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1666 42c69117 2019-11-10 stsp delta_buf, delta_len, accum_buf,
1667 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1668 ab2f42e7 2019-11-10 stsp if (!cached)
1669 ab2f42e7 2019-11-10 stsp free(delta_buf);
1670 e0ab43e7 2018-03-16 stsp n++;
1671 e0ab43e7 2018-03-16 stsp if (err)
1672 e0ab43e7 2018-03-16 stsp goto done;
1673 e0ab43e7 2018-03-16 stsp
1674 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1675 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1676 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1677 f18c433a 2022-05-31 stsp size_t tmp_size = accum_bufsz;
1678 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1679 f18c433a 2022-05-31 stsp accum_bufsz = base_bufsz;
1680 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1681 f18c433a 2022-05-31 stsp base_bufsz = tmp_size;
1682 e0ab43e7 2018-03-16 stsp }
1683 e0ab43e7 2018-03-16 stsp }
1684 e0ab43e7 2018-03-16 stsp
1685 e0ab43e7 2018-03-16 stsp done:
1686 e0ab43e7 2018-03-16 stsp free(base_buf);
1687 e0ab43e7 2018-03-16 stsp if (err) {
1688 e0ab43e7 2018-03-16 stsp free(accum_buf);
1689 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1690 e0ab43e7 2018-03-16 stsp *outlen = 0;
1691 e0ab43e7 2018-03-16 stsp } else {
1692 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1693 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1694 e0ab43e7 2018-03-16 stsp }
1695 efd2a263 2018-01-19 stsp return err;
1696 efd2a263 2018-01-19 stsp }
1697 efd2a263 2018-01-19 stsp
1698 3ee5fc21 2018-01-17 stsp const struct got_error *
1699 24140570 2018-09-09 stsp got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1700 3840f4c9 2018-09-12 stsp FILE *outfile, FILE *base_file, FILE *accum_file)
1701 3ee5fc21 2018-01-17 stsp {
1702 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1703 3ee5fc21 2018-01-17 stsp
1704 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1705 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1706 2ce68b2f 2018-09-09 stsp
1707 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1708 24140570 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1709 24140570 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1710 72eb3431 2018-04-01 stsp
1711 d7464085 2018-07-09 stsp if (pack->map) {
1712 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1713 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_mmap(&obj->size, NULL, NULL,
1714 4788f1ce 2020-03-18 stsp pack->map, mapoff, pack->filesize - mapoff,
1715 4788f1ce 2020-03-18 stsp outfile);
1716 d7464085 2018-07-09 stsp } else {
1717 24140570 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1718 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1719 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_fd(&obj->size, NULL, NULL,
1720 4788f1ce 2020-03-18 stsp pack->fd, outfile);
1721 d7464085 2018-07-09 stsp }
1722 040bf4a1 2018-04-01 stsp } else
1723 4788f1ce 2020-03-18 stsp err = got_pack_dump_delta_chain_to_file(&obj->size,
1724 4788f1ce 2020-03-18 stsp &obj->deltas, pack, outfile, base_file, accum_file);
1725 24140570 2018-09-09 stsp
1726 a1fd68d8 2018-01-12 stsp return err;
1727 a1fd68d8 2018-01-12 stsp }
1728 e0ab43e7 2018-03-16 stsp
1729 e0ab43e7 2018-03-16 stsp const struct got_error *
1730 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1731 7e212e3d 2018-09-09 stsp struct got_object *obj, struct got_pack *pack)
1732 e0ab43e7 2018-03-16 stsp {
1733 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1734 e0ab43e7 2018-03-16 stsp
1735 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1736 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1737 72eb3431 2018-04-01 stsp
1738 48095039 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1739 7e212e3d 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1740 7e212e3d 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1741 d7464085 2018-07-09 stsp if (pack->map) {
1742 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1743 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(buf, len, NULL, NULL,
1744 2e5a6fad 2020-03-18 stsp pack->map, mapoff, pack->filesize - mapoff);
1745 d7464085 2018-07-09 stsp } else {
1746 7e212e3d 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1747 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1748 1e87a3c3 2020-03-18 stsp err = got_inflate_to_mem_fd(buf, len, NULL, NULL,
1749 55fdd257 2020-03-18 stsp obj->size, pack->fd);
1750 e0ab43e7 2018-03-16 stsp }
1751 e0ab43e7 2018-03-16 stsp } else
1752 668a20f6 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(buf, len, &obj->deltas,
1753 668a20f6 2020-03-18 stsp pack);
1754 7e212e3d 2018-09-09 stsp
1755 e0ab43e7 2018-03-16 stsp return err;
1756 e0ab43e7 2018-03-16 stsp }
1757 67fd6849 2022-02-13 stsp
1758 2d9e6abf 2022-05-04 stsp static const struct got_error *
1759 2d9e6abf 2022-05-04 stsp read_raw_delta_data(uint8_t **delta_buf, size_t *delta_len,
1760 2d9e6abf 2022-05-04 stsp size_t *delta_len_compressed, uint64_t *base_size, uint64_t *result_size,
1761 2d9e6abf 2022-05-04 stsp off_t delta_data_offset, struct got_pack *pack, struct got_packidx *packidx)
1762 2d9e6abf 2022-05-04 stsp {
1763 2d9e6abf 2022-05-04 stsp const struct got_error *err = NULL;
1764 2d9e6abf 2022-05-04 stsp
1765 2d9e6abf 2022-05-04 stsp /* Validate decompression and obtain the decompressed size. */
1766 2d9e6abf 2022-05-04 stsp err = read_delta_data(delta_buf, delta_len, delta_len_compressed,
1767 2d9e6abf 2022-05-04 stsp delta_data_offset, pack);
1768 2d9e6abf 2022-05-04 stsp if (err)
1769 2d9e6abf 2022-05-04 stsp return err;
1770 2d9e6abf 2022-05-04 stsp
1771 2d9e6abf 2022-05-04 stsp /* Read delta base/result sizes from head of delta stream. */
1772 2d9e6abf 2022-05-04 stsp err = got_delta_get_sizes(base_size, result_size,
1773 2d9e6abf 2022-05-04 stsp *delta_buf, *delta_len);
1774 2d9e6abf 2022-05-04 stsp if (err)
1775 2d9e6abf 2022-05-04 stsp goto done;
1776 2d9e6abf 2022-05-04 stsp
1777 2d9e6abf 2022-05-04 stsp /* Discard decompressed delta and read it again in compressed form. */
1778 2d9e6abf 2022-05-04 stsp free(*delta_buf);
1779 2d9e6abf 2022-05-04 stsp *delta_buf = malloc(*delta_len_compressed);
1780 2d9e6abf 2022-05-04 stsp if (*delta_buf == NULL) {
1781 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("malloc");
1782 2d9e6abf 2022-05-04 stsp goto done;
1783 2d9e6abf 2022-05-04 stsp }
1784 2d9e6abf 2022-05-04 stsp if (pack->map) {
1785 82031ac8 2022-10-24 op if (delta_data_offset >= pack->filesize) {
1786 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1787 82031ac8 2022-10-24 op goto done;
1788 82031ac8 2022-10-24 op }
1789 2d9e6abf 2022-05-04 stsp memcpy(*delta_buf, pack->map + delta_data_offset,
1790 2d9e6abf 2022-05-04 stsp *delta_len_compressed);
1791 2d9e6abf 2022-05-04 stsp } else {
1792 2d9e6abf 2022-05-04 stsp ssize_t n;
1793 2d9e6abf 2022-05-04 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1794 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("lseek");
1795 2d9e6abf 2022-05-04 stsp goto done;
1796 2d9e6abf 2022-05-04 stsp }
1797 2d9e6abf 2022-05-04 stsp n = read(pack->fd, *delta_buf, *delta_len_compressed);
1798 2d9e6abf 2022-05-04 stsp if (n < 0) {
1799 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("read");
1800 2d9e6abf 2022-05-04 stsp goto done;
1801 2d9e6abf 2022-05-04 stsp } else if (n != *delta_len_compressed) {
1802 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_IO);
1803 2d9e6abf 2022-05-04 stsp goto done;
1804 2d9e6abf 2022-05-04 stsp }
1805 2d9e6abf 2022-05-04 stsp }
1806 2d9e6abf 2022-05-04 stsp done:
1807 2d9e6abf 2022-05-04 stsp if (err) {
1808 2d9e6abf 2022-05-04 stsp free(*delta_buf);
1809 2d9e6abf 2022-05-04 stsp *delta_buf = NULL;
1810 2d9e6abf 2022-05-04 stsp *delta_len = 0;
1811 2d9e6abf 2022-05-04 stsp *delta_len_compressed = 0;
1812 2d9e6abf 2022-05-04 stsp *base_size = 0;
1813 2d9e6abf 2022-05-04 stsp *result_size = 0;
1814 2d9e6abf 2022-05-04 stsp }
1815 2d9e6abf 2022-05-04 stsp return err;
1816 2d9e6abf 2022-05-04 stsp }
1817 2d9e6abf 2022-05-04 stsp
1818 67fd6849 2022-02-13 stsp const struct got_error *
1819 67fd6849 2022-02-13 stsp got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
1820 2d9e6abf 2022-05-04 stsp size_t *delta_compressed_size, off_t *delta_offset, off_t *base_offset,
1821 2d9e6abf 2022-05-04 stsp struct got_object_id *base_id, uint64_t *base_size, uint64_t *result_size,
1822 2d9e6abf 2022-05-04 stsp struct got_pack *pack, struct got_packidx *packidx, int idx)
1823 67fd6849 2022-02-13 stsp {
1824 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
1825 67fd6849 2022-02-13 stsp off_t offset;
1826 67fd6849 2022-02-13 stsp uint8_t type;
1827 67fd6849 2022-02-13 stsp uint64_t size;
1828 67fd6849 2022-02-13 stsp size_t tslen, delta_hdrlen;
1829 2d9e6abf 2022-05-04 stsp off_t delta_data_offset;
1830 67fd6849 2022-02-13 stsp
1831 67fd6849 2022-02-13 stsp *delta_buf = NULL;
1832 67fd6849 2022-02-13 stsp *delta_size = 0;
1833 2d9e6abf 2022-05-04 stsp *delta_compressed_size = 0;
1834 67fd6849 2022-02-13 stsp *delta_offset = 0;
1835 67fd6849 2022-02-13 stsp *base_offset = 0;
1836 67fd6849 2022-02-13 stsp *base_size = 0;
1837 67fd6849 2022-02-13 stsp *result_size = 0;
1838 67fd6849 2022-02-13 stsp
1839 67fd6849 2022-02-13 stsp offset = got_packidx_get_object_offset(packidx, idx);
1840 04aed155 2022-07-24 naddy if (offset == -1)
1841 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1842 67fd6849 2022-02-13 stsp
1843 67fd6849 2022-02-13 stsp if (offset >= pack->filesize)
1844 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PACK_OFFSET);
1845 67fd6849 2022-02-13 stsp
1846 67fd6849 2022-02-13 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1847 67fd6849 2022-02-13 stsp pack, offset);
1848 67fd6849 2022-02-13 stsp if (err)
1849 67fd6849 2022-02-13 stsp return err;
1850 67fd6849 2022-02-13 stsp
1851 67fd6849 2022-02-13 stsp if (tslen + size < tslen || offset + size < size ||
1852 67fd6849 2022-02-13 stsp tslen + offset < tslen)
1853 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PACK_OFFSET);
1854 67fd6849 2022-02-13 stsp
1855 67fd6849 2022-02-13 stsp switch (type) {
1856 67fd6849 2022-02-13 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1857 67fd6849 2022-02-13 stsp err = got_pack_parse_offset_delta(base_offset, &delta_hdrlen,
1858 67fd6849 2022-02-13 stsp pack, offset, tslen);
1859 67fd6849 2022-02-13 stsp if (err)
1860 67fd6849 2022-02-13 stsp return err;
1861 67fd6849 2022-02-13 stsp break;
1862 67fd6849 2022-02-13 stsp case GOT_OBJ_TYPE_REF_DELTA:
1863 67fd6849 2022-02-13 stsp err = got_pack_parse_ref_delta(base_id, pack, offset, tslen);
1864 67fd6849 2022-02-13 stsp if (err)
1865 67fd6849 2022-02-13 stsp return err;
1866 67fd6849 2022-02-13 stsp delta_hdrlen = SHA1_DIGEST_LENGTH;
1867 67fd6849 2022-02-13 stsp break;
1868 67fd6849 2022-02-13 stsp default:
1869 67fd6849 2022-02-13 stsp return got_error_fmt(GOT_ERR_OBJ_TYPE,
1870 04aed155 2022-07-24 naddy "non-delta object type %d found at offset %lld",
1871 04aed155 2022-07-24 naddy type, (long long)offset);
1872 67fd6849 2022-02-13 stsp }
1873 67fd6849 2022-02-13 stsp
1874 67fd6849 2022-02-13 stsp if (tslen + delta_hdrlen < delta_hdrlen ||
1875 67fd6849 2022-02-13 stsp offset + delta_hdrlen < delta_hdrlen)
1876 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_DELTA);
1877 67fd6849 2022-02-13 stsp
1878 2d9e6abf 2022-05-04 stsp delta_data_offset = offset + tslen + delta_hdrlen;
1879 2d9e6abf 2022-05-04 stsp err = read_raw_delta_data(delta_buf, delta_size, delta_compressed_size,
1880 2d9e6abf 2022-05-04 stsp base_size, result_size, delta_data_offset, pack, packidx);
1881 67fd6849 2022-02-13 stsp if (err)
1882 67fd6849 2022-02-13 stsp return err;
1883 67fd6849 2022-02-13 stsp
1884 67fd6849 2022-02-13 stsp if (*delta_size != size) {
1885 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_BAD_DELTA);
1886 67fd6849 2022-02-13 stsp goto done;
1887 67fd6849 2022-02-13 stsp }
1888 67fd6849 2022-02-13 stsp
1889 67fd6849 2022-02-13 stsp *delta_offset = offset;
1890 67fd6849 2022-02-13 stsp done:
1891 67fd6849 2022-02-13 stsp if (err) {
1892 67fd6849 2022-02-13 stsp free(*delta_buf);
1893 67fd6849 2022-02-13 stsp *delta_buf = NULL;
1894 2d9e6abf 2022-05-04 stsp *delta_size = 0;
1895 2d9e6abf 2022-05-04 stsp *delta_compressed_size = 0;
1896 2d9e6abf 2022-05-04 stsp *delta_offset = 0;
1897 2d9e6abf 2022-05-04 stsp *base_offset = 0;
1898 2d9e6abf 2022-05-04 stsp *base_size = 0;
1899 2d9e6abf 2022-05-04 stsp *result_size = 0;
1900 67fd6849 2022-02-13 stsp }
1901 67fd6849 2022-02-13 stsp return err;
1902 67fd6849 2022-02-13 stsp }