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 0a0a3048 2018-01-10 stsp
39 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
40 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
41 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
42 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
43 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
44 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
45 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
46 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
47 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
48 a1fd68d8 2018-01-12 stsp
49 a1fd68d8 2018-01-12 stsp #ifndef MIN
50 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 a1fd68d8 2018-01-12 stsp #endif
52 a1fd68d8 2018-01-12 stsp
53 0a0a3048 2018-01-10 stsp static const struct got_error *
54 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
55 0a0a3048 2018-01-10 stsp {
56 0a0a3048 2018-01-10 stsp int i;
57 0a0a3048 2018-01-10 stsp
58 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
59 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
60 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
61 0a0a3048 2018-01-10 stsp }
62 0a0a3048 2018-01-10 stsp
63 0a0a3048 2018-01-10 stsp return NULL;
64 0a0a3048 2018-01-10 stsp }
65 0a0a3048 2018-01-10 stsp
66 24541888 2018-01-10 stsp static const struct got_error *
67 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
68 0a0a3048 2018-01-10 stsp {
69 0a0a3048 2018-01-10 stsp struct stat sb;
70 0a0a3048 2018-01-10 stsp char *path_pack;
71 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
72 0a0a3048 2018-01-10 stsp char *dot;
73 0a0a3048 2018-01-10 stsp
74 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
75 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
76 0a0a3048 2018-01-10 stsp
77 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
78 0a0a3048 2018-01-10 stsp if (dot == NULL)
79 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
80 0a0a3048 2018-01-10 stsp *dot = '\0';
81 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
82 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_MEM);
83 0a0a3048 2018-01-10 stsp
84 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
85 0a0a3048 2018-01-10 stsp free(path_pack);
86 8251fdbc 2018-01-12 stsp return got_error_from_errno();
87 0a0a3048 2018-01-10 stsp }
88 0a0a3048 2018-01-10 stsp
89 0a0a3048 2018-01-10 stsp free(path_pack);
90 0a0a3048 2018-01-10 stsp *size = sb.st_size;
91 0a0a3048 2018-01-10 stsp return 0;
92 0a0a3048 2018-01-10 stsp }
93 0a0a3048 2018-01-10 stsp
94 0a0a3048 2018-01-10 stsp const struct got_error *
95 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
96 0a0a3048 2018-01-10 stsp {
97 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
98 0a0a3048 2018-01-10 stsp FILE *f;
99 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
100 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
101 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
102 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
103 0a0a3048 2018-01-10 stsp
104 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
105 0ebaf008 2018-01-10 stsp
106 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
107 0a0a3048 2018-01-10 stsp if (f == NULL)
108 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
109 0a0a3048 2018-01-10 stsp
110 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
111 0a0a3048 2018-01-10 stsp if (err)
112 0a0a3048 2018-01-10 stsp return err;
113 0a0a3048 2018-01-10 stsp
114 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
115 0a0a3048 2018-01-10 stsp if (p == NULL) {
116 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
117 0a0a3048 2018-01-10 stsp goto done;
118 0a0a3048 2018-01-10 stsp }
119 0a0a3048 2018-01-10 stsp
120 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
121 0a0a3048 2018-01-10 stsp if (n != 1) {
122 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
123 0a0a3048 2018-01-10 stsp goto done;
124 0a0a3048 2018-01-10 stsp }
125 0a0a3048 2018-01-10 stsp
126 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
127 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
128 0a0a3048 2018-01-10 stsp goto done;
129 0a0a3048 2018-01-10 stsp }
130 0a0a3048 2018-01-10 stsp
131 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
132 0ebaf008 2018-01-10 stsp
133 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
134 0a0a3048 2018-01-10 stsp if (n != 1) {
135 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
136 0a0a3048 2018-01-10 stsp goto done;
137 0a0a3048 2018-01-10 stsp }
138 0a0a3048 2018-01-10 stsp
139 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
140 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
141 0a0a3048 2018-01-10 stsp goto done;
142 0a0a3048 2018-01-10 stsp }
143 0a0a3048 2018-01-10 stsp
144 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
145 0ebaf008 2018-01-10 stsp
146 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
147 0a0a3048 2018-01-10 stsp if (n != 1) {
148 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
149 0a0a3048 2018-01-10 stsp goto done;
150 0a0a3048 2018-01-10 stsp }
151 0a0a3048 2018-01-10 stsp
152 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
153 0a0a3048 2018-01-10 stsp if (err)
154 0a0a3048 2018-01-10 stsp goto done;
155 0a0a3048 2018-01-10 stsp
156 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
157 0ebaf008 2018-01-10 stsp
158 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
159 0a0a3048 2018-01-10 stsp
160 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
161 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
162 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
163 0a0a3048 2018-01-10 stsp goto done;
164 0a0a3048 2018-01-10 stsp }
165 0a0a3048 2018-01-10 stsp
166 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
167 0a0a3048 2018-01-10 stsp if (n != nobj) {
168 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
169 0a0a3048 2018-01-10 stsp goto done;
170 0a0a3048 2018-01-10 stsp }
171 0a0a3048 2018-01-10 stsp
172 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
173 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
174 0ebaf008 2018-01-10 stsp
175 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
176 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
177 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
178 0a0a3048 2018-01-10 stsp goto done;
179 0a0a3048 2018-01-10 stsp }
180 0a0a3048 2018-01-10 stsp
181 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
182 0a0a3048 2018-01-10 stsp if (n != nobj) {
183 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
184 0a0a3048 2018-01-10 stsp goto done;
185 0a0a3048 2018-01-10 stsp }
186 0a0a3048 2018-01-10 stsp
187 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
188 0ebaf008 2018-01-10 stsp
189 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
190 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
191 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
192 0a0a3048 2018-01-10 stsp goto done;
193 0a0a3048 2018-01-10 stsp }
194 0a0a3048 2018-01-10 stsp
195 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
196 0a0a3048 2018-01-10 stsp if (n != nobj) {
197 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
198 0a0a3048 2018-01-10 stsp goto done;
199 0a0a3048 2018-01-10 stsp }
200 0a0a3048 2018-01-10 stsp
201 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
202 0ebaf008 2018-01-10 stsp
203 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
204 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
205 0a0a3048 2018-01-10 stsp goto checksum;
206 0a0a3048 2018-01-10 stsp
207 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
208 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
209 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
210 0a0a3048 2018-01-10 stsp goto done;
211 0a0a3048 2018-01-10 stsp }
212 0a0a3048 2018-01-10 stsp
213 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
214 0a0a3048 2018-01-10 stsp if (n != nobj) {
215 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
216 0a0a3048 2018-01-10 stsp goto done;
217 0a0a3048 2018-01-10 stsp }
218 0a0a3048 2018-01-10 stsp
219 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
220 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
221 0ebaf008 2018-01-10 stsp
222 0a0a3048 2018-01-10 stsp checksum:
223 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
224 0a0a3048 2018-01-10 stsp if (n != 1) {
225 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
226 0a0a3048 2018-01-10 stsp goto done;
227 0a0a3048 2018-01-10 stsp }
228 0a0a3048 2018-01-10 stsp
229 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
230 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
231 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
232 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
233 0a0a3048 2018-01-10 stsp done:
234 0a0a3048 2018-01-10 stsp fclose(f);
235 0a0a3048 2018-01-10 stsp if (err)
236 0a0a3048 2018-01-10 stsp got_packidx_close(p);
237 0a0a3048 2018-01-10 stsp else
238 0a0a3048 2018-01-10 stsp *packidx = p;
239 0a0a3048 2018-01-10 stsp return err;
240 0a0a3048 2018-01-10 stsp }
241 0a0a3048 2018-01-10 stsp
242 0a0a3048 2018-01-10 stsp void
243 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
244 0a0a3048 2018-01-10 stsp {
245 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
246 0a0a3048 2018-01-10 stsp free(packidx->offsets);
247 0a0a3048 2018-01-10 stsp free(packidx->crc32);
248 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
249 0a0a3048 2018-01-10 stsp free(packidx);
250 a1fd68d8 2018-01-12 stsp }
251 a1fd68d8 2018-01-12 stsp
252 a1fd68d8 2018-01-12 stsp static int
253 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
254 a1fd68d8 2018-01-12 stsp {
255 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
256 a1fd68d8 2018-01-12 stsp return 0;
257 a1fd68d8 2018-01-12 stsp
258 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
259 a1fd68d8 2018-01-12 stsp return 0;
260 a1fd68d8 2018-01-12 stsp
261 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
262 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
263 a1fd68d8 2018-01-12 stsp return 0;
264 a1fd68d8 2018-01-12 stsp
265 a1fd68d8 2018-01-12 stsp return 1;
266 a1fd68d8 2018-01-12 stsp }
267 a1fd68d8 2018-01-12 stsp
268 a1fd68d8 2018-01-12 stsp static off_t
269 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
270 a1fd68d8 2018-01-12 stsp {
271 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
272 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
273 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
274 a1fd68d8 2018-01-12 stsp uint64_t loffset;
275 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
276 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
277 a1fd68d8 2018-01-12 stsp return -1;
278 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
279 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
280 a1fd68d8 2018-01-12 stsp }
281 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
282 a1fd68d8 2018-01-12 stsp }
283 a1fd68d8 2018-01-12 stsp
284 a1fd68d8 2018-01-12 stsp static int
285 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
286 a1fd68d8 2018-01-12 stsp {
287 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
288 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
289 a1fd68d8 2018-01-12 stsp int i = 0;
290 a1fd68d8 2018-01-12 stsp
291 a1fd68d8 2018-01-12 stsp if (id0 > 0)
292 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
293 a1fd68d8 2018-01-12 stsp
294 a1fd68d8 2018-01-12 stsp while (i < totobj) {
295 a1fd68d8 2018-01-12 stsp struct got_object_id *oid = &packidx->sorted_ids[i++];
296 a1fd68d8 2018-01-12 stsp uint32_t offset;
297 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
298 a1fd68d8 2018-01-12 stsp
299 2b2ca9f0 2018-01-13 stsp if (cmp < 0)
300 a1fd68d8 2018-01-12 stsp continue;
301 2b2ca9f0 2018-01-13 stsp if (cmp > 0)
302 a1fd68d8 2018-01-12 stsp break;
303 a1fd68d8 2018-01-12 stsp
304 a1fd68d8 2018-01-12 stsp return i;
305 a1fd68d8 2018-01-12 stsp }
306 a1fd68d8 2018-01-12 stsp
307 a1fd68d8 2018-01-12 stsp return -1;
308 a1fd68d8 2018-01-12 stsp }
309 a1fd68d8 2018-01-12 stsp
310 a1fd68d8 2018-01-12 stsp const struct got_error *
311 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
312 a1fd68d8 2018-01-12 stsp {
313 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
314 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
315 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
316 a1fd68d8 2018-01-12 stsp size_t n;
317 a1fd68d8 2018-01-12 stsp
318 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
319 a1fd68d8 2018-01-12 stsp if (n != 1)
320 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
321 a1fd68d8 2018-01-12 stsp
322 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
323 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
324 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
325 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
326 a1fd68d8 2018-01-12 stsp
327 a1fd68d8 2018-01-12 stsp return err;
328 a1fd68d8 2018-01-12 stsp }
329 a1fd68d8 2018-01-12 stsp
330 a1fd68d8 2018-01-12 stsp static const struct got_error *
331 a487c1d0 2018-01-14 stsp dump_plain_object(FILE *infile, uint8_t type, uint64_t size, FILE *outfile)
332 a1fd68d8 2018-01-12 stsp {
333 a487c1d0 2018-01-14 stsp const char *type_tag = got_object_get_type_tag(type);
334 a487c1d0 2018-01-14 stsp size_t n;
335 a1fd68d8 2018-01-12 stsp
336 a487c1d0 2018-01-14 stsp if (type_tag == NULL)
337 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_OBJ_TYPE);
338 a1fd68d8 2018-01-12 stsp
339 a487c1d0 2018-01-14 stsp fprintf(outfile, "%s %llu", type_tag, size);
340 a487c1d0 2018-01-14 stsp fputc('\0', outfile);
341 a487c1d0 2018-01-14 stsp
342 a487c1d0 2018-01-14 stsp while (size > 0) {
343 a487c1d0 2018-01-14 stsp uint8_t data[2048];
344 a487c1d0 2018-01-14 stsp size_t len = MIN(size, sizeof(data));
345 a487c1d0 2018-01-14 stsp
346 a487c1d0 2018-01-14 stsp n = fread(data, len, 1, infile);
347 a487c1d0 2018-01-14 stsp if (n != 1)
348 a487c1d0 2018-01-14 stsp return got_ferror(infile, GOT_ERR_BAD_PACKIDX);
349 a487c1d0 2018-01-14 stsp
350 a487c1d0 2018-01-14 stsp n = fwrite(data, len, 1, outfile);
351 a487c1d0 2018-01-14 stsp if (n != 1)
352 a487c1d0 2018-01-14 stsp return got_ferror(outfile, GOT_ERR_BAD_PACKIDX);
353 a487c1d0 2018-01-14 stsp
354 a487c1d0 2018-01-14 stsp size -= len;
355 a1fd68d8 2018-01-12 stsp }
356 a1fd68d8 2018-01-12 stsp
357 a487c1d0 2018-01-14 stsp return NULL;
358 a487c1d0 2018-01-14 stsp }
359 a487c1d0 2018-01-14 stsp
360 a487c1d0 2018-01-14 stsp static const struct got_error *
361 a487c1d0 2018-01-14 stsp decode_type_and_size(uint8_t *type, uint64_t *size, FILE *packfile)
362 a487c1d0 2018-01-14 stsp {
363 a487c1d0 2018-01-14 stsp uint8_t t = 0;
364 a487c1d0 2018-01-14 stsp uint64_t s = 0;
365 a487c1d0 2018-01-14 stsp uint8_t sizeN;
366 a487c1d0 2018-01-14 stsp size_t n;
367 a487c1d0 2018-01-14 stsp int i = 0;
368 a487c1d0 2018-01-14 stsp
369 a1fd68d8 2018-01-12 stsp do {
370 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
371 a487c1d0 2018-01-14 stsp if (i > 9)
372 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
373 a1fd68d8 2018-01-12 stsp
374 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
375 a487c1d0 2018-01-14 stsp if (n != 1)
376 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
377 8251fdbc 2018-01-12 stsp
378 a1fd68d8 2018-01-12 stsp if (i == 0) {
379 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
380 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
381 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
382 a1fd68d8 2018-01-12 stsp } else {
383 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
384 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
385 a1fd68d8 2018-01-12 stsp }
386 a1fd68d8 2018-01-12 stsp i++;
387 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
388 a1fd68d8 2018-01-12 stsp
389 a487c1d0 2018-01-14 stsp *type = t;
390 a487c1d0 2018-01-14 stsp *size = s;
391 a487c1d0 2018-01-14 stsp return NULL;
392 a487c1d0 2018-01-14 stsp }
393 a1fd68d8 2018-01-12 stsp
394 a487c1d0 2018-01-14 stsp static const struct got_error *
395 a487c1d0 2018-01-14 stsp dump_packed_object(FILE **f, FILE *packfile, off_t offset)
396 a487c1d0 2018-01-14 stsp {
397 a487c1d0 2018-01-14 stsp const struct got_error *err = NULL;
398 a487c1d0 2018-01-14 stsp const char *template = "/tmp/got.XXXXXXXXXX";
399 a487c1d0 2018-01-14 stsp uint8_t type;
400 a487c1d0 2018-01-14 stsp uint64_t size;
401 a487c1d0 2018-01-14 stsp FILE *outfile = NULL;
402 a487c1d0 2018-01-14 stsp
403 a487c1d0 2018-01-14 stsp *f = got_opentemp();
404 a487c1d0 2018-01-14 stsp if (*f == NULL) {
405 a487c1d0 2018-01-14 stsp err = got_error(GOT_ERR_FILE_OPEN);
406 a1fd68d8 2018-01-12 stsp goto done;
407 a1fd68d8 2018-01-12 stsp }
408 a1fd68d8 2018-01-12 stsp
409 a487c1d0 2018-01-14 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
410 a487c1d0 2018-01-14 stsp err = got_error_from_errno();
411 a487c1d0 2018-01-14 stsp goto done;
412 a487c1d0 2018-01-14 stsp }
413 a1fd68d8 2018-01-12 stsp
414 a487c1d0 2018-01-14 stsp err = decode_type_and_size(&type, &size, packfile);
415 a487c1d0 2018-01-14 stsp if (err)
416 a487c1d0 2018-01-14 stsp goto done;
417 a1fd68d8 2018-01-12 stsp
418 a487c1d0 2018-01-14 stsp switch (type) {
419 a487c1d0 2018-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
420 a487c1d0 2018-01-14 stsp case GOT_OBJ_TYPE_TREE:
421 a487c1d0 2018-01-14 stsp case GOT_OBJ_TYPE_BLOB:
422 a487c1d0 2018-01-14 stsp err = dump_plain_object(packfile, type, size, *f);
423 a487c1d0 2018-01-14 stsp break;
424 a487c1d0 2018-01-14 stsp case GOT_OBJ_TYPE_REF_DELTA:
425 a487c1d0 2018-01-14 stsp case GOT_OBJ_TYPE_TAG:
426 a487c1d0 2018-01-14 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
427 a487c1d0 2018-01-14 stsp default:
428 a487c1d0 2018-01-14 stsp err = got_error(GOT_ERR_NOT_IMPL);
429 a487c1d0 2018-01-14 stsp goto done;
430 a1fd68d8 2018-01-12 stsp }
431 a1fd68d8 2018-01-12 stsp
432 a1fd68d8 2018-01-12 stsp rewind(*f);
433 a1fd68d8 2018-01-12 stsp done:
434 a1fd68d8 2018-01-12 stsp if (err && *f)
435 a1fd68d8 2018-01-12 stsp fclose(*f);
436 a1fd68d8 2018-01-12 stsp return err;
437 0a0a3048 2018-01-10 stsp }
438 c54542a0 2018-01-13 stsp
439 a1fd68d8 2018-01-12 stsp static const struct got_error *
440 a1fd68d8 2018-01-12 stsp extract_object(FILE **f, const char *path_packdir,
441 a1fd68d8 2018-01-12 stsp struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
442 a1fd68d8 2018-01-12 stsp {
443 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
444 a1fd68d8 2018-01-12 stsp int idx = get_object_idx(packidx, id);
445 a1fd68d8 2018-01-12 stsp off_t offset;
446 a1fd68d8 2018-01-12 stsp char *path_packfile;
447 a1fd68d8 2018-01-12 stsp FILE *packfile;
448 a1fd68d8 2018-01-12 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
449 a1fd68d8 2018-01-12 stsp char *sha1str;
450 a1fd68d8 2018-01-12 stsp
451 a1fd68d8 2018-01-12 stsp *f = NULL;
452 a1fd68d8 2018-01-12 stsp if (idx == -1) /* object not found in pack index */
453 a1fd68d8 2018-01-12 stsp return NULL;
454 a1fd68d8 2018-01-12 stsp
455 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
456 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
457 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
458 a1fd68d8 2018-01-12 stsp
459 a1fd68d8 2018-01-12 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
460 a1fd68d8 2018-01-12 stsp hex, sizeof(hex));
461 a1fd68d8 2018-01-12 stsp if (sha1str == NULL)
462 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
463 a1fd68d8 2018-01-12 stsp
464 a1fd68d8 2018-01-12 stsp if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
465 a1fd68d8 2018-01-12 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
466 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_NO_MEM);
467 a1fd68d8 2018-01-12 stsp
468 a1fd68d8 2018-01-12 stsp packfile = fopen(path_packfile, "rb");
469 a1fd68d8 2018-01-12 stsp if (packfile == NULL) {
470 f334529e 2018-01-12 stsp err = got_error_from_errno();
471 a1fd68d8 2018-01-12 stsp goto done;
472 a1fd68d8 2018-01-12 stsp }
473 a1fd68d8 2018-01-12 stsp
474 a1fd68d8 2018-01-12 stsp err = read_packfile_hdr(packfile, packidx);
475 a1fd68d8 2018-01-12 stsp if (err)
476 a1fd68d8 2018-01-12 stsp goto done;
477 a1fd68d8 2018-01-12 stsp
478 a1fd68d8 2018-01-12 stsp printf("Dumping object at offset %llu\n", offset);
479 a1fd68d8 2018-01-12 stsp err = dump_packed_object(f, packfile, offset);
480 a1fd68d8 2018-01-12 stsp if (err)
481 a1fd68d8 2018-01-12 stsp goto done;
482 a1fd68d8 2018-01-12 stsp
483 a1fd68d8 2018-01-12 stsp done:
484 a1fd68d8 2018-01-12 stsp free(path_packfile);
485 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
486 f334529e 2018-01-12 stsp err = got_error_from_errno();
487 a1fd68d8 2018-01-12 stsp return err;
488 a1fd68d8 2018-01-12 stsp }
489 a1fd68d8 2018-01-12 stsp
490 a1fd68d8 2018-01-12 stsp const struct got_error *
491 a1fd68d8 2018-01-12 stsp got_packfile_extract_object(FILE **f, struct got_object_id *id,
492 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
493 a1fd68d8 2018-01-12 stsp {
494 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
495 a1fd68d8 2018-01-12 stsp DIR *packdir = NULL;
496 a1fd68d8 2018-01-12 stsp struct dirent *dent;
497 a1fd68d8 2018-01-12 stsp char *path_packdir = got_repo_get_path_objects_pack(repo);
498 a1fd68d8 2018-01-12 stsp
499 a1fd68d8 2018-01-12 stsp if (path_packdir == NULL) {
500 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_NO_MEM);
501 a1fd68d8 2018-01-12 stsp goto done;
502 a1fd68d8 2018-01-12 stsp }
503 a1fd68d8 2018-01-12 stsp
504 a1fd68d8 2018-01-12 stsp packdir = opendir(path_packdir);
505 a1fd68d8 2018-01-12 stsp if (packdir == NULL) {
506 f334529e 2018-01-12 stsp err = got_error_from_errno();
507 a1fd68d8 2018-01-12 stsp goto done;
508 a1fd68d8 2018-01-12 stsp }
509 a1fd68d8 2018-01-12 stsp
510 a1fd68d8 2018-01-12 stsp while ((dent = readdir(packdir)) != NULL) {
511 a1fd68d8 2018-01-12 stsp struct got_packidx_v2_hdr *packidx;
512 a1fd68d8 2018-01-12 stsp char *path_packidx, *path_object;
513 a1fd68d8 2018-01-12 stsp
514 a1fd68d8 2018-01-12 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
515 a1fd68d8 2018-01-12 stsp continue;
516 a1fd68d8 2018-01-12 stsp
517 a1fd68d8 2018-01-12 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
518 a1fd68d8 2018-01-12 stsp dent->d_name) == -1) {
519 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_NO_MEM);
520 a1fd68d8 2018-01-12 stsp goto done;
521 a1fd68d8 2018-01-12 stsp }
522 a1fd68d8 2018-01-12 stsp
523 a1fd68d8 2018-01-12 stsp err = got_packidx_open(&packidx, path_packidx);
524 a1fd68d8 2018-01-12 stsp free(path_packidx);
525 a1fd68d8 2018-01-12 stsp if (err)
526 a1fd68d8 2018-01-12 stsp goto done;
527 a1fd68d8 2018-01-12 stsp
528 a1fd68d8 2018-01-12 stsp err = extract_object(f, path_packdir, packidx, id);
529 a1fd68d8 2018-01-12 stsp if (err)
530 a1fd68d8 2018-01-12 stsp goto done;
531 a1fd68d8 2018-01-12 stsp if (*f != NULL)
532 a1fd68d8 2018-01-12 stsp break;
533 a1fd68d8 2018-01-12 stsp }
534 a1fd68d8 2018-01-12 stsp
535 a1fd68d8 2018-01-12 stsp done:
536 a1fd68d8 2018-01-12 stsp free(path_packdir);
537 f334529e 2018-01-12 stsp if (packdir && closedir(packdir) != 0 && err == 0)
538 f334529e 2018-01-12 stsp err = got_error_from_errno();
539 a1fd68d8 2018-01-12 stsp return err;
540 a1fd68d8 2018-01-12 stsp }