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