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