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