Blame


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