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 d7464085 2018-07-09 stsp if (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 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
894 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
895 a53d2f13 2018-03-17 stsp size_t delta_data_offset, 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 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
900 a53d2f13 2018-03-17 stsp delta_type, delta_size, 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 b0e2201a 2018-06-22 stsp err = add_delta(deltas, pack->path_packfile, delta_offset, tslen,
955 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
956 bdd2fbb3 2018-02-11 stsp if (err)
957 bdd2fbb3 2018-02-11 stsp return err;
958 bdd2fbb3 2018-02-11 stsp
959 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
960 b0e2201a 2018-06-22 stsp if (base_offset >= pack->filesize)
961 0c048b15 2018-04-27 stsp return got_error(GOT_ERR_PACK_OFFSET);
962 b107e67f 2018-01-19 stsp
963 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
964 d7464085 2018-07-09 stsp pack, base_offset);
965 b107e67f 2018-01-19 stsp if (err)
966 b107e67f 2018-01-19 stsp return err;
967 b107e67f 2018-01-19 stsp
968 b0e2201a 2018-06-22 stsp return resolve_delta_chain(deltas, repo, packidx, pack, base_offset,
969 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
970 c3703302 2018-01-23 stsp }
971 c3703302 2018-01-23 stsp
972 c3703302 2018-01-23 stsp static const struct got_error *
973 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
974 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack,
975 652b2703 2018-06-22 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
976 652b2703 2018-06-22 stsp unsigned int recursion)
977 c3703302 2018-01-23 stsp {
978 6b9c9673 2018-01-23 stsp const struct got_error *err;
979 6b9c9673 2018-01-23 stsp struct got_object_id id;
980 6b9c9673 2018-01-23 stsp int idx;
981 6b9c9673 2018-01-23 stsp off_t base_offset;
982 6b9c9673 2018-01-23 stsp uint8_t base_type;
983 6b9c9673 2018-01-23 stsp uint64_t base_size;
984 6b9c9673 2018-01-23 stsp size_t base_tslen;
985 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
986 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
987 a53d2f13 2018-03-17 stsp size_t delta_len;
988 6b9c9673 2018-01-23 stsp
989 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
990 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
991 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + sizeof(id);
992 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
993 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
994 bdd2fbb3 2018-02-11 stsp
995 d7464085 2018-07-09 stsp if (pack->map == NULL) {
996 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
997 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
998 d7464085 2018-07-09 stsp return got_error_from_errno();
999 d7464085 2018-07-09 stsp }
1000 a53d2f13 2018-03-17 stsp
1001 d7464085 2018-07-09 stsp
1002 d7464085 2018-07-09 stsp if (pack->map) {
1003 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_offset;
1004 d7464085 2018-07-09 stsp memcpy(&id, pack->map + mapoff, sizeof(id));
1005 d7464085 2018-07-09 stsp mapoff += sizeof(id);
1006 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
1007 d7464085 2018-07-09 stsp mapoff, pack->filesize - delta_data_offset);
1008 d7464085 2018-07-09 stsp if (err)
1009 d7464085 2018-07-09 stsp return err;
1010 d7464085 2018-07-09 stsp } else {
1011 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &id, sizeof(id));
1012 d7464085 2018-07-09 stsp if (n < 0)
1013 d7464085 2018-07-09 stsp return got_error_from_errno();
1014 d7464085 2018-07-09 stsp if (n != sizeof(id))
1015 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1016 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
1017 d7464085 2018-07-09 stsp if (err)
1018 d7464085 2018-07-09 stsp return err;
1019 d7464085 2018-07-09 stsp }
1020 d7464085 2018-07-09 stsp
1021 b0e2201a 2018-06-22 stsp err = add_delta(deltas, pack->path_packfile, delta_offset, tslen,
1022 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
1023 bdd2fbb3 2018-02-11 stsp if (err)
1024 bdd2fbb3 2018-02-11 stsp return err;
1025 bdd2fbb3 2018-02-11 stsp
1026 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
1027 652b2703 2018-06-22 stsp idx = get_object_idx(packidx, &id, repo);
1028 652b2703 2018-06-22 stsp if (idx == -1)
1029 652b2703 2018-06-22 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1030 6b9c9673 2018-01-23 stsp
1031 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
1032 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
1033 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1034 999f19f6 2018-03-17 stsp }
1035 6b9c9673 2018-01-23 stsp
1036 b0e2201a 2018-06-22 stsp if (base_offset >= pack->filesize) {
1037 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1038 0c048b15 2018-04-27 stsp goto done;
1039 0c048b15 2018-04-27 stsp }
1040 6b9c9673 2018-01-23 stsp
1041 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
1042 d7464085 2018-07-09 stsp pack, base_offset);
1043 6b9c9673 2018-01-23 stsp if (err)
1044 6b9c9673 2018-01-23 stsp goto done;
1045 6b9c9673 2018-01-23 stsp
1046 b0e2201a 2018-06-22 stsp err = resolve_delta_chain(deltas, repo, packidx, pack, base_offset,
1047 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
1048 6b9c9673 2018-01-23 stsp done:
1049 6b9c9673 2018-01-23 stsp return err;
1050 6b9c9673 2018-01-23 stsp }
1051 6b9c9673 2018-01-23 stsp
1052 6b9c9673 2018-01-23 stsp static const struct got_error *
1053 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
1054 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1055 b0e2201a 2018-06-22 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1056 6b9c9673 2018-01-23 stsp {
1057 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
1058 c3703302 2018-01-23 stsp
1059 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
1060 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
1061 5b7e13a7 2018-04-02 stsp
1062 c3703302 2018-01-23 stsp switch (delta_type) {
1063 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
1064 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
1065 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
1066 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
1067 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
1068 b0e2201a 2018-06-22 stsp err = add_delta(deltas, pack->path_packfile, delta_offset,
1069 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, 0, NULL, 0);
1070 4e8cda55 2018-01-19 stsp break;
1071 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1072 b0e2201a 2018-06-22 stsp err = resolve_offset_delta(deltas, repo, packidx, pack,
1073 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1074 96f5e8b3 2018-01-23 stsp break;
1075 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
1076 b0e2201a 2018-06-22 stsp err = resolve_ref_delta(deltas, repo, packidx, pack,
1077 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1078 6b9c9673 2018-01-23 stsp break;
1079 4e8cda55 2018-01-19 stsp default:
1080 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
1081 4e8cda55 2018-01-19 stsp }
1082 96f5e8b3 2018-01-23 stsp
1083 96f5e8b3 2018-01-23 stsp return err;
1084 96f5e8b3 2018-01-23 stsp }
1085 96f5e8b3 2018-01-23 stsp
1086 96f5e8b3 2018-01-23 stsp static const struct got_error *
1087 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
1088 b0e2201a 2018-06-22 stsp struct got_packidx *packidx, struct got_pack *pack,
1089 b0e2201a 2018-06-22 stsp struct got_object_id *id, off_t offset, size_t tslen,
1090 6fd11751 2018-06-04 stsp int delta_type, size_t delta_size)
1091 96f5e8b3 2018-01-23 stsp {
1092 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
1093 96f5e8b3 2018-01-23 stsp int resolved_type;
1094 4e8cda55 2018-01-19 stsp
1095 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
1096 b107e67f 2018-01-19 stsp if (*obj == NULL)
1097 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1098 b107e67f 2018-01-19 stsp
1099 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
1100 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
1101 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1102 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1103 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
1104 b107e67f 2018-01-19 stsp
1105 b0e2201a 2018-06-22 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
1106 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
1107 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1108 96f5e8b3 2018-01-23 stsp goto done;
1109 96f5e8b3 2018-01-23 stsp }
1110 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1111 96f5e8b3 2018-01-23 stsp
1112 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
1113 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1114 c3703302 2018-01-23 stsp
1115 b0e2201a 2018-06-22 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packidx, pack, offset,
1116 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
1117 96f5e8b3 2018-01-23 stsp if (err)
1118 96f5e8b3 2018-01-23 stsp goto done;
1119 96f5e8b3 2018-01-23 stsp
1120 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1121 96f5e8b3 2018-01-23 stsp if (err)
1122 96f5e8b3 2018-01-23 stsp goto done;
1123 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
1124 96f5e8b3 2018-01-23 stsp
1125 96f5e8b3 2018-01-23 stsp done:
1126 96f5e8b3 2018-01-23 stsp if (err) {
1127 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
1128 96f5e8b3 2018-01-23 stsp *obj = NULL;
1129 96f5e8b3 2018-01-23 stsp }
1130 96f5e8b3 2018-01-23 stsp return err;
1131 b107e67f 2018-01-19 stsp }
1132 b107e67f 2018-01-19 stsp
1133 b107e67f 2018-01-19 stsp static const struct got_error *
1134 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
1135 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1136 a1fd68d8 2018-01-12 stsp {
1137 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1138 a1fd68d8 2018-01-12 stsp off_t offset;
1139 65cf1e80 2018-03-16 stsp char *path_packfile;
1140 6d89869a 2018-03-17 stsp struct got_pack *pack;
1141 6c00b545 2018-01-17 stsp uint8_t type;
1142 6c00b545 2018-01-17 stsp uint64_t size;
1143 3ee5fc21 2018-01-17 stsp size_t tslen;
1144 a1fd68d8 2018-01-12 stsp
1145 6c00b545 2018-01-17 stsp *obj = NULL;
1146 a1fd68d8 2018-01-12 stsp
1147 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
1148 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
1149 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1150 a1fd68d8 2018-01-12 stsp
1151 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_packfile, repo, packidx);
1152 6b9c9673 2018-01-23 stsp if (err)
1153 6b9c9673 2018-01-23 stsp return err;
1154 a1fd68d8 2018-01-12 stsp
1155 6d89869a 2018-03-17 stsp pack = get_cached_pack(path_packfile, repo);
1156 6d89869a 2018-03-17 stsp if (pack == NULL) {
1157 6d89869a 2018-03-17 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
1158 6d89869a 2018-03-17 stsp if (err)
1159 6d89869a 2018-03-17 stsp goto done;
1160 6d89869a 2018-03-17 stsp }
1161 7e656b93 2018-03-17 stsp
1162 d7464085 2018-07-09 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
1163 a1fd68d8 2018-01-12 stsp if (err)
1164 a1fd68d8 2018-01-12 stsp goto done;
1165 a1fd68d8 2018-01-12 stsp
1166 6c00b545 2018-01-17 stsp switch (type) {
1167 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1168 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1169 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1170 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1171 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
1172 6ccb713b 2018-01-19 stsp offset + tslen, size);
1173 6c00b545 2018-01-17 stsp break;
1174 6ccb713b 2018-01-19 stsp
1175 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1176 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1177 b0e2201a 2018-06-22 stsp err = open_delta_object(obj, repo, packidx, pack, id, offset,
1178 b0e2201a 2018-06-22 stsp tslen, type, size);
1179 b107e67f 2018-01-19 stsp break;
1180 b107e67f 2018-01-19 stsp
1181 6c00b545 2018-01-17 stsp default:
1182 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1183 6c00b545 2018-01-17 stsp goto done;
1184 6c00b545 2018-01-17 stsp }
1185 a1fd68d8 2018-01-12 stsp done:
1186 65cf1e80 2018-03-16 stsp free(path_packfile);
1187 a1fd68d8 2018-01-12 stsp return err;
1188 a1fd68d8 2018-01-12 stsp }
1189 a1fd68d8 2018-01-12 stsp
1190 a1fd68d8 2018-01-12 stsp const struct got_error *
1191 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1192 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1193 a1fd68d8 2018-01-12 stsp {
1194 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1195 6fd11751 2018-06-04 stsp struct got_packidx *packidx = NULL;
1196 6b9c9673 2018-01-23 stsp int idx;
1197 a1fd68d8 2018-01-12 stsp
1198 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1199 6b9c9673 2018-01-23 stsp if (err)
1200 6b9c9673 2018-01-23 stsp return err;
1201 a1fd68d8 2018-01-12 stsp
1202 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
1203 7e656b93 2018-03-17 stsp if (err)
1204 7e656b93 2018-03-17 stsp return err;
1205 7e656b93 2018-03-17 stsp
1206 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1207 3ee5fc21 2018-01-17 stsp return err;
1208 3ee5fc21 2018-01-17 stsp }
1209 efd2a263 2018-01-19 stsp
1210 efd2a263 2018-01-19 stsp static const struct got_error *
1211 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1212 22484865 2018-03-13 stsp {
1213 22484865 2018-03-13 stsp struct got_delta *delta;
1214 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1215 22484865 2018-03-13 stsp
1216 22484865 2018-03-13 stsp *max_size = 0;
1217 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1218 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1219 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1220 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1221 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1222 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1223 22484865 2018-03-13 stsp const struct got_error *err;
1224 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1225 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1226 22484865 2018-03-13 stsp if (err)
1227 22484865 2018-03-13 stsp return err;
1228 22484865 2018-03-13 stsp } else
1229 22484865 2018-03-13 stsp base_size = delta->size;
1230 22484865 2018-03-13 stsp if (base_size > *max_size)
1231 22484865 2018-03-13 stsp *max_size = base_size;
1232 22484865 2018-03-13 stsp if (result_size > *max_size)
1233 22484865 2018-03-13 stsp *max_size = result_size;
1234 22484865 2018-03-13 stsp }
1235 bd1223b9 2018-03-14 stsp
1236 9feb4ff2 2018-03-14 stsp return NULL;
1237 22484865 2018-03-13 stsp }
1238 22484865 2018-03-13 stsp
1239 22484865 2018-03-13 stsp static const struct got_error *
1240 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1241 72eb3431 2018-04-01 stsp FILE *outfile, struct got_repository *repo)
1242 efd2a263 2018-01-19 stsp {
1243 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1244 6691714a 2018-01-23 stsp struct got_delta *delta;
1245 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1246 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1247 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1248 22484865 2018-03-13 stsp uint64_t max_size;
1249 6691714a 2018-01-23 stsp int n = 0;
1250 b29656e2 2018-03-16 stsp
1251 b29656e2 2018-03-16 stsp *result_size = 0;
1252 3ee5fc21 2018-01-17 stsp
1253 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1254 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1255 efd2a263 2018-01-19 stsp
1256 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1257 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1258 22484865 2018-03-13 stsp if (err)
1259 22484865 2018-03-13 stsp return err;
1260 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1261 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1262 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1263 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1264 8628c62d 2018-03-15 stsp } else {
1265 22484865 2018-03-13 stsp base_file = got_opentemp();
1266 8628c62d 2018-03-15 stsp if (base_file == NULL)
1267 8628c62d 2018-03-15 stsp return got_error_from_errno();
1268 efd2a263 2018-01-19 stsp
1269 22484865 2018-03-13 stsp accum_file = got_opentemp();
1270 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1271 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1272 8628c62d 2018-03-15 stsp fclose(base_file);
1273 8628c62d 2018-03-15 stsp return err;
1274 8628c62d 2018-03-15 stsp }
1275 6691714a 2018-01-23 stsp }
1276 efd2a263 2018-01-19 stsp
1277 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1278 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1279 a6b158cc 2018-02-11 stsp if (n == 0) {
1280 72eb3431 2018-04-01 stsp struct got_pack *pack;
1281 d7464085 2018-07-09 stsp size_t base_len, mapoff;
1282 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1283 9db65d41 2018-03-14 stsp
1284 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1285 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1286 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1287 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1288 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1289 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1290 72eb3431 2018-04-01 stsp goto done;
1291 72eb3431 2018-04-01 stsp }
1292 72eb3431 2018-04-01 stsp
1293 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1294 72eb3431 2018-04-01 stsp if (pack == NULL) {
1295 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1296 a6b158cc 2018-02-11 stsp goto done;
1297 a6b158cc 2018-02-11 stsp }
1298 6691714a 2018-01-23 stsp
1299 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1300 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1301 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1302 0c048b15 2018-04-27 stsp goto done;
1303 0c048b15 2018-04-27 stsp }
1304 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1305 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1306 d7464085 2018-07-09 stsp == -1) {
1307 d7464085 2018-07-09 stsp err = got_error_from_errno();
1308 d7464085 2018-07-09 stsp goto done;
1309 d7464085 2018-07-09 stsp }
1310 bdd2fbb3 2018-02-11 stsp }
1311 d7464085 2018-07-09 stsp if (base_file) {
1312 d7464085 2018-07-09 stsp if (pack->map) {
1313 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1314 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1315 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1316 d7464085 2018-07-09 stsp pack->filesize - mapoff, base_file);
1317 d7464085 2018-07-09 stsp } else
1318 d7464085 2018-07-09 stsp err = got_inflate_to_file_fd(&base_len,
1319 d7464085 2018-07-09 stsp pack->fd, base_file);
1320 d7464085 2018-07-09 stsp } else {
1321 d7464085 2018-07-09 stsp if (pack->map) {
1322 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1323 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1324 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1325 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1326 d7464085 2018-07-09 stsp } else
1327 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1328 d7464085 2018-07-09 stsp &base_len, pack->fd);
1329 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1330 8628c62d 2018-03-15 stsp uint8_t *p;
1331 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1332 8628c62d 2018-03-15 stsp if (p == NULL) {
1333 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1334 8628c62d 2018-03-15 stsp goto done;
1335 8628c62d 2018-03-15 stsp }
1336 8628c62d 2018-03-15 stsp base_buf = p;
1337 8628c62d 2018-03-15 stsp }
1338 8628c62d 2018-03-15 stsp }
1339 a6b158cc 2018-02-11 stsp if (err)
1340 a6b158cc 2018-02-11 stsp goto done;
1341 a6b158cc 2018-02-11 stsp n++;
1342 8628c62d 2018-03-15 stsp if (base_file)
1343 8628c62d 2018-03-15 stsp rewind(base_file);
1344 a6b158cc 2018-02-11 stsp continue;
1345 9db65d41 2018-03-14 stsp }
1346 885d3e02 2018-01-27 stsp
1347 8628c62d 2018-03-15 stsp if (base_buf) {
1348 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1349 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1350 8628c62d 2018-03-15 stsp n++;
1351 8628c62d 2018-03-15 stsp } else {
1352 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1353 a53d2f13 2018-03-17 stsp delta->delta_len,
1354 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1355 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1356 b29656e2 2018-03-16 stsp &accum_size);
1357 8628c62d 2018-03-15 stsp }
1358 6691714a 2018-01-23 stsp if (err)
1359 6691714a 2018-01-23 stsp goto done;
1360 6691714a 2018-01-23 stsp
1361 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1362 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1363 8628c62d 2018-03-15 stsp if (base_buf) {
1364 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1365 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1366 8628c62d 2018-03-15 stsp base_buf = tmp;
1367 8628c62d 2018-03-15 stsp } else {
1368 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1369 8628c62d 2018-03-15 stsp accum_file = base_file;
1370 8628c62d 2018-03-15 stsp base_file = tmp;
1371 8628c62d 2018-03-15 stsp rewind(base_file);
1372 8628c62d 2018-03-15 stsp rewind(accum_file);
1373 8628c62d 2018-03-15 stsp }
1374 6691714a 2018-01-23 stsp }
1375 6691714a 2018-01-23 stsp }
1376 6691714a 2018-01-23 stsp
1377 6691714a 2018-01-23 stsp done:
1378 8628c62d 2018-03-15 stsp free(base_buf);
1379 8628c62d 2018-03-15 stsp if (accum_buf) {
1380 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1381 8628c62d 2018-03-15 stsp free(accum_buf);
1382 8628c62d 2018-03-15 stsp if (len != accum_size)
1383 8628c62d 2018-03-15 stsp return got_ferror(outfile, GOT_ERR_IO);
1384 8628c62d 2018-03-15 stsp }
1385 8628c62d 2018-03-15 stsp if (base_file)
1386 8628c62d 2018-03-15 stsp fclose(base_file);
1387 8628c62d 2018-03-15 stsp if (accum_file)
1388 8628c62d 2018-03-15 stsp fclose(accum_file);
1389 6691714a 2018-01-23 stsp rewind(outfile);
1390 b29656e2 2018-03-16 stsp if (err == NULL)
1391 b29656e2 2018-03-16 stsp *result_size = accum_size;
1392 e0ab43e7 2018-03-16 stsp return err;
1393 e0ab43e7 2018-03-16 stsp }
1394 e0ab43e7 2018-03-16 stsp
1395 e0ab43e7 2018-03-16 stsp static const struct got_error *
1396 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1397 72eb3431 2018-04-01 stsp struct got_delta_chain *deltas, struct got_repository *repo)
1398 e0ab43e7 2018-03-16 stsp {
1399 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1400 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1401 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1402 e0ab43e7 2018-03-16 stsp size_t accum_size;
1403 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1404 e0ab43e7 2018-03-16 stsp int n = 0;
1405 e0ab43e7 2018-03-16 stsp
1406 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1407 e0ab43e7 2018-03-16 stsp *outlen = 0;
1408 e0ab43e7 2018-03-16 stsp
1409 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1410 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1411 e0ab43e7 2018-03-16 stsp
1412 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1413 e0ab43e7 2018-03-16 stsp if (err)
1414 e0ab43e7 2018-03-16 stsp return err;
1415 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1416 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1417 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1418 e0ab43e7 2018-03-16 stsp
1419 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1420 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1421 e0ab43e7 2018-03-16 stsp if (n == 0) {
1422 72eb3431 2018-04-01 stsp struct got_pack *pack;
1423 e0ab43e7 2018-03-16 stsp size_t base_len;
1424 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1425 e0ab43e7 2018-03-16 stsp
1426 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1427 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1428 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1429 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1430 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1431 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1432 72eb3431 2018-04-01 stsp goto done;
1433 72eb3431 2018-04-01 stsp }
1434 72eb3431 2018-04-01 stsp
1435 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1436 72eb3431 2018-04-01 stsp if (pack == NULL) {
1437 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1438 e0ab43e7 2018-03-16 stsp goto done;
1439 e0ab43e7 2018-03-16 stsp }
1440 e0ab43e7 2018-03-16 stsp
1441 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1442 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1443 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1444 0c048b15 2018-04-27 stsp goto done;
1445 0c048b15 2018-04-27 stsp }
1446 d7464085 2018-07-09 stsp if (pack->map) {
1447 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1448 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1449 d7464085 2018-07-09 stsp &base_len, pack->map, mapoff,
1450 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1451 d7464085 2018-07-09 stsp } else {
1452 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1453 d7464085 2018-07-09 stsp == -1) {
1454 d7464085 2018-07-09 stsp err = got_error_from_errno();
1455 d7464085 2018-07-09 stsp goto done;
1456 d7464085 2018-07-09 stsp }
1457 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1458 d7464085 2018-07-09 stsp &base_len, pack->fd);
1459 e0ab43e7 2018-03-16 stsp }
1460 d7464085 2018-07-09 stsp if (err)
1461 d7464085 2018-07-09 stsp goto done;
1462 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1463 e0ab43e7 2018-03-16 stsp uint8_t *p;
1464 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1465 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1466 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1467 e0ab43e7 2018-03-16 stsp goto done;
1468 e0ab43e7 2018-03-16 stsp }
1469 e0ab43e7 2018-03-16 stsp base_buf = p;
1470 e0ab43e7 2018-03-16 stsp }
1471 e0ab43e7 2018-03-16 stsp n++;
1472 e0ab43e7 2018-03-16 stsp continue;
1473 e0ab43e7 2018-03-16 stsp }
1474 e0ab43e7 2018-03-16 stsp
1475 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1476 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1477 e0ab43e7 2018-03-16 stsp n++;
1478 e0ab43e7 2018-03-16 stsp if (err)
1479 e0ab43e7 2018-03-16 stsp goto done;
1480 e0ab43e7 2018-03-16 stsp
1481 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1482 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1483 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1484 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1485 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1486 e0ab43e7 2018-03-16 stsp }
1487 e0ab43e7 2018-03-16 stsp }
1488 e0ab43e7 2018-03-16 stsp
1489 e0ab43e7 2018-03-16 stsp done:
1490 e0ab43e7 2018-03-16 stsp free(base_buf);
1491 e0ab43e7 2018-03-16 stsp if (err) {
1492 e0ab43e7 2018-03-16 stsp free(accum_buf);
1493 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1494 e0ab43e7 2018-03-16 stsp *outlen = 0;
1495 e0ab43e7 2018-03-16 stsp } else {
1496 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1497 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1498 e0ab43e7 2018-03-16 stsp }
1499 efd2a263 2018-01-19 stsp return err;
1500 efd2a263 2018-01-19 stsp }
1501 efd2a263 2018-01-19 stsp
1502 3ee5fc21 2018-01-17 stsp const struct got_error *
1503 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1504 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1505 3ee5fc21 2018-01-17 stsp {
1506 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1507 3ee5fc21 2018-01-17 stsp
1508 8b2180d4 2018-04-26 stsp *f = NULL;
1509 8b2180d4 2018-04-26 stsp
1510 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1511 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1512 3ee5fc21 2018-01-17 stsp
1513 040bf4a1 2018-04-01 stsp *f = got_opentemp();
1514 040bf4a1 2018-04-01 stsp if (*f == NULL) {
1515 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1516 040bf4a1 2018-04-01 stsp goto done;
1517 ef2bccd9 2018-03-16 stsp }
1518 3ee5fc21 2018-01-17 stsp
1519 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1520 72eb3431 2018-04-01 stsp struct got_pack *pack;
1521 72eb3431 2018-04-01 stsp
1522 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1523 72eb3431 2018-04-01 stsp if (pack == NULL) {
1524 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1525 72eb3431 2018-04-01 stsp if (err)
1526 72eb3431 2018-04-01 stsp goto done;
1527 72eb3431 2018-04-01 stsp }
1528 72eb3431 2018-04-01 stsp
1529 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1530 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1531 0c048b15 2018-04-27 stsp goto done;
1532 0c048b15 2018-04-27 stsp }
1533 72eb3431 2018-04-01 stsp
1534 d7464085 2018-07-09 stsp if (pack->map) {
1535 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1536 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(&obj->size, pack->map,
1537 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff, *f);
1538 d7464085 2018-07-09 stsp } else {
1539 d7464085 2018-07-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1540 d7464085 2018-07-09 stsp err = got_error_from_errno();
1541 d7464085 2018-07-09 stsp goto done;
1542 d7464085 2018-07-09 stsp }
1543 d7464085 2018-07-09 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd, *f);
1544 d7464085 2018-07-09 stsp }
1545 040bf4a1 2018-04-01 stsp } else
1546 8b2180d4 2018-04-26 stsp err = dump_delta_chain_to_file(&obj->size,
1547 8b2180d4 2018-04-26 stsp &obj->deltas, *f, repo);
1548 3ee5fc21 2018-01-17 stsp done:
1549 d0f3be7c 2018-03-17 stsp if (err && *f) {
1550 3ee5fc21 2018-01-17 stsp fclose(*f);
1551 d0f3be7c 2018-03-17 stsp *f = NULL;
1552 d0f3be7c 2018-03-17 stsp }
1553 a1fd68d8 2018-01-12 stsp return err;
1554 a1fd68d8 2018-01-12 stsp }
1555 e0ab43e7 2018-03-16 stsp
1556 e0ab43e7 2018-03-16 stsp const struct got_error *
1557 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1558 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1559 e0ab43e7 2018-03-16 stsp {
1560 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1561 e0ab43e7 2018-03-16 stsp
1562 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1563 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1564 e0ab43e7 2018-03-16 stsp
1565 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1566 72eb3431 2018-04-01 stsp struct got_pack *pack;
1567 72eb3431 2018-04-01 stsp
1568 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1569 72eb3431 2018-04-01 stsp if (pack == NULL) {
1570 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1571 72eb3431 2018-04-01 stsp if (err)
1572 72eb3431 2018-04-01 stsp goto done;
1573 72eb3431 2018-04-01 stsp }
1574 72eb3431 2018-04-01 stsp
1575 0c048b15 2018-04-27 stsp if (obj->pack_offset >= pack->filesize) {
1576 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1577 0c048b15 2018-04-27 stsp goto done;
1578 0c048b15 2018-04-27 stsp }
1579 d7464085 2018-07-09 stsp if (pack->map) {
1580 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1581 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(buf, len, pack->map,
1582 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
1583 d7464085 2018-07-09 stsp } else {
1584 d7464085 2018-07-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1) {
1585 d7464085 2018-07-09 stsp err = got_error_from_errno();
1586 d7464085 2018-07-09 stsp goto done;
1587 d7464085 2018-07-09 stsp }
1588 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1589 e0ab43e7 2018-03-16 stsp }
1590 e0ab43e7 2018-03-16 stsp } else
1591 72eb3431 2018-04-01 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, repo);
1592 e0ab43e7 2018-03-16 stsp done:
1593 e0ab43e7 2018-03-16 stsp return err;
1594 e0ab43e7 2018-03-16 stsp }