Blame


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