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