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 a1fd68d8 2018-01-12 stsp #include <errno.h>
23 0a0a3048 2018-01-10 stsp #include <stdio.h>
24 a1fd68d8 2018-01-12 stsp #include <stdint.h>
25 0a0a3048 2018-01-10 stsp #include <stdlib.h>
26 0a0a3048 2018-01-10 stsp #include <string.h>
27 0a0a3048 2018-01-10 stsp #include <limits.h>
28 0a0a3048 2018-01-10 stsp #include <sha1.h>
29 0a0a3048 2018-01-10 stsp #include <endian.h>
30 a1fd68d8 2018-01-12 stsp #include <zlib.h>
31 0a0a3048 2018-01-10 stsp
32 0a0a3048 2018-01-10 stsp #include "got_error.h"
33 a1fd68d8 2018-01-12 stsp #include "got_object.h"
34 a1fd68d8 2018-01-12 stsp #include "got_repository.h"
35 a1fd68d8 2018-01-12 stsp #include "got_sha1.h"
36 0a0a3048 2018-01-10 stsp #include "pack.h"
37 a1fd68d8 2018-01-12 stsp #include "path.h"
38 efd2a263 2018-01-19 stsp #include "delta.h"
39 eef6493a 2018-01-19 stsp #include "object.h"
40 0a0a3048 2018-01-10 stsp
41 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
42 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
43 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
44 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
45 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
46 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
47 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
48 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
49 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
50 a1fd68d8 2018-01-12 stsp
51 a1fd68d8 2018-01-12 stsp #ifndef MIN
52 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 a1fd68d8 2018-01-12 stsp #endif
54 a1fd68d8 2018-01-12 stsp
55 0a0a3048 2018-01-10 stsp static const struct got_error *
56 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
57 0a0a3048 2018-01-10 stsp {
58 0a0a3048 2018-01-10 stsp int i;
59 0a0a3048 2018-01-10 stsp
60 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
61 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
62 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
63 0a0a3048 2018-01-10 stsp }
64 0a0a3048 2018-01-10 stsp
65 0a0a3048 2018-01-10 stsp return NULL;
66 0a0a3048 2018-01-10 stsp }
67 0a0a3048 2018-01-10 stsp
68 24541888 2018-01-10 stsp static const struct got_error *
69 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
70 0a0a3048 2018-01-10 stsp {
71 0a0a3048 2018-01-10 stsp struct stat sb;
72 0a0a3048 2018-01-10 stsp char *path_pack;
73 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
74 0a0a3048 2018-01-10 stsp char *dot;
75 0a0a3048 2018-01-10 stsp
76 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
77 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
78 0a0a3048 2018-01-10 stsp
79 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
80 0a0a3048 2018-01-10 stsp if (dot == NULL)
81 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
82 0a0a3048 2018-01-10 stsp *dot = '\0';
83 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
84 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_MEM);
85 0a0a3048 2018-01-10 stsp
86 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
87 0a0a3048 2018-01-10 stsp free(path_pack);
88 8251fdbc 2018-01-12 stsp return got_error_from_errno();
89 0a0a3048 2018-01-10 stsp }
90 0a0a3048 2018-01-10 stsp
91 0a0a3048 2018-01-10 stsp free(path_pack);
92 0a0a3048 2018-01-10 stsp *size = sb.st_size;
93 0a0a3048 2018-01-10 stsp return 0;
94 0a0a3048 2018-01-10 stsp }
95 0a0a3048 2018-01-10 stsp
96 0a0a3048 2018-01-10 stsp const struct got_error *
97 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
98 0a0a3048 2018-01-10 stsp {
99 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
100 0a0a3048 2018-01-10 stsp FILE *f;
101 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
102 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
103 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
104 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
105 0a0a3048 2018-01-10 stsp
106 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
107 0ebaf008 2018-01-10 stsp
108 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
109 0a0a3048 2018-01-10 stsp if (f == NULL)
110 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
111 0a0a3048 2018-01-10 stsp
112 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
113 0a0a3048 2018-01-10 stsp if (err)
114 0a0a3048 2018-01-10 stsp return err;
115 0a0a3048 2018-01-10 stsp
116 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
117 0a0a3048 2018-01-10 stsp if (p == NULL) {
118 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
119 0a0a3048 2018-01-10 stsp goto done;
120 0a0a3048 2018-01-10 stsp }
121 0a0a3048 2018-01-10 stsp
122 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
123 0a0a3048 2018-01-10 stsp if (n != 1) {
124 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
125 0a0a3048 2018-01-10 stsp goto done;
126 0a0a3048 2018-01-10 stsp }
127 0a0a3048 2018-01-10 stsp
128 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
129 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
130 0a0a3048 2018-01-10 stsp goto done;
131 0a0a3048 2018-01-10 stsp }
132 0a0a3048 2018-01-10 stsp
133 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
134 0ebaf008 2018-01-10 stsp
135 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
136 0a0a3048 2018-01-10 stsp if (n != 1) {
137 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
138 0a0a3048 2018-01-10 stsp goto done;
139 0a0a3048 2018-01-10 stsp }
140 0a0a3048 2018-01-10 stsp
141 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
142 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
143 0a0a3048 2018-01-10 stsp goto done;
144 0a0a3048 2018-01-10 stsp }
145 0a0a3048 2018-01-10 stsp
146 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
147 0ebaf008 2018-01-10 stsp
148 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
149 0a0a3048 2018-01-10 stsp if (n != 1) {
150 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
151 0a0a3048 2018-01-10 stsp goto done;
152 0a0a3048 2018-01-10 stsp }
153 0a0a3048 2018-01-10 stsp
154 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
155 0a0a3048 2018-01-10 stsp if (err)
156 0a0a3048 2018-01-10 stsp goto done;
157 0a0a3048 2018-01-10 stsp
158 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
159 0ebaf008 2018-01-10 stsp
160 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
161 0a0a3048 2018-01-10 stsp
162 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
163 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
164 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
165 0a0a3048 2018-01-10 stsp goto done;
166 0a0a3048 2018-01-10 stsp }
167 0a0a3048 2018-01-10 stsp
168 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
169 0a0a3048 2018-01-10 stsp if (n != nobj) {
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 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
175 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
176 0ebaf008 2018-01-10 stsp
177 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
178 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
179 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
180 0a0a3048 2018-01-10 stsp goto done;
181 0a0a3048 2018-01-10 stsp }
182 0a0a3048 2018-01-10 stsp
183 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
184 0a0a3048 2018-01-10 stsp if (n != nobj) {
185 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
186 0a0a3048 2018-01-10 stsp goto done;
187 0a0a3048 2018-01-10 stsp }
188 0a0a3048 2018-01-10 stsp
189 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
190 0ebaf008 2018-01-10 stsp
191 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
192 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
193 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
194 0a0a3048 2018-01-10 stsp goto done;
195 0a0a3048 2018-01-10 stsp }
196 0a0a3048 2018-01-10 stsp
197 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
198 0a0a3048 2018-01-10 stsp if (n != nobj) {
199 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
200 0a0a3048 2018-01-10 stsp goto done;
201 0a0a3048 2018-01-10 stsp }
202 0a0a3048 2018-01-10 stsp
203 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
204 0ebaf008 2018-01-10 stsp
205 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
206 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
207 0a0a3048 2018-01-10 stsp goto checksum;
208 0a0a3048 2018-01-10 stsp
209 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
210 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
211 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
212 0a0a3048 2018-01-10 stsp goto done;
213 0a0a3048 2018-01-10 stsp }
214 0a0a3048 2018-01-10 stsp
215 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
216 0a0a3048 2018-01-10 stsp if (n != nobj) {
217 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
218 0a0a3048 2018-01-10 stsp goto done;
219 0a0a3048 2018-01-10 stsp }
220 0a0a3048 2018-01-10 stsp
221 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
222 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
223 0ebaf008 2018-01-10 stsp
224 0a0a3048 2018-01-10 stsp checksum:
225 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
226 0a0a3048 2018-01-10 stsp if (n != 1) {
227 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
228 0a0a3048 2018-01-10 stsp goto done;
229 0a0a3048 2018-01-10 stsp }
230 0a0a3048 2018-01-10 stsp
231 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
232 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
233 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
234 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
235 0a0a3048 2018-01-10 stsp done:
236 0a0a3048 2018-01-10 stsp fclose(f);
237 0a0a3048 2018-01-10 stsp if (err)
238 0a0a3048 2018-01-10 stsp got_packidx_close(p);
239 0a0a3048 2018-01-10 stsp else
240 0a0a3048 2018-01-10 stsp *packidx = p;
241 0a0a3048 2018-01-10 stsp return err;
242 0a0a3048 2018-01-10 stsp }
243 0a0a3048 2018-01-10 stsp
244 0a0a3048 2018-01-10 stsp void
245 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
246 0a0a3048 2018-01-10 stsp {
247 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
248 0a0a3048 2018-01-10 stsp free(packidx->offsets);
249 0a0a3048 2018-01-10 stsp free(packidx->crc32);
250 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
251 0a0a3048 2018-01-10 stsp free(packidx);
252 a1fd68d8 2018-01-12 stsp }
253 a1fd68d8 2018-01-12 stsp
254 a1fd68d8 2018-01-12 stsp static int
255 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
256 a1fd68d8 2018-01-12 stsp {
257 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
258 a1fd68d8 2018-01-12 stsp return 0;
259 a1fd68d8 2018-01-12 stsp
260 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
261 a1fd68d8 2018-01-12 stsp return 0;
262 a1fd68d8 2018-01-12 stsp
263 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
264 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
265 a1fd68d8 2018-01-12 stsp return 0;
266 a1fd68d8 2018-01-12 stsp
267 a1fd68d8 2018-01-12 stsp return 1;
268 a1fd68d8 2018-01-12 stsp }
269 a1fd68d8 2018-01-12 stsp
270 a1fd68d8 2018-01-12 stsp static off_t
271 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
272 a1fd68d8 2018-01-12 stsp {
273 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
274 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
275 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
276 a1fd68d8 2018-01-12 stsp uint64_t loffset;
277 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
278 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
279 a1fd68d8 2018-01-12 stsp return -1;
280 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
281 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
282 a1fd68d8 2018-01-12 stsp }
283 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
284 a1fd68d8 2018-01-12 stsp }
285 a1fd68d8 2018-01-12 stsp
286 a1fd68d8 2018-01-12 stsp static int
287 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
288 a1fd68d8 2018-01-12 stsp {
289 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
290 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
291 a1fd68d8 2018-01-12 stsp int i = 0;
292 a1fd68d8 2018-01-12 stsp
293 a1fd68d8 2018-01-12 stsp if (id0 > 0)
294 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
295 a1fd68d8 2018-01-12 stsp
296 a1fd68d8 2018-01-12 stsp while (i < totobj) {
297 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
298 a1fd68d8 2018-01-12 stsp uint32_t offset;
299 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
300 a1fd68d8 2018-01-12 stsp
301 6c00b545 2018-01-17 stsp if (cmp == 0)
302 6c00b545 2018-01-17 stsp return i;
303 6c00b545 2018-01-17 stsp else if (cmp > 0)
304 a1fd68d8 2018-01-12 stsp break;
305 6c00b545 2018-01-17 stsp i++;
306 a1fd68d8 2018-01-12 stsp }
307 a1fd68d8 2018-01-12 stsp
308 a1fd68d8 2018-01-12 stsp return -1;
309 6b9c9673 2018-01-23 stsp }
310 6b9c9673 2018-01-23 stsp
311 6b9c9673 2018-01-23 stsp static const struct got_error *
312 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
313 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
314 6b9c9673 2018-01-23 stsp {
315 6b9c9673 2018-01-23 stsp const struct got_error *err;
316 6b9c9673 2018-01-23 stsp char *path_packdir;
317 6b9c9673 2018-01-23 stsp DIR *packdir;
318 6b9c9673 2018-01-23 stsp struct dirent *dent;
319 6b9c9673 2018-01-23 stsp char *path_packidx;
320 6b9c9673 2018-01-23 stsp
321 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
322 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
323 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
324 6b9c9673 2018-01-23 stsp
325 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
326 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
327 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
328 6b9c9673 2018-01-23 stsp goto done;
329 6b9c9673 2018-01-23 stsp }
330 6b9c9673 2018-01-23 stsp
331 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
332 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
333 6b9c9673 2018-01-23 stsp continue;
334 6b9c9673 2018-01-23 stsp
335 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
336 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
337 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
338 6b9c9673 2018-01-23 stsp goto done;
339 6b9c9673 2018-01-23 stsp }
340 6b9c9673 2018-01-23 stsp
341 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
342 6b9c9673 2018-01-23 stsp free(path_packidx);
343 6b9c9673 2018-01-23 stsp if (err)
344 6b9c9673 2018-01-23 stsp goto done;
345 6b9c9673 2018-01-23 stsp
346 6b9c9673 2018-01-23 stsp *idx = get_object_idx(*packidx, id);
347 6b9c9673 2018-01-23 stsp if (*idx != -1) {
348 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
349 6b9c9673 2018-01-23 stsp goto done;
350 6b9c9673 2018-01-23 stsp }
351 6b9c9673 2018-01-23 stsp
352 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
353 6b9c9673 2018-01-23 stsp *packidx = NULL;
354 6b9c9673 2018-01-23 stsp }
355 6b9c9673 2018-01-23 stsp
356 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
357 6b9c9673 2018-01-23 stsp done:
358 6b9c9673 2018-01-23 stsp free(path_packdir);
359 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
360 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
361 6b9c9673 2018-01-23 stsp return err;
362 6b9c9673 2018-01-23 stsp }
363 6b9c9673 2018-01-23 stsp
364 c7fe698a 2018-01-23 stsp static const struct got_error *
365 6b9c9673 2018-01-23 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
366 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx)
367 6b9c9673 2018-01-23 stsp {
368 6b9c9673 2018-01-23 stsp char *path_packdir;
369 6b9c9673 2018-01-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
370 6b9c9673 2018-01-23 stsp char *sha1str;
371 6b9c9673 2018-01-23 stsp char *path_packidx;
372 6b9c9673 2018-01-23 stsp
373 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
374 6b9c9673 2018-01-23 stsp
375 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
376 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
377 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
378 6b9c9673 2018-01-23 stsp
379 6b9c9673 2018-01-23 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
380 6b9c9673 2018-01-23 stsp hex, sizeof(hex));
381 6b9c9673 2018-01-23 stsp if (sha1str == NULL)
382 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
383 6b9c9673 2018-01-23 stsp
384 6b9c9673 2018-01-23 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
385 6b9c9673 2018-01-23 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
386 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
387 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
388 6b9c9673 2018-01-23 stsp }
389 6b9c9673 2018-01-23 stsp
390 6b9c9673 2018-01-23 stsp return NULL;
391 a1fd68d8 2018-01-12 stsp }
392 a1fd68d8 2018-01-12 stsp
393 a1fd68d8 2018-01-12 stsp const struct got_error *
394 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
395 a1fd68d8 2018-01-12 stsp {
396 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
397 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
398 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
399 a1fd68d8 2018-01-12 stsp size_t n;
400 a1fd68d8 2018-01-12 stsp
401 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
402 a1fd68d8 2018-01-12 stsp if (n != 1)
403 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
404 a1fd68d8 2018-01-12 stsp
405 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
406 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
407 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
408 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
409 a1fd68d8 2018-01-12 stsp
410 a1fd68d8 2018-01-12 stsp return err;
411 a487c1d0 2018-01-14 stsp }
412 a487c1d0 2018-01-14 stsp
413 a487c1d0 2018-01-14 stsp static const struct got_error *
414 c7fe698a 2018-01-23 stsp open_packfile(FILE **packfile, char **path_packfile,
415 c7fe698a 2018-01-23 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
416 c7fe698a 2018-01-23 stsp {
417 c7fe698a 2018-01-23 stsp const struct got_error *err;
418 c7fe698a 2018-01-23 stsp
419 c7fe698a 2018-01-23 stsp *packfile = NULL;
420 c7fe698a 2018-01-23 stsp
421 c7fe698a 2018-01-23 stsp err = get_packfile_path(path_packfile, repo, packidx);
422 c7fe698a 2018-01-23 stsp if (err)
423 c7fe698a 2018-01-23 stsp return err;
424 c7fe698a 2018-01-23 stsp
425 c7fe698a 2018-01-23 stsp *packfile = fopen(*path_packfile, "rb");
426 c7fe698a 2018-01-23 stsp if (*packfile == NULL) {
427 c7fe698a 2018-01-23 stsp err = got_error_from_errno();
428 c7fe698a 2018-01-23 stsp free(*path_packfile);
429 c7fe698a 2018-01-23 stsp return err;
430 c7fe698a 2018-01-23 stsp }
431 c7fe698a 2018-01-23 stsp
432 c7fe698a 2018-01-23 stsp err = read_packfile_hdr(*packfile, packidx);
433 c7fe698a 2018-01-23 stsp if (err) {
434 c7fe698a 2018-01-23 stsp fclose(*packfile);
435 c7fe698a 2018-01-23 stsp *packfile = NULL;
436 c7fe698a 2018-01-23 stsp }
437 c7fe698a 2018-01-23 stsp return err;
438 c7fe698a 2018-01-23 stsp }
439 c7fe698a 2018-01-23 stsp
440 c7fe698a 2018-01-23 stsp static const struct got_error *
441 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
442 c3703302 2018-01-23 stsp FILE *packfile)
443 a487c1d0 2018-01-14 stsp {
444 a487c1d0 2018-01-14 stsp uint8_t t = 0;
445 a487c1d0 2018-01-14 stsp uint64_t s = 0;
446 a487c1d0 2018-01-14 stsp uint8_t sizeN;
447 a487c1d0 2018-01-14 stsp size_t n;
448 a487c1d0 2018-01-14 stsp int i = 0;
449 a487c1d0 2018-01-14 stsp
450 a1fd68d8 2018-01-12 stsp do {
451 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
452 a487c1d0 2018-01-14 stsp if (i > 9)
453 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
454 a1fd68d8 2018-01-12 stsp
455 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
456 a487c1d0 2018-01-14 stsp if (n != 1)
457 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
458 8251fdbc 2018-01-12 stsp
459 a1fd68d8 2018-01-12 stsp if (i == 0) {
460 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
461 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
462 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
463 a1fd68d8 2018-01-12 stsp } else {
464 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
465 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
466 a1fd68d8 2018-01-12 stsp }
467 a1fd68d8 2018-01-12 stsp i++;
468 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
469 a1fd68d8 2018-01-12 stsp
470 a487c1d0 2018-01-14 stsp *type = t;
471 a487c1d0 2018-01-14 stsp *size = s;
472 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
473 a487c1d0 2018-01-14 stsp return NULL;
474 0a0a3048 2018-01-10 stsp }
475 c54542a0 2018-01-13 stsp
476 a1fd68d8 2018-01-12 stsp static const struct got_error *
477 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
478 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
479 6ccb713b 2018-01-19 stsp {
480 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
481 6ccb713b 2018-01-19 stsp if (*obj == NULL)
482 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
483 6ccb713b 2018-01-19 stsp
484 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
485 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
486 6ccb713b 2018-01-19 stsp free(*obj);
487 6ccb713b 2018-01-19 stsp *obj = NULL;
488 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
489 6ccb713b 2018-01-19 stsp }
490 6ccb713b 2018-01-19 stsp
491 6ccb713b 2018-01-19 stsp (*obj)->type = type;
492 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
493 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
494 6ccb713b 2018-01-19 stsp (*obj)->size = size;
495 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
496 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
497 b107e67f 2018-01-19 stsp
498 b107e67f 2018-01-19 stsp return NULL;
499 b107e67f 2018-01-19 stsp }
500 b107e67f 2018-01-19 stsp
501 b107e67f 2018-01-19 stsp static const struct got_error *
502 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
503 b107e67f 2018-01-19 stsp {
504 b107e67f 2018-01-19 stsp int64_t o = 0;
505 b107e67f 2018-01-19 stsp uint8_t offN;
506 b107e67f 2018-01-19 stsp size_t n;
507 b107e67f 2018-01-19 stsp int i = 0;
508 b107e67f 2018-01-19 stsp
509 b107e67f 2018-01-19 stsp do {
510 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
511 b107e67f 2018-01-19 stsp if (i > 8)
512 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
513 b107e67f 2018-01-19 stsp
514 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
515 b107e67f 2018-01-19 stsp if (n != 1)
516 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
517 b107e67f 2018-01-19 stsp
518 b107e67f 2018-01-19 stsp if (i == 0)
519 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
520 b107e67f 2018-01-19 stsp else {
521 cecc778e 2018-01-23 stsp o++;
522 b107e67f 2018-01-19 stsp o <<= 7;
523 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
524 b107e67f 2018-01-19 stsp }
525 b107e67f 2018-01-19 stsp i++;
526 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
527 6ccb713b 2018-01-19 stsp
528 b107e67f 2018-01-19 stsp *offset = o;
529 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
530 6ccb713b 2018-01-19 stsp return NULL;
531 6ccb713b 2018-01-19 stsp }
532 6ccb713b 2018-01-19 stsp
533 6ccb713b 2018-01-19 stsp static const struct got_error *
534 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
535 b107e67f 2018-01-19 stsp {
536 96f5e8b3 2018-01-23 stsp const struct got_error *err;
537 b107e67f 2018-01-19 stsp int64_t negoffset;
538 b107e67f 2018-01-19 stsp size_t negofflen;
539 b107e67f 2018-01-19 stsp
540 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
541 b107e67f 2018-01-19 stsp if (err)
542 b107e67f 2018-01-19 stsp return err;
543 b107e67f 2018-01-19 stsp
544 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
545 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
546 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
547 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
548 b107e67f 2018-01-19 stsp
549 96f5e8b3 2018-01-23 stsp return NULL;
550 96f5e8b3 2018-01-23 stsp }
551 96f5e8b3 2018-01-23 stsp
552 a3500804 2018-01-23 stsp static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
553 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *, const char *, int, off_t, size_t);
554 a3500804 2018-01-23 stsp
555 96f5e8b3 2018-01-23 stsp static const struct got_error *
556 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
557 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
558 6b9c9673 2018-01-23 stsp off_t delta_offset)
559 a3500804 2018-01-23 stsp {
560 a3500804 2018-01-23 stsp const struct got_error *err;
561 c3703302 2018-01-23 stsp off_t base_offset;
562 c3703302 2018-01-23 stsp uint8_t base_type;
563 c3703302 2018-01-23 stsp uint64_t base_size;
564 c3703302 2018-01-23 stsp size_t base_tslen;
565 a3500804 2018-01-23 stsp
566 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
567 a3500804 2018-01-23 stsp if (err)
568 a3500804 2018-01-23 stsp return err;
569 a3500804 2018-01-23 stsp
570 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
571 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
572 b107e67f 2018-01-19 stsp return got_error_from_errno();
573 b107e67f 2018-01-19 stsp
574 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
575 b107e67f 2018-01-19 stsp packfile);
576 b107e67f 2018-01-19 stsp if (err)
577 b107e67f 2018-01-19 stsp return err;
578 b107e67f 2018-01-19 stsp
579 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
580 c3703302 2018-01-23 stsp base_type, base_offset + base_tslen, base_size);
581 c3703302 2018-01-23 stsp }
582 c3703302 2018-01-23 stsp
583 c3703302 2018-01-23 stsp static const struct got_error *
584 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
585 6b9c9673 2018-01-23 stsp FILE *packfile, const char *path_packfile, off_t delta_offset)
586 c3703302 2018-01-23 stsp {
587 6b9c9673 2018-01-23 stsp const struct got_error *err;
588 6b9c9673 2018-01-23 stsp struct got_object_id id;
589 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
590 6b9c9673 2018-01-23 stsp int idx;
591 6b9c9673 2018-01-23 stsp off_t base_offset;
592 6b9c9673 2018-01-23 stsp uint8_t base_type;
593 6b9c9673 2018-01-23 stsp uint64_t base_size;
594 6b9c9673 2018-01-23 stsp size_t base_tslen;
595 6b9c9673 2018-01-23 stsp size_t n;
596 6b9c9673 2018-01-23 stsp FILE *base_packfile;
597 6b9c9673 2018-01-23 stsp char *path_base_packfile;
598 6b9c9673 2018-01-23 stsp
599 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
600 6b9c9673 2018-01-23 stsp if (n != 1)
601 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
602 6b9c9673 2018-01-23 stsp
603 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
604 6b9c9673 2018-01-23 stsp if (err)
605 6b9c9673 2018-01-23 stsp return err;
606 6b9c9673 2018-01-23 stsp
607 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
608 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
609 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
610 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
611 6b9c9673 2018-01-23 stsp }
612 6b9c9673 2018-01-23 stsp
613 c7fe698a 2018-01-23 stsp err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
614 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
615 6b9c9673 2018-01-23 stsp if (err)
616 6b9c9673 2018-01-23 stsp return err;
617 6b9c9673 2018-01-23 stsp
618 6b9c9673 2018-01-23 stsp if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
619 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
620 6b9c9673 2018-01-23 stsp goto done;
621 6b9c9673 2018-01-23 stsp }
622 6b9c9673 2018-01-23 stsp
623 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
624 6b9c9673 2018-01-23 stsp base_packfile);
625 6b9c9673 2018-01-23 stsp if (err)
626 6b9c9673 2018-01-23 stsp goto done;
627 6b9c9673 2018-01-23 stsp
628 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(deltas, repo, base_packfile,
629 6b9c9673 2018-01-23 stsp path_base_packfile, base_type, base_offset + base_tslen, base_size);
630 6b9c9673 2018-01-23 stsp done:
631 6b9c9673 2018-01-23 stsp free(path_base_packfile);
632 6b9c9673 2018-01-23 stsp if (base_packfile && fclose(base_packfile) == -1 && err == 0)
633 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
634 6b9c9673 2018-01-23 stsp return err;
635 6b9c9673 2018-01-23 stsp }
636 6b9c9673 2018-01-23 stsp
637 6b9c9673 2018-01-23 stsp static const struct got_error *
638 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
639 6b9c9673 2018-01-23 stsp FILE *packfile, const char *path_packfile, int delta_type,
640 6b9c9673 2018-01-23 stsp off_t delta_offset, size_t delta_size)
641 6b9c9673 2018-01-23 stsp {
642 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
643 c3703302 2018-01-23 stsp struct got_delta *delta;
644 c3703302 2018-01-23 stsp
645 c3703302 2018-01-23 stsp delta = got_delta_open(path_packfile, delta_type, delta_offset,
646 96f5e8b3 2018-01-23 stsp delta_size);
647 c3703302 2018-01-23 stsp if (delta == NULL)
648 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
649 96f5e8b3 2018-01-23 stsp deltas->nentries++;
650 6691714a 2018-01-23 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
651 c3703302 2018-01-23 stsp /* In case of error below, delta is freed in got_object_close(). */
652 96f5e8b3 2018-01-23 stsp
653 c3703302 2018-01-23 stsp switch (delta_type) {
654 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
655 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
656 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
657 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
658 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
659 4e8cda55 2018-01-19 stsp break;
660 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
661 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
662 6b9c9673 2018-01-23 stsp path_packfile, delta_offset);
663 96f5e8b3 2018-01-23 stsp break;
664 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
665 6b9c9673 2018-01-23 stsp err = resolve_ref_delta(deltas, repo, packfile, path_packfile,
666 6b9c9673 2018-01-23 stsp delta_offset);
667 6b9c9673 2018-01-23 stsp break;
668 4e8cda55 2018-01-19 stsp default:
669 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
670 4e8cda55 2018-01-19 stsp }
671 96f5e8b3 2018-01-23 stsp
672 96f5e8b3 2018-01-23 stsp return err;
673 96f5e8b3 2018-01-23 stsp }
674 96f5e8b3 2018-01-23 stsp
675 96f5e8b3 2018-01-23 stsp static const struct got_error *
676 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
677 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
678 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
679 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
680 96f5e8b3 2018-01-23 stsp {
681 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
682 96f5e8b3 2018-01-23 stsp struct got_object_id base_id;
683 96f5e8b3 2018-01-23 stsp uint8_t base_type;
684 96f5e8b3 2018-01-23 stsp int resolved_type;
685 96f5e8b3 2018-01-23 stsp uint64_t base_size;
686 96f5e8b3 2018-01-23 stsp size_t base_tslen;
687 4e8cda55 2018-01-19 stsp
688 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
689 b107e67f 2018-01-19 stsp if (*obj == NULL)
690 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
691 b107e67f 2018-01-19 stsp
692 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
693 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
694 96f5e8b3 2018-01-23 stsp (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
695 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
696 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
697 b107e67f 2018-01-19 stsp
698 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
699 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
700 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
701 96f5e8b3 2018-01-23 stsp goto done;
702 96f5e8b3 2018-01-23 stsp }
703 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
704 96f5e8b3 2018-01-23 stsp
705 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
706 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
707 c3703302 2018-01-23 stsp
708 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
709 a48db7e5 2018-01-23 stsp path_packfile, delta_type, offset, delta_size);
710 96f5e8b3 2018-01-23 stsp if (err)
711 96f5e8b3 2018-01-23 stsp goto done;
712 96f5e8b3 2018-01-23 stsp
713 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
714 96f5e8b3 2018-01-23 stsp if (err)
715 96f5e8b3 2018-01-23 stsp goto done;
716 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
717 96f5e8b3 2018-01-23 stsp
718 96f5e8b3 2018-01-23 stsp done:
719 96f5e8b3 2018-01-23 stsp if (err) {
720 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
721 96f5e8b3 2018-01-23 stsp *obj = NULL;
722 96f5e8b3 2018-01-23 stsp }
723 96f5e8b3 2018-01-23 stsp return err;
724 b107e67f 2018-01-19 stsp }
725 b107e67f 2018-01-19 stsp
726 b107e67f 2018-01-19 stsp static const struct got_error *
727 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
728 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
729 a1fd68d8 2018-01-12 stsp {
730 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
731 a1fd68d8 2018-01-12 stsp off_t offset;
732 6c00b545 2018-01-17 stsp char *path_packfile;
733 6c00b545 2018-01-17 stsp FILE *packfile;
734 6c00b545 2018-01-17 stsp uint8_t type;
735 6c00b545 2018-01-17 stsp uint64_t size;
736 3ee5fc21 2018-01-17 stsp size_t tslen;
737 a1fd68d8 2018-01-12 stsp
738 6c00b545 2018-01-17 stsp *obj = NULL;
739 a1fd68d8 2018-01-12 stsp
740 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
741 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
742 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
743 a1fd68d8 2018-01-12 stsp
744 c7fe698a 2018-01-23 stsp err = open_packfile(&packfile, &path_packfile, repo, packidx);
745 6b9c9673 2018-01-23 stsp if (err)
746 6b9c9673 2018-01-23 stsp return err;
747 a1fd68d8 2018-01-12 stsp
748 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
749 6c00b545 2018-01-17 stsp err = got_error_from_errno();
750 6c00b545 2018-01-17 stsp goto done;
751 6c00b545 2018-01-17 stsp }
752 6c00b545 2018-01-17 stsp
753 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&type, &size, &tslen, packfile);
754 a1fd68d8 2018-01-12 stsp if (err)
755 a1fd68d8 2018-01-12 stsp goto done;
756 a1fd68d8 2018-01-12 stsp
757 6c00b545 2018-01-17 stsp switch (type) {
758 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
759 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
760 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
761 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
762 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
763 6ccb713b 2018-01-19 stsp offset + tslen, size);
764 6c00b545 2018-01-17 stsp break;
765 6ccb713b 2018-01-19 stsp
766 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
767 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
768 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
769 a48db7e5 2018-01-23 stsp packfile, id, offset, tslen, type, size);
770 b107e67f 2018-01-19 stsp break;
771 b107e67f 2018-01-19 stsp
772 6c00b545 2018-01-17 stsp default:
773 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
774 6c00b545 2018-01-17 stsp goto done;
775 6c00b545 2018-01-17 stsp }
776 a1fd68d8 2018-01-12 stsp done:
777 a1fd68d8 2018-01-12 stsp free(path_packfile);
778 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
779 f334529e 2018-01-12 stsp err = got_error_from_errno();
780 a1fd68d8 2018-01-12 stsp return err;
781 a1fd68d8 2018-01-12 stsp }
782 a1fd68d8 2018-01-12 stsp
783 a1fd68d8 2018-01-12 stsp const struct got_error *
784 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
785 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
786 a1fd68d8 2018-01-12 stsp {
787 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
788 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
789 6b9c9673 2018-01-23 stsp int idx;
790 a1fd68d8 2018-01-12 stsp
791 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
792 6b9c9673 2018-01-23 stsp if (err)
793 6b9c9673 2018-01-23 stsp return err;
794 a1fd68d8 2018-01-12 stsp
795 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
796 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
797 3ee5fc21 2018-01-17 stsp return err;
798 3ee5fc21 2018-01-17 stsp }
799 3ee5fc21 2018-01-17 stsp
800 3ee5fc21 2018-01-17 stsp static const struct got_error *
801 3ee5fc21 2018-01-17 stsp dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
802 3ee5fc21 2018-01-17 stsp {
803 3ee5fc21 2018-01-17 stsp size_t n;
804 3ee5fc21 2018-01-17 stsp
805 3ee5fc21 2018-01-17 stsp while (size > 0) {
806 3ee5fc21 2018-01-17 stsp uint8_t data[2048];
807 3ee5fc21 2018-01-17 stsp size_t len = MIN(size, sizeof(data));
808 3ee5fc21 2018-01-17 stsp
809 3ee5fc21 2018-01-17 stsp n = fread(data, len, 1, infile);
810 3ee5fc21 2018-01-17 stsp if (n != 1)
811 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
812 3ee5fc21 2018-01-17 stsp
813 3ee5fc21 2018-01-17 stsp n = fwrite(data, len, 1, outfile);
814 3ee5fc21 2018-01-17 stsp if (n != 1)
815 efd2a263 2018-01-19 stsp return got_ferror(outfile, GOT_ERR_IO);
816 3ee5fc21 2018-01-17 stsp
817 3ee5fc21 2018-01-17 stsp size -= len;
818 3ee5fc21 2018-01-17 stsp }
819 3ee5fc21 2018-01-17 stsp
820 3ee5fc21 2018-01-17 stsp rewind(outfile);
821 3ee5fc21 2018-01-17 stsp return NULL;
822 3ee5fc21 2018-01-17 stsp }
823 efd2a263 2018-01-19 stsp
824 efd2a263 2018-01-19 stsp static const struct got_error *
825 710bb5ca 2018-01-23 stsp dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
826 efd2a263 2018-01-19 stsp {
827 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
828 6691714a 2018-01-23 stsp struct got_delta *delta;
829 6691714a 2018-01-23 stsp FILE *base_file, *accum_file;
830 6691714a 2018-01-23 stsp int n = 0;
831 3ee5fc21 2018-01-17 stsp
832 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
833 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
834 efd2a263 2018-01-19 stsp
835 6691714a 2018-01-23 stsp base_file = got_opentemp();
836 6691714a 2018-01-23 stsp if (base_file == NULL)
837 6691714a 2018-01-23 stsp return got_error_from_errno();
838 efd2a263 2018-01-19 stsp
839 6691714a 2018-01-23 stsp accum_file = got_opentemp();
840 6691714a 2018-01-23 stsp if (accum_file == NULL) {
841 6691714a 2018-01-23 stsp err = got_error_from_errno();
842 6691714a 2018-01-23 stsp fclose(base_file);
843 efd2a263 2018-01-19 stsp return err;
844 6691714a 2018-01-23 stsp }
845 efd2a263 2018-01-19 stsp
846 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
847 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
848 6691714a 2018-01-23 stsp FILE *delta_file = fopen(delta->path_packfile, "rb");
849 6691714a 2018-01-23 stsp if (delta_file == NULL) {
850 6691714a 2018-01-23 stsp err = got_error_from_errno();
851 6691714a 2018-01-23 stsp goto done;
852 6691714a 2018-01-23 stsp }
853 6691714a 2018-01-23 stsp
854 6691714a 2018-01-23 stsp if (fseeko(delta_file, delta->offset, SEEK_SET) != 0) {
855 6691714a 2018-01-23 stsp fclose(delta_file);
856 6691714a 2018-01-23 stsp err = got_error_from_errno();
857 6691714a 2018-01-23 stsp goto done;
858 6691714a 2018-01-23 stsp }
859 6691714a 2018-01-23 stsp
860 6691714a 2018-01-23 stsp err = got_delta_apply(delta, base_file, delta_file,
861 6691714a 2018-01-23 stsp /* Final delta application writes to the output file. */
862 710bb5ca 2018-01-23 stsp ++n < deltas->nentries ? accum_file : outfile);
863 6691714a 2018-01-23 stsp fclose(delta_file);
864 6691714a 2018-01-23 stsp if (err)
865 6691714a 2018-01-23 stsp goto done;
866 6691714a 2018-01-23 stsp
867 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
868 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
869 6691714a 2018-01-23 stsp FILE *tmp = accum_file;
870 6691714a 2018-01-23 stsp accum_file = base_file;
871 6691714a 2018-01-23 stsp base_file = tmp;
872 6691714a 2018-01-23 stsp rewind(base_file);
873 6691714a 2018-01-23 stsp rewind(accum_file);
874 6691714a 2018-01-23 stsp }
875 6691714a 2018-01-23 stsp }
876 6691714a 2018-01-23 stsp
877 6691714a 2018-01-23 stsp done:
878 6691714a 2018-01-23 stsp fclose(base_file);
879 6691714a 2018-01-23 stsp fclose(accum_file);
880 6691714a 2018-01-23 stsp rewind(outfile);
881 efd2a263 2018-01-19 stsp return err;
882 efd2a263 2018-01-19 stsp }
883 efd2a263 2018-01-19 stsp
884 3ee5fc21 2018-01-17 stsp const struct got_error *
885 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
886 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
887 3ee5fc21 2018-01-17 stsp {
888 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
889 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
890 3ee5fc21 2018-01-17 stsp
891 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
892 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
893 3ee5fc21 2018-01-17 stsp
894 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
895 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
896 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
897 3ee5fc21 2018-01-17 stsp goto done;
898 3ee5fc21 2018-01-17 stsp }
899 3ee5fc21 2018-01-17 stsp
900 6691714a 2018-01-23 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
901 6691714a 2018-01-23 stsp packfile = fopen(obj->path_packfile, "rb");
902 6691714a 2018-01-23 stsp if (packfile == NULL) {
903 6691714a 2018-01-23 stsp err = got_error_from_errno();
904 6691714a 2018-01-23 stsp goto done;
905 6691714a 2018-01-23 stsp }
906 3ee5fc21 2018-01-17 stsp
907 6691714a 2018-01-23 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
908 6691714a 2018-01-23 stsp err = got_error_from_errno();
909 6691714a 2018-01-23 stsp goto done;
910 6691714a 2018-01-23 stsp }
911 3ee5fc21 2018-01-17 stsp
912 3ee5fc21 2018-01-17 stsp err = dump_plain_object(packfile, obj->type, obj->size, *f);
913 6691714a 2018-01-23 stsp } else
914 710bb5ca 2018-01-23 stsp err = dump_delta_chain(&obj->deltas, *f);
915 3ee5fc21 2018-01-17 stsp done:
916 3ee5fc21 2018-01-17 stsp if (packfile)
917 3ee5fc21 2018-01-17 stsp fclose(packfile);
918 3ee5fc21 2018-01-17 stsp if (err && *f)
919 3ee5fc21 2018-01-17 stsp fclose(*f);
920 a1fd68d8 2018-01-12 stsp return err;
921 a1fd68d8 2018-01-12 stsp }