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