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 fc76cabb 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
33 fc76cabb 2018-12-25 stsp uint8_t *commit_sha1)
35 c48c4a9c 2018-03-11 stsp struct stat sb;
38 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
39 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
41 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
42 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
43 0a585a0d 2018-03-17 stsp return got_error_from_errno();
45 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
46 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
47 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
48 c48c4a9c 2018-03-11 stsp free(*entry);
49 c48c4a9c 2018-03-11 stsp *entry = NULL;
53 c48c4a9c 2018-03-11 stsp (*entry)->ctime_sec = sb.st_ctime;
54 c48c4a9c 2018-03-11 stsp (*entry)->ctime_nsec = sb.st_ctimensec;
55 c48c4a9c 2018-03-11 stsp (*entry)->mtime_sec = sb.st_mtime;
56 c48c4a9c 2018-03-11 stsp (*entry)->mtime_nsec = sb.st_mtimensec;
57 c48c4a9c 2018-03-11 stsp (*entry)->uid = sb.st_uid;
58 c48c4a9c 2018-03-11 stsp (*entry)->gid = sb.st_gid;
59 c48c4a9c 2018-03-11 stsp (*entry)->size = (sb.st_size & 0xffffffff);
60 c48c4a9c 2018-03-11 stsp if (sb.st_mode & S_IFLNK)
61 c48c4a9c 2018-03-11 stsp (*entry)->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
63 c48c4a9c 2018-03-11 stsp (*entry)->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
64 c48c4a9c 2018-03-11 stsp (*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
65 c48c4a9c 2018-03-11 stsp GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
66 c48c4a9c 2018-03-11 stsp memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
67 fc76cabb 2018-12-25 stsp memcpy((*entry)->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
68 c34b20a2 2018-03-12 stsp len = strlen(relpath);
69 c48c4a9c 2018-03-11 stsp if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
70 c48c4a9c 2018-03-11 stsp len = GOT_INDEX_ENTRY_F_PATH_LEN;
71 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
73 c48c4a9c 2018-03-11 stsp return NULL;
77 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
79 c48c4a9c 2018-03-11 stsp free(entry->path);
80 c48c4a9c 2018-03-11 stsp free(entry);
83 9d31a1d8 2018-03-11 stsp const struct got_error *
84 9d31a1d8 2018-03-11 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
85 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry)
87 9d31a1d8 2018-03-11 stsp /* TODO keep entries sorted by name */
88 9d31a1d8 2018-03-11 stsp TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
89 9d31a1d8 2018-03-11 stsp fileindex->nentries++;
90 9d31a1d8 2018-03-11 stsp return NULL;
93 9d31a1d8 2018-03-11 stsp struct got_fileindex *
94 7426bbfd 2018-12-24 stsp got_fileindex_alloc(void)
96 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex;
98 9d31a1d8 2018-03-11 stsp fileindex = calloc(1, sizeof(*fileindex));
99 9d31a1d8 2018-03-11 stsp if (fileindex)
100 9d31a1d8 2018-03-11 stsp TAILQ_INIT(&fileindex->entries);
101 9d31a1d8 2018-03-11 stsp return fileindex;
105 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
107 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
109 9d31a1d8 2018-03-11 stsp while (!TAILQ_EMPTY(&fileindex->entries)) {
110 9d31a1d8 2018-03-11 stsp entry = TAILQ_FIRST(&fileindex->entries);
111 9d31a1d8 2018-03-11 stsp TAILQ_REMOVE(&fileindex->entries, entry, entry);
112 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(entry);
113 9d31a1d8 2018-03-11 stsp fileindex->nentries--;
115 9d31a1d8 2018-03-11 stsp free(fileindex);
118 c34b20a2 2018-03-12 stsp static const struct got_error *
119 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
123 c34b20a2 2018-03-12 stsp val = htobe64(val);
124 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
125 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
126 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
127 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
128 c34b20a2 2018-03-12 stsp return NULL;
131 c34b20a2 2018-03-12 stsp static const struct got_error *
132 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
136 c34b20a2 2018-03-12 stsp val = htobe32(val);
137 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
138 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
139 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
140 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
141 c34b20a2 2018-03-12 stsp return NULL;
144 c34b20a2 2018-03-12 stsp static const struct got_error *
145 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
149 c34b20a2 2018-03-12 stsp val = htobe16(val);
150 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
151 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
152 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
153 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
154 c34b20a2 2018-03-12 stsp return NULL;
157 c34b20a2 2018-03-12 stsp static const struct got_error *
158 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
160 c34b20a2 2018-03-12 stsp size_t n, len, pad;
161 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
163 c34b20a2 2018-03-12 stsp len = strlen(path);
164 c34b20a2 2018-03-12 stsp pad = (len % 8);
166 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
167 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
168 c34b20a2 2018-03-12 stsp if (n != len)
169 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
170 c34b20a2 2018-03-12 stsp if (pad == 0)
171 c34b20a2 2018-03-12 stsp return NULL;
172 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
173 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
174 c34b20a2 2018-03-12 stsp if (n != pad)
175 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
176 c34b20a2 2018-03-12 stsp return NULL;
179 c34b20a2 2018-03-12 stsp static const struct got_error *
180 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
181 c34b20a2 2018-03-12 stsp FILE *outfile)
183 c34b20a2 2018-03-12 stsp const struct got_error *err;
186 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
188 c34b20a2 2018-03-12 stsp return err;
189 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
191 c34b20a2 2018-03-12 stsp return err;
192 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
194 c34b20a2 2018-03-12 stsp return err;
195 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
197 c34b20a2 2018-03-12 stsp return err;
199 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
201 c34b20a2 2018-03-12 stsp return err;
202 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
204 c34b20a2 2018-03-12 stsp return err;
205 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
207 c34b20a2 2018-03-12 stsp return err;
209 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
211 c34b20a2 2018-03-12 stsp return err;
213 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
214 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
215 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
216 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
218 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
219 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
220 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
221 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
223 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
225 c34b20a2 2018-03-12 stsp return err;
227 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
228 c34b20a2 2018-03-12 stsp return err;
231 9d31a1d8 2018-03-11 stsp const struct got_error *
232 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
234 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
235 c34b20a2 2018-03-12 stsp struct got_fileindex_entry *entry;
236 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
237 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
239 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
240 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
241 c34b20a2 2018-03-12 stsp uint8_t buf[len];
243 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
245 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
246 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
247 c34b20a2 2018-03-12 stsp hdr.nentries = htobe32(fileindex->nentries);
249 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
250 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
251 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
252 c34b20a2 2018-03-12 stsp if (n != len)
253 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
255 c34b20a2 2018-03-12 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
256 c34b20a2 2018-03-12 stsp const struct got_error *err;
257 c34b20a2 2018-03-12 stsp err = write_fileindex_entry(&ctx, entry, outfile);
259 c34b20a2 2018-03-12 stsp return err;
262 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
263 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
264 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
265 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
267 52a74475 2018-12-24 stsp return NULL;
270 52a74475 2018-12-24 stsp static const struct got_error *
271 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
275 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
276 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
277 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
278 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
279 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
280 52a74475 2018-12-24 stsp return NULL;
283 52a74475 2018-12-24 stsp static const struct got_error *
284 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
288 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
289 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
290 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
291 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
292 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
293 52a74475 2018-12-24 stsp return NULL;
296 52a74475 2018-12-24 stsp static const struct got_error *
297 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
301 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
302 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
303 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
304 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
305 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
306 52a74475 2018-12-24 stsp return NULL;
309 52a74475 2018-12-24 stsp static const struct got_error *
310 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
312 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
313 52a74475 2018-12-24 stsp uint8_t buf[8];
314 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
316 52a74475 2018-12-24 stsp *path = malloc(totlen);
317 52a74475 2018-12-24 stsp if (*path == NULL)
318 52a74475 2018-12-24 stsp return got_error_from_errno();
321 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
322 52a74475 2018-12-24 stsp if (n != sizeof(buf))
323 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
324 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
325 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
326 52a74475 2018-12-24 stsp if (p == NULL) {
327 52a74475 2018-12-24 stsp err = got_error_from_errno();
330 52a74475 2018-12-24 stsp totlen += sizeof(buf);
333 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
334 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
335 52a74475 2018-12-24 stsp len += sizeof(buf);
336 52a74475 2018-12-24 stsp } while (strchr(buf, '\0') == NULL);
339 52a74475 2018-12-24 stsp free(*path);
340 52a74475 2018-12-24 stsp *path = NULL;
342 52a74475 2018-12-24 stsp return err;
345 52a74475 2018-12-24 stsp static const struct got_error *
346 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
347 52a74475 2018-12-24 stsp FILE *infile)
349 52a74475 2018-12-24 stsp const struct got_error *err;
350 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
353 52a74475 2018-12-24 stsp *entryp = NULL;
355 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
356 52a74475 2018-12-24 stsp if (entry == NULL)
357 52a74475 2018-12-24 stsp return got_error_from_errno();
359 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
362 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
365 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
368 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
372 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
375 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
378 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
382 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
386 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
387 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
388 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
391 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
393 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
394 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
395 fc76cabb 2018-12-25 stsp err = got_ferror(infile, GOT_ERR_IO);
398 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
400 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
404 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
407 52a74475 2018-12-24 stsp free(entry);
409 52a74475 2018-12-24 stsp *entryp = entry;
410 52a74475 2018-12-24 stsp return err;
413 52a74475 2018-12-24 stsp const struct got_error *
414 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
416 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
417 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
418 52a74475 2018-12-24 stsp SHA1_CTX ctx;
419 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
420 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
421 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
423 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
424 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
425 52a74475 2018-12-24 stsp uint8_t buf[len];
428 52a74475 2018-12-24 stsp SHA1Init(&ctx);
430 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
431 52a74475 2018-12-24 stsp if (n != len)
432 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
434 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
436 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
437 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
438 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
439 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
441 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
442 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
443 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
444 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
446 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
447 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
449 52a74475 2018-12-24 stsp return err;
450 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
452 52a74475 2018-12-24 stsp return err;
455 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
456 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
457 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
458 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
459 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
460 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
462 9d31a1d8 2018-03-11 stsp return NULL;