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