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