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 6b9c9673 2018-01-23 stsp if (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 6b9c9673 2018-01-23 stsp 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 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
415 c3703302 2018-01-23 stsp FILE *packfile)
416 a487c1d0 2018-01-14 stsp {
417 a487c1d0 2018-01-14 stsp uint8_t t = 0;
418 a487c1d0 2018-01-14 stsp uint64_t s = 0;
419 a487c1d0 2018-01-14 stsp uint8_t sizeN;
420 a487c1d0 2018-01-14 stsp size_t n;
421 a487c1d0 2018-01-14 stsp int i = 0;
422 a487c1d0 2018-01-14 stsp
423 a1fd68d8 2018-01-12 stsp do {
424 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
425 a487c1d0 2018-01-14 stsp if (i > 9)
426 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
427 a1fd68d8 2018-01-12 stsp
428 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
429 a487c1d0 2018-01-14 stsp if (n != 1)
430 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
431 8251fdbc 2018-01-12 stsp
432 a1fd68d8 2018-01-12 stsp if (i == 0) {
433 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
434 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
435 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
436 a1fd68d8 2018-01-12 stsp } else {
437 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
438 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
439 a1fd68d8 2018-01-12 stsp }
440 a1fd68d8 2018-01-12 stsp i++;
441 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
442 a1fd68d8 2018-01-12 stsp
443 a487c1d0 2018-01-14 stsp *type = t;
444 a487c1d0 2018-01-14 stsp *size = s;
445 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
446 a487c1d0 2018-01-14 stsp return NULL;
447 0a0a3048 2018-01-10 stsp }
448 c54542a0 2018-01-13 stsp
449 a1fd68d8 2018-01-12 stsp static const struct got_error *
450 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
451 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
452 6ccb713b 2018-01-19 stsp {
453 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
454 6ccb713b 2018-01-19 stsp if (*obj == NULL)
455 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
456 6ccb713b 2018-01-19 stsp
457 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
458 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
459 6ccb713b 2018-01-19 stsp free(*obj);
460 6ccb713b 2018-01-19 stsp *obj = NULL;
461 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
462 6ccb713b 2018-01-19 stsp }
463 6ccb713b 2018-01-19 stsp
464 6ccb713b 2018-01-19 stsp (*obj)->type = type;
465 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
466 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
467 6ccb713b 2018-01-19 stsp (*obj)->size = size;
468 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
469 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
470 b107e67f 2018-01-19 stsp
471 b107e67f 2018-01-19 stsp return NULL;
472 b107e67f 2018-01-19 stsp }
473 b107e67f 2018-01-19 stsp
474 b107e67f 2018-01-19 stsp static const struct got_error *
475 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
476 b107e67f 2018-01-19 stsp {
477 b107e67f 2018-01-19 stsp int64_t o = 0;
478 b107e67f 2018-01-19 stsp uint8_t offN;
479 b107e67f 2018-01-19 stsp size_t n;
480 b107e67f 2018-01-19 stsp int i = 0;
481 b107e67f 2018-01-19 stsp
482 b107e67f 2018-01-19 stsp do {
483 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
484 b107e67f 2018-01-19 stsp if (i > 8)
485 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
486 b107e67f 2018-01-19 stsp
487 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
488 b107e67f 2018-01-19 stsp if (n != 1)
489 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
490 b107e67f 2018-01-19 stsp
491 b107e67f 2018-01-19 stsp if (i == 0)
492 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
493 b107e67f 2018-01-19 stsp else {
494 cecc778e 2018-01-23 stsp o++;
495 b107e67f 2018-01-19 stsp o <<= 7;
496 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
497 b107e67f 2018-01-19 stsp }
498 b107e67f 2018-01-19 stsp i++;
499 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
500 6ccb713b 2018-01-19 stsp
501 b107e67f 2018-01-19 stsp *offset = o;
502 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
503 6ccb713b 2018-01-19 stsp return NULL;
504 6ccb713b 2018-01-19 stsp }
505 6ccb713b 2018-01-19 stsp
506 6ccb713b 2018-01-19 stsp static const struct got_error *
507 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
508 b107e67f 2018-01-19 stsp {
509 96f5e8b3 2018-01-23 stsp const struct got_error *err;
510 b107e67f 2018-01-19 stsp int64_t negoffset;
511 b107e67f 2018-01-19 stsp size_t negofflen;
512 b107e67f 2018-01-19 stsp
513 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
514 b107e67f 2018-01-19 stsp if (err)
515 b107e67f 2018-01-19 stsp return err;
516 b107e67f 2018-01-19 stsp
517 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
518 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
519 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
520 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
521 b107e67f 2018-01-19 stsp
522 96f5e8b3 2018-01-23 stsp return NULL;
523 96f5e8b3 2018-01-23 stsp }
524 96f5e8b3 2018-01-23 stsp
525 a3500804 2018-01-23 stsp static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
526 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *, const char *, int, off_t, size_t);
527 a3500804 2018-01-23 stsp
528 96f5e8b3 2018-01-23 stsp static const struct got_error *
529 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
530 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
531 6b9c9673 2018-01-23 stsp off_t delta_offset)
532 a3500804 2018-01-23 stsp {
533 a3500804 2018-01-23 stsp const struct got_error *err;
534 c3703302 2018-01-23 stsp off_t base_offset;
535 c3703302 2018-01-23 stsp uint8_t base_type;
536 c3703302 2018-01-23 stsp uint64_t base_size;
537 c3703302 2018-01-23 stsp size_t base_tslen;
538 a3500804 2018-01-23 stsp
539 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
540 a3500804 2018-01-23 stsp if (err)
541 a3500804 2018-01-23 stsp return err;
542 a3500804 2018-01-23 stsp
543 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
544 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
545 b107e67f 2018-01-19 stsp return got_error_from_errno();
546 b107e67f 2018-01-19 stsp
547 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
548 b107e67f 2018-01-19 stsp packfile);
549 b107e67f 2018-01-19 stsp if (err)
550 b107e67f 2018-01-19 stsp return err;
551 b107e67f 2018-01-19 stsp
552 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
553 c3703302 2018-01-23 stsp base_type, base_offset + base_tslen, base_size);
554 c3703302 2018-01-23 stsp }
555 c3703302 2018-01-23 stsp
556 c3703302 2018-01-23 stsp static const struct got_error *
557 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
558 6b9c9673 2018-01-23 stsp FILE *packfile, const char *path_packfile, off_t delta_offset)
559 c3703302 2018-01-23 stsp {
560 6b9c9673 2018-01-23 stsp const struct got_error *err;
561 6b9c9673 2018-01-23 stsp struct got_object_id id;
562 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
563 6b9c9673 2018-01-23 stsp int idx;
564 6b9c9673 2018-01-23 stsp off_t base_offset;
565 6b9c9673 2018-01-23 stsp uint8_t base_type;
566 6b9c9673 2018-01-23 stsp uint64_t base_size;
567 6b9c9673 2018-01-23 stsp size_t base_tslen;
568 6b9c9673 2018-01-23 stsp size_t n;
569 6b9c9673 2018-01-23 stsp FILE *base_packfile;
570 6b9c9673 2018-01-23 stsp char *path_base_packfile;
571 6b9c9673 2018-01-23 stsp
572 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
573 6b9c9673 2018-01-23 stsp if (n != 1)
574 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
575 6b9c9673 2018-01-23 stsp
576 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
577 6b9c9673 2018-01-23 stsp if (err)
578 6b9c9673 2018-01-23 stsp return err;
579 6b9c9673 2018-01-23 stsp
580 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
581 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
582 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
583 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
584 6b9c9673 2018-01-23 stsp }
585 6b9c9673 2018-01-23 stsp
586 6b9c9673 2018-01-23 stsp err = get_packfile_path(&path_base_packfile, repo, packidx);
587 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
588 6b9c9673 2018-01-23 stsp if (err)
589 6b9c9673 2018-01-23 stsp return err;
590 6b9c9673 2018-01-23 stsp
591 6b9c9673 2018-01-23 stsp base_packfile = fopen(path_base_packfile, "rb");
592 6b9c9673 2018-01-23 stsp if (base_packfile == NULL) {
593 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
594 6b9c9673 2018-01-23 stsp goto done;
595 6b9c9673 2018-01-23 stsp }
596 6b9c9673 2018-01-23 stsp
597 6b9c9673 2018-01-23 stsp if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
598 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
599 6b9c9673 2018-01-23 stsp goto done;
600 6b9c9673 2018-01-23 stsp }
601 6b9c9673 2018-01-23 stsp
602 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
603 6b9c9673 2018-01-23 stsp base_packfile);
604 6b9c9673 2018-01-23 stsp if (err)
605 6b9c9673 2018-01-23 stsp goto done;
606 6b9c9673 2018-01-23 stsp
607 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(deltas, repo, base_packfile,
608 6b9c9673 2018-01-23 stsp path_base_packfile, base_type, base_offset + base_tslen, base_size);
609 6b9c9673 2018-01-23 stsp done:
610 6b9c9673 2018-01-23 stsp free(path_base_packfile);
611 6b9c9673 2018-01-23 stsp if (base_packfile && fclose(base_packfile) == -1 && err == 0)
612 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
613 6b9c9673 2018-01-23 stsp return err;
614 6b9c9673 2018-01-23 stsp }
615 6b9c9673 2018-01-23 stsp
616 6b9c9673 2018-01-23 stsp static const struct got_error *
617 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
618 6b9c9673 2018-01-23 stsp FILE *packfile, const char *path_packfile, int delta_type,
619 6b9c9673 2018-01-23 stsp off_t delta_offset, size_t delta_size)
620 6b9c9673 2018-01-23 stsp {
621 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
622 c3703302 2018-01-23 stsp struct got_delta *delta;
623 c3703302 2018-01-23 stsp
624 c3703302 2018-01-23 stsp delta = got_delta_open(path_packfile, delta_type, delta_offset,
625 96f5e8b3 2018-01-23 stsp delta_size);
626 c3703302 2018-01-23 stsp if (delta == NULL)
627 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
628 96f5e8b3 2018-01-23 stsp deltas->nentries++;
629 c3703302 2018-01-23 stsp SIMPLEQ_INSERT_TAIL(&deltas->entries, delta, entry);
630 c3703302 2018-01-23 stsp /* In case of error below, delta is freed in got_object_close(). */
631 96f5e8b3 2018-01-23 stsp
632 c3703302 2018-01-23 stsp switch (delta_type) {
633 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
634 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
635 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
636 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
637 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
638 4e8cda55 2018-01-19 stsp break;
639 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
640 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
641 6b9c9673 2018-01-23 stsp path_packfile, delta_offset);
642 96f5e8b3 2018-01-23 stsp break;
643 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
644 6b9c9673 2018-01-23 stsp err = resolve_ref_delta(deltas, repo, packfile, path_packfile,
645 6b9c9673 2018-01-23 stsp delta_offset);
646 6b9c9673 2018-01-23 stsp break;
647 4e8cda55 2018-01-19 stsp default:
648 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
649 4e8cda55 2018-01-19 stsp }
650 96f5e8b3 2018-01-23 stsp
651 96f5e8b3 2018-01-23 stsp return err;
652 96f5e8b3 2018-01-23 stsp }
653 96f5e8b3 2018-01-23 stsp
654 96f5e8b3 2018-01-23 stsp static const struct got_error *
655 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
656 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
657 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
658 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
659 96f5e8b3 2018-01-23 stsp {
660 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
661 96f5e8b3 2018-01-23 stsp struct got_object_id base_id;
662 96f5e8b3 2018-01-23 stsp uint8_t base_type;
663 96f5e8b3 2018-01-23 stsp int resolved_type;
664 96f5e8b3 2018-01-23 stsp uint64_t base_size;
665 96f5e8b3 2018-01-23 stsp size_t base_tslen;
666 4e8cda55 2018-01-19 stsp
667 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
668 b107e67f 2018-01-19 stsp if (*obj == NULL)
669 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
670 b107e67f 2018-01-19 stsp
671 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
672 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
673 96f5e8b3 2018-01-23 stsp (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
674 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
675 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
676 b107e67f 2018-01-19 stsp
677 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
678 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
679 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
680 96f5e8b3 2018-01-23 stsp goto done;
681 96f5e8b3 2018-01-23 stsp }
682 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
683 96f5e8b3 2018-01-23 stsp
684 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
685 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
686 c3703302 2018-01-23 stsp
687 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
688 a48db7e5 2018-01-23 stsp path_packfile, delta_type, offset, delta_size);
689 96f5e8b3 2018-01-23 stsp if (err)
690 96f5e8b3 2018-01-23 stsp goto done;
691 96f5e8b3 2018-01-23 stsp
692 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
693 96f5e8b3 2018-01-23 stsp if (err)
694 96f5e8b3 2018-01-23 stsp goto done;
695 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
696 96f5e8b3 2018-01-23 stsp
697 96f5e8b3 2018-01-23 stsp done:
698 96f5e8b3 2018-01-23 stsp if (err) {
699 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
700 96f5e8b3 2018-01-23 stsp *obj = NULL;
701 96f5e8b3 2018-01-23 stsp }
702 96f5e8b3 2018-01-23 stsp return err;
703 b107e67f 2018-01-19 stsp }
704 b107e67f 2018-01-19 stsp
705 b107e67f 2018-01-19 stsp static const struct got_error *
706 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
707 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
708 a1fd68d8 2018-01-12 stsp {
709 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
710 a1fd68d8 2018-01-12 stsp off_t offset;
711 6c00b545 2018-01-17 stsp char *path_packfile;
712 6c00b545 2018-01-17 stsp FILE *packfile;
713 6c00b545 2018-01-17 stsp uint8_t type;
714 6c00b545 2018-01-17 stsp uint64_t size;
715 3ee5fc21 2018-01-17 stsp size_t tslen;
716 a1fd68d8 2018-01-12 stsp
717 6c00b545 2018-01-17 stsp *obj = NULL;
718 a1fd68d8 2018-01-12 stsp
719 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
720 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
721 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
722 a1fd68d8 2018-01-12 stsp
723 6b9c9673 2018-01-23 stsp err = get_packfile_path(&path_packfile, repo, packidx);
724 6b9c9673 2018-01-23 stsp if (err)
725 6b9c9673 2018-01-23 stsp return err;
726 a1fd68d8 2018-01-12 stsp
727 a1fd68d8 2018-01-12 stsp packfile = fopen(path_packfile, "rb");
728 a1fd68d8 2018-01-12 stsp if (packfile == NULL) {
729 f334529e 2018-01-12 stsp err = got_error_from_errno();
730 a1fd68d8 2018-01-12 stsp goto done;
731 a1fd68d8 2018-01-12 stsp }
732 a1fd68d8 2018-01-12 stsp
733 a1fd68d8 2018-01-12 stsp err = read_packfile_hdr(packfile, packidx);
734 a1fd68d8 2018-01-12 stsp if (err)
735 a1fd68d8 2018-01-12 stsp goto done;
736 a1fd68d8 2018-01-12 stsp
737 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
738 6c00b545 2018-01-17 stsp err = got_error_from_errno();
739 6c00b545 2018-01-17 stsp goto done;
740 6c00b545 2018-01-17 stsp }
741 6c00b545 2018-01-17 stsp
742 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&type, &size, &tslen, packfile);
743 a1fd68d8 2018-01-12 stsp if (err)
744 a1fd68d8 2018-01-12 stsp goto done;
745 a1fd68d8 2018-01-12 stsp
746 6c00b545 2018-01-17 stsp switch (type) {
747 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
748 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
749 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
750 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
751 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
752 6ccb713b 2018-01-19 stsp offset + tslen, size);
753 6c00b545 2018-01-17 stsp break;
754 6ccb713b 2018-01-19 stsp
755 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
756 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
757 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
758 a48db7e5 2018-01-23 stsp packfile, id, offset, tslen, type, size);
759 b107e67f 2018-01-19 stsp break;
760 b107e67f 2018-01-19 stsp
761 6c00b545 2018-01-17 stsp default:
762 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
763 6c00b545 2018-01-17 stsp goto done;
764 6c00b545 2018-01-17 stsp }
765 a1fd68d8 2018-01-12 stsp done:
766 a1fd68d8 2018-01-12 stsp free(path_packfile);
767 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
768 f334529e 2018-01-12 stsp err = got_error_from_errno();
769 a1fd68d8 2018-01-12 stsp return err;
770 a1fd68d8 2018-01-12 stsp }
771 a1fd68d8 2018-01-12 stsp
772 a1fd68d8 2018-01-12 stsp const struct got_error *
773 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
774 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
775 a1fd68d8 2018-01-12 stsp {
776 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
777 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
778 6b9c9673 2018-01-23 stsp int idx;
779 a1fd68d8 2018-01-12 stsp
780 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
781 6b9c9673 2018-01-23 stsp if (err)
782 6b9c9673 2018-01-23 stsp return err;
783 a1fd68d8 2018-01-12 stsp
784 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
785 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
786 3ee5fc21 2018-01-17 stsp return err;
787 3ee5fc21 2018-01-17 stsp }
788 3ee5fc21 2018-01-17 stsp
789 3ee5fc21 2018-01-17 stsp static const struct got_error *
790 3ee5fc21 2018-01-17 stsp dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
791 3ee5fc21 2018-01-17 stsp {
792 3ee5fc21 2018-01-17 stsp size_t n;
793 3ee5fc21 2018-01-17 stsp
794 3ee5fc21 2018-01-17 stsp while (size > 0) {
795 3ee5fc21 2018-01-17 stsp uint8_t data[2048];
796 3ee5fc21 2018-01-17 stsp size_t len = MIN(size, sizeof(data));
797 3ee5fc21 2018-01-17 stsp
798 3ee5fc21 2018-01-17 stsp n = fread(data, len, 1, infile);
799 3ee5fc21 2018-01-17 stsp if (n != 1)
800 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
801 3ee5fc21 2018-01-17 stsp
802 3ee5fc21 2018-01-17 stsp n = fwrite(data, len, 1, outfile);
803 3ee5fc21 2018-01-17 stsp if (n != 1)
804 efd2a263 2018-01-19 stsp return got_ferror(outfile, GOT_ERR_IO);
805 3ee5fc21 2018-01-17 stsp
806 3ee5fc21 2018-01-17 stsp size -= len;
807 3ee5fc21 2018-01-17 stsp }
808 3ee5fc21 2018-01-17 stsp
809 3ee5fc21 2018-01-17 stsp rewind(outfile);
810 3ee5fc21 2018-01-17 stsp return NULL;
811 3ee5fc21 2018-01-17 stsp }
812 efd2a263 2018-01-19 stsp
813 efd2a263 2018-01-19 stsp static const struct got_error *
814 efd2a263 2018-01-19 stsp dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
815 efd2a263 2018-01-19 stsp size_t size, FILE *outfile)
816 efd2a263 2018-01-19 stsp {
817 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
818 efd2a263 2018-01-19 stsp struct got_object_id base_id;
819 efd2a263 2018-01-19 stsp struct got_object *base_obj;
820 b107e67f 2018-01-19 stsp size_t n;
821 3ee5fc21 2018-01-17 stsp
822 efd2a263 2018-01-19 stsp if (size < sizeof(base_id))
823 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
824 efd2a263 2018-01-19 stsp
825 efd2a263 2018-01-19 stsp n = fread(&base_id, sizeof(base_id), 1, infile);
826 efd2a263 2018-01-19 stsp if (n != 1)
827 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
828 efd2a263 2018-01-19 stsp
829 efd2a263 2018-01-19 stsp size -= sizeof(base_id);
830 efd2a263 2018-01-19 stsp if (size <= 0)
831 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
832 efd2a263 2018-01-19 stsp
833 efd2a263 2018-01-19 stsp err = got_object_open(&base_obj, repo, &base_id);
834 efd2a263 2018-01-19 stsp if (err)
835 efd2a263 2018-01-19 stsp return err;
836 efd2a263 2018-01-19 stsp
837 efd2a263 2018-01-19 stsp err = got_delta_apply(repo, infile, size, base_obj, outfile);
838 efd2a263 2018-01-19 stsp got_object_close(base_obj);
839 efd2a263 2018-01-19 stsp return err;
840 efd2a263 2018-01-19 stsp }
841 efd2a263 2018-01-19 stsp
842 3ee5fc21 2018-01-17 stsp const struct got_error *
843 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
844 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
845 3ee5fc21 2018-01-17 stsp {
846 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
847 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
848 3ee5fc21 2018-01-17 stsp
849 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
850 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
851 3ee5fc21 2018-01-17 stsp
852 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
853 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
854 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
855 3ee5fc21 2018-01-17 stsp goto done;
856 3ee5fc21 2018-01-17 stsp }
857 3ee5fc21 2018-01-17 stsp
858 3ee5fc21 2018-01-17 stsp packfile = fopen(obj->path_packfile, "rb");
859 3ee5fc21 2018-01-17 stsp if (packfile == NULL) {
860 f334529e 2018-01-12 stsp err = got_error_from_errno();
861 3ee5fc21 2018-01-17 stsp goto done;
862 3ee5fc21 2018-01-17 stsp }
863 3ee5fc21 2018-01-17 stsp
864 3ee5fc21 2018-01-17 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
865 3ee5fc21 2018-01-17 stsp err = got_error_from_errno();
866 3ee5fc21 2018-01-17 stsp goto done;
867 3ee5fc21 2018-01-17 stsp }
868 3ee5fc21 2018-01-17 stsp
869 3ee5fc21 2018-01-17 stsp switch (obj->type) {
870 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
871 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
872 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
873 b432fb3b 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
874 3ee5fc21 2018-01-17 stsp err = dump_plain_object(packfile, obj->type, obj->size, *f);
875 3ee5fc21 2018-01-17 stsp break;
876 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_REF_DELTA:
877 efd2a263 2018-01-19 stsp err = dump_ref_delta_object(repo, packfile, obj->type,
878 efd2a263 2018-01-19 stsp obj->size, *f);
879 efd2a263 2018-01-19 stsp break;
880 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
881 3ee5fc21 2018-01-17 stsp default:
882 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
883 3ee5fc21 2018-01-17 stsp goto done;
884 3ee5fc21 2018-01-17 stsp }
885 3ee5fc21 2018-01-17 stsp done:
886 3ee5fc21 2018-01-17 stsp if (packfile)
887 3ee5fc21 2018-01-17 stsp fclose(packfile);
888 3ee5fc21 2018-01-17 stsp if (err && *f)
889 3ee5fc21 2018-01-17 stsp fclose(*f);
890 a1fd68d8 2018-01-12 stsp return err;
891 a1fd68d8 2018-01-12 stsp }