2 c48c4a9c 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
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>
26 c48c4a9c 2018-03-11 stsp #include "got_error.h"
28 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
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)
34 c48c4a9c 2018-03-11 stsp struct stat sb;
37 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
38 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
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();
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;
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;
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;
71 c48c4a9c 2018-03-11 stsp return NULL;
75 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
77 c48c4a9c 2018-03-11 stsp free(entry->path);
78 c48c4a9c 2018-03-11 stsp free(entry);
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)
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;
91 9d31a1d8 2018-03-11 stsp struct got_fileindex *
92 7426bbfd 2018-12-24 stsp got_fileindex_alloc(void)
94 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex;
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;
103 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
105 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
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--;
113 9d31a1d8 2018-03-11 stsp free(fileindex);
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)
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;
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)
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;
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)
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;
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)
158 c34b20a2 2018-03-12 stsp size_t n, len, pad;
159 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
161 c34b20a2 2018-03-12 stsp len = strlen(path);
162 c34b20a2 2018-03-12 stsp pad = (len % 8);
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;
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)
181 c34b20a2 2018-03-12 stsp const struct got_error *err;
184 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
186 c34b20a2 2018-03-12 stsp return err;
187 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
189 c34b20a2 2018-03-12 stsp return err;
190 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
192 c34b20a2 2018-03-12 stsp return err;
193 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
195 c34b20a2 2018-03-12 stsp return err;
197 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
199 c34b20a2 2018-03-12 stsp return err;
200 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
202 c34b20a2 2018-03-12 stsp return err;
203 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
205 c34b20a2 2018-03-12 stsp return err;
207 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
209 c34b20a2 2018-03-12 stsp return err;
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);
216 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
218 c34b20a2 2018-03-12 stsp return err;
220 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
221 c34b20a2 2018-03-12 stsp return err;
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)
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];
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];
236 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
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);
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);
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);
252 c34b20a2 2018-03-12 stsp return err;
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);
260 52a74475 2018-12-24 stsp return NULL;
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)
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;
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)
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;
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)
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;
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)
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);
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();
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();
323 52a74475 2018-12-24 stsp totlen += sizeof(buf);
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);
332 52a74475 2018-12-24 stsp free(*path);
333 52a74475 2018-12-24 stsp *path = NULL;
335 52a74475 2018-12-24 stsp return err;
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)
342 52a74475 2018-12-24 stsp const struct got_error *err;
343 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
346 52a74475 2018-12-24 stsp *entryp = NULL;
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();
352 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
355 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
358 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
361 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
365 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
368 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
371 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
375 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
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);
384 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
386 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
390 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
393 52a74475 2018-12-24 stsp free(entry);
395 52a74475 2018-12-24 stsp *entryp = entry;
396 52a74475 2018-12-24 stsp return err;
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)
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];
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];
414 52a74475 2018-12-24 stsp SHA1Init(&ctx);
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);
420 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
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);
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);
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);
435 52a74475 2018-12-24 stsp return err;
436 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
438 52a74475 2018-12-24 stsp return err;
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);
448 9d31a1d8 2018-03-11 stsp return NULL;