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 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
32 51514078 2018-12-25 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
34 c48c4a9c 2018-03-11 stsp struct stat sb;
36 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
37 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
39 51514078 2018-12-25 stsp entry->ctime_sec = sb.st_ctime;
40 51514078 2018-12-25 stsp entry->ctime_nsec = sb.st_ctimensec;
41 51514078 2018-12-25 stsp entry->mtime_sec = sb.st_mtime;
42 51514078 2018-12-25 stsp entry->mtime_nsec = sb.st_mtimensec;
43 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
44 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
45 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
46 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
47 51514078 2018-12-25 stsp entry->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
49 51514078 2018-12-25 stsp entry->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
50 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
51 51514078 2018-12-25 stsp GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
52 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
53 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
55 51514078 2018-12-25 stsp return NULL;
58 51514078 2018-12-25 stsp const struct got_error *
59 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
60 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
61 51514078 2018-12-25 stsp uint8_t *commit_sha1)
65 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
66 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
67 0a585a0d 2018-03-17 stsp return got_error_from_errno();
69 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
70 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
71 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
72 c48c4a9c 2018-03-11 stsp free(*entry);
73 c48c4a9c 2018-03-11 stsp *entry = NULL;
77 c34b20a2 2018-03-12 stsp len = strlen(relpath);
78 c48c4a9c 2018-03-11 stsp if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
79 c48c4a9c 2018-03-11 stsp len = GOT_INDEX_ENTRY_F_PATH_LEN;
80 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
82 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
83 51514078 2018-12-25 stsp commit_sha1);
87 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
89 c48c4a9c 2018-03-11 stsp free(entry->path);
90 c48c4a9c 2018-03-11 stsp free(entry);
93 9d31a1d8 2018-03-11 stsp const struct got_error *
94 9d31a1d8 2018-03-11 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
95 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry)
97 9d31a1d8 2018-03-11 stsp /* TODO keep entries sorted by name */
98 9d31a1d8 2018-03-11 stsp TAILQ_INSERT_TAIL(&fileindex->entries, entry, entry);
99 9d31a1d8 2018-03-11 stsp fileindex->nentries++;
100 9d31a1d8 2018-03-11 stsp return NULL;
103 51514078 2018-12-25 stsp struct got_fileindex_entry *
104 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
106 51514078 2018-12-25 stsp struct got_fileindex_entry *entry;
107 51514078 2018-12-25 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
108 51514078 2018-12-25 stsp if (strcmp(entry->path, path) == 0)
109 51514078 2018-12-25 stsp return entry;
112 51514078 2018-12-25 stsp return NULL;
115 9d31a1d8 2018-03-11 stsp struct got_fileindex *
116 7426bbfd 2018-12-24 stsp got_fileindex_alloc(void)
118 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex;
120 9d31a1d8 2018-03-11 stsp fileindex = calloc(1, sizeof(*fileindex));
121 9d31a1d8 2018-03-11 stsp if (fileindex)
122 9d31a1d8 2018-03-11 stsp TAILQ_INIT(&fileindex->entries);
123 9d31a1d8 2018-03-11 stsp return fileindex;
127 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
129 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
131 9d31a1d8 2018-03-11 stsp while (!TAILQ_EMPTY(&fileindex->entries)) {
132 9d31a1d8 2018-03-11 stsp entry = TAILQ_FIRST(&fileindex->entries);
133 9d31a1d8 2018-03-11 stsp TAILQ_REMOVE(&fileindex->entries, entry, entry);
134 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(entry);
135 9d31a1d8 2018-03-11 stsp fileindex->nentries--;
137 9d31a1d8 2018-03-11 stsp free(fileindex);
140 c34b20a2 2018-03-12 stsp static const struct got_error *
141 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
145 c34b20a2 2018-03-12 stsp val = htobe64(val);
146 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
147 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
148 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
149 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
150 c34b20a2 2018-03-12 stsp return NULL;
153 c34b20a2 2018-03-12 stsp static const struct got_error *
154 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
158 c34b20a2 2018-03-12 stsp val = htobe32(val);
159 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
160 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
161 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
162 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
163 c34b20a2 2018-03-12 stsp return NULL;
166 c34b20a2 2018-03-12 stsp static const struct got_error *
167 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
171 c34b20a2 2018-03-12 stsp val = htobe16(val);
172 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
173 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
174 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
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_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
182 c34b20a2 2018-03-12 stsp size_t n, len, pad;
183 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
185 c34b20a2 2018-03-12 stsp len = strlen(path);
186 c34b20a2 2018-03-12 stsp pad = (len % 8);
188 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
189 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
190 c34b20a2 2018-03-12 stsp if (n != len)
191 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
192 c34b20a2 2018-03-12 stsp if (pad == 0)
193 c34b20a2 2018-03-12 stsp return NULL;
194 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
195 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
196 c34b20a2 2018-03-12 stsp if (n != pad)
197 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
198 c34b20a2 2018-03-12 stsp return NULL;
201 c34b20a2 2018-03-12 stsp static const struct got_error *
202 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
203 c34b20a2 2018-03-12 stsp FILE *outfile)
205 c34b20a2 2018-03-12 stsp const struct got_error *err;
208 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
210 c34b20a2 2018-03-12 stsp return err;
211 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
213 c34b20a2 2018-03-12 stsp return err;
214 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
216 c34b20a2 2018-03-12 stsp return err;
217 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
219 c34b20a2 2018-03-12 stsp return err;
221 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
223 c34b20a2 2018-03-12 stsp return err;
224 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
226 c34b20a2 2018-03-12 stsp return err;
227 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
229 c34b20a2 2018-03-12 stsp return err;
231 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
233 c34b20a2 2018-03-12 stsp return err;
235 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
236 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
237 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
238 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
240 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
241 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
242 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
243 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
245 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
247 c34b20a2 2018-03-12 stsp return err;
249 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
250 c34b20a2 2018-03-12 stsp return err;
253 9d31a1d8 2018-03-11 stsp const struct got_error *
254 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
256 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
257 c34b20a2 2018-03-12 stsp struct got_fileindex_entry *entry;
258 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
259 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
261 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
262 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
263 c34b20a2 2018-03-12 stsp uint8_t buf[len];
265 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
267 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
268 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
269 c34b20a2 2018-03-12 stsp hdr.nentries = htobe32(fileindex->nentries);
271 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
272 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
273 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
274 c34b20a2 2018-03-12 stsp if (n != len)
275 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
277 c34b20a2 2018-03-12 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
278 c34b20a2 2018-03-12 stsp const struct got_error *err;
279 c34b20a2 2018-03-12 stsp err = write_fileindex_entry(&ctx, entry, outfile);
281 c34b20a2 2018-03-12 stsp return err;
284 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
285 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
286 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
287 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
289 52a74475 2018-12-24 stsp return NULL;
292 52a74475 2018-12-24 stsp static const struct got_error *
293 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
297 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
298 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
299 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
300 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
301 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
302 52a74475 2018-12-24 stsp return NULL;
305 52a74475 2018-12-24 stsp static const struct got_error *
306 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
310 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
311 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
312 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
313 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
314 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
315 52a74475 2018-12-24 stsp return NULL;
318 52a74475 2018-12-24 stsp static const struct got_error *
319 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
323 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
324 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
325 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
326 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
327 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
328 52a74475 2018-12-24 stsp return NULL;
331 52a74475 2018-12-24 stsp static const struct got_error *
332 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
334 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
335 52a74475 2018-12-24 stsp uint8_t buf[8];
336 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
338 52a74475 2018-12-24 stsp *path = malloc(totlen);
339 52a74475 2018-12-24 stsp if (*path == NULL)
340 52a74475 2018-12-24 stsp return got_error_from_errno();
343 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
344 52a74475 2018-12-24 stsp if (n != sizeof(buf))
345 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
346 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
347 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
348 52a74475 2018-12-24 stsp if (p == NULL) {
349 52a74475 2018-12-24 stsp err = got_error_from_errno();
352 52a74475 2018-12-24 stsp totlen += sizeof(buf);
355 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
356 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
357 52a74475 2018-12-24 stsp len += sizeof(buf);
358 52a74475 2018-12-24 stsp } while (strchr(buf, '\0') == NULL);
361 52a74475 2018-12-24 stsp free(*path);
362 52a74475 2018-12-24 stsp *path = NULL;
364 52a74475 2018-12-24 stsp return err;
367 52a74475 2018-12-24 stsp static const struct got_error *
368 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
369 52a74475 2018-12-24 stsp FILE *infile)
371 52a74475 2018-12-24 stsp const struct got_error *err;
372 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
375 52a74475 2018-12-24 stsp *entryp = NULL;
377 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
378 52a74475 2018-12-24 stsp if (entry == NULL)
379 52a74475 2018-12-24 stsp return got_error_from_errno();
381 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
384 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
387 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
390 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
394 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
397 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
400 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
404 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
408 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
409 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
410 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
413 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
415 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
416 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
417 fc76cabb 2018-12-25 stsp err = got_ferror(infile, GOT_ERR_IO);
420 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
422 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
426 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
429 52a74475 2018-12-24 stsp free(entry);
431 52a74475 2018-12-24 stsp *entryp = entry;
432 52a74475 2018-12-24 stsp return err;
435 52a74475 2018-12-24 stsp const struct got_error *
436 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
438 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
439 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
440 52a74475 2018-12-24 stsp SHA1_CTX ctx;
441 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
442 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
443 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
445 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
446 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
447 52a74475 2018-12-24 stsp uint8_t buf[len];
450 52a74475 2018-12-24 stsp SHA1Init(&ctx);
452 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
453 51514078 2018-12-25 stsp if (n != len) {
454 51514078 2018-12-25 stsp if (n == 0) /* EOF */
455 51514078 2018-12-25 stsp return NULL;
456 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
459 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
461 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
462 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
463 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
464 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
466 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
467 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
468 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
469 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
471 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
472 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
474 52a74475 2018-12-24 stsp return err;
475 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
477 52a74475 2018-12-24 stsp return err;
480 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
481 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
482 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
483 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
484 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
485 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
487 9d31a1d8 2018-03-11 stsp return NULL;