Blame


1 c48c4a9c 2018-03-11 stsp /*
2 c48c4a9c 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 c48c4a9c 2018-03-11 stsp *
4 c48c4a9c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 c48c4a9c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 c48c4a9c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
7 c48c4a9c 2018-03-11 stsp *
8 c48c4a9c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c48c4a9c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c48c4a9c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c48c4a9c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c48c4a9c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c48c4a9c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c48c4a9c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c48c4a9c 2018-03-11 stsp */
16 c48c4a9c 2018-03-11 stsp
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
19 c48c4a9c 2018-03-11 stsp
20 c48c4a9c 2018-03-11 stsp #include <stdio.h>
21 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
22 c48c4a9c 2018-03-11 stsp #include <string.h>
23 c48c4a9c 2018-03-11 stsp #include <sha1.h>
24 c34b20a2 2018-03-12 stsp #include <endian.h>
25 c48c4a9c 2018-03-11 stsp
26 c48c4a9c 2018-03-11 stsp #include "got_error.h"
27 c48c4a9c 2018-03-11 stsp
28 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
29 c48c4a9c 2018-03-11 stsp
30 c48c4a9c 2018-03-11 stsp const struct got_error *
31 7426bbfd 2018-12-24 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
32 c34b20a2 2018-03-12 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1)
33 c48c4a9c 2018-03-11 stsp {
34 c48c4a9c 2018-03-11 stsp struct stat sb;
35 c48c4a9c 2018-03-11 stsp size_t len;
36 c48c4a9c 2018-03-11 stsp
37 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
38 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
39 c48c4a9c 2018-03-11 stsp
40 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
41 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
42 0a585a0d 2018-03-17 stsp return got_error_from_errno();
43 c48c4a9c 2018-03-11 stsp
44 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
45 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
46 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
47 c48c4a9c 2018-03-11 stsp free(*entry);
48 c48c4a9c 2018-03-11 stsp *entry = NULL;
49 0a585a0d 2018-03-17 stsp return err;
50 c48c4a9c 2018-03-11 stsp }
51 c48c4a9c 2018-03-11 stsp
52 c48c4a9c 2018-03-11 stsp (*entry)->ctime_sec = sb.st_ctime;
53 c48c4a9c 2018-03-11 stsp (*entry)->ctime_nsec = sb.st_ctimensec;
54 c48c4a9c 2018-03-11 stsp (*entry)->mtime_sec = sb.st_mtime;
55 c48c4a9c 2018-03-11 stsp (*entry)->mtime_nsec = sb.st_mtimensec;
56 c48c4a9c 2018-03-11 stsp (*entry)->uid = sb.st_uid;
57 c48c4a9c 2018-03-11 stsp (*entry)->gid = sb.st_gid;
58 c48c4a9c 2018-03-11 stsp (*entry)->size = (sb.st_size & 0xffffffff);
59 c48c4a9c 2018-03-11 stsp if (sb.st_mode & S_IFLNK)
60 c48c4a9c 2018-03-11 stsp (*entry)->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
61 c48c4a9c 2018-03-11 stsp else
62 c48c4a9c 2018-03-11 stsp (*entry)->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
63 c48c4a9c 2018-03-11 stsp (*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
64 c48c4a9c 2018-03-11 stsp GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
65 c48c4a9c 2018-03-11 stsp memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
66 c34b20a2 2018-03-12 stsp len = strlen(relpath);
67 c48c4a9c 2018-03-11 stsp if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
68 c48c4a9c 2018-03-11 stsp len = GOT_INDEX_ENTRY_F_PATH_LEN;
69 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
70 c48c4a9c 2018-03-11 stsp
71 c48c4a9c 2018-03-11 stsp return NULL;
72 c48c4a9c 2018-03-11 stsp }
73 c48c4a9c 2018-03-11 stsp
74 c48c4a9c 2018-03-11 stsp void
75 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
76 c48c4a9c 2018-03-11 stsp {
77 c48c4a9c 2018-03-11 stsp free(entry->path);
78 c48c4a9c 2018-03-11 stsp free(entry);
79 c48c4a9c 2018-03-11 stsp }
80 9d31a1d8 2018-03-11 stsp
81 9d31a1d8 2018-03-11 stsp const struct got_error *
82 9d31a1d8 2018-03-11 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
83 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry)
84 9d31a1d8 2018-03-11 stsp {
85 9d31a1d8 2018-03-11 stsp /* TODO keep entries sorted by name */
86 9d31a1d8 2018-03-11 stsp TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
87 9d31a1d8 2018-03-11 stsp fileindex->nentries++;
88 9d31a1d8 2018-03-11 stsp return NULL;
89 9d31a1d8 2018-03-11 stsp }
90 9d31a1d8 2018-03-11 stsp
91 9d31a1d8 2018-03-11 stsp struct got_fileindex *
92 7426bbfd 2018-12-24 stsp got_fileindex_alloc(void)
93 9d31a1d8 2018-03-11 stsp {
94 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex;
95 9d31a1d8 2018-03-11 stsp
96 9d31a1d8 2018-03-11 stsp fileindex = calloc(1, sizeof(*fileindex));
97 9d31a1d8 2018-03-11 stsp if (fileindex)
98 9d31a1d8 2018-03-11 stsp TAILQ_INIT(&fileindex->entries);
99 9d31a1d8 2018-03-11 stsp return fileindex;
100 9d31a1d8 2018-03-11 stsp }
101 9d31a1d8 2018-03-11 stsp
102 9d31a1d8 2018-03-11 stsp void
103 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
104 9d31a1d8 2018-03-11 stsp {
105 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
106 9d31a1d8 2018-03-11 stsp
107 9d31a1d8 2018-03-11 stsp while (!TAILQ_EMPTY(&fileindex->entries)) {
108 9d31a1d8 2018-03-11 stsp entry = TAILQ_FIRST(&fileindex->entries);
109 9d31a1d8 2018-03-11 stsp TAILQ_REMOVE(&fileindex->entries, entry, entry);
110 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(entry);
111 9d31a1d8 2018-03-11 stsp fileindex->nentries--;
112 9d31a1d8 2018-03-11 stsp }
113 9d31a1d8 2018-03-11 stsp free(fileindex);
114 9d31a1d8 2018-03-11 stsp }
115 9d31a1d8 2018-03-11 stsp
116 c34b20a2 2018-03-12 stsp static const struct got_error *
117 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
118 c34b20a2 2018-03-12 stsp {
119 c34b20a2 2018-03-12 stsp size_t n;
120 c34b20a2 2018-03-12 stsp
121 c34b20a2 2018-03-12 stsp val = htobe64(val);
122 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
123 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
124 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
125 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
126 c34b20a2 2018-03-12 stsp return NULL;
127 c34b20a2 2018-03-12 stsp }
128 c34b20a2 2018-03-12 stsp
129 c34b20a2 2018-03-12 stsp static const struct got_error *
130 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
131 c34b20a2 2018-03-12 stsp {
132 c34b20a2 2018-03-12 stsp size_t n;
133 c34b20a2 2018-03-12 stsp
134 c34b20a2 2018-03-12 stsp val = htobe32(val);
135 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
136 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
137 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
138 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
139 c34b20a2 2018-03-12 stsp return NULL;
140 c34b20a2 2018-03-12 stsp }
141 c34b20a2 2018-03-12 stsp
142 c34b20a2 2018-03-12 stsp static const struct got_error *
143 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
144 c34b20a2 2018-03-12 stsp {
145 c34b20a2 2018-03-12 stsp size_t n;
146 c34b20a2 2018-03-12 stsp
147 c34b20a2 2018-03-12 stsp val = htobe16(val);
148 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
149 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
150 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
151 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
152 c34b20a2 2018-03-12 stsp return NULL;
153 c34b20a2 2018-03-12 stsp }
154 c34b20a2 2018-03-12 stsp
155 c34b20a2 2018-03-12 stsp static const struct got_error *
156 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
157 c34b20a2 2018-03-12 stsp {
158 c34b20a2 2018-03-12 stsp size_t n, len, pad;
159 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
160 c34b20a2 2018-03-12 stsp
161 c34b20a2 2018-03-12 stsp len = strlen(path);
162 c34b20a2 2018-03-12 stsp pad = (len % 8);
163 c34b20a2 2018-03-12 stsp
164 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
165 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
166 c34b20a2 2018-03-12 stsp if (n != len)
167 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
168 c34b20a2 2018-03-12 stsp if (pad == 0)
169 c34b20a2 2018-03-12 stsp return NULL;
170 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
171 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
172 c34b20a2 2018-03-12 stsp if (n != pad)
173 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
174 c34b20a2 2018-03-12 stsp return NULL;
175 c34b20a2 2018-03-12 stsp }
176 c34b20a2 2018-03-12 stsp
177 c34b20a2 2018-03-12 stsp static const struct got_error *
178 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
179 c34b20a2 2018-03-12 stsp FILE *outfile)
180 c34b20a2 2018-03-12 stsp {
181 c34b20a2 2018-03-12 stsp const struct got_error *err;
182 23b19d00 2018-03-12 stsp size_t n;
183 c34b20a2 2018-03-12 stsp
184 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
185 c34b20a2 2018-03-12 stsp if (err)
186 c34b20a2 2018-03-12 stsp return err;
187 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
188 c34b20a2 2018-03-12 stsp if (err)
189 c34b20a2 2018-03-12 stsp return err;
190 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
191 c34b20a2 2018-03-12 stsp if (err)
192 c34b20a2 2018-03-12 stsp return err;
193 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
194 c34b20a2 2018-03-12 stsp if (err)
195 c34b20a2 2018-03-12 stsp return err;
196 c34b20a2 2018-03-12 stsp
197 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
198 c34b20a2 2018-03-12 stsp if (err)
199 c34b20a2 2018-03-12 stsp return err;
200 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
201 c34b20a2 2018-03-12 stsp if (err)
202 c34b20a2 2018-03-12 stsp return err;
203 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
204 c34b20a2 2018-03-12 stsp if (err)
205 c34b20a2 2018-03-12 stsp return err;
206 c34b20a2 2018-03-12 stsp
207 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
208 c34b20a2 2018-03-12 stsp if (err)
209 c34b20a2 2018-03-12 stsp return err;
210 c34b20a2 2018-03-12 stsp
211 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
212 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
213 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
214 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
215 c34b20a2 2018-03-12 stsp
216 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
217 c34b20a2 2018-03-12 stsp if (err)
218 c34b20a2 2018-03-12 stsp return err;
219 c34b20a2 2018-03-12 stsp
220 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
221 c34b20a2 2018-03-12 stsp return err;
222 c34b20a2 2018-03-12 stsp }
223 c34b20a2 2018-03-12 stsp
224 9d31a1d8 2018-03-11 stsp const struct got_error *
225 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
226 9d31a1d8 2018-03-11 stsp {
227 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
228 c34b20a2 2018-03-12 stsp struct got_fileindex_entry *entry;
229 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
230 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
231 c34b20a2 2018-03-12 stsp size_t n;
232 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
233 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
234 c34b20a2 2018-03-12 stsp uint8_t buf[len];
235 c34b20a2 2018-03-12 stsp
236 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
237 c34b20a2 2018-03-12 stsp
238 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
239 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
240 c34b20a2 2018-03-12 stsp hdr.nentries = htobe32(fileindex->nentries);
241 c34b20a2 2018-03-12 stsp
242 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
243 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
244 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
245 c34b20a2 2018-03-12 stsp if (n != len)
246 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
247 c34b20a2 2018-03-12 stsp
248 c34b20a2 2018-03-12 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
249 c34b20a2 2018-03-12 stsp const struct got_error *err;
250 c34b20a2 2018-03-12 stsp err = write_fileindex_entry(&ctx, entry, outfile);
251 c34b20a2 2018-03-12 stsp if (err)
252 c34b20a2 2018-03-12 stsp return err;
253 c34b20a2 2018-03-12 stsp }
254 c34b20a2 2018-03-12 stsp
255 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
256 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
257 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
258 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
259 52a74475 2018-12-24 stsp
260 52a74475 2018-12-24 stsp return NULL;
261 52a74475 2018-12-24 stsp }
262 52a74475 2018-12-24 stsp
263 52a74475 2018-12-24 stsp static const struct got_error *
264 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
265 52a74475 2018-12-24 stsp {
266 52a74475 2018-12-24 stsp size_t n;
267 52a74475 2018-12-24 stsp
268 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
269 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
270 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
271 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
272 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
273 52a74475 2018-12-24 stsp return NULL;
274 52a74475 2018-12-24 stsp }
275 52a74475 2018-12-24 stsp
276 52a74475 2018-12-24 stsp static const struct got_error *
277 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
278 52a74475 2018-12-24 stsp {
279 52a74475 2018-12-24 stsp size_t n;
280 52a74475 2018-12-24 stsp
281 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
282 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
283 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
284 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
285 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
286 52a74475 2018-12-24 stsp return NULL;
287 52a74475 2018-12-24 stsp }
288 52a74475 2018-12-24 stsp
289 52a74475 2018-12-24 stsp static const struct got_error *
290 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
291 52a74475 2018-12-24 stsp {
292 52a74475 2018-12-24 stsp size_t n;
293 52a74475 2018-12-24 stsp
294 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
295 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
296 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
297 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
298 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
299 52a74475 2018-12-24 stsp return NULL;
300 52a74475 2018-12-24 stsp }
301 52a74475 2018-12-24 stsp
302 52a74475 2018-12-24 stsp static const struct got_error *
303 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
304 52a74475 2018-12-24 stsp {
305 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
306 52a74475 2018-12-24 stsp uint8_t buf[8];
307 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
308 52a74475 2018-12-24 stsp
309 52a74475 2018-12-24 stsp *path = malloc(totlen);
310 52a74475 2018-12-24 stsp if (*path == NULL)
311 52a74475 2018-12-24 stsp return got_error_from_errno();
312 52a74475 2018-12-24 stsp
313 52a74475 2018-12-24 stsp do {
314 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
315 52a74475 2018-12-24 stsp if (n != sizeof(buf))
316 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
317 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
318 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
319 52a74475 2018-12-24 stsp if (p == NULL) {
320 52a74475 2018-12-24 stsp err = got_error_from_errno();
321 52a74475 2018-12-24 stsp break;
322 52a74475 2018-12-24 stsp }
323 52a74475 2018-12-24 stsp totlen += sizeof(buf);
324 52a74475 2018-12-24 stsp *path = p;
325 52a74475 2018-12-24 stsp }
326 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
327 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
328 52a74475 2018-12-24 stsp len += sizeof(buf);
329 52a74475 2018-12-24 stsp } while (strchr(buf, '\0') == NULL);
330 52a74475 2018-12-24 stsp
331 52a74475 2018-12-24 stsp if (err) {
332 52a74475 2018-12-24 stsp free(*path);
333 52a74475 2018-12-24 stsp *path = NULL;
334 52a74475 2018-12-24 stsp }
335 52a74475 2018-12-24 stsp return err;
336 52a74475 2018-12-24 stsp }
337 52a74475 2018-12-24 stsp
338 52a74475 2018-12-24 stsp static const struct got_error *
339 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
340 52a74475 2018-12-24 stsp FILE *infile)
341 52a74475 2018-12-24 stsp {
342 52a74475 2018-12-24 stsp const struct got_error *err;
343 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
344 52a74475 2018-12-24 stsp size_t n;
345 52a74475 2018-12-24 stsp
346 52a74475 2018-12-24 stsp *entryp = NULL;
347 52a74475 2018-12-24 stsp
348 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
349 52a74475 2018-12-24 stsp if (entry == NULL)
350 52a74475 2018-12-24 stsp return got_error_from_errno();
351 c34b20a2 2018-03-12 stsp
352 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
353 52a74475 2018-12-24 stsp if (err)
354 52a74475 2018-12-24 stsp goto done;
355 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
356 52a74475 2018-12-24 stsp if (err)
357 52a74475 2018-12-24 stsp goto done;
358 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
359 52a74475 2018-12-24 stsp if (err)
360 52a74475 2018-12-24 stsp goto done;
361 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
362 52a74475 2018-12-24 stsp if (err)
363 52a74475 2018-12-24 stsp goto done;
364 52a74475 2018-12-24 stsp
365 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
366 52a74475 2018-12-24 stsp if (err)
367 52a74475 2018-12-24 stsp goto done;
368 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
369 52a74475 2018-12-24 stsp if (err)
370 52a74475 2018-12-24 stsp goto done;
371 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
372 52a74475 2018-12-24 stsp if (err)
373 52a74475 2018-12-24 stsp goto done;
374 52a74475 2018-12-24 stsp
375 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
376 52a74475 2018-12-24 stsp if (err)
377 52a74475 2018-12-24 stsp goto done;
378 52a74475 2018-12-24 stsp
379 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
380 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
381 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
382 52a74475 2018-12-24 stsp goto done;
383 52a74475 2018-12-24 stsp }
384 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
385 52a74475 2018-12-24 stsp
386 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
387 52a74475 2018-12-24 stsp if (err)
388 52a74475 2018-12-24 stsp goto done;
389 52a74475 2018-12-24 stsp
390 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
391 52a74475 2018-12-24 stsp done:
392 52a74475 2018-12-24 stsp if (err)
393 52a74475 2018-12-24 stsp free(entry);
394 52a74475 2018-12-24 stsp else
395 52a74475 2018-12-24 stsp *entryp = entry;
396 52a74475 2018-12-24 stsp return err;
397 52a74475 2018-12-24 stsp }
398 52a74475 2018-12-24 stsp
399 52a74475 2018-12-24 stsp const struct got_error *
400 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
401 52a74475 2018-12-24 stsp {
402 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
403 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
404 52a74475 2018-12-24 stsp SHA1_CTX ctx;
405 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
406 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
407 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
408 52a74475 2018-12-24 stsp size_t n;
409 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
410 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
411 52a74475 2018-12-24 stsp uint8_t buf[len];
412 52a74475 2018-12-24 stsp int i;
413 52a74475 2018-12-24 stsp
414 52a74475 2018-12-24 stsp SHA1Init(&ctx);
415 52a74475 2018-12-24 stsp
416 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
417 52a74475 2018-12-24 stsp if (n != len)
418 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
419 52a74475 2018-12-24 stsp
420 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
421 52a74475 2018-12-24 stsp
422 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
423 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
424 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
425 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
426 52a74475 2018-12-24 stsp
427 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
428 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
429 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
430 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
431 52a74475 2018-12-24 stsp
432 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
433 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
434 52a74475 2018-12-24 stsp if (err)
435 52a74475 2018-12-24 stsp return err;
436 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
437 52a74475 2018-12-24 stsp if (err)
438 52a74475 2018-12-24 stsp return err;
439 52a74475 2018-12-24 stsp }
440 52a74475 2018-12-24 stsp
441 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
442 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
443 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
444 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
445 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
446 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
447 52a74475 2018-12-24 stsp
448 9d31a1d8 2018-03-11 stsp return NULL;
449 9d31a1d8 2018-03-11 stsp }