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 0a0a3048 2018-01-10 stsp
40 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
41 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
42 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
43 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
44 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
45 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
46 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
47 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
48 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
49 a1fd68d8 2018-01-12 stsp
50 a1fd68d8 2018-01-12 stsp #ifndef MIN
51 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 a1fd68d8 2018-01-12 stsp #endif
53 a1fd68d8 2018-01-12 stsp
54 0a0a3048 2018-01-10 stsp static const struct got_error *
55 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
56 0a0a3048 2018-01-10 stsp {
57 0a0a3048 2018-01-10 stsp int i;
58 0a0a3048 2018-01-10 stsp
59 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
60 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
61 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
62 0a0a3048 2018-01-10 stsp }
63 0a0a3048 2018-01-10 stsp
64 0a0a3048 2018-01-10 stsp return NULL;
65 0a0a3048 2018-01-10 stsp }
66 0a0a3048 2018-01-10 stsp
67 24541888 2018-01-10 stsp static const struct got_error *
68 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
69 0a0a3048 2018-01-10 stsp {
70 0a0a3048 2018-01-10 stsp struct stat sb;
71 0a0a3048 2018-01-10 stsp char *path_pack;
72 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
73 0a0a3048 2018-01-10 stsp char *dot;
74 0a0a3048 2018-01-10 stsp
75 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
76 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
77 0a0a3048 2018-01-10 stsp
78 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
79 0a0a3048 2018-01-10 stsp if (dot == NULL)
80 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
81 0a0a3048 2018-01-10 stsp *dot = '\0';
82 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
83 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_MEM);
84 0a0a3048 2018-01-10 stsp
85 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
86 0a0a3048 2018-01-10 stsp free(path_pack);
87 8251fdbc 2018-01-12 stsp return got_error_from_errno();
88 0a0a3048 2018-01-10 stsp }
89 0a0a3048 2018-01-10 stsp
90 0a0a3048 2018-01-10 stsp free(path_pack);
91 0a0a3048 2018-01-10 stsp *size = sb.st_size;
92 0a0a3048 2018-01-10 stsp return 0;
93 0a0a3048 2018-01-10 stsp }
94 0a0a3048 2018-01-10 stsp
95 0a0a3048 2018-01-10 stsp const struct got_error *
96 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
97 0a0a3048 2018-01-10 stsp {
98 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
99 0a0a3048 2018-01-10 stsp FILE *f;
100 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
101 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
102 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
103 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
104 0a0a3048 2018-01-10 stsp
105 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
106 0ebaf008 2018-01-10 stsp
107 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
108 0a0a3048 2018-01-10 stsp if (f == NULL)
109 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
110 0a0a3048 2018-01-10 stsp
111 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
112 0a0a3048 2018-01-10 stsp if (err)
113 0a0a3048 2018-01-10 stsp return err;
114 0a0a3048 2018-01-10 stsp
115 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
116 0a0a3048 2018-01-10 stsp if (p == NULL) {
117 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
118 0a0a3048 2018-01-10 stsp goto done;
119 0a0a3048 2018-01-10 stsp }
120 0a0a3048 2018-01-10 stsp
121 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
122 0a0a3048 2018-01-10 stsp if (n != 1) {
123 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
124 0a0a3048 2018-01-10 stsp goto done;
125 0a0a3048 2018-01-10 stsp }
126 0a0a3048 2018-01-10 stsp
127 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
128 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
129 0a0a3048 2018-01-10 stsp goto done;
130 0a0a3048 2018-01-10 stsp }
131 0a0a3048 2018-01-10 stsp
132 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
133 0ebaf008 2018-01-10 stsp
134 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
135 0a0a3048 2018-01-10 stsp if (n != 1) {
136 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
137 0a0a3048 2018-01-10 stsp goto done;
138 0a0a3048 2018-01-10 stsp }
139 0a0a3048 2018-01-10 stsp
140 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
141 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
142 0a0a3048 2018-01-10 stsp goto done;
143 0a0a3048 2018-01-10 stsp }
144 0a0a3048 2018-01-10 stsp
145 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
146 0ebaf008 2018-01-10 stsp
147 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
148 0a0a3048 2018-01-10 stsp if (n != 1) {
149 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
150 0a0a3048 2018-01-10 stsp goto done;
151 0a0a3048 2018-01-10 stsp }
152 0a0a3048 2018-01-10 stsp
153 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
154 0a0a3048 2018-01-10 stsp if (err)
155 0a0a3048 2018-01-10 stsp goto done;
156 0a0a3048 2018-01-10 stsp
157 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
158 0ebaf008 2018-01-10 stsp
159 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
160 0a0a3048 2018-01-10 stsp
161 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
162 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
163 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
164 0a0a3048 2018-01-10 stsp goto done;
165 0a0a3048 2018-01-10 stsp }
166 0a0a3048 2018-01-10 stsp
167 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
168 0a0a3048 2018-01-10 stsp if (n != nobj) {
169 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
170 0a0a3048 2018-01-10 stsp goto done;
171 0a0a3048 2018-01-10 stsp }
172 0a0a3048 2018-01-10 stsp
173 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
174 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
175 0ebaf008 2018-01-10 stsp
176 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
177 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
178 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
179 0a0a3048 2018-01-10 stsp goto done;
180 0a0a3048 2018-01-10 stsp }
181 0a0a3048 2018-01-10 stsp
182 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
183 0a0a3048 2018-01-10 stsp if (n != nobj) {
184 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
185 0a0a3048 2018-01-10 stsp goto done;
186 0a0a3048 2018-01-10 stsp }
187 0a0a3048 2018-01-10 stsp
188 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
189 0ebaf008 2018-01-10 stsp
190 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
191 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
192 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
193 0a0a3048 2018-01-10 stsp goto done;
194 0a0a3048 2018-01-10 stsp }
195 0a0a3048 2018-01-10 stsp
196 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
197 0a0a3048 2018-01-10 stsp if (n != nobj) {
198 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
199 0a0a3048 2018-01-10 stsp goto done;
200 0a0a3048 2018-01-10 stsp }
201 0a0a3048 2018-01-10 stsp
202 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
203 0ebaf008 2018-01-10 stsp
204 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
205 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
206 0a0a3048 2018-01-10 stsp goto checksum;
207 0a0a3048 2018-01-10 stsp
208 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
209 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
210 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
211 0a0a3048 2018-01-10 stsp goto done;
212 0a0a3048 2018-01-10 stsp }
213 0a0a3048 2018-01-10 stsp
214 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
215 0a0a3048 2018-01-10 stsp if (n != nobj) {
216 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
217 0a0a3048 2018-01-10 stsp goto done;
218 0a0a3048 2018-01-10 stsp }
219 0a0a3048 2018-01-10 stsp
220 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
221 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
222 0ebaf008 2018-01-10 stsp
223 0a0a3048 2018-01-10 stsp checksum:
224 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
225 0a0a3048 2018-01-10 stsp if (n != 1) {
226 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
227 0a0a3048 2018-01-10 stsp goto done;
228 0a0a3048 2018-01-10 stsp }
229 0a0a3048 2018-01-10 stsp
230 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
231 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
232 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
233 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
234 0a0a3048 2018-01-10 stsp done:
235 0a0a3048 2018-01-10 stsp fclose(f);
236 0a0a3048 2018-01-10 stsp if (err)
237 0a0a3048 2018-01-10 stsp got_packidx_close(p);
238 0a0a3048 2018-01-10 stsp else
239 0a0a3048 2018-01-10 stsp *packidx = p;
240 0a0a3048 2018-01-10 stsp return err;
241 0a0a3048 2018-01-10 stsp }
242 0a0a3048 2018-01-10 stsp
243 0a0a3048 2018-01-10 stsp void
244 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
245 0a0a3048 2018-01-10 stsp {
246 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
247 0a0a3048 2018-01-10 stsp free(packidx->offsets);
248 0a0a3048 2018-01-10 stsp free(packidx->crc32);
249 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
250 0a0a3048 2018-01-10 stsp free(packidx);
251 a1fd68d8 2018-01-12 stsp }
252 a1fd68d8 2018-01-12 stsp
253 a1fd68d8 2018-01-12 stsp static int
254 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
255 a1fd68d8 2018-01-12 stsp {
256 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
257 a1fd68d8 2018-01-12 stsp return 0;
258 a1fd68d8 2018-01-12 stsp
259 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
260 a1fd68d8 2018-01-12 stsp return 0;
261 a1fd68d8 2018-01-12 stsp
262 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
263 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
264 a1fd68d8 2018-01-12 stsp return 0;
265 a1fd68d8 2018-01-12 stsp
266 a1fd68d8 2018-01-12 stsp return 1;
267 a1fd68d8 2018-01-12 stsp }
268 a1fd68d8 2018-01-12 stsp
269 a1fd68d8 2018-01-12 stsp static off_t
270 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
271 a1fd68d8 2018-01-12 stsp {
272 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
273 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
274 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
275 a1fd68d8 2018-01-12 stsp uint64_t loffset;
276 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
277 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
278 a1fd68d8 2018-01-12 stsp return -1;
279 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
280 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
281 a1fd68d8 2018-01-12 stsp }
282 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
283 a1fd68d8 2018-01-12 stsp }
284 a1fd68d8 2018-01-12 stsp
285 a1fd68d8 2018-01-12 stsp static int
286 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
287 a1fd68d8 2018-01-12 stsp {
288 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
289 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
290 a1fd68d8 2018-01-12 stsp int i = 0;
291 a1fd68d8 2018-01-12 stsp
292 a1fd68d8 2018-01-12 stsp if (id0 > 0)
293 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
294 a1fd68d8 2018-01-12 stsp
295 a1fd68d8 2018-01-12 stsp while (i < totobj) {
296 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
297 a1fd68d8 2018-01-12 stsp uint32_t offset;
298 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
299 a1fd68d8 2018-01-12 stsp
300 6c00b545 2018-01-17 stsp if (cmp == 0)
301 6c00b545 2018-01-17 stsp return i;
302 6c00b545 2018-01-17 stsp else if (cmp > 0)
303 a1fd68d8 2018-01-12 stsp break;
304 6c00b545 2018-01-17 stsp 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 a487c1d0 2018-01-14 stsp }
329 a487c1d0 2018-01-14 stsp
330 a487c1d0 2018-01-14 stsp static const struct got_error *
331 3ee5fc21 2018-01-17 stsp decode_type_and_size(uint8_t *type, uint64_t *size, size_t *len, FILE *packfile)
332 a487c1d0 2018-01-14 stsp {
333 a487c1d0 2018-01-14 stsp uint8_t t = 0;
334 a487c1d0 2018-01-14 stsp uint64_t s = 0;
335 a487c1d0 2018-01-14 stsp uint8_t sizeN;
336 a487c1d0 2018-01-14 stsp size_t n;
337 a487c1d0 2018-01-14 stsp int i = 0;
338 a487c1d0 2018-01-14 stsp
339 a1fd68d8 2018-01-12 stsp do {
340 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
341 a487c1d0 2018-01-14 stsp if (i > 9)
342 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
343 a1fd68d8 2018-01-12 stsp
344 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
345 a487c1d0 2018-01-14 stsp if (n != 1)
346 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
347 8251fdbc 2018-01-12 stsp
348 a1fd68d8 2018-01-12 stsp if (i == 0) {
349 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
350 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
351 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
352 a1fd68d8 2018-01-12 stsp } else {
353 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
354 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
355 a1fd68d8 2018-01-12 stsp }
356 a1fd68d8 2018-01-12 stsp i++;
357 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
358 a1fd68d8 2018-01-12 stsp
359 a487c1d0 2018-01-14 stsp *type = t;
360 a487c1d0 2018-01-14 stsp *size = s;
361 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
362 a487c1d0 2018-01-14 stsp return NULL;
363 0a0a3048 2018-01-10 stsp }
364 c54542a0 2018-01-13 stsp
365 a1fd68d8 2018-01-12 stsp static const struct got_error *
366 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
367 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
368 6ccb713b 2018-01-19 stsp {
369 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
370 6ccb713b 2018-01-19 stsp if (*obj == NULL)
371 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
372 6ccb713b 2018-01-19 stsp
373 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
374 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
375 6ccb713b 2018-01-19 stsp free(*obj);
376 6ccb713b 2018-01-19 stsp *obj = NULL;
377 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
378 6ccb713b 2018-01-19 stsp }
379 6ccb713b 2018-01-19 stsp
380 6ccb713b 2018-01-19 stsp (*obj)->type = type;
381 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
382 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
383 6ccb713b 2018-01-19 stsp (*obj)->size = size;
384 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
385 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
386 b107e67f 2018-01-19 stsp
387 b107e67f 2018-01-19 stsp return NULL;
388 b107e67f 2018-01-19 stsp }
389 b107e67f 2018-01-19 stsp
390 b107e67f 2018-01-19 stsp static const struct got_error *
391 b107e67f 2018-01-19 stsp decode_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
392 b107e67f 2018-01-19 stsp {
393 b107e67f 2018-01-19 stsp int64_t o = 0;
394 b107e67f 2018-01-19 stsp uint8_t offN;
395 b107e67f 2018-01-19 stsp size_t n;
396 b107e67f 2018-01-19 stsp int i = 0;
397 b107e67f 2018-01-19 stsp
398 b107e67f 2018-01-19 stsp do {
399 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
400 b107e67f 2018-01-19 stsp if (i > 8)
401 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
402 b107e67f 2018-01-19 stsp
403 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
404 b107e67f 2018-01-19 stsp if (n != 1)
405 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
406 b107e67f 2018-01-19 stsp
407 b107e67f 2018-01-19 stsp if (i == 0)
408 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
409 b107e67f 2018-01-19 stsp else {
410 b107e67f 2018-01-19 stsp int j;
411 b107e67f 2018-01-19 stsp int64_t v = 128;
412 b107e67f 2018-01-19 stsp o <<= 7;
413 b107e67f 2018-01-19 stsp o |= (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
414 b107e67f 2018-01-19 stsp o += v;
415 b107e67f 2018-01-19 stsp for (j = 0; j < i; j++) {
416 b107e67f 2018-01-19 stsp v <<= 7;
417 b107e67f 2018-01-19 stsp o += v;
418 b107e67f 2018-01-19 stsp }
419 b107e67f 2018-01-19 stsp }
420 b107e67f 2018-01-19 stsp i++;
421 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
422 6ccb713b 2018-01-19 stsp
423 b107e67f 2018-01-19 stsp *offset = o;
424 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
425 6ccb713b 2018-01-19 stsp return NULL;
426 6ccb713b 2018-01-19 stsp }
427 6ccb713b 2018-01-19 stsp
428 6ccb713b 2018-01-19 stsp static const struct got_error *
429 b107e67f 2018-01-19 stsp open_offset_delta_object(struct got_object **obj, struct got_repository *repo,
430 b107e67f 2018-01-19 stsp const char *path_packfile, FILE *packfile, struct got_object_id *id,
431 b107e67f 2018-01-19 stsp off_t offset, size_t size)
432 b107e67f 2018-01-19 stsp {
433 b107e67f 2018-01-19 stsp const struct got_error *err = NULL;
434 b107e67f 2018-01-19 stsp int64_t negoffset;
435 b107e67f 2018-01-19 stsp size_t negofflen;
436 b107e67f 2018-01-19 stsp off_t base_obj_offset;
437 b107e67f 2018-01-19 stsp struct got_object *base_obj;
438 b107e67f 2018-01-19 stsp struct got_object_id base_id;
439 b107e67f 2018-01-19 stsp uint8_t base_type;
440 b107e67f 2018-01-19 stsp uint64_t base_size;
441 b107e67f 2018-01-19 stsp size_t base_tslen;
442 b107e67f 2018-01-19 stsp
443 b107e67f 2018-01-19 stsp err = decode_negative_offset(&negoffset, &negofflen, packfile);
444 b107e67f 2018-01-19 stsp if (err)
445 b107e67f 2018-01-19 stsp return err;
446 b107e67f 2018-01-19 stsp
447 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
448 b107e67f 2018-01-19 stsp base_obj_offset = (offset - negoffset);
449 b107e67f 2018-01-19 stsp if (base_obj_offset <= 0)
450 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
451 b107e67f 2018-01-19 stsp
452 b107e67f 2018-01-19 stsp if (fseeko(packfile, base_obj_offset, SEEK_SET) != 0)
453 b107e67f 2018-01-19 stsp return got_error_from_errno();
454 b107e67f 2018-01-19 stsp
455 b107e67f 2018-01-19 stsp err = decode_type_and_size(&base_type, &base_size, &base_tslen,
456 b107e67f 2018-01-19 stsp packfile);
457 b107e67f 2018-01-19 stsp if (err)
458 b107e67f 2018-01-19 stsp return err;
459 b107e67f 2018-01-19 stsp
460 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
461 b107e67f 2018-01-19 stsp if (*obj == NULL)
462 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
463 b107e67f 2018-01-19 stsp
464 b107e67f 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
465 b107e67f 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
466 b107e67f 2018-01-19 stsp free(*obj);
467 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
468 b107e67f 2018-01-19 stsp }
469 b107e67f 2018-01-19 stsp (*obj)->type = GOT_OBJ_TYPE_OFFSET_DELTA;
470 b107e67f 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
471 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
472 b107e67f 2018-01-19 stsp (*obj)->size = size;
473 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
474 b107e67f 2018-01-19 stsp (*obj)->pack_offset = offset;
475 b107e67f 2018-01-19 stsp (*obj)->base_type = base_type;
476 b107e67f 2018-01-19 stsp (*obj)->base_size = base_size;
477 b107e67f 2018-01-19 stsp (*obj)->base_obj_offset = base_obj_offset;
478 b107e67f 2018-01-19 stsp
479 b107e67f 2018-01-19 stsp return NULL;
480 b107e67f 2018-01-19 stsp }
481 b107e67f 2018-01-19 stsp
482 b107e67f 2018-01-19 stsp static const struct got_error *
483 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
484 6c00b545 2018-01-17 stsp const char *path_packdir, struct got_packidx_v2_hdr *packidx,
485 6c00b545 2018-01-17 stsp struct got_object_id *id)
486 a1fd68d8 2018-01-12 stsp {
487 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
488 a1fd68d8 2018-01-12 stsp int idx = get_object_idx(packidx, id);
489 a1fd68d8 2018-01-12 stsp off_t offset;
490 a1fd68d8 2018-01-12 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
491 a1fd68d8 2018-01-12 stsp char *sha1str;
492 6c00b545 2018-01-17 stsp char *path_packfile;
493 6c00b545 2018-01-17 stsp FILE *packfile;
494 6c00b545 2018-01-17 stsp uint8_t type;
495 6c00b545 2018-01-17 stsp uint64_t size;
496 3ee5fc21 2018-01-17 stsp size_t tslen;
497 a1fd68d8 2018-01-12 stsp
498 6c00b545 2018-01-17 stsp *obj = NULL;
499 a1fd68d8 2018-01-12 stsp if (idx == -1) /* object not found in pack index */
500 a1fd68d8 2018-01-12 stsp return NULL;
501 a1fd68d8 2018-01-12 stsp
502 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
503 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
504 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
505 a1fd68d8 2018-01-12 stsp
506 a1fd68d8 2018-01-12 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
507 a1fd68d8 2018-01-12 stsp hex, sizeof(hex));
508 a1fd68d8 2018-01-12 stsp if (sha1str == NULL)
509 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
510 a1fd68d8 2018-01-12 stsp
511 a1fd68d8 2018-01-12 stsp if (asprintf(&path_packfile, "%s/%s%s%s", path_packdir,
512 a1fd68d8 2018-01-12 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1)
513 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_NO_MEM);
514 a1fd68d8 2018-01-12 stsp
515 a1fd68d8 2018-01-12 stsp packfile = fopen(path_packfile, "rb");
516 a1fd68d8 2018-01-12 stsp if (packfile == NULL) {
517 f334529e 2018-01-12 stsp err = got_error_from_errno();
518 a1fd68d8 2018-01-12 stsp goto done;
519 a1fd68d8 2018-01-12 stsp }
520 a1fd68d8 2018-01-12 stsp
521 a1fd68d8 2018-01-12 stsp err = read_packfile_hdr(packfile, packidx);
522 a1fd68d8 2018-01-12 stsp if (err)
523 a1fd68d8 2018-01-12 stsp goto done;
524 a1fd68d8 2018-01-12 stsp
525 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
526 6c00b545 2018-01-17 stsp err = got_error_from_errno();
527 6c00b545 2018-01-17 stsp goto done;
528 6c00b545 2018-01-17 stsp }
529 6c00b545 2018-01-17 stsp
530 3ee5fc21 2018-01-17 stsp err = decode_type_and_size(&type, &size, &tslen, packfile);
531 a1fd68d8 2018-01-12 stsp if (err)
532 a1fd68d8 2018-01-12 stsp goto done;
533 a1fd68d8 2018-01-12 stsp
534 6c00b545 2018-01-17 stsp switch (type) {
535 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
536 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
537 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
538 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
539 6ccb713b 2018-01-19 stsp offset + tslen, size);
540 6c00b545 2018-01-17 stsp break;
541 6ccb713b 2018-01-19 stsp
542 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
543 b107e67f 2018-01-19 stsp err = open_offset_delta_object(obj, repo, path_packfile,
544 b107e67f 2018-01-19 stsp packfile, id, offset + tslen, size);
545 b107e67f 2018-01-19 stsp break;
546 b107e67f 2018-01-19 stsp
547 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_REF_DELTA:
548 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TAG:
549 b107e67f 2018-01-19 stsp break;
550 6c00b545 2018-01-17 stsp default:
551 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
552 6c00b545 2018-01-17 stsp goto done;
553 6c00b545 2018-01-17 stsp }
554 a1fd68d8 2018-01-12 stsp done:
555 a1fd68d8 2018-01-12 stsp free(path_packfile);
556 6c00b545 2018-01-17 stsp if (err)
557 6c00b545 2018-01-17 stsp free(*obj);
558 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
559 f334529e 2018-01-12 stsp err = got_error_from_errno();
560 a1fd68d8 2018-01-12 stsp return err;
561 a1fd68d8 2018-01-12 stsp }
562 a1fd68d8 2018-01-12 stsp
563 a1fd68d8 2018-01-12 stsp const struct got_error *
564 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
565 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
566 a1fd68d8 2018-01-12 stsp {
567 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
568 a1fd68d8 2018-01-12 stsp DIR *packdir = NULL;
569 a1fd68d8 2018-01-12 stsp struct dirent *dent;
570 a1fd68d8 2018-01-12 stsp char *path_packdir = got_repo_get_path_objects_pack(repo);
571 a1fd68d8 2018-01-12 stsp
572 a1fd68d8 2018-01-12 stsp if (path_packdir == NULL) {
573 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_NO_MEM);
574 a1fd68d8 2018-01-12 stsp goto done;
575 a1fd68d8 2018-01-12 stsp }
576 a1fd68d8 2018-01-12 stsp
577 a1fd68d8 2018-01-12 stsp packdir = opendir(path_packdir);
578 a1fd68d8 2018-01-12 stsp if (packdir == NULL) {
579 f334529e 2018-01-12 stsp err = got_error_from_errno();
580 a1fd68d8 2018-01-12 stsp goto done;
581 a1fd68d8 2018-01-12 stsp }
582 a1fd68d8 2018-01-12 stsp
583 a1fd68d8 2018-01-12 stsp while ((dent = readdir(packdir)) != NULL) {
584 a1fd68d8 2018-01-12 stsp struct got_packidx_v2_hdr *packidx;
585 a1fd68d8 2018-01-12 stsp char *path_packidx, *path_object;
586 a1fd68d8 2018-01-12 stsp
587 a1fd68d8 2018-01-12 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
588 a1fd68d8 2018-01-12 stsp continue;
589 a1fd68d8 2018-01-12 stsp
590 a1fd68d8 2018-01-12 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
591 a1fd68d8 2018-01-12 stsp dent->d_name) == -1) {
592 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_NO_MEM);
593 a1fd68d8 2018-01-12 stsp goto done;
594 a1fd68d8 2018-01-12 stsp }
595 a1fd68d8 2018-01-12 stsp
596 a1fd68d8 2018-01-12 stsp err = got_packidx_open(&packidx, path_packidx);
597 a1fd68d8 2018-01-12 stsp free(path_packidx);
598 a1fd68d8 2018-01-12 stsp if (err)
599 a1fd68d8 2018-01-12 stsp goto done;
600 a1fd68d8 2018-01-12 stsp
601 6c00b545 2018-01-17 stsp err = open_packed_object(obj, repo, path_packdir, packidx, id);
602 bbcf6d65 2018-01-17 stsp got_packidx_close(packidx);
603 a1fd68d8 2018-01-12 stsp if (err)
604 a1fd68d8 2018-01-12 stsp goto done;
605 6c00b545 2018-01-17 stsp if (*obj != NULL)
606 a1fd68d8 2018-01-12 stsp break;
607 a1fd68d8 2018-01-12 stsp }
608 a1fd68d8 2018-01-12 stsp
609 a1fd68d8 2018-01-12 stsp done:
610 a1fd68d8 2018-01-12 stsp free(path_packdir);
611 f334529e 2018-01-12 stsp if (packdir && closedir(packdir) != 0 && err == 0)
612 3ee5fc21 2018-01-17 stsp err = got_error_from_errno();
613 3ee5fc21 2018-01-17 stsp return err;
614 3ee5fc21 2018-01-17 stsp }
615 3ee5fc21 2018-01-17 stsp
616 3ee5fc21 2018-01-17 stsp static const struct got_error *
617 3ee5fc21 2018-01-17 stsp dump_plain_object(FILE *infile, uint8_t type, size_t size, FILE *outfile)
618 3ee5fc21 2018-01-17 stsp {
619 3ee5fc21 2018-01-17 stsp size_t n;
620 3ee5fc21 2018-01-17 stsp
621 3ee5fc21 2018-01-17 stsp while (size > 0) {
622 3ee5fc21 2018-01-17 stsp uint8_t data[2048];
623 3ee5fc21 2018-01-17 stsp size_t len = MIN(size, sizeof(data));
624 3ee5fc21 2018-01-17 stsp
625 3ee5fc21 2018-01-17 stsp n = fread(data, len, 1, infile);
626 3ee5fc21 2018-01-17 stsp if (n != 1)
627 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
628 3ee5fc21 2018-01-17 stsp
629 3ee5fc21 2018-01-17 stsp n = fwrite(data, len, 1, outfile);
630 3ee5fc21 2018-01-17 stsp if (n != 1)
631 efd2a263 2018-01-19 stsp return got_ferror(outfile, GOT_ERR_IO);
632 3ee5fc21 2018-01-17 stsp
633 3ee5fc21 2018-01-17 stsp size -= len;
634 3ee5fc21 2018-01-17 stsp }
635 3ee5fc21 2018-01-17 stsp
636 3ee5fc21 2018-01-17 stsp rewind(outfile);
637 3ee5fc21 2018-01-17 stsp return NULL;
638 3ee5fc21 2018-01-17 stsp }
639 efd2a263 2018-01-19 stsp
640 efd2a263 2018-01-19 stsp static const struct got_error *
641 efd2a263 2018-01-19 stsp dump_ref_delta_object(struct got_repository *repo, FILE *infile, uint8_t type,
642 efd2a263 2018-01-19 stsp size_t size, FILE *outfile)
643 efd2a263 2018-01-19 stsp {
644 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
645 efd2a263 2018-01-19 stsp struct got_object_id base_id;
646 efd2a263 2018-01-19 stsp struct got_object *base_obj;
647 b107e67f 2018-01-19 stsp size_t n;
648 3ee5fc21 2018-01-17 stsp
649 efd2a263 2018-01-19 stsp if (size < sizeof(base_id))
650 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
651 efd2a263 2018-01-19 stsp
652 efd2a263 2018-01-19 stsp n = fread(&base_id, sizeof(base_id), 1, infile);
653 efd2a263 2018-01-19 stsp if (n != 1)
654 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
655 efd2a263 2018-01-19 stsp
656 efd2a263 2018-01-19 stsp size -= sizeof(base_id);
657 efd2a263 2018-01-19 stsp if (size <= 0)
658 efd2a263 2018-01-19 stsp return got_ferror(infile, GOT_ERR_BAD_PACKFILE);
659 efd2a263 2018-01-19 stsp
660 efd2a263 2018-01-19 stsp err = got_object_open(&base_obj, repo, &base_id);
661 efd2a263 2018-01-19 stsp if (err)
662 efd2a263 2018-01-19 stsp return err;
663 efd2a263 2018-01-19 stsp
664 efd2a263 2018-01-19 stsp err = got_delta_apply(repo, infile, size, base_obj, outfile);
665 efd2a263 2018-01-19 stsp got_object_close(base_obj);
666 efd2a263 2018-01-19 stsp return err;
667 efd2a263 2018-01-19 stsp }
668 efd2a263 2018-01-19 stsp
669 3ee5fc21 2018-01-17 stsp const struct got_error *
670 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
671 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
672 3ee5fc21 2018-01-17 stsp {
673 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
674 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
675 3ee5fc21 2018-01-17 stsp
676 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
677 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
678 3ee5fc21 2018-01-17 stsp
679 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
680 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
681 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
682 3ee5fc21 2018-01-17 stsp goto done;
683 3ee5fc21 2018-01-17 stsp }
684 3ee5fc21 2018-01-17 stsp
685 3ee5fc21 2018-01-17 stsp packfile = fopen(obj->path_packfile, "rb");
686 3ee5fc21 2018-01-17 stsp if (packfile == NULL) {
687 f334529e 2018-01-12 stsp err = got_error_from_errno();
688 3ee5fc21 2018-01-17 stsp goto done;
689 3ee5fc21 2018-01-17 stsp }
690 3ee5fc21 2018-01-17 stsp
691 3ee5fc21 2018-01-17 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
692 3ee5fc21 2018-01-17 stsp err = got_error_from_errno();
693 3ee5fc21 2018-01-17 stsp goto done;
694 3ee5fc21 2018-01-17 stsp }
695 3ee5fc21 2018-01-17 stsp
696 3ee5fc21 2018-01-17 stsp switch (obj->type) {
697 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
698 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
699 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
700 3ee5fc21 2018-01-17 stsp err = dump_plain_object(packfile, obj->type, obj->size, *f);
701 3ee5fc21 2018-01-17 stsp break;
702 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_REF_DELTA:
703 efd2a263 2018-01-19 stsp err = dump_ref_delta_object(repo, packfile, obj->type,
704 efd2a263 2018-01-19 stsp obj->size, *f);
705 efd2a263 2018-01-19 stsp break;
706 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_TAG:
707 3ee5fc21 2018-01-17 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
708 3ee5fc21 2018-01-17 stsp default:
709 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
710 3ee5fc21 2018-01-17 stsp goto done;
711 3ee5fc21 2018-01-17 stsp }
712 3ee5fc21 2018-01-17 stsp done:
713 3ee5fc21 2018-01-17 stsp if (packfile)
714 3ee5fc21 2018-01-17 stsp fclose(packfile);
715 3ee5fc21 2018-01-17 stsp if (err && *f)
716 3ee5fc21 2018-01-17 stsp fclose(*f);
717 a1fd68d8 2018-01-12 stsp return err;
718 a1fd68d8 2018-01-12 stsp }