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