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 8b2180d4 2018-04-26 stsp open_packfile(int *fd, const char *path_packfile,
634 6fd11751 2018-06-04 stsp struct got_repository *repo, struct got_packidx *packidx)
635 7e656b93 2018-03-17 stsp {
636 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
637 7e656b93 2018-03-17 stsp
638 8b2180d4 2018-04-26 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
639 8b2180d4 2018-04-26 stsp if (*fd == -1)
640 8b2180d4 2018-04-26 stsp return got_error_from_errno();
641 7e656b93 2018-03-17 stsp
642 7e656b93 2018-03-17 stsp if (packidx) {
643 8b2180d4 2018-04-26 stsp err = read_packfile_hdr(*fd, packidx);
644 7e656b93 2018-03-17 stsp if (err) {
645 8b2180d4 2018-04-26 stsp close(*fd);
646 8b2180d4 2018-04-26 stsp *fd = -1;
647 7e656b93 2018-03-17 stsp }
648 7e656b93 2018-03-17 stsp }
649 7e656b93 2018-03-17 stsp return err;
650 7e656b93 2018-03-17 stsp }
651 7e656b93 2018-03-17 stsp
652 d7464085 2018-07-09 stsp const struct got_error *
653 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
654 7e656b93 2018-03-17 stsp {
655 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
656 d7464085 2018-07-09 stsp
657 e8f89a81 2018-07-13 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1)
658 d7464085 2018-07-09 stsp err = got_error_from_errno();
659 8b2180d4 2018-04-26 stsp close(pack->fd);
660 8b2180d4 2018-04-26 stsp pack->fd = -1;
661 4589e373 2018-03-17 stsp free(pack->path_packfile);
662 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
663 4589e373 2018-03-17 stsp pack->filesize = 0;
664 d7464085 2018-07-09 stsp
665 d7464085 2018-07-09 stsp return err;
666 7e656b93 2018-03-17 stsp }
667 7e656b93 2018-03-17 stsp
668 7e656b93 2018-03-17 stsp static const struct got_error *
669 7e656b93 2018-03-17 stsp cache_pack(struct got_pack **packp, const char *path_packfile,
670 6fd11751 2018-06-04 stsp struct got_packidx *packidx, struct got_repository *repo)
671 7e656b93 2018-03-17 stsp {
672 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
673 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
674 7e656b93 2018-03-17 stsp int i;
675 7e656b93 2018-03-17 stsp
676 7e656b93 2018-03-17 stsp if (packp)
677 7e656b93 2018-03-17 stsp *packp = NULL;
678 7e656b93 2018-03-17 stsp
679 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
680 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
681 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
682 7e656b93 2018-03-17 stsp break;
683 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
684 7e656b93 2018-03-17 stsp return NULL;
685 c7fe698a 2018-01-23 stsp }
686 7e656b93 2018-03-17 stsp
687 7e656b93 2018-03-17 stsp if (i == nitems(repo->packs) - 1) {
688 d7464085 2018-07-09 stsp err = got_pack_close(&repo->packs[i - 1]);
689 d7464085 2018-07-09 stsp if (err)
690 d7464085 2018-07-09 stsp return err;
691 7e656b93 2018-03-17 stsp memmove(&repo->packs[1], &repo->packs[0],
692 7e656b93 2018-03-17 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
693 7e656b93 2018-03-17 stsp i = 0;
694 7e656b93 2018-03-17 stsp }
695 7e656b93 2018-03-17 stsp
696 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
697 7e656b93 2018-03-17 stsp
698 7e656b93 2018-03-17 stsp pack->path_packfile = strdup(path_packfile);
699 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL) {
700 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
701 7e656b93 2018-03-17 stsp goto done;
702 7e656b93 2018-03-17 stsp }
703 7e656b93 2018-03-17 stsp
704 8b2180d4 2018-04-26 stsp err = open_packfile(&pack->fd, path_packfile, repo, packidx);
705 7e656b93 2018-03-17 stsp if (err)
706 7e656b93 2018-03-17 stsp goto done;
707 7e656b93 2018-03-17 stsp
708 7e656b93 2018-03-17 stsp err = get_packfile_size(&pack->filesize, path_packfile);
709 d7464085 2018-07-09 stsp if (err)
710 d7464085 2018-07-09 stsp goto done;
711 d7464085 2018-07-09 stsp
712 af9b7fee 2018-07-09 stsp #ifndef GOT_PACK_NO_MMAP
713 d7464085 2018-07-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
714 d7464085 2018-07-09 stsp pack->fd, 0);
715 57363308 2018-07-09 stsp if (pack->map == MAP_FAILED)
716 57363308 2018-07-09 stsp pack->map = NULL; /* fall back to read(2) */
717 af9b7fee 2018-07-09 stsp #endif
718 7e656b93 2018-03-17 stsp done:
719 7e656b93 2018-03-17 stsp if (err) {
720 f5feadcc 2018-06-04 stsp if (pack) {
721 7e656b93 2018-03-17 stsp free(pack->path_packfile);
722 f5feadcc 2018-06-04 stsp memset(pack, 0, sizeof(*pack));
723 f5feadcc 2018-06-04 stsp }
724 7e656b93 2018-03-17 stsp } else if (packp)
725 7e656b93 2018-03-17 stsp *packp = pack;
726 c7fe698a 2018-01-23 stsp return err;
727 c7fe698a 2018-01-23 stsp }
728 c7fe698a 2018-01-23 stsp
729 7e656b93 2018-03-17 stsp struct got_pack *
730 7e656b93 2018-03-17 stsp get_cached_pack(const char *path_packfile, struct got_repository *repo)
731 7e656b93 2018-03-17 stsp {
732 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
733 7e656b93 2018-03-17 stsp int i;
734 7e656b93 2018-03-17 stsp
735 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
736 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
737 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
738 7e656b93 2018-03-17 stsp break;
739 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
740 7e656b93 2018-03-17 stsp return pack;
741 7e656b93 2018-03-17 stsp }
742 7e656b93 2018-03-17 stsp
743 7e656b93 2018-03-17 stsp return NULL;
744 7e656b93 2018-03-17 stsp }
745 7e656b93 2018-03-17 stsp
746 c7fe698a 2018-01-23 stsp static const struct got_error *
747 d7464085 2018-07-09 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
748 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
749 a487c1d0 2018-01-14 stsp {
750 a487c1d0 2018-01-14 stsp uint8_t t = 0;
751 a487c1d0 2018-01-14 stsp uint64_t s = 0;
752 a487c1d0 2018-01-14 stsp uint8_t sizeN;
753 d7464085 2018-07-09 stsp size_t mapoff = 0;
754 a487c1d0 2018-01-14 stsp int i = 0;
755 d7464085 2018-07-09 stsp
756 d7464085 2018-07-09 stsp *len = 0;
757 d7464085 2018-07-09 stsp
758 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
759 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
760 d7464085 2018-07-09 stsp
761 d7464085 2018-07-09 stsp if (pack->map) {
762 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
763 d7464085 2018-07-09 stsp } else {
764 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
765 d7464085 2018-07-09 stsp return got_error_from_errno();
766 d7464085 2018-07-09 stsp }
767 a487c1d0 2018-01-14 stsp
768 a1fd68d8 2018-01-12 stsp do {
769 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
770 a487c1d0 2018-01-14 stsp if (i > 9)
771 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
772 a1fd68d8 2018-01-12 stsp
773 d7464085 2018-07-09 stsp if (pack->map) {
774 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
775 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
776 d7464085 2018-07-09 stsp } else {
777 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
778 d7464085 2018-07-09 stsp if (n < 0)
779 d7464085 2018-07-09 stsp return got_error_from_errno();
780 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
781 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
782 d7464085 2018-07-09 stsp }
783 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
784 8251fdbc 2018-01-12 stsp
785 a1fd68d8 2018-01-12 stsp if (i == 0) {
786 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
787 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
788 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
789 a1fd68d8 2018-01-12 stsp } else {
790 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
791 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
792 a1fd68d8 2018-01-12 stsp }
793 a1fd68d8 2018-01-12 stsp i++;
794 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
795 a1fd68d8 2018-01-12 stsp
796 a487c1d0 2018-01-14 stsp *type = t;
797 a487c1d0 2018-01-14 stsp *size = s;
798 a487c1d0 2018-01-14 stsp return NULL;
799 0a0a3048 2018-01-10 stsp }
800 c54542a0 2018-01-13 stsp
801 a1fd68d8 2018-01-12 stsp static const struct got_error *
802 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
803 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
804 6ccb713b 2018-01-19 stsp {
805 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
806 6ccb713b 2018-01-19 stsp if (*obj == NULL)
807 0a585a0d 2018-03-17 stsp return got_error_from_errno();
808 6ccb713b 2018-01-19 stsp
809 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
810 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
811 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
812 6ccb713b 2018-01-19 stsp free(*obj);
813 6ccb713b 2018-01-19 stsp *obj = NULL;
814 0a585a0d 2018-03-17 stsp return err;
815 6ccb713b 2018-01-19 stsp }
816 6ccb713b 2018-01-19 stsp
817 6ccb713b 2018-01-19 stsp (*obj)->type = type;
818 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
819 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
820 6ccb713b 2018-01-19 stsp (*obj)->size = size;
821 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
822 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
823 b107e67f 2018-01-19 stsp
824 b107e67f 2018-01-19 stsp return NULL;
825 b107e67f 2018-01-19 stsp }
826 b107e67f 2018-01-19 stsp
827 b107e67f 2018-01-19 stsp static const struct got_error *
828 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
829 d7464085 2018-07-09 stsp off_t delta_offset)
830 b107e67f 2018-01-19 stsp {
831 b107e67f 2018-01-19 stsp int64_t o = 0;
832 b107e67f 2018-01-19 stsp uint8_t offN;
833 b107e67f 2018-01-19 stsp int i = 0;
834 b107e67f 2018-01-19 stsp
835 d7464085 2018-07-09 stsp *len = 0;
836 d7464085 2018-07-09 stsp
837 b107e67f 2018-01-19 stsp do {
838 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
839 b107e67f 2018-01-19 stsp if (i > 8)
840 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
841 b107e67f 2018-01-19 stsp
842 d7464085 2018-07-09 stsp if (pack->map) {
843 d7464085 2018-07-09 stsp size_t mapoff;
844 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
845 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
846 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
847 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
848 d7464085 2018-07-09 stsp } else {
849 d7464085 2018-07-09 stsp ssize_t n;
850 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
851 d7464085 2018-07-09 stsp if (n < 0)
852 d7464085 2018-07-09 stsp return got_error_from_errno();
853 d7464085 2018-07-09 stsp if (n != sizeof(offN))
854 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
855 d7464085 2018-07-09 stsp }
856 d7464085 2018-07-09 stsp *len += sizeof(offN);
857 b107e67f 2018-01-19 stsp
858 b107e67f 2018-01-19 stsp if (i == 0)
859 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
860 b107e67f 2018-01-19 stsp else {
861 cecc778e 2018-01-23 stsp o++;
862 b107e67f 2018-01-19 stsp o <<= 7;
863 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
864 b107e67f 2018-01-19 stsp }
865 b107e67f 2018-01-19 stsp i++;
866 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
867 6ccb713b 2018-01-19 stsp
868 b107e67f 2018-01-19 stsp *offset = o;
869 6ccb713b 2018-01-19 stsp return NULL;
870 6ccb713b 2018-01-19 stsp }
871 6ccb713b 2018-01-19 stsp
872 6ccb713b 2018-01-19 stsp static const struct got_error *
873 d7464085 2018-07-09 stsp parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
874 d7464085 2018-07-09 stsp off_t offset, int tslen)
875 b107e67f 2018-01-19 stsp {
876 96f5e8b3 2018-01-23 stsp const struct got_error *err;
877 b107e67f 2018-01-19 stsp int64_t negoffset;
878 b107e67f 2018-01-19 stsp size_t negofflen;
879 b107e67f 2018-01-19 stsp
880 d7464085 2018-07-09 stsp *len = 0;
881 d7464085 2018-07-09 stsp
882 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
883 d7464085 2018-07-09 stsp offset + tslen);
884 b107e67f 2018-01-19 stsp if (err)
885 b107e67f 2018-01-19 stsp return err;
886 b107e67f 2018-01-19 stsp
887 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
888 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
889 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
890 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
891 b107e67f 2018-01-19 stsp
892 d7464085 2018-07-09 stsp *len = negofflen;
893 96f5e8b3 2018-01-23 stsp return NULL;
894 96f5e8b3 2018-01-23 stsp }
895 96f5e8b3 2018-01-23 stsp
896 0e22967e 2018-02-11 stsp static const struct got_error *
897 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
898 b0e2201a 2018-06-22 stsp struct got_packidx *, struct got_pack *, off_t, size_t, int,
899 652b2703 2018-06-22 stsp size_t, unsigned int);
900 a3500804 2018-01-23 stsp
901 96f5e8b3 2018-01-23 stsp static const struct got_error *
902 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
903 c336f889 2018-07-23 stsp int delta_type, size_t delta_size, size_t delta_data_offset,
904 c336f889 2018-07-23 stsp uint8_t *delta_buf, size_t delta_len)
905 bdd2fbb3 2018-02-11 stsp {
906 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
907 bdd2fbb3 2018-02-11 stsp
908 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
909 c336f889 2018-07-23 stsp delta_data_offset, delta_buf,
910 a53d2f13 2018-03-17 stsp delta_len);
911 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
912 0a585a0d 2018-03-17 stsp return got_error_from_errno();
913 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
914 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
915 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
916 bdd2fbb3 2018-02-11 stsp return NULL;
917 bdd2fbb3 2018-02-11 stsp }
918 bdd2fbb3 2018-02-11 stsp
919 bdd2fbb3 2018-02-11 stsp static const struct got_error *
920 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
921 b0e2201a 2018-06-22 stsp struct got_repository *repo, struct got_packidx *packidx,
922 b0e2201a 2018-06-22 stsp struct got_pack *pack, off_t delta_offset, size_t tslen,
923 b0e2201a 2018-06-22 stsp int delta_type, size_t delta_size, unsigned int recursion)
924 bdd2fbb3 2018-02-11 stsp
925 a3500804 2018-01-23 stsp {
926 a3500804 2018-01-23 stsp const struct got_error *err;
927 c3703302 2018-01-23 stsp off_t base_offset;
928 c3703302 2018-01-23 stsp uint8_t base_type;
929 c3703302 2018-01-23 stsp uint64_t base_size;
930 c3703302 2018-01-23 stsp size_t base_tslen;
931 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
932 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
933 d7464085 2018-07-09 stsp size_t delta_len, consumed;
934 a3500804 2018-01-23 stsp
935 d7464085 2018-07-09 stsp err = parse_offset_delta(&base_offset, &consumed, pack,
936 d7464085 2018-07-09 stsp delta_offset, tslen);
937 a3500804 2018-01-23 stsp if (err)
938 a3500804 2018-01-23 stsp return err;
939 a3500804 2018-01-23 stsp
940 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
941 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
942 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
943 a53d2f13 2018-03-17 stsp
944 d7464085 2018-07-09 stsp if (pack->map == NULL) {
945 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
946 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
947 d7464085 2018-07-09 stsp return got_error_from_errno();
948 d7464085 2018-07-09 stsp }
949 bdd2fbb3 2018-02-11 stsp
950 d7464085 2018-07-09 stsp if (pack->map) {
951 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
952 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
953 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
954 d7464085 2018-07-09 stsp if (err)
955 d7464085 2018-07-09 stsp return err;
956 d7464085 2018-07-09 stsp } else {
957 d7464085 2018-07-09 stsp
958 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
959 d7464085 2018-07-09 stsp if (err)
960 d7464085 2018-07-09 stsp return err;
961 d7464085 2018-07-09 stsp }
962 d7464085 2018-07-09 stsp
963 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
964 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
965 bdd2fbb3 2018-02-11 stsp if (err)
966 ab3ad429 2018-07-23 stsp goto done;
967 bdd2fbb3 2018-02-11 stsp
968 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
969 ab3ad429 2018-07-23 stsp if (base_offset >= pack->filesize) {
970 ab3ad429 2018-07-23 stsp err = got_error(GOT_ERR_PACK_OFFSET);
971 ab3ad429 2018-07-23 stsp goto done;
972 ab3ad429 2018-07-23 stsp }
973 b107e67f 2018-01-19 stsp
974 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
975 d7464085 2018-07-09 stsp pack, base_offset);
976 b107e67f 2018-01-19 stsp if (err)
977 ab3ad429 2018-07-23 stsp goto done;
978 b107e67f 2018-01-19 stsp
979 ab3ad429 2018-07-23 stsp err = resolve_delta_chain(deltas, repo, packidx, pack, base_offset,
980 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
981 ab3ad429 2018-07-23 stsp done:
982 ab3ad429 2018-07-23 stsp if (err)
983 ab3ad429 2018-07-23 stsp free(delta_buf);
984 ab3ad429 2018-07-23 stsp return err;
985 c3703302 2018-01-23 stsp }
986 c3703302 2018-01-23 stsp
987 c3703302 2018-01-23 stsp static const struct got_error *
988 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
989 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack,
990 652b2703 2018-06-22 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
991 652b2703 2018-06-22 stsp unsigned int recursion)
992 c3703302 2018-01-23 stsp {
993 6b9c9673 2018-01-23 stsp const struct got_error *err;
994 6b9c9673 2018-01-23 stsp struct got_object_id id;
995 6b9c9673 2018-01-23 stsp int idx;
996 6b9c9673 2018-01-23 stsp off_t base_offset;
997 6b9c9673 2018-01-23 stsp uint8_t base_type;
998 6b9c9673 2018-01-23 stsp uint64_t base_size;
999 6b9c9673 2018-01-23 stsp size_t base_tslen;
1000 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
1001 3efa19e7 2018-07-13 stsp uint8_t *delta_buf = NULL;
1002 a53d2f13 2018-03-17 stsp size_t delta_len;
1003 6b9c9673 2018-01-23 stsp
1004 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
1005 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1006 45202a8f 2018-07-11 stsp delta_data_offset = delta_offset + tslen;
1007 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
1008 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1009 bdd2fbb3 2018-02-11 stsp
1010 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1011 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1012 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
1013 d7464085 2018-07-09 stsp return got_error_from_errno();
1014 d7464085 2018-07-09 stsp }
1015 a53d2f13 2018-03-17 stsp
1016 d7464085 2018-07-09 stsp
1017 d7464085 2018-07-09 stsp if (pack->map) {
1018 45202a8f 2018-07-11 stsp size_t mapoff = (size_t)delta_data_offset;
1019 d7464085 2018-07-09 stsp memcpy(&id, pack->map + mapoff, sizeof(id));
1020 d7464085 2018-07-09 stsp mapoff += sizeof(id);
1021 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
1022 45202a8f 2018-07-11 stsp mapoff, pack->filesize - mapoff);
1023 d7464085 2018-07-09 stsp if (err)
1024 3efa19e7 2018-07-13 stsp goto done;
1025 d7464085 2018-07-09 stsp } else {
1026 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &id, sizeof(id));
1027 d7464085 2018-07-09 stsp if (n < 0)
1028 d7464085 2018-07-09 stsp return got_error_from_errno();
1029 d7464085 2018-07-09 stsp if (n != sizeof(id))
1030 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1031 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
1032 d7464085 2018-07-09 stsp if (err)
1033 3efa19e7 2018-07-13 stsp goto done;
1034 d7464085 2018-07-09 stsp }
1035 d7464085 2018-07-09 stsp
1036 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1037 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
1038 bdd2fbb3 2018-02-11 stsp if (err)
1039 3efa19e7 2018-07-13 stsp goto done;
1040 bdd2fbb3 2018-02-11 stsp
1041 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
1042 3413160a 2018-09-09 stsp idx = get_object_idx(packidx, &id);
1043 3efa19e7 2018-07-13 stsp if (idx == -1) {
1044 3efa19e7 2018-07-13 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
1045 3efa19e7 2018-07-13 stsp goto done;
1046 3efa19e7 2018-07-13 stsp }
1047 6b9c9673 2018-01-23 stsp
1048 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
1049 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
1050 3efa19e7 2018-07-13 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
1051 3efa19e7 2018-07-13 stsp goto done;
1052 999f19f6 2018-03-17 stsp }
1053 6b9c9673 2018-01-23 stsp
1054 b0e2201a 2018-06-22 stsp if (base_offset >= pack->filesize) {
1055 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1056 0c048b15 2018-04-27 stsp goto done;
1057 0c048b15 2018-04-27 stsp }
1058 6b9c9673 2018-01-23 stsp
1059 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
1060 d7464085 2018-07-09 stsp pack, base_offset);
1061 6b9c9673 2018-01-23 stsp if (err)
1062 6b9c9673 2018-01-23 stsp goto done;
1063 6b9c9673 2018-01-23 stsp
1064 b0e2201a 2018-06-22 stsp err = resolve_delta_chain(deltas, repo, packidx, pack, base_offset,
1065 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
1066 6b9c9673 2018-01-23 stsp done:
1067 3efa19e7 2018-07-13 stsp if (err)
1068 3efa19e7 2018-07-13 stsp free(delta_buf);
1069 6b9c9673 2018-01-23 stsp return err;
1070 6b9c9673 2018-01-23 stsp }
1071 6b9c9673 2018-01-23 stsp
1072 6b9c9673 2018-01-23 stsp static const struct got_error *
1073 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
1074 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1075 b0e2201a 2018-06-22 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1076 6b9c9673 2018-01-23 stsp {
1077 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
1078 c3703302 2018-01-23 stsp
1079 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
1080 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
1081 5b7e13a7 2018-04-02 stsp
1082 c3703302 2018-01-23 stsp switch (delta_type) {
1083 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
1084 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
1085 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
1086 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
1087 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
1088 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
1089 c336f889 2018-07-23 stsp delta_size, 0, NULL, 0);
1090 4e8cda55 2018-01-19 stsp break;
1091 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1092 b0e2201a 2018-06-22 stsp err = resolve_offset_delta(deltas, repo, packidx, pack,
1093 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1094 96f5e8b3 2018-01-23 stsp break;
1095 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
1096 b0e2201a 2018-06-22 stsp err = resolve_ref_delta(deltas, repo, packidx, pack,
1097 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1098 6b9c9673 2018-01-23 stsp break;
1099 4e8cda55 2018-01-19 stsp default:
1100 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
1101 4e8cda55 2018-01-19 stsp }
1102 96f5e8b3 2018-01-23 stsp
1103 96f5e8b3 2018-01-23 stsp return err;
1104 96f5e8b3 2018-01-23 stsp }
1105 96f5e8b3 2018-01-23 stsp
1106 96f5e8b3 2018-01-23 stsp static const struct got_error *
1107 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
1108 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack,
1109 b0e2201a 2018-06-22 stsp struct got_object_id *id, off_t offset, size_t tslen,
1110 6fd11751 2018-06-04 stsp int delta_type, size_t delta_size)
1111 96f5e8b3 2018-01-23 stsp {
1112 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
1113 96f5e8b3 2018-01-23 stsp int resolved_type;
1114 4e8cda55 2018-01-19 stsp
1115 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
1116 b107e67f 2018-01-19 stsp if (*obj == NULL)
1117 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1118 b107e67f 2018-01-19 stsp
1119 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
1120 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
1121 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1122 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1123 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
1124 b107e67f 2018-01-19 stsp
1125 b0e2201a 2018-06-22 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
1126 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
1127 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1128 96f5e8b3 2018-01-23 stsp goto done;
1129 96f5e8b3 2018-01-23 stsp }
1130 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1131 96f5e8b3 2018-01-23 stsp
1132 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
1133 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1134 c3703302 2018-01-23 stsp
1135 b0e2201a 2018-06-22 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packidx, pack, offset,
1136 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
1137 96f5e8b3 2018-01-23 stsp if (err)
1138 96f5e8b3 2018-01-23 stsp goto done;
1139 96f5e8b3 2018-01-23 stsp
1140 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1141 96f5e8b3 2018-01-23 stsp if (err)
1142 96f5e8b3 2018-01-23 stsp goto done;
1143 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
1144 96f5e8b3 2018-01-23 stsp
1145 96f5e8b3 2018-01-23 stsp done:
1146 96f5e8b3 2018-01-23 stsp if (err) {
1147 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
1148 96f5e8b3 2018-01-23 stsp *obj = NULL;
1149 96f5e8b3 2018-01-23 stsp }
1150 96f5e8b3 2018-01-23 stsp return err;
1151 b107e67f 2018-01-19 stsp }
1152 b107e67f 2018-01-19 stsp
1153 b107e67f 2018-01-19 stsp static const struct got_error *
1154 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
1155 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1156 a1fd68d8 2018-01-12 stsp {
1157 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1158 a1fd68d8 2018-01-12 stsp off_t offset;
1159 65cf1e80 2018-03-16 stsp char *path_packfile;
1160 6d89869a 2018-03-17 stsp struct got_pack *pack;
1161 6c00b545 2018-01-17 stsp uint8_t type;
1162 6c00b545 2018-01-17 stsp uint64_t size;
1163 3ee5fc21 2018-01-17 stsp size_t tslen;
1164 a1fd68d8 2018-01-12 stsp
1165 6c00b545 2018-01-17 stsp *obj = NULL;
1166 a1fd68d8 2018-01-12 stsp
1167 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
1168 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
1169 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1170 a1fd68d8 2018-01-12 stsp
1171 4a9c75d9 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
1172 6b9c9673 2018-01-23 stsp if (err)
1173 6b9c9673 2018-01-23 stsp return err;
1174 a1fd68d8 2018-01-12 stsp
1175 6d89869a 2018-03-17 stsp pack = get_cached_pack(path_packfile, repo);
1176 6d89869a 2018-03-17 stsp if (pack == NULL) {
1177 6d89869a 2018-03-17 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
1178 6d89869a 2018-03-17 stsp if (err)
1179 6d89869a 2018-03-17 stsp goto done;
1180 6d89869a 2018-03-17 stsp }
1181 7e656b93 2018-03-17 stsp
1182 d7464085 2018-07-09 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
1183 a1fd68d8 2018-01-12 stsp if (err)
1184 a1fd68d8 2018-01-12 stsp goto done;
1185 a1fd68d8 2018-01-12 stsp
1186 6c00b545 2018-01-17 stsp switch (type) {
1187 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1188 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1189 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1190 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1191 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
1192 6ccb713b 2018-01-19 stsp offset + tslen, size);
1193 6c00b545 2018-01-17 stsp break;
1194 6ccb713b 2018-01-19 stsp
1195 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1196 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1197 b0e2201a 2018-06-22 stsp err = open_delta_object(obj, repo, packidx, pack, id, offset,
1198 b0e2201a 2018-06-22 stsp tslen, type, size);
1199 b107e67f 2018-01-19 stsp break;
1200 b107e67f 2018-01-19 stsp
1201 6c00b545 2018-01-17 stsp default:
1202 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1203 6c00b545 2018-01-17 stsp goto done;
1204 6c00b545 2018-01-17 stsp }
1205 a1fd68d8 2018-01-12 stsp done:
1206 65cf1e80 2018-03-16 stsp free(path_packfile);
1207 a1fd68d8 2018-01-12 stsp return err;
1208 a1fd68d8 2018-01-12 stsp }
1209 a1fd68d8 2018-01-12 stsp
1210 a1fd68d8 2018-01-12 stsp const struct got_error *
1211 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1212 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1213 a1fd68d8 2018-01-12 stsp {
1214 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1215 6fd11751 2018-06-04 stsp struct got_packidx *packidx = NULL;
1216 6b9c9673 2018-01-23 stsp int idx;
1217 a1fd68d8 2018-01-12 stsp
1218 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1219 6b9c9673 2018-01-23 stsp if (err)
1220 6b9c9673 2018-01-23 stsp return err;
1221 a1fd68d8 2018-01-12 stsp
1222 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
1223 7e656b93 2018-03-17 stsp if (err)
1224 7e656b93 2018-03-17 stsp return err;
1225 7e656b93 2018-03-17 stsp
1226 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1227 3ee5fc21 2018-01-17 stsp return err;
1228 3ee5fc21 2018-01-17 stsp }
1229 efd2a263 2018-01-19 stsp
1230 efd2a263 2018-01-19 stsp static const struct got_error *
1231 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1232 22484865 2018-03-13 stsp {
1233 22484865 2018-03-13 stsp struct got_delta *delta;
1234 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1235 22484865 2018-03-13 stsp
1236 22484865 2018-03-13 stsp *max_size = 0;
1237 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1238 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1239 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1240 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1241 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1242 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1243 22484865 2018-03-13 stsp const struct got_error *err;
1244 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1245 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1246 22484865 2018-03-13 stsp if (err)
1247 22484865 2018-03-13 stsp return err;
1248 22484865 2018-03-13 stsp } else
1249 22484865 2018-03-13 stsp base_size = delta->size;
1250 22484865 2018-03-13 stsp if (base_size > *max_size)
1251 22484865 2018-03-13 stsp *max_size = base_size;
1252 22484865 2018-03-13 stsp if (result_size > *max_size)
1253 22484865 2018-03-13 stsp *max_size = result_size;
1254 22484865 2018-03-13 stsp }
1255 bd1223b9 2018-03-14 stsp
1256 9feb4ff2 2018-03-14 stsp return NULL;
1257 22484865 2018-03-13 stsp }
1258 22484865 2018-03-13 stsp
1259 22484865 2018-03-13 stsp static const struct got_error *
1260 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1261 c336f889 2018-07-23 stsp const char *path_packfile, FILE *outfile, struct got_repository *repo)
1262 efd2a263 2018-01-19 stsp {
1263 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1264 6691714a 2018-01-23 stsp struct got_delta *delta;
1265 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1266 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1267 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1268 22484865 2018-03-13 stsp uint64_t max_size;
1269 6691714a 2018-01-23 stsp int n = 0;
1270 b29656e2 2018-03-16 stsp
1271 b29656e2 2018-03-16 stsp *result_size = 0;
1272 3ee5fc21 2018-01-17 stsp
1273 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1274 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1275 efd2a263 2018-01-19 stsp
1276 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1277 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1278 22484865 2018-03-13 stsp if (err)
1279 22484865 2018-03-13 stsp return err;
1280 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1281 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1282 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1283 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1284 8628c62d 2018-03-15 stsp } else {
1285 22484865 2018-03-13 stsp base_file = got_opentemp();
1286 8628c62d 2018-03-15 stsp if (base_file == NULL)
1287 8628c62d 2018-03-15 stsp return got_error_from_errno();
1288 efd2a263 2018-01-19 stsp
1289 22484865 2018-03-13 stsp accum_file = got_opentemp();
1290 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1291 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1292 8628c62d 2018-03-15 stsp fclose(base_file);
1293 8628c62d 2018-03-15 stsp return err;
1294 8628c62d 2018-03-15 stsp }
1295 6691714a 2018-01-23 stsp }
1296 efd2a263 2018-01-19 stsp
1297 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1298 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1299 a6b158cc 2018-02-11 stsp if (n == 0) {
1300 72eb3431 2018-04-01 stsp struct got_pack *pack;
1301 d7464085 2018-07-09 stsp size_t base_len, mapoff;
1302 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1303 9db65d41 2018-03-14 stsp
1304 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1305 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1306 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1307 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1308 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1309 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1310 72eb3431 2018-04-01 stsp goto done;
1311 72eb3431 2018-04-01 stsp }
1312 72eb3431 2018-04-01 stsp
1313 c336f889 2018-07-23 stsp pack = get_cached_pack(path_packfile, repo);
1314 72eb3431 2018-04-01 stsp if (pack == NULL) {
1315 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1316 a6b158cc 2018-02-11 stsp goto done;
1317 a6b158cc 2018-02-11 stsp }
1318 6691714a 2018-01-23 stsp
1319 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1320 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1321 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1322 0c048b15 2018-04-27 stsp goto done;
1323 0c048b15 2018-04-27 stsp }
1324 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1325 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1326 d7464085 2018-07-09 stsp == -1) {
1327 d7464085 2018-07-09 stsp err = got_error_from_errno();
1328 d7464085 2018-07-09 stsp goto done;
1329 d7464085 2018-07-09 stsp }
1330 bdd2fbb3 2018-02-11 stsp }
1331 d7464085 2018-07-09 stsp if (base_file) {
1332 d7464085 2018-07-09 stsp if (pack->map) {
1333 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1334 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1335 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1336 d7464085 2018-07-09 stsp pack->filesize - mapoff, base_file);
1337 d7464085 2018-07-09 stsp } else
1338 d7464085 2018-07-09 stsp err = got_inflate_to_file_fd(&base_len,
1339 d7464085 2018-07-09 stsp pack->fd, base_file);
1340 d7464085 2018-07-09 stsp } else {
1341 d7464085 2018-07-09 stsp if (pack->map) {
1342 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1343 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1344 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1345 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1346 d7464085 2018-07-09 stsp } else
1347 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1348 d7464085 2018-07-09 stsp &base_len, pack->fd);
1349 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1350 8628c62d 2018-03-15 stsp uint8_t *p;
1351 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1352 8628c62d 2018-03-15 stsp if (p == NULL) {
1353 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1354 8628c62d 2018-03-15 stsp goto done;
1355 8628c62d 2018-03-15 stsp }
1356 8628c62d 2018-03-15 stsp base_buf = p;
1357 8628c62d 2018-03-15 stsp }
1358 8628c62d 2018-03-15 stsp }
1359 a6b158cc 2018-02-11 stsp if (err)
1360 a6b158cc 2018-02-11 stsp goto done;
1361 a6b158cc 2018-02-11 stsp n++;
1362 8628c62d 2018-03-15 stsp if (base_file)
1363 8628c62d 2018-03-15 stsp rewind(base_file);
1364 a6b158cc 2018-02-11 stsp continue;
1365 9db65d41 2018-03-14 stsp }
1366 885d3e02 2018-01-27 stsp
1367 8628c62d 2018-03-15 stsp if (base_buf) {
1368 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1369 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1370 8628c62d 2018-03-15 stsp n++;
1371 8628c62d 2018-03-15 stsp } else {
1372 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1373 a53d2f13 2018-03-17 stsp delta->delta_len,
1374 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1375 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1376 b29656e2 2018-03-16 stsp &accum_size);
1377 8628c62d 2018-03-15 stsp }
1378 6691714a 2018-01-23 stsp if (err)
1379 6691714a 2018-01-23 stsp goto done;
1380 6691714a 2018-01-23 stsp
1381 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1382 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1383 8628c62d 2018-03-15 stsp if (base_buf) {
1384 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1385 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1386 8628c62d 2018-03-15 stsp base_buf = tmp;
1387 8628c62d 2018-03-15 stsp } else {
1388 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1389 8628c62d 2018-03-15 stsp accum_file = base_file;
1390 8628c62d 2018-03-15 stsp base_file = tmp;
1391 8628c62d 2018-03-15 stsp rewind(base_file);
1392 8628c62d 2018-03-15 stsp rewind(accum_file);
1393 8628c62d 2018-03-15 stsp }
1394 6691714a 2018-01-23 stsp }
1395 6691714a 2018-01-23 stsp }
1396 6691714a 2018-01-23 stsp
1397 6691714a 2018-01-23 stsp done:
1398 8628c62d 2018-03-15 stsp free(base_buf);
1399 8628c62d 2018-03-15 stsp if (accum_buf) {
1400 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1401 8628c62d 2018-03-15 stsp free(accum_buf);
1402 8628c62d 2018-03-15 stsp if (len != accum_size)
1403 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1404 8628c62d 2018-03-15 stsp }
1405 8628c62d 2018-03-15 stsp if (base_file)
1406 8628c62d 2018-03-15 stsp fclose(base_file);
1407 8628c62d 2018-03-15 stsp if (accum_file)
1408 8628c62d 2018-03-15 stsp fclose(accum_file);
1409 6691714a 2018-01-23 stsp rewind(outfile);
1410 b29656e2 2018-03-16 stsp if (err == NULL)
1411 b29656e2 2018-03-16 stsp *result_size = accum_size;
1412 e0ab43e7 2018-03-16 stsp return err;
1413 e0ab43e7 2018-03-16 stsp }
1414 e0ab43e7 2018-03-16 stsp
1415 e0ab43e7 2018-03-16 stsp static const struct got_error *
1416 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1417 c336f889 2018-07-23 stsp struct got_delta_chain *deltas, const char *path_packfile,
1418 c336f889 2018-07-23 stsp struct got_repository *repo)
1419 e0ab43e7 2018-03-16 stsp {
1420 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1421 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1422 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1423 e0ab43e7 2018-03-16 stsp size_t accum_size;
1424 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1425 e0ab43e7 2018-03-16 stsp int n = 0;
1426 e0ab43e7 2018-03-16 stsp
1427 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1428 e0ab43e7 2018-03-16 stsp *outlen = 0;
1429 e0ab43e7 2018-03-16 stsp
1430 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1431 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1432 e0ab43e7 2018-03-16 stsp
1433 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1434 e0ab43e7 2018-03-16 stsp if (err)
1435 e0ab43e7 2018-03-16 stsp return err;
1436 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1437 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1438 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1439 e0ab43e7 2018-03-16 stsp
1440 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1441 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1442 e0ab43e7 2018-03-16 stsp if (n == 0) {
1443 72eb3431 2018-04-01 stsp struct got_pack *pack;
1444 e0ab43e7 2018-03-16 stsp size_t base_len;
1445 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1446 e0ab43e7 2018-03-16 stsp
1447 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1448 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1449 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1450 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1451 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1452 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1453 72eb3431 2018-04-01 stsp goto done;
1454 72eb3431 2018-04-01 stsp }
1455 72eb3431 2018-04-01 stsp
1456 c336f889 2018-07-23 stsp pack = get_cached_pack(path_packfile, repo);
1457 72eb3431 2018-04-01 stsp if (pack == NULL) {
1458 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1459 e0ab43e7 2018-03-16 stsp goto done;
1460 e0ab43e7 2018-03-16 stsp }
1461 e0ab43e7 2018-03-16 stsp
1462 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1463 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1464 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1465 0c048b15 2018-04-27 stsp goto done;
1466 0c048b15 2018-04-27 stsp }
1467 d7464085 2018-07-09 stsp if (pack->map) {
1468 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1469 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1470 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1471 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1472 d7464085 2018-07-09 stsp } else {
1473 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1474 d7464085 2018-07-09 stsp == -1) {
1475 d7464085 2018-07-09 stsp err = got_error_from_errno();
1476 d7464085 2018-07-09 stsp goto done;
1477 d7464085 2018-07-09 stsp }
1478 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1479 d7464085 2018-07-09 stsp &base_len, pack->fd);
1480 e0ab43e7 2018-03-16 stsp }
1481 d7464085 2018-07-09 stsp if (err)
1482 d7464085 2018-07-09 stsp goto done;
1483 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1484 e0ab43e7 2018-03-16 stsp uint8_t *p;
1485 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1486 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1487 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1488 e0ab43e7 2018-03-16 stsp goto done;
1489 e0ab43e7 2018-03-16 stsp }
1490 e0ab43e7 2018-03-16 stsp base_buf = p;
1491 e0ab43e7 2018-03-16 stsp }
1492 e0ab43e7 2018-03-16 stsp n++;
1493 e0ab43e7 2018-03-16 stsp continue;
1494 e0ab43e7 2018-03-16 stsp }
1495 e0ab43e7 2018-03-16 stsp
1496 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1497 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1498 e0ab43e7 2018-03-16 stsp n++;
1499 e0ab43e7 2018-03-16 stsp if (err)
1500 e0ab43e7 2018-03-16 stsp goto done;
1501 e0ab43e7 2018-03-16 stsp
1502 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1503 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1504 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1505 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1506 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1507 e0ab43e7 2018-03-16 stsp }
1508 e0ab43e7 2018-03-16 stsp }
1509 e0ab43e7 2018-03-16 stsp
1510 e0ab43e7 2018-03-16 stsp done:
1511 e0ab43e7 2018-03-16 stsp free(base_buf);
1512 e0ab43e7 2018-03-16 stsp if (err) {
1513 e0ab43e7 2018-03-16 stsp free(accum_buf);
1514 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1515 e0ab43e7 2018-03-16 stsp *outlen = 0;
1516 e0ab43e7 2018-03-16 stsp } else {
1517 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1518 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1519 e0ab43e7 2018-03-16 stsp }
1520 efd2a263 2018-01-19 stsp return err;
1521 efd2a263 2018-01-19 stsp }
1522 efd2a263 2018-01-19 stsp
1523 3ee5fc21 2018-01-17 stsp const struct got_error *
1524 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1525 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1526 3ee5fc21 2018-01-17 stsp {
1527 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1528 3ee5fc21 2018-01-17 stsp
1529 8b2180d4 2018-04-26 stsp *f = NULL;
1530 8b2180d4 2018-04-26 stsp
1531 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1532 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1533 3ee5fc21 2018-01-17 stsp
1534 040bf4a1 2018-04-01 stsp *f = got_opentemp();
1535 040bf4a1 2018-04-01 stsp if (*f == NULL) {
1536 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1537 040bf4a1 2018-04-01 stsp goto done;
1538 ef2bccd9 2018-03-16 stsp }
1539 3ee5fc21 2018-01-17 stsp
1540 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1541 72eb3431 2018-04-01 stsp struct got_pack *pack;
1542 72eb3431 2018-04-01 stsp
1543 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1544 72eb3431 2018-04-01 stsp if (pack == NULL) {
1545 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1546 72eb3431 2018-04-01 stsp if (err)
1547 72eb3431 2018-04-01 stsp goto done;
1548 72eb3431 2018-04-01 stsp }
1549 72eb3431 2018-04-01 stsp
1550 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1551 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1552 0c048b15 2018-04-27 stsp goto done;
1553 0c048b15 2018-04-27 stsp }
1554 72eb3431 2018-04-01 stsp
1555 d7464085 2018-07-09 stsp if (pack->map) {
1556 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1557 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(&obj->size, pack->map,
1558 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff, *f);
1559 d7464085 2018-07-09 stsp } else {
1560 d7464085 2018-07-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1561 d7464085 2018-07-09 stsp err = got_error_from_errno();
1562 d7464085 2018-07-09 stsp goto done;
1563 d7464085 2018-07-09 stsp }
1564 d7464085 2018-07-09 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1565 d7464085 2018-07-09 stsp }
1566 040bf4a1 2018-04-01 stsp } else
1567 8b2180d4 2018-04-26 stsp err = dump_delta_chain_to_file(&obj->size,
1568 c336f889 2018-07-23 stsp &obj->deltas, obj->path_packfile, *f, repo);
1569 3ee5fc21 2018-01-17 stsp done:
1570 d0f3be7c 2018-03-17 stsp if (err && *f) {
1571 3ee5fc21 2018-01-17 stsp fclose(*f);
1572 d0f3be7c 2018-03-17 stsp *f = NULL;
1573 d0f3be7c 2018-03-17 stsp }
1574 a1fd68d8 2018-01-12 stsp return err;
1575 a1fd68d8 2018-01-12 stsp }
1576 e0ab43e7 2018-03-16 stsp
1577 e0ab43e7 2018-03-16 stsp const struct got_error *
1578 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1579 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1580 e0ab43e7 2018-03-16 stsp {
1581 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1582 e0ab43e7 2018-03-16 stsp
1583 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1584 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1585 e0ab43e7 2018-03-16 stsp
1586 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1587 72eb3431 2018-04-01 stsp struct got_pack *pack;
1588 72eb3431 2018-04-01 stsp
1589 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1590 72eb3431 2018-04-01 stsp if (pack == NULL) {
1591 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1592 72eb3431 2018-04-01 stsp if (err)
1593 72eb3431 2018-04-01 stsp goto done;
1594 72eb3431 2018-04-01 stsp }
1595 72eb3431 2018-04-01 stsp
1596 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1597 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1598 0c048b15 2018-04-27 stsp goto done;
1599 0c048b15 2018-04-27 stsp }
1600 d7464085 2018-07-09 stsp if (pack->map) {
1601 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1602 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(buf, len, pack->map,
1603 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
1604 d7464085 2018-07-09 stsp } else {
1605 d7464085 2018-07-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1606 d7464085 2018-07-09 stsp err = got_error_from_errno();
1607 d7464085 2018-07-09 stsp goto done;
1608 d7464085 2018-07-09 stsp }
1609 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1610 e0ab43e7 2018-03-16 stsp }
1611 e0ab43e7 2018-03-16 stsp } else
1612 c336f889 2018-07-23 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas,
1613 c336f889 2018-07-23 stsp obj->path_packfile, repo);
1614 e0ab43e7 2018-03-16 stsp done:
1615 e0ab43e7 2018-03-16 stsp return err;
1616 e0ab43e7 2018-03-16 stsp }