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