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