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 57b35b75 2018-06-22 stsp #include <sys/mman.h>
21 0a0a3048 2018-01-10 stsp
22 a1fd68d8 2018-01-12 stsp #include <dirent.h>
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 0a0a3048 2018-01-10 stsp
34 0a0a3048 2018-01-10 stsp #include "got_error.h"
35 a1fd68d8 2018-01-12 stsp #include "got_object.h"
36 a1fd68d8 2018-01-12 stsp #include "got_repository.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 718b3ab0 2018-03-17 stsp #include "got_lib_repository.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 #define GOT_PACK_PREFIX "pack-"
52 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
53 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
54 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
55 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
56 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
57 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
58 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
59 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
60 a1fd68d8 2018-01-12 stsp
61 a1fd68d8 2018-01-12 stsp #ifndef MIN
62 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 a1fd68d8 2018-01-12 stsp #endif
64 a1fd68d8 2018-01-12 stsp
65 0a0a3048 2018-01-10 stsp static const struct got_error *
66 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
67 0a0a3048 2018-01-10 stsp {
68 0a0a3048 2018-01-10 stsp int i;
69 0a0a3048 2018-01-10 stsp
70 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
71 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
72 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
73 0a0a3048 2018-01-10 stsp }
74 0a0a3048 2018-01-10 stsp
75 0a0a3048 2018-01-10 stsp return NULL;
76 0a0a3048 2018-01-10 stsp }
77 0a0a3048 2018-01-10 stsp
78 24541888 2018-01-10 stsp static const struct got_error *
79 1c7e24f1 2018-04-02 stsp get_packfile_size(size_t *size, const char *path)
80 0a0a3048 2018-01-10 stsp {
81 0a0a3048 2018-01-10 stsp struct stat sb;
82 0a0a3048 2018-01-10 stsp char *dot;
83 0a0a3048 2018-01-10 stsp
84 97128b57 2018-04-02 stsp *size = 0;
85 97128b57 2018-04-02 stsp
86 1c7e24f1 2018-04-02 stsp dot = strrchr(path, '.');
87 0a0a3048 2018-01-10 stsp if (dot == NULL)
88 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
89 0a0a3048 2018-01-10 stsp
90 1c7e24f1 2018-04-02 stsp /* Path must point to a pack index or to a pack file. */
91 72fb0363 2018-06-04 stsp if (strcmp(dot, GOT_PACKIDX_SUFFIX) == 0) {
92 97128b57 2018-04-02 stsp const struct got_error *err = NULL;
93 1c7e24f1 2018-04-02 stsp char *path_pack;
94 1c7e24f1 2018-04-02 stsp char base_path[PATH_MAX];
95 1c7e24f1 2018-04-02 stsp
96 1c7e24f1 2018-04-02 stsp /* Convert pack index path to pack file path. */
97 1c7e24f1 2018-04-02 stsp if (strlcpy(base_path, path, PATH_MAX) > PATH_MAX)
98 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_NO_SPACE);
99 1c7e24f1 2018-04-02 stsp dot = strrchr(base_path, '.');
100 1c7e24f1 2018-04-02 stsp if (dot == NULL)
101 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
102 1c7e24f1 2018-04-02 stsp *dot = '\0';
103 1c7e24f1 2018-04-02 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
104 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
105 1c7e24f1 2018-04-02 stsp
106 97128b57 2018-04-02 stsp if (stat(path_pack, &sb) != 0)
107 97128b57 2018-04-02 stsp err = got_error_from_errno();
108 0a0a3048 2018-01-10 stsp free(path_pack);
109 97128b57 2018-04-02 stsp if (err)
110 97128b57 2018-04-02 stsp return err;
111 56cca8e5 2018-06-04 stsp } else if (strcmp(dot, GOT_PACKFILE_SUFFIX) == 0) {
112 1c7e24f1 2018-04-02 stsp if (stat(path, &sb) != 0)
113 1c7e24f1 2018-04-02 stsp return got_error_from_errno();
114 1c7e24f1 2018-04-02 stsp } else
115 1c7e24f1 2018-04-02 stsp return got_error(GOT_ERR_BAD_PATH);
116 0a0a3048 2018-01-10 stsp
117 0a0a3048 2018-01-10 stsp *size = sb.st_size;
118 0a0a3048 2018-01-10 stsp return 0;
119 0a0a3048 2018-01-10 stsp }
120 0a0a3048 2018-01-10 stsp
121 0a0a3048 2018-01-10 stsp const struct got_error *
122 817c5a18 2018-09-09 stsp got_packidx_init_hdr(struct got_packidx *p, int verify)
123 0a0a3048 2018-01-10 stsp {
124 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
125 817c5a18 2018-09-09 stsp struct got_packidx_v2_hdr *h;
126 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
127 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
128 817c5a18 2018-09-09 stsp size_t nobj, len_fanout, len_ids, offset, remain;
129 817c5a18 2018-09-09 stsp ssize_t n;
130 0a0a3048 2018-01-10 stsp
131 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
132 0ebaf008 2018-01-10 stsp
133 57b35b75 2018-06-22 stsp h = &p->hdr;
134 57b35b75 2018-06-22 stsp offset = 0;
135 57b35b75 2018-06-22 stsp remain = p->len;
136 0a0a3048 2018-01-10 stsp
137 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
138 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
139 0a0a3048 2018-01-10 stsp goto done;
140 0a0a3048 2018-01-10 stsp }
141 fc79a48d 2018-07-09 stsp if (p->map)
142 fc79a48d 2018-07-09 stsp h->magic = (uint32_t *)(p->map + offset);
143 fc79a48d 2018-07-09 stsp else {
144 fc79a48d 2018-07-09 stsp h->magic = malloc(sizeof(*h->magic));
145 fc79a48d 2018-07-09 stsp if (h->magic == NULL) {
146 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
147 fc79a48d 2018-07-09 stsp goto done;
148 fc79a48d 2018-07-09 stsp }
149 fc79a48d 2018-07-09 stsp n = read(p->fd, h->magic, sizeof(*h->magic));
150 fc79a48d 2018-07-09 stsp if (n != sizeof(*h->magic)) {
151 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
152 fc79a48d 2018-07-09 stsp goto done;
153 fc79a48d 2018-07-09 stsp }
154 fc79a48d 2018-07-09 stsp }
155 57b35b75 2018-06-22 stsp if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
156 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
157 57b35b75 2018-06-22 stsp goto done;
158 57b35b75 2018-06-22 stsp }
159 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
160 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
161 0a0a3048 2018-01-10 stsp
162 0cb74cf4 2018-07-08 stsp if (verify)
163 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
164 0ebaf008 2018-01-10 stsp
165 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
166 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
167 0a0a3048 2018-01-10 stsp goto done;
168 0a0a3048 2018-01-10 stsp }
169 fc79a48d 2018-07-09 stsp if (p->map)
170 fc79a48d 2018-07-09 stsp h->version = (uint32_t *)(p->map + offset);
171 fc79a48d 2018-07-09 stsp else {
172 fc79a48d 2018-07-09 stsp h->version = malloc(sizeof(*h->version));
173 fc79a48d 2018-07-09 stsp if (h->version == NULL) {
174 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
175 fc79a48d 2018-07-09 stsp goto done;
176 fc79a48d 2018-07-09 stsp }
177 fc79a48d 2018-07-09 stsp n = read(p->fd, h->version, sizeof(*h->version));
178 fc79a48d 2018-07-09 stsp if (n != sizeof(*h->version)) {
179 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
180 fc79a48d 2018-07-09 stsp goto done;
181 fc79a48d 2018-07-09 stsp }
182 fc79a48d 2018-07-09 stsp }
183 57b35b75 2018-06-22 stsp if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
184 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
185 0a0a3048 2018-01-10 stsp goto done;
186 0a0a3048 2018-01-10 stsp }
187 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
188 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
189 0a0a3048 2018-01-10 stsp
190 0cb74cf4 2018-07-08 stsp if (verify)
191 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
192 0ebaf008 2018-01-10 stsp
193 57b35b75 2018-06-22 stsp len_fanout =
194 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
195 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
196 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
197 0a0a3048 2018-01-10 stsp goto done;
198 0a0a3048 2018-01-10 stsp }
199 fc79a48d 2018-07-09 stsp if (p->map)
200 fc79a48d 2018-07-09 stsp h->fanout_table = (uint32_t *)(p->map + offset);
201 fc79a48d 2018-07-09 stsp else {
202 fc79a48d 2018-07-09 stsp h->fanout_table = malloc(len_fanout);
203 fc79a48d 2018-07-09 stsp if (h->fanout_table == NULL) {
204 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
205 fc79a48d 2018-07-09 stsp goto done;
206 fc79a48d 2018-07-09 stsp }
207 fc79a48d 2018-07-09 stsp n = read(p->fd, h->fanout_table, len_fanout);
208 fc79a48d 2018-07-09 stsp if (n != len_fanout) {
209 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
210 fc79a48d 2018-07-09 stsp goto done;
211 fc79a48d 2018-07-09 stsp }
212 fc79a48d 2018-07-09 stsp }
213 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
214 0a0a3048 2018-01-10 stsp if (err)
215 0a0a3048 2018-01-10 stsp goto done;
216 0cb74cf4 2018-07-08 stsp if (verify)
217 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
218 57b35b75 2018-06-22 stsp offset += len_fanout;
219 57b35b75 2018-06-22 stsp remain -= len_fanout;
220 0a0a3048 2018-01-10 stsp
221 c8262310 2018-06-04 stsp nobj = betoh32(h->fanout_table[0xff]);
222 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
223 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
224 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
225 0a0a3048 2018-01-10 stsp goto done;
226 0a0a3048 2018-01-10 stsp }
227 fc79a48d 2018-07-09 stsp if (p->map)
228 fc79a48d 2018-07-09 stsp h->sorted_ids =
229 fc79a48d 2018-07-09 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
230 fc79a48d 2018-07-09 stsp else {
231 fc79a48d 2018-07-09 stsp h->sorted_ids = malloc(len_ids);
232 fc79a48d 2018-07-09 stsp if (h->sorted_ids == NULL) {
233 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
234 fc79a48d 2018-07-09 stsp goto done;
235 fc79a48d 2018-07-09 stsp }
236 fc79a48d 2018-07-09 stsp n = read(p->fd, h->sorted_ids, len_ids);
237 fc79a48d 2018-07-09 stsp if (n != len_ids) {
238 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
239 fc79a48d 2018-07-09 stsp goto done;
240 fc79a48d 2018-07-09 stsp }
241 fc79a48d 2018-07-09 stsp }
242 0cb74cf4 2018-07-08 stsp if (verify)
243 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
244 57b35b75 2018-06-22 stsp offset += len_ids;
245 57b35b75 2018-06-22 stsp remain -= len_ids;
246 0a0a3048 2018-01-10 stsp
247 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
248 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
249 0a0a3048 2018-01-10 stsp goto done;
250 0a0a3048 2018-01-10 stsp }
251 fc79a48d 2018-07-09 stsp if (p->map)
252 fc79a48d 2018-07-09 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
253 fc79a48d 2018-07-09 stsp else {
254 fc79a48d 2018-07-09 stsp h->crc32 = malloc(nobj * sizeof(*h->crc32));
255 fc79a48d 2018-07-09 stsp if (h->crc32 == NULL) {
256 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
257 fc79a48d 2018-07-09 stsp goto done;
258 fc79a48d 2018-07-09 stsp }
259 fc79a48d 2018-07-09 stsp n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
260 fc79a48d 2018-07-09 stsp 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 fc79a48d 2018-07-09 stsp if (n != nobj * sizeof(*h->offsets)) {
284 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
285 fc79a48d 2018-07-09 stsp goto done;
286 fc79a48d 2018-07-09 stsp }
287 fc79a48d 2018-07-09 stsp }
288 0cb74cf4 2018-07-08 stsp if (verify)
289 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
290 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
291 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
292 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
293 0ebaf008 2018-01-10 stsp
294 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
295 57b35b75 2018-06-22 stsp if (p->len <= 0x80000000)
296 0a0a3048 2018-01-10 stsp goto checksum;
297 0a0a3048 2018-01-10 stsp
298 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->large_offsets)) {
299 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
300 0a0a3048 2018-01-10 stsp goto done;
301 0a0a3048 2018-01-10 stsp }
302 fc79a48d 2018-07-09 stsp if (p->map)
303 fc79a48d 2018-07-09 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
304 fc79a48d 2018-07-09 stsp else {
305 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->large_offsets));
306 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
307 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
308 fc79a48d 2018-07-09 stsp goto done;
309 fc79a48d 2018-07-09 stsp }
310 fc79a48d 2018-07-09 stsp n = read(p->fd, h->large_offsets,
311 fc79a48d 2018-07-09 stsp nobj * sizeof(*h->large_offsets));
312 fc79a48d 2018-07-09 stsp if (n != nobj * sizeof(*h->large_offsets)) {
313 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
314 fc79a48d 2018-07-09 stsp goto done;
315 fc79a48d 2018-07-09 stsp }
316 fc79a48d 2018-07-09 stsp }
317 0cb74cf4 2018-07-08 stsp if (verify)
318 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
319 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->large_offsets));
320 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->large_offsets);
321 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->large_offsets);
322 0ebaf008 2018-01-10 stsp
323 0a0a3048 2018-01-10 stsp checksum:
324 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
325 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
326 0a0a3048 2018-01-10 stsp goto done;
327 0a0a3048 2018-01-10 stsp }
328 fc79a48d 2018-07-09 stsp if (p->map)
329 fc79a48d 2018-07-09 stsp h->trailer =
330 fc79a48d 2018-07-09 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
331 fc79a48d 2018-07-09 stsp else {
332 fc79a48d 2018-07-09 stsp h->trailer = malloc(sizeof(*h->trailer));
333 fc79a48d 2018-07-09 stsp if (h->trailer == NULL) {
334 fc79a48d 2018-07-09 stsp err = got_error_from_errno();
335 fc79a48d 2018-07-09 stsp goto done;
336 fc79a48d 2018-07-09 stsp }
337 fc79a48d 2018-07-09 stsp n = read(p->fd, h->trailer, sizeof(*h->trailer));
338 fc79a48d 2018-07-09 stsp if (n != sizeof(*h->trailer)) {
339 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
340 fc79a48d 2018-07-09 stsp goto done;
341 fc79a48d 2018-07-09 stsp }
342 fc79a48d 2018-07-09 stsp }
343 0cb74cf4 2018-07-08 stsp if (verify) {
344 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
345 0cb74cf4 2018-07-08 stsp SHA1Final(sha1, &ctx);
346 0cb74cf4 2018-07-08 stsp if (memcmp(h->trailer->packidx_sha1, sha1,
347 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
348 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
349 0cb74cf4 2018-07-08 stsp }
350 0a0a3048 2018-01-10 stsp done:
351 817c5a18 2018-09-09 stsp return err;
352 817c5a18 2018-09-09 stsp }
353 817c5a18 2018-09-09 stsp
354 817c5a18 2018-09-09 stsp const struct got_error *
355 817c5a18 2018-09-09 stsp got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
356 817c5a18 2018-09-09 stsp {
357 817c5a18 2018-09-09 stsp const struct got_error *err = NULL;
358 817c5a18 2018-09-09 stsp struct got_packidx *p;
359 817c5a18 2018-09-09 stsp
360 817c5a18 2018-09-09 stsp *packidx = NULL;
361 817c5a18 2018-09-09 stsp
362 817c5a18 2018-09-09 stsp p = calloc(1, sizeof(*p));
363 817c5a18 2018-09-09 stsp if (p == NULL)
364 817c5a18 2018-09-09 stsp return got_error_from_errno();
365 817c5a18 2018-09-09 stsp
366 817c5a18 2018-09-09 stsp p->fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
367 817c5a18 2018-09-09 stsp if (p->fd == -1)
368 817c5a18 2018-09-09 stsp return got_error_from_errno();
369 817c5a18 2018-09-09 stsp
370 817c5a18 2018-09-09 stsp err = get_packfile_size(&p->len, path);
371 817c5a18 2018-09-09 stsp if (err) {
372 817c5a18 2018-09-09 stsp close(p->fd);
373 817c5a18 2018-09-09 stsp free(p);
374 817c5a18 2018-09-09 stsp return err;
375 817c5a18 2018-09-09 stsp }
376 817c5a18 2018-09-09 stsp if (p->len < sizeof(p->hdr)) {
377 817c5a18 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
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
383 817c5a18 2018-09-09 stsp p->path_packidx = strdup(path);
384 817c5a18 2018-09-09 stsp if (p->path_packidx == NULL) {
385 817c5a18 2018-09-09 stsp err = got_error_from_errno();
386 817c5a18 2018-09-09 stsp goto done;
387 817c5a18 2018-09-09 stsp }
388 817c5a18 2018-09-09 stsp
389 817c5a18 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
390 817c5a18 2018-09-09 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
391 817c5a18 2018-09-09 stsp if (p->map == MAP_FAILED)
392 817c5a18 2018-09-09 stsp p->map = NULL; /* fall back to read(2) */
393 817c5a18 2018-09-09 stsp #endif
394 817c5a18 2018-09-09 stsp
395 817c5a18 2018-09-09 stsp err = got_packidx_init_hdr(p, verify);
396 817c5a18 2018-09-09 stsp done:
397 0a0a3048 2018-01-10 stsp if (err)
398 0a0a3048 2018-01-10 stsp got_packidx_close(p);
399 0a0a3048 2018-01-10 stsp else
400 0a0a3048 2018-01-10 stsp *packidx = p;
401 817c5a18 2018-09-09 stsp
402 0a0a3048 2018-01-10 stsp return err;
403 0a0a3048 2018-01-10 stsp }
404 0a0a3048 2018-01-10 stsp
405 57b35b75 2018-06-22 stsp const struct got_error *
406 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
407 0a0a3048 2018-01-10 stsp {
408 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
409 57b35b75 2018-06-22 stsp
410 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
411 fc79a48d 2018-07-09 stsp if (packidx->map) {
412 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
413 57b35b75 2018-06-22 stsp err = got_error_from_errno();
414 fc79a48d 2018-07-09 stsp } else {
415 fc79a48d 2018-07-09 stsp free(packidx->hdr.magic);
416 fc79a48d 2018-07-09 stsp free(packidx->hdr.version);
417 fc79a48d 2018-07-09 stsp free(packidx->hdr.fanout_table);
418 fc79a48d 2018-07-09 stsp free(packidx->hdr.sorted_ids);
419 fc79a48d 2018-07-09 stsp free(packidx->hdr.crc32);
420 fc79a48d 2018-07-09 stsp free(packidx->hdr.offsets);
421 fc79a48d 2018-07-09 stsp free(packidx->hdr.large_offsets);
422 fc79a48d 2018-07-09 stsp free(packidx->hdr.trailer);
423 57b35b75 2018-06-22 stsp }
424 57b35b75 2018-06-22 stsp close(packidx->fd);
425 0a0a3048 2018-01-10 stsp free(packidx);
426 57b35b75 2018-06-22 stsp
427 57b35b75 2018-06-22 stsp return err;
428 a1fd68d8 2018-01-12 stsp }
429 a1fd68d8 2018-01-12 stsp
430 a1fd68d8 2018-01-12 stsp static int
431 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
432 a1fd68d8 2018-01-12 stsp {
433 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
434 a1fd68d8 2018-01-12 stsp return 0;
435 a1fd68d8 2018-01-12 stsp
436 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
437 a1fd68d8 2018-01-12 stsp return 0;
438 a1fd68d8 2018-01-12 stsp
439 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
440 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
441 a1fd68d8 2018-01-12 stsp return 0;
442 a1fd68d8 2018-01-12 stsp
443 a1fd68d8 2018-01-12 stsp return 1;
444 a1fd68d8 2018-01-12 stsp }
445 a1fd68d8 2018-01-12 stsp
446 a1fd68d8 2018-01-12 stsp static off_t
447 6fd11751 2018-06-04 stsp get_object_offset(struct got_packidx *packidx, int idx)
448 a1fd68d8 2018-01-12 stsp {
449 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
450 6fd11751 2018-06-04 stsp uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
451 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
452 a1fd68d8 2018-01-12 stsp uint64_t loffset;
453 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
454 6fd11751 2018-06-04 stsp if (idx < 0 || idx > totobj ||
455 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
456 a1fd68d8 2018-01-12 stsp return -1;
457 6fd11751 2018-06-04 stsp loffset = betoh64(packidx->hdr.large_offsets[idx]);
458 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
459 a1fd68d8 2018-01-12 stsp }
460 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
461 a1fd68d8 2018-01-12 stsp }
462 a1fd68d8 2018-01-12 stsp
463 a1fd68d8 2018-01-12 stsp static int
464 3413160a 2018-09-09 stsp get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
465 a1fd68d8 2018-01-12 stsp {
466 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
467 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
468 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
469 a1fd68d8 2018-01-12 stsp
470 a1fd68d8 2018-01-12 stsp if (id0 > 0)
471 40aeb19c 2018-06-22 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
472 a1fd68d8 2018-01-12 stsp
473 40aeb19c 2018-06-22 stsp while (left <= right) {
474 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
475 40aeb19c 2018-06-22 stsp int i, cmp;
476 a1fd68d8 2018-01-12 stsp
477 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
478 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
479 57b35b75 2018-06-22 stsp cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
480 16dcbf91 2018-04-01 stsp if (cmp == 0)
481 6c00b545 2018-01-17 stsp return i;
482 40aeb19c 2018-06-22 stsp else if (cmp > 0)
483 40aeb19c 2018-06-22 stsp left = i + 1;
484 40aeb19c 2018-06-22 stsp else if (cmp < 0)
485 40aeb19c 2018-06-22 stsp right = i - 1;
486 a1fd68d8 2018-01-12 stsp }
487 a1fd68d8 2018-01-12 stsp
488 a1fd68d8 2018-01-12 stsp return -1;
489 79b11c62 2018-03-09 stsp }
490 79b11c62 2018-03-09 stsp
491 57b35b75 2018-06-22 stsp static const struct got_error *
492 6fd11751 2018-06-04 stsp cache_packidx(struct got_packidx *packidx, struct got_repository *repo)
493 87c99799 2018-03-16 stsp {
494 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
495 79b11c62 2018-03-09 stsp int i;
496 79b11c62 2018-03-09 stsp
497 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
498 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
499 79b11c62 2018-03-09 stsp break;
500 79b11c62 2018-03-09 stsp }
501 79b11c62 2018-03-09 stsp
502 65cf1e80 2018-03-16 stsp if (i == nitems(repo->packidx_cache)) {
503 57b35b75 2018-06-22 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
504 57b35b75 2018-06-22 stsp if (err)
505 57b35b75 2018-06-22 stsp return err;
506 65cf1e80 2018-03-16 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
507 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache) -
508 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache[0]));
509 79b11c62 2018-03-09 stsp i = 0;
510 79b11c62 2018-03-09 stsp }
511 79b11c62 2018-03-09 stsp
512 49c99f91 2018-06-22 stsp repo->packidx_cache[i] = packidx;
513 57b35b75 2018-06-22 stsp return NULL;
514 79b11c62 2018-03-09 stsp }
515 79b11c62 2018-03-09 stsp
516 6b9c9673 2018-01-23 stsp static const struct got_error *
517 6fd11751 2018-06-04 stsp search_packidx(struct got_packidx **packidx, int *idx,
518 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
519 6b9c9673 2018-01-23 stsp {
520 6b9c9673 2018-01-23 stsp const struct got_error *err;
521 6b9c9673 2018-01-23 stsp char *path_packdir;
522 6b9c9673 2018-01-23 stsp DIR *packdir;
523 6b9c9673 2018-01-23 stsp struct dirent *dent;
524 6b9c9673 2018-01-23 stsp char *path_packidx;
525 79b11c62 2018-03-09 stsp int i;
526 6b9c9673 2018-01-23 stsp
527 65cf1e80 2018-03-16 stsp /* Search pack index cache. */
528 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
529 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
530 79b11c62 2018-03-09 stsp break;
531 3413160a 2018-09-09 stsp *idx = get_object_idx(repo->packidx_cache[i], id);
532 79b11c62 2018-03-09 stsp if (*idx != -1) {
533 6bb255dc 2018-03-17 stsp *packidx = repo->packidx_cache[i];
534 79b11c62 2018-03-09 stsp return NULL;
535 79b11c62 2018-03-09 stsp }
536 79b11c62 2018-03-09 stsp }
537 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
538 79b11c62 2018-03-09 stsp
539 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
540 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
541 0a585a0d 2018-03-17 stsp return got_error_from_errno();
542 6b9c9673 2018-01-23 stsp
543 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
544 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
545 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
546 6b9c9673 2018-01-23 stsp goto done;
547 6b9c9673 2018-01-23 stsp }
548 6b9c9673 2018-01-23 stsp
549 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
550 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
551 6b9c9673 2018-01-23 stsp continue;
552 6b9c9673 2018-01-23 stsp
553 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
554 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
555 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
556 6b9c9673 2018-01-23 stsp goto done;
557 6b9c9673 2018-01-23 stsp }
558 6b9c9673 2018-01-23 stsp
559 0cb74cf4 2018-07-08 stsp err = got_packidx_open(packidx, path_packidx, 0);
560 6b9c9673 2018-01-23 stsp free(path_packidx);
561 6b9c9673 2018-01-23 stsp if (err)
562 6b9c9673 2018-01-23 stsp goto done;
563 6b9c9673 2018-01-23 stsp
564 3413160a 2018-09-09 stsp *idx = get_object_idx(*packidx, id);
565 6b9c9673 2018-01-23 stsp if (*idx != -1) {
566 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
567 57b35b75 2018-06-22 stsp err = cache_packidx(*packidx, repo);
568 6b9c9673 2018-01-23 stsp goto done;
569 6b9c9673 2018-01-23 stsp }
570 6b9c9673 2018-01-23 stsp
571 57b35b75 2018-06-22 stsp err = got_packidx_close(*packidx);
572 6b9c9673 2018-01-23 stsp *packidx = NULL;
573 57b35b75 2018-06-22 stsp if (err)
574 57b35b75 2018-06-22 stsp goto done;
575 6b9c9673 2018-01-23 stsp }
576 6b9c9673 2018-01-23 stsp
577 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
578 6b9c9673 2018-01-23 stsp done:
579 6b9c9673 2018-01-23 stsp free(path_packdir);
580 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
581 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
582 6b9c9673 2018-01-23 stsp return err;
583 6b9c9673 2018-01-23 stsp }
584 6b9c9673 2018-01-23 stsp
585 c7fe698a 2018-01-23 stsp static const struct got_error *
586 4a9c75d9 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
587 c7fe698a 2018-01-23 stsp {
588 6fd11751 2018-06-04 stsp size_t size;
589 c7fe698a 2018-01-23 stsp
590 6fd11751 2018-06-04 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
591 6fd11751 2018-06-04 stsp size = strlen(packidx->path_packidx) + 2;
592 6fd11751 2018-06-04 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
593 6fd11751 2018-06-04 stsp return got_error(GOT_ERR_BAD_PATH);
594 6fd11751 2018-06-04 stsp
595 6fd11751 2018-06-04 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
596 6fd11751 2018-06-04 stsp if (*path_packfile == NULL)
597 0a585a0d 2018-03-17 stsp return got_error_from_errno();
598 c7fe698a 2018-01-23 stsp
599 6fd11751 2018-06-04 stsp /* Copy up to and excluding ".idx". */
600 d475dd0d 2018-06-04 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
601 72fb0363 2018-06-04 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
602 d475dd0d 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
603 87c99799 2018-03-16 stsp
604 6fd11751 2018-06-04 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
605 6fd11751 2018-06-04 stsp return got_error(GOT_ERR_NO_SPACE);
606 87c99799 2018-03-16 stsp
607 65cf1e80 2018-03-16 stsp return NULL;
608 65cf1e80 2018-03-16 stsp }
609 65cf1e80 2018-03-16 stsp
610 d7464085 2018-07-09 stsp static const struct got_error *
611 6fd11751 2018-06-04 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
612 65cf1e80 2018-03-16 stsp {
613 65cf1e80 2018-03-16 stsp const struct got_error *err = NULL;
614 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
615 65cf1e80 2018-03-16 stsp struct got_packfile_hdr hdr;
616 8b2180d4 2018-04-26 stsp ssize_t n;
617 65cf1e80 2018-03-16 stsp
618 8b2180d4 2018-04-26 stsp n = read(fd, &hdr, sizeof(hdr));
619 8b2180d4 2018-04-26 stsp if (n < 0)
620 8b2180d4 2018-04-26 stsp return got_error_from_errno();
621 8b2180d4 2018-04-26 stsp if (n != sizeof(hdr))
622 8b2180d4 2018-04-26 stsp return got_error(GOT_ERR_BAD_PACKFILE);
623 65cf1e80 2018-03-16 stsp
624 65cf1e80 2018-03-16 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
625 65cf1e80 2018-03-16 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
626 65cf1e80 2018-03-16 stsp betoh32(hdr.nobjects) != totobj)
627 65cf1e80 2018-03-16 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
628 65cf1e80 2018-03-16 stsp
629 65cf1e80 2018-03-16 stsp return err;
630 7e656b93 2018-03-17 stsp }
631 7e656b93 2018-03-17 stsp
632 7e656b93 2018-03-17 stsp static const struct got_error *
633 4fb0d740 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
634 7e656b93 2018-03-17 stsp {
635 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
636 7e656b93 2018-03-17 stsp
637 8b2180d4 2018-04-26 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
638 8b2180d4 2018-04-26 stsp if (*fd == -1)
639 8b2180d4 2018-04-26 stsp return got_error_from_errno();
640 7e656b93 2018-03-17 stsp
641 7e656b93 2018-03-17 stsp if (packidx) {
642 8b2180d4 2018-04-26 stsp err = read_packfile_hdr(*fd, packidx);
643 7e656b93 2018-03-17 stsp if (err) {
644 8b2180d4 2018-04-26 stsp close(*fd);
645 8b2180d4 2018-04-26 stsp *fd = -1;
646 7e656b93 2018-03-17 stsp }
647 7e656b93 2018-03-17 stsp }
648 7e656b93 2018-03-17 stsp return err;
649 7e656b93 2018-03-17 stsp }
650 7e656b93 2018-03-17 stsp
651 d7464085 2018-07-09 stsp const struct got_error *
652 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
653 7e656b93 2018-03-17 stsp {
654 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
655 d7464085 2018-07-09 stsp
656 e8f89a81 2018-07-13 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1)
657 d7464085 2018-07-09 stsp err = got_error_from_errno();
658 8b2180d4 2018-04-26 stsp close(pack->fd);
659 8b2180d4 2018-04-26 stsp pack->fd = -1;
660 4589e373 2018-03-17 stsp free(pack->path_packfile);
661 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
662 4589e373 2018-03-17 stsp pack->filesize = 0;
663 d7464085 2018-07-09 stsp
664 d7464085 2018-07-09 stsp return err;
665 7e656b93 2018-03-17 stsp }
666 7e656b93 2018-03-17 stsp
667 7e656b93 2018-03-17 stsp static const struct got_error *
668 7e656b93 2018-03-17 stsp cache_pack(struct got_pack **packp, const char *path_packfile,
669 6fd11751 2018-06-04 stsp struct got_packidx *packidx, struct got_repository *repo)
670 7e656b93 2018-03-17 stsp {
671 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
672 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
673 7e656b93 2018-03-17 stsp int i;
674 7e656b93 2018-03-17 stsp
675 7e656b93 2018-03-17 stsp if (packp)
676 7e656b93 2018-03-17 stsp *packp = NULL;
677 7e656b93 2018-03-17 stsp
678 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
679 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
680 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
681 7e656b93 2018-03-17 stsp break;
682 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
683 7e656b93 2018-03-17 stsp return NULL;
684 c7fe698a 2018-01-23 stsp }
685 7e656b93 2018-03-17 stsp
686 7e656b93 2018-03-17 stsp if (i == nitems(repo->packs) - 1) {
687 d7464085 2018-07-09 stsp err = got_pack_close(&repo->packs[i - 1]);
688 d7464085 2018-07-09 stsp if (err)
689 d7464085 2018-07-09 stsp return err;
690 7e656b93 2018-03-17 stsp memmove(&repo->packs[1], &repo->packs[0],
691 7e656b93 2018-03-17 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
692 7e656b93 2018-03-17 stsp i = 0;
693 7e656b93 2018-03-17 stsp }
694 7e656b93 2018-03-17 stsp
695 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
696 7e656b93 2018-03-17 stsp
697 7e656b93 2018-03-17 stsp pack->path_packfile = strdup(path_packfile);
698 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL) {
699 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
700 7e656b93 2018-03-17 stsp goto done;
701 7e656b93 2018-03-17 stsp }
702 7e656b93 2018-03-17 stsp
703 4fb0d740 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
704 7e656b93 2018-03-17 stsp if (err)
705 7e656b93 2018-03-17 stsp goto done;
706 7e656b93 2018-03-17 stsp
707 7e656b93 2018-03-17 stsp err = get_packfile_size(&pack->filesize, path_packfile);
708 d7464085 2018-07-09 stsp if (err)
709 d7464085 2018-07-09 stsp goto done;
710 d7464085 2018-07-09 stsp
711 af9b7fee 2018-07-09 stsp #ifndef GOT_PACK_NO_MMAP
712 d7464085 2018-07-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
713 d7464085 2018-07-09 stsp pack->fd, 0);
714 57363308 2018-07-09 stsp if (pack->map == MAP_FAILED)
715 57363308 2018-07-09 stsp pack->map = NULL; /* fall back to read(2) */
716 af9b7fee 2018-07-09 stsp #endif
717 7e656b93 2018-03-17 stsp done:
718 7e656b93 2018-03-17 stsp if (err) {
719 f5feadcc 2018-06-04 stsp if (pack) {
720 7e656b93 2018-03-17 stsp free(pack->path_packfile);
721 f5feadcc 2018-06-04 stsp memset(pack, 0, sizeof(*pack));
722 f5feadcc 2018-06-04 stsp }
723 7e656b93 2018-03-17 stsp } else if (packp)
724 7e656b93 2018-03-17 stsp *packp = pack;
725 c7fe698a 2018-01-23 stsp return err;
726 c7fe698a 2018-01-23 stsp }
727 c7fe698a 2018-01-23 stsp
728 7e656b93 2018-03-17 stsp struct got_pack *
729 7e656b93 2018-03-17 stsp get_cached_pack(const char *path_packfile, struct got_repository *repo)
730 7e656b93 2018-03-17 stsp {
731 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
732 7e656b93 2018-03-17 stsp int i;
733 7e656b93 2018-03-17 stsp
734 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
735 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
736 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
737 7e656b93 2018-03-17 stsp break;
738 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
739 7e656b93 2018-03-17 stsp return pack;
740 7e656b93 2018-03-17 stsp }
741 7e656b93 2018-03-17 stsp
742 7e656b93 2018-03-17 stsp return NULL;
743 7e656b93 2018-03-17 stsp }
744 7e656b93 2018-03-17 stsp
745 c7fe698a 2018-01-23 stsp static const struct got_error *
746 d7464085 2018-07-09 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
747 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
748 a487c1d0 2018-01-14 stsp {
749 a487c1d0 2018-01-14 stsp uint8_t t = 0;
750 a487c1d0 2018-01-14 stsp uint64_t s = 0;
751 a487c1d0 2018-01-14 stsp uint8_t sizeN;
752 d7464085 2018-07-09 stsp size_t mapoff = 0;
753 a487c1d0 2018-01-14 stsp int i = 0;
754 d7464085 2018-07-09 stsp
755 d7464085 2018-07-09 stsp *len = 0;
756 d7464085 2018-07-09 stsp
757 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
758 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
759 d7464085 2018-07-09 stsp
760 d7464085 2018-07-09 stsp if (pack->map) {
761 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
762 d7464085 2018-07-09 stsp } else {
763 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
764 d7464085 2018-07-09 stsp return got_error_from_errno();
765 d7464085 2018-07-09 stsp }
766 a487c1d0 2018-01-14 stsp
767 a1fd68d8 2018-01-12 stsp do {
768 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
769 a487c1d0 2018-01-14 stsp if (i > 9)
770 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
771 a1fd68d8 2018-01-12 stsp
772 d7464085 2018-07-09 stsp if (pack->map) {
773 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
774 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
775 d7464085 2018-07-09 stsp } else {
776 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
777 d7464085 2018-07-09 stsp if (n < 0)
778 d7464085 2018-07-09 stsp return got_error_from_errno();
779 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
780 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
781 d7464085 2018-07-09 stsp }
782 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
783 8251fdbc 2018-01-12 stsp
784 a1fd68d8 2018-01-12 stsp if (i == 0) {
785 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
786 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
787 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
788 a1fd68d8 2018-01-12 stsp } else {
789 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
790 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
791 a1fd68d8 2018-01-12 stsp }
792 a1fd68d8 2018-01-12 stsp i++;
793 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
794 a1fd68d8 2018-01-12 stsp
795 a487c1d0 2018-01-14 stsp *type = t;
796 a487c1d0 2018-01-14 stsp *size = s;
797 a487c1d0 2018-01-14 stsp return NULL;
798 0a0a3048 2018-01-10 stsp }
799 c54542a0 2018-01-13 stsp
800 a1fd68d8 2018-01-12 stsp static const struct got_error *
801 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
802 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
803 6ccb713b 2018-01-19 stsp {
804 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
805 6ccb713b 2018-01-19 stsp if (*obj == NULL)
806 0a585a0d 2018-03-17 stsp return got_error_from_errno();
807 6ccb713b 2018-01-19 stsp
808 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
809 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
810 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
811 6ccb713b 2018-01-19 stsp free(*obj);
812 6ccb713b 2018-01-19 stsp *obj = NULL;
813 0a585a0d 2018-03-17 stsp return err;
814 6ccb713b 2018-01-19 stsp }
815 6ccb713b 2018-01-19 stsp
816 6ccb713b 2018-01-19 stsp (*obj)->type = type;
817 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
818 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
819 6ccb713b 2018-01-19 stsp (*obj)->size = size;
820 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
821 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
822 b107e67f 2018-01-19 stsp
823 b107e67f 2018-01-19 stsp return NULL;
824 b107e67f 2018-01-19 stsp }
825 b107e67f 2018-01-19 stsp
826 b107e67f 2018-01-19 stsp static const struct got_error *
827 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
828 d7464085 2018-07-09 stsp off_t delta_offset)
829 b107e67f 2018-01-19 stsp {
830 b107e67f 2018-01-19 stsp int64_t o = 0;
831 b107e67f 2018-01-19 stsp uint8_t offN;
832 b107e67f 2018-01-19 stsp int i = 0;
833 b107e67f 2018-01-19 stsp
834 d7464085 2018-07-09 stsp *len = 0;
835 d7464085 2018-07-09 stsp
836 b107e67f 2018-01-19 stsp do {
837 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
838 b107e67f 2018-01-19 stsp if (i > 8)
839 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
840 b107e67f 2018-01-19 stsp
841 d7464085 2018-07-09 stsp if (pack->map) {
842 d7464085 2018-07-09 stsp size_t mapoff;
843 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
844 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
845 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
846 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
847 d7464085 2018-07-09 stsp } else {
848 d7464085 2018-07-09 stsp ssize_t n;
849 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
850 d7464085 2018-07-09 stsp if (n < 0)
851 d7464085 2018-07-09 stsp return got_error_from_errno();
852 d7464085 2018-07-09 stsp if (n != sizeof(offN))
853 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
854 d7464085 2018-07-09 stsp }
855 d7464085 2018-07-09 stsp *len += sizeof(offN);
856 b107e67f 2018-01-19 stsp
857 b107e67f 2018-01-19 stsp if (i == 0)
858 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
859 b107e67f 2018-01-19 stsp else {
860 cecc778e 2018-01-23 stsp o++;
861 b107e67f 2018-01-19 stsp o <<= 7;
862 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
863 b107e67f 2018-01-19 stsp }
864 b107e67f 2018-01-19 stsp i++;
865 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
866 6ccb713b 2018-01-19 stsp
867 b107e67f 2018-01-19 stsp *offset = o;
868 6ccb713b 2018-01-19 stsp return NULL;
869 6ccb713b 2018-01-19 stsp }
870 6ccb713b 2018-01-19 stsp
871 6ccb713b 2018-01-19 stsp static const struct got_error *
872 d7464085 2018-07-09 stsp parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
873 d7464085 2018-07-09 stsp off_t offset, int tslen)
874 b107e67f 2018-01-19 stsp {
875 96f5e8b3 2018-01-23 stsp const struct got_error *err;
876 b107e67f 2018-01-19 stsp int64_t negoffset;
877 b107e67f 2018-01-19 stsp size_t negofflen;
878 b107e67f 2018-01-19 stsp
879 d7464085 2018-07-09 stsp *len = 0;
880 d7464085 2018-07-09 stsp
881 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
882 d7464085 2018-07-09 stsp offset + tslen);
883 b107e67f 2018-01-19 stsp if (err)
884 b107e67f 2018-01-19 stsp return err;
885 b107e67f 2018-01-19 stsp
886 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
887 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
888 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
889 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
890 b107e67f 2018-01-19 stsp
891 d7464085 2018-07-09 stsp *len = negofflen;
892 96f5e8b3 2018-01-23 stsp return NULL;
893 96f5e8b3 2018-01-23 stsp }
894 96f5e8b3 2018-01-23 stsp
895 0e22967e 2018-02-11 stsp static const struct got_error *
896 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
897 c8ecd499 2018-09-09 stsp struct got_pack *, off_t, size_t, int, size_t, unsigned int);
898 a3500804 2018-01-23 stsp
899 96f5e8b3 2018-01-23 stsp static const struct got_error *
900 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
901 c336f889 2018-07-23 stsp int delta_type, size_t delta_size, size_t delta_data_offset,
902 c336f889 2018-07-23 stsp uint8_t *delta_buf, size_t delta_len)
903 bdd2fbb3 2018-02-11 stsp {
904 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
905 bdd2fbb3 2018-02-11 stsp
906 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
907 c336f889 2018-07-23 stsp delta_data_offset, delta_buf,
908 a53d2f13 2018-03-17 stsp delta_len);
909 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
910 0a585a0d 2018-03-17 stsp return got_error_from_errno();
911 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
912 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
913 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
914 bdd2fbb3 2018-02-11 stsp return NULL;
915 bdd2fbb3 2018-02-11 stsp }
916 bdd2fbb3 2018-02-11 stsp
917 bdd2fbb3 2018-02-11 stsp static const struct got_error *
918 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
919 c8ecd499 2018-09-09 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
920 c8ecd499 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
921 bdd2fbb3 2018-02-11 stsp
922 a3500804 2018-01-23 stsp {
923 a3500804 2018-01-23 stsp const struct got_error *err;
924 c3703302 2018-01-23 stsp off_t base_offset;
925 c3703302 2018-01-23 stsp uint8_t base_type;
926 c3703302 2018-01-23 stsp uint64_t base_size;
927 c3703302 2018-01-23 stsp size_t base_tslen;
928 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
929 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
930 d7464085 2018-07-09 stsp size_t delta_len, consumed;
931 a3500804 2018-01-23 stsp
932 d7464085 2018-07-09 stsp err = parse_offset_delta(&base_offset, &consumed, pack,
933 d7464085 2018-07-09 stsp delta_offset, tslen);
934 a3500804 2018-01-23 stsp if (err)
935 a3500804 2018-01-23 stsp return err;
936 a3500804 2018-01-23 stsp
937 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
938 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
939 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
940 a53d2f13 2018-03-17 stsp
941 d7464085 2018-07-09 stsp if (pack->map == NULL) {
942 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
943 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
944 d7464085 2018-07-09 stsp return got_error_from_errno();
945 d7464085 2018-07-09 stsp }
946 bdd2fbb3 2018-02-11 stsp
947 d7464085 2018-07-09 stsp if (pack->map) {
948 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
949 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
950 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
951 d7464085 2018-07-09 stsp if (err)
952 d7464085 2018-07-09 stsp return err;
953 d7464085 2018-07-09 stsp } else {
954 d7464085 2018-07-09 stsp
955 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
956 d7464085 2018-07-09 stsp if (err)
957 d7464085 2018-07-09 stsp return err;
958 d7464085 2018-07-09 stsp }
959 d7464085 2018-07-09 stsp
960 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
961 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
962 bdd2fbb3 2018-02-11 stsp if (err)
963 ab3ad429 2018-07-23 stsp goto done;
964 bdd2fbb3 2018-02-11 stsp
965 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
966 ab3ad429 2018-07-23 stsp if (base_offset >= pack->filesize) {
967 ab3ad429 2018-07-23 stsp err = got_error(GOT_ERR_PACK_OFFSET);
968 ab3ad429 2018-07-23 stsp goto done;
969 ab3ad429 2018-07-23 stsp }
970 b107e67f 2018-01-19 stsp
971 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
972 d7464085 2018-07-09 stsp pack, base_offset);
973 b107e67f 2018-01-19 stsp if (err)
974 ab3ad429 2018-07-23 stsp goto done;
975 b107e67f 2018-01-19 stsp
976 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(deltas, packidx, pack, base_offset,
977 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
978 ab3ad429 2018-07-23 stsp done:
979 ab3ad429 2018-07-23 stsp if (err)
980 ab3ad429 2018-07-23 stsp free(delta_buf);
981 ab3ad429 2018-07-23 stsp return err;
982 c3703302 2018-01-23 stsp }
983 c3703302 2018-01-23 stsp
984 c3703302 2018-01-23 stsp static const struct got_error *
985 c8ecd499 2018-09-09 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
986 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
987 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
988 c3703302 2018-01-23 stsp {
989 6b9c9673 2018-01-23 stsp const struct got_error *err;
990 6b9c9673 2018-01-23 stsp struct got_object_id id;
991 6b9c9673 2018-01-23 stsp int idx;
992 6b9c9673 2018-01-23 stsp off_t base_offset;
993 6b9c9673 2018-01-23 stsp uint8_t base_type;
994 6b9c9673 2018-01-23 stsp uint64_t base_size;
995 6b9c9673 2018-01-23 stsp size_t base_tslen;
996 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
997 3efa19e7 2018-07-13 stsp uint8_t *delta_buf = NULL;
998 a53d2f13 2018-03-17 stsp size_t delta_len;
999 6b9c9673 2018-01-23 stsp
1000 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
1001 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1002 45202a8f 2018-07-11 stsp delta_data_offset = delta_offset + tslen;
1003 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
1004 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1005 bdd2fbb3 2018-02-11 stsp
1006 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1007 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1008 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
1009 d7464085 2018-07-09 stsp return got_error_from_errno();
1010 d7464085 2018-07-09 stsp }
1011 a53d2f13 2018-03-17 stsp
1012 d7464085 2018-07-09 stsp
1013 d7464085 2018-07-09 stsp if (pack->map) {
1014 45202a8f 2018-07-11 stsp size_t mapoff = (size_t)delta_data_offset;
1015 d7464085 2018-07-09 stsp memcpy(&id, pack->map + mapoff, sizeof(id));
1016 d7464085 2018-07-09 stsp mapoff += sizeof(id);
1017 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
1018 45202a8f 2018-07-11 stsp mapoff, pack->filesize - mapoff);
1019 d7464085 2018-07-09 stsp if (err)
1020 3efa19e7 2018-07-13 stsp goto done;
1021 d7464085 2018-07-09 stsp } else {
1022 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &id, sizeof(id));
1023 d7464085 2018-07-09 stsp if (n < 0)
1024 d7464085 2018-07-09 stsp return got_error_from_errno();
1025 d7464085 2018-07-09 stsp if (n != sizeof(id))
1026 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1027 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
1028 d7464085 2018-07-09 stsp if (err)
1029 3efa19e7 2018-07-13 stsp goto done;
1030 d7464085 2018-07-09 stsp }
1031 d7464085 2018-07-09 stsp
1032 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1033 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
1034 bdd2fbb3 2018-02-11 stsp if (err)
1035 3efa19e7 2018-07-13 stsp goto done;
1036 bdd2fbb3 2018-02-11 stsp
1037 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
1038 3413160a 2018-09-09 stsp idx = get_object_idx(packidx, &id);
1039 3efa19e7 2018-07-13 stsp if (idx == -1) {
1040 3efa19e7 2018-07-13 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
1041 3efa19e7 2018-07-13 stsp goto done;
1042 3efa19e7 2018-07-13 stsp }
1043 6b9c9673 2018-01-23 stsp
1044 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
1045 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
1046 3efa19e7 2018-07-13 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
1047 3efa19e7 2018-07-13 stsp goto done;
1048 999f19f6 2018-03-17 stsp }
1049 6b9c9673 2018-01-23 stsp
1050 b0e2201a 2018-06-22 stsp if (base_offset >= pack->filesize) {
1051 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1052 0c048b15 2018-04-27 stsp goto done;
1053 0c048b15 2018-04-27 stsp }
1054 6b9c9673 2018-01-23 stsp
1055 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
1056 d7464085 2018-07-09 stsp pack, base_offset);
1057 6b9c9673 2018-01-23 stsp if (err)
1058 6b9c9673 2018-01-23 stsp goto done;
1059 6b9c9673 2018-01-23 stsp
1060 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(deltas, packidx, pack, base_offset,
1061 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
1062 6b9c9673 2018-01-23 stsp done:
1063 3efa19e7 2018-07-13 stsp if (err)
1064 3efa19e7 2018-07-13 stsp free(delta_buf);
1065 6b9c9673 2018-01-23 stsp return err;
1066 6b9c9673 2018-01-23 stsp }
1067 6b9c9673 2018-01-23 stsp
1068 6b9c9673 2018-01-23 stsp static const struct got_error *
1069 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
1070 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1071 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
1072 6b9c9673 2018-01-23 stsp {
1073 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
1074 c3703302 2018-01-23 stsp
1075 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
1076 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
1077 5b7e13a7 2018-04-02 stsp
1078 c3703302 2018-01-23 stsp switch (delta_type) {
1079 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
1080 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
1081 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
1082 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
1083 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
1084 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
1085 c336f889 2018-07-23 stsp delta_size, 0, NULL, 0);
1086 4e8cda55 2018-01-19 stsp break;
1087 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1088 c8ecd499 2018-09-09 stsp err = resolve_offset_delta(deltas, packidx, pack,
1089 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1090 96f5e8b3 2018-01-23 stsp break;
1091 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
1092 c8ecd499 2018-09-09 stsp err = resolve_ref_delta(deltas, packidx, pack,
1093 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1094 6b9c9673 2018-01-23 stsp break;
1095 4e8cda55 2018-01-19 stsp default:
1096 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
1097 4e8cda55 2018-01-19 stsp }
1098 96f5e8b3 2018-01-23 stsp
1099 96f5e8b3 2018-01-23 stsp return err;
1100 96f5e8b3 2018-01-23 stsp }
1101 96f5e8b3 2018-01-23 stsp
1102 96f5e8b3 2018-01-23 stsp static const struct got_error *
1103 a98c62b1 2018-09-09 stsp open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1104 a98c62b1 2018-09-09 stsp struct got_pack *pack, struct got_object_id *id, off_t offset,
1105 a98c62b1 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size)
1106 96f5e8b3 2018-01-23 stsp {
1107 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
1108 96f5e8b3 2018-01-23 stsp int resolved_type;
1109 4e8cda55 2018-01-19 stsp
1110 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
1111 b107e67f 2018-01-19 stsp if (*obj == NULL)
1112 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1113 b107e67f 2018-01-19 stsp
1114 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
1115 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
1116 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1117 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1118 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
1119 b107e67f 2018-01-19 stsp
1120 b0e2201a 2018-06-22 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
1121 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
1122 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1123 96f5e8b3 2018-01-23 stsp goto done;
1124 96f5e8b3 2018-01-23 stsp }
1125 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1126 96f5e8b3 2018-01-23 stsp
1127 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
1128 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1129 c3703302 2018-01-23 stsp
1130 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
1131 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
1132 96f5e8b3 2018-01-23 stsp if (err)
1133 96f5e8b3 2018-01-23 stsp goto done;
1134 96f5e8b3 2018-01-23 stsp
1135 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1136 96f5e8b3 2018-01-23 stsp if (err)
1137 96f5e8b3 2018-01-23 stsp goto done;
1138 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
1139 96f5e8b3 2018-01-23 stsp
1140 96f5e8b3 2018-01-23 stsp done:
1141 96f5e8b3 2018-01-23 stsp if (err) {
1142 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
1143 96f5e8b3 2018-01-23 stsp *obj = NULL;
1144 96f5e8b3 2018-01-23 stsp }
1145 96f5e8b3 2018-01-23 stsp return err;
1146 b107e67f 2018-01-19 stsp }
1147 b107e67f 2018-01-19 stsp
1148 b107e67f 2018-01-19 stsp static const struct got_error *
1149 35c73765 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_pack *pack,
1150 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1151 a1fd68d8 2018-01-12 stsp {
1152 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1153 a1fd68d8 2018-01-12 stsp off_t offset;
1154 6c00b545 2018-01-17 stsp uint8_t type;
1155 6c00b545 2018-01-17 stsp uint64_t size;
1156 3ee5fc21 2018-01-17 stsp size_t tslen;
1157 a1fd68d8 2018-01-12 stsp
1158 6c00b545 2018-01-17 stsp *obj = NULL;
1159 a1fd68d8 2018-01-12 stsp
1160 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
1161 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
1162 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1163 a1fd68d8 2018-01-12 stsp
1164 d7464085 2018-07-09 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
1165 a1fd68d8 2018-01-12 stsp if (err)
1166 35c73765 2018-09-09 stsp return err;
1167 a1fd68d8 2018-01-12 stsp
1168 6c00b545 2018-01-17 stsp switch (type) {
1169 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1170 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1171 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1172 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1173 35c73765 2018-09-09 stsp err = open_plain_object(obj, pack->path_packfile, id, type,
1174 6ccb713b 2018-01-19 stsp offset + tslen, size);
1175 6c00b545 2018-01-17 stsp break;
1176 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1177 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1178 a98c62b1 2018-09-09 stsp err = open_delta_object(obj, packidx, pack, id, offset,
1179 b0e2201a 2018-06-22 stsp tslen, type, size);
1180 b107e67f 2018-01-19 stsp break;
1181 6c00b545 2018-01-17 stsp default:
1182 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1183 35c73765 2018-09-09 stsp break;
1184 6c00b545 2018-01-17 stsp }
1185 35c73765 2018-09-09 stsp
1186 a1fd68d8 2018-01-12 stsp return err;
1187 a1fd68d8 2018-01-12 stsp }
1188 a1fd68d8 2018-01-12 stsp
1189 a1fd68d8 2018-01-12 stsp const struct got_error *
1190 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1191 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1192 a1fd68d8 2018-01-12 stsp {
1193 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1194 6fd11751 2018-06-04 stsp struct got_packidx *packidx = NULL;
1195 35c73765 2018-09-09 stsp struct got_pack *pack;
1196 6b9c9673 2018-01-23 stsp int idx;
1197 35c73765 2018-09-09 stsp char *path_packfile;
1198 a1fd68d8 2018-01-12 stsp
1199 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1200 6b9c9673 2018-01-23 stsp if (err)
1201 6b9c9673 2018-01-23 stsp return err;
1202 a1fd68d8 2018-01-12 stsp
1203 35c73765 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
1204 7e656b93 2018-03-17 stsp if (err)
1205 7e656b93 2018-03-17 stsp return err;
1206 7e656b93 2018-03-17 stsp
1207 35c73765 2018-09-09 stsp pack = get_cached_pack(path_packfile, repo);
1208 35c73765 2018-09-09 stsp if (pack == NULL) {
1209 35c73765 2018-09-09 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
1210 35c73765 2018-09-09 stsp if (err)
1211 35c73765 2018-09-09 stsp goto done;
1212 35c73765 2018-09-09 stsp }
1213 35c73765 2018-09-09 stsp
1214 35c73765 2018-09-09 stsp err = open_packed_object(obj, pack, packidx, idx, id);
1215 35c73765 2018-09-09 stsp if (err)
1216 35c73765 2018-09-09 stsp goto done;
1217 35c73765 2018-09-09 stsp
1218 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1219 35c73765 2018-09-09 stsp done:
1220 35c73765 2018-09-09 stsp free(path_packfile);
1221 3ee5fc21 2018-01-17 stsp return err;
1222 3ee5fc21 2018-01-17 stsp }
1223 efd2a263 2018-01-19 stsp
1224 efd2a263 2018-01-19 stsp static const struct got_error *
1225 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1226 22484865 2018-03-13 stsp {
1227 22484865 2018-03-13 stsp struct got_delta *delta;
1228 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1229 22484865 2018-03-13 stsp
1230 22484865 2018-03-13 stsp *max_size = 0;
1231 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1232 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1233 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1234 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1235 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1236 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1237 22484865 2018-03-13 stsp const struct got_error *err;
1238 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1239 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1240 22484865 2018-03-13 stsp if (err)
1241 22484865 2018-03-13 stsp return err;
1242 22484865 2018-03-13 stsp } else
1243 22484865 2018-03-13 stsp base_size = delta->size;
1244 22484865 2018-03-13 stsp if (base_size > *max_size)
1245 22484865 2018-03-13 stsp *max_size = base_size;
1246 22484865 2018-03-13 stsp if (result_size > *max_size)
1247 22484865 2018-03-13 stsp *max_size = result_size;
1248 22484865 2018-03-13 stsp }
1249 bd1223b9 2018-03-14 stsp
1250 9feb4ff2 2018-03-14 stsp return NULL;
1251 22484865 2018-03-13 stsp }
1252 22484865 2018-03-13 stsp
1253 22484865 2018-03-13 stsp static const struct got_error *
1254 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1255 2ce68b2f 2018-09-09 stsp struct got_pack *pack, FILE *outfile)
1256 efd2a263 2018-01-19 stsp {
1257 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1258 6691714a 2018-01-23 stsp struct got_delta *delta;
1259 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1260 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1261 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1262 22484865 2018-03-13 stsp uint64_t max_size;
1263 6691714a 2018-01-23 stsp int n = 0;
1264 b29656e2 2018-03-16 stsp
1265 b29656e2 2018-03-16 stsp *result_size = 0;
1266 3ee5fc21 2018-01-17 stsp
1267 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1268 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1269 efd2a263 2018-01-19 stsp
1270 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1271 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1272 22484865 2018-03-13 stsp if (err)
1273 22484865 2018-03-13 stsp return err;
1274 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1275 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1276 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1277 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1278 8628c62d 2018-03-15 stsp } else {
1279 22484865 2018-03-13 stsp base_file = got_opentemp();
1280 8628c62d 2018-03-15 stsp if (base_file == NULL)
1281 8628c62d 2018-03-15 stsp return got_error_from_errno();
1282 efd2a263 2018-01-19 stsp
1283 22484865 2018-03-13 stsp accum_file = got_opentemp();
1284 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1285 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1286 8628c62d 2018-03-15 stsp fclose(base_file);
1287 8628c62d 2018-03-15 stsp return err;
1288 8628c62d 2018-03-15 stsp }
1289 6691714a 2018-01-23 stsp }
1290 efd2a263 2018-01-19 stsp
1291 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1292 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1293 a6b158cc 2018-02-11 stsp if (n == 0) {
1294 d7464085 2018-07-09 stsp size_t base_len, mapoff;
1295 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1296 9db65d41 2018-03-14 stsp
1297 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1298 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1299 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1300 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1301 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1302 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1303 72eb3431 2018-04-01 stsp goto done;
1304 72eb3431 2018-04-01 stsp }
1305 72eb3431 2018-04-01 stsp
1306 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1307 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1308 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1309 0c048b15 2018-04-27 stsp goto done;
1310 0c048b15 2018-04-27 stsp }
1311 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1312 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1313 d7464085 2018-07-09 stsp == -1) {
1314 d7464085 2018-07-09 stsp err = got_error_from_errno();
1315 d7464085 2018-07-09 stsp goto done;
1316 d7464085 2018-07-09 stsp }
1317 bdd2fbb3 2018-02-11 stsp }
1318 d7464085 2018-07-09 stsp if (base_file) {
1319 d7464085 2018-07-09 stsp if (pack->map) {
1320 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1321 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1322 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1323 d7464085 2018-07-09 stsp pack->filesize - mapoff, base_file);
1324 d7464085 2018-07-09 stsp } else
1325 d7464085 2018-07-09 stsp err = got_inflate_to_file_fd(&base_len,
1326 d7464085 2018-07-09 stsp pack->fd, base_file);
1327 d7464085 2018-07-09 stsp } else {
1328 d7464085 2018-07-09 stsp if (pack->map) {
1329 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1330 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1331 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1332 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1333 d7464085 2018-07-09 stsp } else
1334 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1335 d7464085 2018-07-09 stsp &base_len, pack->fd);
1336 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1337 8628c62d 2018-03-15 stsp uint8_t *p;
1338 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1339 8628c62d 2018-03-15 stsp if (p == NULL) {
1340 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1341 8628c62d 2018-03-15 stsp goto done;
1342 8628c62d 2018-03-15 stsp }
1343 8628c62d 2018-03-15 stsp base_buf = p;
1344 8628c62d 2018-03-15 stsp }
1345 8628c62d 2018-03-15 stsp }
1346 a6b158cc 2018-02-11 stsp if (err)
1347 a6b158cc 2018-02-11 stsp goto done;
1348 a6b158cc 2018-02-11 stsp n++;
1349 8628c62d 2018-03-15 stsp if (base_file)
1350 8628c62d 2018-03-15 stsp rewind(base_file);
1351 a6b158cc 2018-02-11 stsp continue;
1352 9db65d41 2018-03-14 stsp }
1353 885d3e02 2018-01-27 stsp
1354 8628c62d 2018-03-15 stsp if (base_buf) {
1355 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1356 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1357 8628c62d 2018-03-15 stsp n++;
1358 8628c62d 2018-03-15 stsp } else {
1359 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1360 a53d2f13 2018-03-17 stsp delta->delta_len,
1361 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1362 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1363 b29656e2 2018-03-16 stsp &accum_size);
1364 8628c62d 2018-03-15 stsp }
1365 6691714a 2018-01-23 stsp if (err)
1366 6691714a 2018-01-23 stsp goto done;
1367 6691714a 2018-01-23 stsp
1368 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1369 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1370 8628c62d 2018-03-15 stsp if (base_buf) {
1371 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1372 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1373 8628c62d 2018-03-15 stsp base_buf = tmp;
1374 8628c62d 2018-03-15 stsp } else {
1375 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1376 8628c62d 2018-03-15 stsp accum_file = base_file;
1377 8628c62d 2018-03-15 stsp base_file = tmp;
1378 8628c62d 2018-03-15 stsp rewind(base_file);
1379 8628c62d 2018-03-15 stsp rewind(accum_file);
1380 8628c62d 2018-03-15 stsp }
1381 6691714a 2018-01-23 stsp }
1382 6691714a 2018-01-23 stsp }
1383 6691714a 2018-01-23 stsp
1384 6691714a 2018-01-23 stsp done:
1385 8628c62d 2018-03-15 stsp free(base_buf);
1386 8628c62d 2018-03-15 stsp if (accum_buf) {
1387 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1388 8628c62d 2018-03-15 stsp free(accum_buf);
1389 8628c62d 2018-03-15 stsp if (len != accum_size)
1390 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1391 8628c62d 2018-03-15 stsp }
1392 8628c62d 2018-03-15 stsp if (base_file)
1393 8628c62d 2018-03-15 stsp fclose(base_file);
1394 8628c62d 2018-03-15 stsp if (accum_file)
1395 8628c62d 2018-03-15 stsp fclose(accum_file);
1396 6691714a 2018-01-23 stsp rewind(outfile);
1397 b29656e2 2018-03-16 stsp if (err == NULL)
1398 b29656e2 2018-03-16 stsp *result_size = accum_size;
1399 e0ab43e7 2018-03-16 stsp return err;
1400 e0ab43e7 2018-03-16 stsp }
1401 e0ab43e7 2018-03-16 stsp
1402 e0ab43e7 2018-03-16 stsp static const struct got_error *
1403 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1404 48095039 2018-09-09 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1405 e0ab43e7 2018-03-16 stsp {
1406 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1407 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1408 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1409 e0ab43e7 2018-03-16 stsp size_t accum_size;
1410 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1411 e0ab43e7 2018-03-16 stsp int n = 0;
1412 e0ab43e7 2018-03-16 stsp
1413 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1414 e0ab43e7 2018-03-16 stsp *outlen = 0;
1415 e0ab43e7 2018-03-16 stsp
1416 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1417 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1418 e0ab43e7 2018-03-16 stsp
1419 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1420 e0ab43e7 2018-03-16 stsp if (err)
1421 e0ab43e7 2018-03-16 stsp return err;
1422 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1423 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1424 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1425 e0ab43e7 2018-03-16 stsp
1426 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1427 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1428 e0ab43e7 2018-03-16 stsp if (n == 0) {
1429 e0ab43e7 2018-03-16 stsp size_t base_len;
1430 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1431 e0ab43e7 2018-03-16 stsp
1432 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1433 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1434 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1435 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1436 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1437 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1438 72eb3431 2018-04-01 stsp goto done;
1439 72eb3431 2018-04-01 stsp }
1440 72eb3431 2018-04-01 stsp
1441 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1442 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1443 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1444 0c048b15 2018-04-27 stsp goto done;
1445 0c048b15 2018-04-27 stsp }
1446 d7464085 2018-07-09 stsp if (pack->map) {
1447 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1448 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1449 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1450 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1451 d7464085 2018-07-09 stsp } else {
1452 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1453 d7464085 2018-07-09 stsp == -1) {
1454 d7464085 2018-07-09 stsp err = got_error_from_errno();
1455 d7464085 2018-07-09 stsp goto done;
1456 d7464085 2018-07-09 stsp }
1457 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1458 d7464085 2018-07-09 stsp &base_len, pack->fd);
1459 e0ab43e7 2018-03-16 stsp }
1460 d7464085 2018-07-09 stsp if (err)
1461 d7464085 2018-07-09 stsp goto done;
1462 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1463 e0ab43e7 2018-03-16 stsp uint8_t *p;
1464 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1465 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1466 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1467 e0ab43e7 2018-03-16 stsp goto done;
1468 e0ab43e7 2018-03-16 stsp }
1469 e0ab43e7 2018-03-16 stsp base_buf = p;
1470 e0ab43e7 2018-03-16 stsp }
1471 e0ab43e7 2018-03-16 stsp n++;
1472 e0ab43e7 2018-03-16 stsp continue;
1473 e0ab43e7 2018-03-16 stsp }
1474 e0ab43e7 2018-03-16 stsp
1475 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1476 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1477 e0ab43e7 2018-03-16 stsp n++;
1478 e0ab43e7 2018-03-16 stsp if (err)
1479 e0ab43e7 2018-03-16 stsp goto done;
1480 e0ab43e7 2018-03-16 stsp
1481 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1482 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1483 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1484 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1485 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1486 e0ab43e7 2018-03-16 stsp }
1487 e0ab43e7 2018-03-16 stsp }
1488 e0ab43e7 2018-03-16 stsp
1489 e0ab43e7 2018-03-16 stsp done:
1490 e0ab43e7 2018-03-16 stsp free(base_buf);
1491 e0ab43e7 2018-03-16 stsp if (err) {
1492 e0ab43e7 2018-03-16 stsp free(accum_buf);
1493 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1494 e0ab43e7 2018-03-16 stsp *outlen = 0;
1495 e0ab43e7 2018-03-16 stsp } else {
1496 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1497 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1498 e0ab43e7 2018-03-16 stsp }
1499 efd2a263 2018-01-19 stsp return err;
1500 efd2a263 2018-01-19 stsp }
1501 efd2a263 2018-01-19 stsp
1502 3ee5fc21 2018-01-17 stsp const struct got_error *
1503 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1504 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1505 3ee5fc21 2018-01-17 stsp {
1506 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1507 2ce68b2f 2018-09-09 stsp struct got_pack *pack;
1508 3ee5fc21 2018-01-17 stsp
1509 8b2180d4 2018-04-26 stsp *f = NULL;
1510 8b2180d4 2018-04-26 stsp
1511 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1512 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1513 2ce68b2f 2018-09-09 stsp
1514 2ce68b2f 2018-09-09 stsp pack = get_cached_pack(obj->path_packfile, repo);
1515 2ce68b2f 2018-09-09 stsp if (pack == NULL) {
1516 2ce68b2f 2018-09-09 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1517 2ce68b2f 2018-09-09 stsp if (err)
1518 2ce68b2f 2018-09-09 stsp return err;
1519 2ce68b2f 2018-09-09 stsp }
1520 3ee5fc21 2018-01-17 stsp
1521 040bf4a1 2018-04-01 stsp *f = got_opentemp();
1522 040bf4a1 2018-04-01 stsp if (*f == NULL) {
1523 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1524 040bf4a1 2018-04-01 stsp goto done;
1525 ef2bccd9 2018-03-16 stsp }
1526 3ee5fc21 2018-01-17 stsp
1527 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1528 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1529 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1530 0c048b15 2018-04-27 stsp goto done;
1531 0c048b15 2018-04-27 stsp }
1532 72eb3431 2018-04-01 stsp
1533 d7464085 2018-07-09 stsp if (pack->map) {
1534 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1535 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(&obj->size, pack->map,
1536 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff, *f);
1537 d7464085 2018-07-09 stsp } else {
1538 d7464085 2018-07-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1539 d7464085 2018-07-09 stsp err = got_error_from_errno();
1540 d7464085 2018-07-09 stsp goto done;
1541 d7464085 2018-07-09 stsp }
1542 d7464085 2018-07-09 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1543 d7464085 2018-07-09 stsp }
1544 040bf4a1 2018-04-01 stsp } else
1545 2ce68b2f 2018-09-09 stsp err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1546 2ce68b2f 2018-09-09 stsp *f);
1547 3ee5fc21 2018-01-17 stsp done:
1548 d0f3be7c 2018-03-17 stsp if (err && *f) {
1549 3ee5fc21 2018-01-17 stsp fclose(*f);
1550 d0f3be7c 2018-03-17 stsp *f = NULL;
1551 d0f3be7c 2018-03-17 stsp }
1552 a1fd68d8 2018-01-12 stsp return err;
1553 a1fd68d8 2018-01-12 stsp }
1554 e0ab43e7 2018-03-16 stsp
1555 e0ab43e7 2018-03-16 stsp const struct got_error *
1556 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1557 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1558 e0ab43e7 2018-03-16 stsp {
1559 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1560 48095039 2018-09-09 stsp struct got_pack *pack;
1561 e0ab43e7 2018-03-16 stsp
1562 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1563 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1564 e0ab43e7 2018-03-16 stsp
1565 48095039 2018-09-09 stsp pack = get_cached_pack(obj->path_packfile, repo);
1566 48095039 2018-09-09 stsp if (pack == NULL) {
1567 48095039 2018-09-09 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1568 48095039 2018-09-09 stsp if (err)
1569 48095039 2018-09-09 stsp goto done;
1570 48095039 2018-09-09 stsp }
1571 72eb3431 2018-04-01 stsp
1572 48095039 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1573 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1574 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1575 0c048b15 2018-04-27 stsp goto done;
1576 0c048b15 2018-04-27 stsp }
1577 d7464085 2018-07-09 stsp if (pack->map) {
1578 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1579 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(buf, len, pack->map,
1580 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
1581 d7464085 2018-07-09 stsp } else {
1582 d7464085 2018-07-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1583 d7464085 2018-07-09 stsp err = got_error_from_errno();
1584 d7464085 2018-07-09 stsp goto done;
1585 d7464085 2018-07-09 stsp }
1586 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1587 e0ab43e7 2018-03-16 stsp }
1588 e0ab43e7 2018-03-16 stsp } else
1589 48095039 2018-09-09 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
1590 e0ab43e7 2018-03-16 stsp done:
1591 e0ab43e7 2018-03-16 stsp return err;
1592 e0ab43e7 2018-03-16 stsp }