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