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 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
183 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
185 c34b20a2 2018-03-12 stsp len = strlen(path);
186 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
188 3c5b70f2 2018-12-27 stsp if (pad == 0)
189 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
191 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
192 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
193 c34b20a2 2018-03-12 stsp if (n != len)
194 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
195 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
196 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
197 c34b20a2 2018-03-12 stsp if (n != pad)
198 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
199 c34b20a2 2018-03-12 stsp return NULL;
202 c34b20a2 2018-03-12 stsp static const struct got_error *
203 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
204 c34b20a2 2018-03-12 stsp FILE *outfile)
206 c34b20a2 2018-03-12 stsp const struct got_error *err;
209 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
211 c34b20a2 2018-03-12 stsp return err;
212 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
214 c34b20a2 2018-03-12 stsp return err;
215 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
217 c34b20a2 2018-03-12 stsp return err;
218 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
220 c34b20a2 2018-03-12 stsp return err;
222 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
224 c34b20a2 2018-03-12 stsp return err;
225 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
227 c34b20a2 2018-03-12 stsp return err;
228 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
230 c34b20a2 2018-03-12 stsp return err;
232 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
234 c34b20a2 2018-03-12 stsp return err;
236 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
237 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
238 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
239 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
241 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
242 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
243 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
244 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
246 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
248 c34b20a2 2018-03-12 stsp return err;
250 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
251 c34b20a2 2018-03-12 stsp return err;
254 9d31a1d8 2018-03-11 stsp const struct got_error *
255 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
257 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
258 c34b20a2 2018-03-12 stsp struct got_fileindex_entry *entry;
259 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
260 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
262 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
263 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
264 c34b20a2 2018-03-12 stsp uint8_t buf[len];
266 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
268 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
269 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
270 c34b20a2 2018-03-12 stsp hdr.nentries = htobe32(fileindex->nentries);
272 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
273 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
274 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
275 c34b20a2 2018-03-12 stsp if (n != len)
276 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
278 c34b20a2 2018-03-12 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
279 c34b20a2 2018-03-12 stsp const struct got_error *err;
280 c34b20a2 2018-03-12 stsp err = write_fileindex_entry(&ctx, entry, outfile);
282 c34b20a2 2018-03-12 stsp return err;
285 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
286 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
287 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
288 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
290 52a74475 2018-12-24 stsp return NULL;
293 52a74475 2018-12-24 stsp static const struct got_error *
294 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
298 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
299 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
300 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
301 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
302 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
303 52a74475 2018-12-24 stsp return NULL;
306 52a74475 2018-12-24 stsp static const struct got_error *
307 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
311 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
312 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
313 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
314 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
315 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
316 52a74475 2018-12-24 stsp return NULL;
319 52a74475 2018-12-24 stsp static const struct got_error *
320 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
324 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
325 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
326 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
327 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
328 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
329 52a74475 2018-12-24 stsp return NULL;
332 52a74475 2018-12-24 stsp static const struct got_error *
333 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
335 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
336 52a74475 2018-12-24 stsp uint8_t buf[8];
337 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
339 52a74475 2018-12-24 stsp *path = malloc(totlen);
340 52a74475 2018-12-24 stsp if (*path == NULL)
341 52a74475 2018-12-24 stsp return got_error_from_errno();
344 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
345 52a74475 2018-12-24 stsp if (n != sizeof(buf))
346 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
347 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
348 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
349 52a74475 2018-12-24 stsp if (p == NULL) {
350 52a74475 2018-12-24 stsp err = got_error_from_errno();
353 52a74475 2018-12-24 stsp totlen += sizeof(buf);
356 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
357 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
358 52a74475 2018-12-24 stsp len += sizeof(buf);
359 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
362 52a74475 2018-12-24 stsp free(*path);
363 52a74475 2018-12-24 stsp *path = NULL;
365 52a74475 2018-12-24 stsp return err;
368 52a74475 2018-12-24 stsp static const struct got_error *
369 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
370 52a74475 2018-12-24 stsp FILE *infile)
372 52a74475 2018-12-24 stsp const struct got_error *err;
373 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
376 52a74475 2018-12-24 stsp *entryp = NULL;
378 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
379 52a74475 2018-12-24 stsp if (entry == NULL)
380 52a74475 2018-12-24 stsp return got_error_from_errno();
382 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
385 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
388 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
391 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
395 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
398 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
401 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
405 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
409 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
410 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
411 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
414 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
416 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
417 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
418 fc76cabb 2018-12-25 stsp err = got_ferror(infile, GOT_ERR_IO);
421 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
423 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
427 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
430 52a74475 2018-12-24 stsp free(entry);
432 52a74475 2018-12-24 stsp *entryp = entry;
433 52a74475 2018-12-24 stsp return err;
436 52a74475 2018-12-24 stsp const struct got_error *
437 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
439 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
440 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
441 52a74475 2018-12-24 stsp SHA1_CTX ctx;
442 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
443 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
444 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
446 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
447 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
448 52a74475 2018-12-24 stsp uint8_t buf[len];
451 52a74475 2018-12-24 stsp SHA1Init(&ctx);
453 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
454 51514078 2018-12-25 stsp if (n != len) {
455 51514078 2018-12-25 stsp if (n == 0) /* EOF */
456 51514078 2018-12-25 stsp return NULL;
457 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
460 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
462 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
463 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
464 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
465 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
467 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
468 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
469 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
470 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
472 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
473 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
475 52a74475 2018-12-24 stsp return err;
476 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
478 52a74475 2018-12-24 stsp return err;
481 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
482 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
483 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
484 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
485 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
486 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
488 9d31a1d8 2018-03-11 stsp return NULL;