Blame


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