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