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 c34b20a2 2018-03-12 stsp got_fileindex_entry_open(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 c48c4a9c 2018-03-11 stsp got_fileindex_entry_close(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 9d31a1d8 2018-03-11 stsp got_fileindex_open(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 9d31a1d8 2018-03-11 stsp got_fileindex_close(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 9d31a1d8 2018-03-11 stsp got_fileindex_entry_close(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)
119 c34b20a2 2018-03-12 stsp uint8_t buf[sizeof(uint64_t)];
122 c34b20a2 2018-03-12 stsp val = htobe64(val);
123 c34b20a2 2018-03-12 stsp memcpy(buf, &val, sizeof(val));
124 c34b20a2 2018-03-12 stsp SHA1Update(ctx, buf, sizeof(val));
125 c34b20a2 2018-03-12 stsp n = fwrite(buf, 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)
134 c34b20a2 2018-03-12 stsp uint8_t buf[sizeof(uint32_t)];
137 c34b20a2 2018-03-12 stsp val = htobe32(val);
138 c34b20a2 2018-03-12 stsp memcpy(buf, &val, sizeof(val));
139 c34b20a2 2018-03-12 stsp SHA1Update(ctx, buf, sizeof(val));
140 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, sizeof(val), outfile);
141 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
142 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
143 c34b20a2 2018-03-12 stsp return NULL;
146 c34b20a2 2018-03-12 stsp static const struct got_error *
147 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
149 c34b20a2 2018-03-12 stsp uint8_t buf[sizeof(uint16_t)];
152 c34b20a2 2018-03-12 stsp val = htobe16(val);
153 c34b20a2 2018-03-12 stsp memcpy(buf, &val, sizeof(val));
154 c34b20a2 2018-03-12 stsp SHA1Update(ctx, buf, sizeof(val));
155 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, sizeof(val), outfile);
156 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
157 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
158 c34b20a2 2018-03-12 stsp return NULL;
161 c34b20a2 2018-03-12 stsp static const struct got_error *
162 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
164 c34b20a2 2018-03-12 stsp size_t n, len, pad;
165 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
167 c34b20a2 2018-03-12 stsp len = strlen(path);
168 c34b20a2 2018-03-12 stsp pad = (len % 8);
170 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
171 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
172 c34b20a2 2018-03-12 stsp if (n != len)
173 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
174 c34b20a2 2018-03-12 stsp if (pad == 0)
175 c34b20a2 2018-03-12 stsp return NULL;
176 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
177 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
178 c34b20a2 2018-03-12 stsp if (n != pad)
179 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
180 c34b20a2 2018-03-12 stsp return NULL;
183 c34b20a2 2018-03-12 stsp static const struct got_error *
184 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
185 c34b20a2 2018-03-12 stsp FILE *outfile)
187 c34b20a2 2018-03-12 stsp const struct got_error *err;
190 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
192 c34b20a2 2018-03-12 stsp return err;
193 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
195 c34b20a2 2018-03-12 stsp return err;
196 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
198 c34b20a2 2018-03-12 stsp return err;
199 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
201 c34b20a2 2018-03-12 stsp return err;
203 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
205 c34b20a2 2018-03-12 stsp return err;
206 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
208 c34b20a2 2018-03-12 stsp return err;
209 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
211 c34b20a2 2018-03-12 stsp return err;
213 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
215 c34b20a2 2018-03-12 stsp return err;
217 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
218 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
219 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
220 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
222 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
224 c34b20a2 2018-03-12 stsp return err;
226 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
227 c34b20a2 2018-03-12 stsp return err;
230 9d31a1d8 2018-03-11 stsp const struct got_error *
231 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
233 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
234 c34b20a2 2018-03-12 stsp struct got_fileindex_entry *entry;
235 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
236 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
238 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
239 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
240 c34b20a2 2018-03-12 stsp uint8_t buf[len];
242 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
244 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
245 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
246 c34b20a2 2018-03-12 stsp hdr.nentries = htobe32(fileindex->nentries);
248 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
249 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
250 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
251 c34b20a2 2018-03-12 stsp if (n != len)
252 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
254 c34b20a2 2018-03-12 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
255 c34b20a2 2018-03-12 stsp const struct got_error *err;
256 c34b20a2 2018-03-12 stsp err = write_fileindex_entry(&ctx, entry, outfile);
258 c34b20a2 2018-03-12 stsp return err;
261 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
262 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
263 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
264 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
266 52a74475 2018-12-24 stsp return NULL;
269 52a74475 2018-12-24 stsp static const struct got_error *
270 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
272 52a74475 2018-12-24 stsp uint8_t buf[sizeof(uint64_t)];
275 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
276 52a74475 2018-12-24 stsp if (n != sizeof(buf))
277 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
278 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
279 52a74475 2018-12-24 stsp memcpy(val, buf, sizeof(*val));
280 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
281 52a74475 2018-12-24 stsp return NULL;
284 52a74475 2018-12-24 stsp static const struct got_error *
285 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
287 52a74475 2018-12-24 stsp uint8_t buf[sizeof(uint32_t)];
290 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
291 52a74475 2018-12-24 stsp if (n != sizeof(buf))
292 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
293 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
294 52a74475 2018-12-24 stsp memcpy(val, buf, sizeof(*val));
295 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
296 52a74475 2018-12-24 stsp return NULL;
299 52a74475 2018-12-24 stsp static const struct got_error *
300 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
302 52a74475 2018-12-24 stsp uint8_t buf[sizeof(uint16_t)];
305 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
306 52a74475 2018-12-24 stsp if (n != sizeof(buf))
307 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
308 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
309 52a74475 2018-12-24 stsp memcpy(val, buf, sizeof(*val));
310 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
311 52a74475 2018-12-24 stsp return NULL;
314 52a74475 2018-12-24 stsp static const struct got_error *
315 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
317 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
318 52a74475 2018-12-24 stsp uint8_t buf[8];
319 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
321 52a74475 2018-12-24 stsp *path = malloc(totlen);
322 52a74475 2018-12-24 stsp if (*path == NULL)
323 52a74475 2018-12-24 stsp return got_error_from_errno();
326 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
327 52a74475 2018-12-24 stsp if (n != sizeof(buf))
328 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
329 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
330 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
331 52a74475 2018-12-24 stsp if (p == NULL) {
332 52a74475 2018-12-24 stsp err = got_error_from_errno();
335 52a74475 2018-12-24 stsp totlen += sizeof(buf);
338 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
339 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
340 52a74475 2018-12-24 stsp len += sizeof(buf);
341 52a74475 2018-12-24 stsp } while (strchr(buf, '\0') == NULL);
344 52a74475 2018-12-24 stsp free(*path);
345 52a74475 2018-12-24 stsp *path = NULL;
347 52a74475 2018-12-24 stsp return err;
350 52a74475 2018-12-24 stsp static const struct got_error *
351 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
352 52a74475 2018-12-24 stsp FILE *infile)
354 52a74475 2018-12-24 stsp const struct got_error *err;
355 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
358 52a74475 2018-12-24 stsp *entryp = NULL;
360 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
361 52a74475 2018-12-24 stsp if (entry == NULL)
362 52a74475 2018-12-24 stsp return got_error_from_errno();
364 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
367 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
370 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
373 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
377 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
380 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
383 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
387 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
391 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
392 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
393 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
396 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
398 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
402 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
405 52a74475 2018-12-24 stsp free(entry);
407 52a74475 2018-12-24 stsp *entryp = entry;
408 52a74475 2018-12-24 stsp return err;
411 52a74475 2018-12-24 stsp const struct got_error *
412 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
414 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
415 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
416 52a74475 2018-12-24 stsp SHA1_CTX ctx;
417 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
418 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
419 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
421 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
422 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
423 52a74475 2018-12-24 stsp uint8_t buf[len];
426 52a74475 2018-12-24 stsp SHA1Init(&ctx);
428 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
429 52a74475 2018-12-24 stsp if (n != len)
430 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
432 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
434 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
435 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
436 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
437 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
439 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
440 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
441 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
442 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
444 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
445 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
447 52a74475 2018-12-24 stsp return err;
448 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
450 52a74475 2018-12-24 stsp return err;
453 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
454 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
455 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
456 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
457 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
458 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
460 9d31a1d8 2018-03-11 stsp return NULL;