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;
104 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
105 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
107 512f0d0e 2019-01-02 stsp TAILQ_REMOVE(&fileindex->entries, entry, entry);
108 512f0d0e 2019-01-02 stsp fileindex->nentries--;
111 51514078 2018-12-25 stsp struct got_fileindex_entry *
112 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
114 51514078 2018-12-25 stsp struct got_fileindex_entry *entry;
115 51514078 2018-12-25 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
116 51514078 2018-12-25 stsp if (strcmp(entry->path, path) == 0)
117 51514078 2018-12-25 stsp return entry;
120 51514078 2018-12-25 stsp return NULL;
123 512f0d0e 2019-01-02 stsp const struct got_error *
124 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
125 512f0d0e 2019-01-02 stsp const struct got_error *(cb)(void *, struct got_fileindex_entry *),
126 512f0d0e 2019-01-02 stsp void *cb_arg)
128 512f0d0e 2019-01-02 stsp const struct got_error *err = NULL;
129 e1ed7f77 2019-01-06 stsp struct got_fileindex_entry *entry, *tmp;
131 e1ed7f77 2019-01-06 stsp TAILQ_FOREACH_SAFE(entry, &fileindex->entries, entry, tmp) {
132 512f0d0e 2019-01-02 stsp err = cb(cb_arg, entry);
137 512f0d0e 2019-01-02 stsp return err;
140 9d31a1d8 2018-03-11 stsp struct got_fileindex *
141 7426bbfd 2018-12-24 stsp got_fileindex_alloc(void)
143 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex;
145 9d31a1d8 2018-03-11 stsp fileindex = calloc(1, sizeof(*fileindex));
146 9d31a1d8 2018-03-11 stsp if (fileindex)
147 9d31a1d8 2018-03-11 stsp TAILQ_INIT(&fileindex->entries);
148 9d31a1d8 2018-03-11 stsp return fileindex;
152 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
154 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
156 9d31a1d8 2018-03-11 stsp while (!TAILQ_EMPTY(&fileindex->entries)) {
157 9d31a1d8 2018-03-11 stsp entry = TAILQ_FIRST(&fileindex->entries);
158 9d31a1d8 2018-03-11 stsp TAILQ_REMOVE(&fileindex->entries, entry, entry);
159 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(entry);
160 9d31a1d8 2018-03-11 stsp fileindex->nentries--;
162 9d31a1d8 2018-03-11 stsp free(fileindex);
165 c34b20a2 2018-03-12 stsp static const struct got_error *
166 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
170 c34b20a2 2018-03-12 stsp val = htobe64(val);
171 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
172 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
173 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
174 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
175 c34b20a2 2018-03-12 stsp return NULL;
178 c34b20a2 2018-03-12 stsp static const struct got_error *
179 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
183 c34b20a2 2018-03-12 stsp val = htobe32(val);
184 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
185 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
186 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
187 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
188 c34b20a2 2018-03-12 stsp return NULL;
191 c34b20a2 2018-03-12 stsp static const struct got_error *
192 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
196 c34b20a2 2018-03-12 stsp val = htobe16(val);
197 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
198 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
199 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
200 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
201 c34b20a2 2018-03-12 stsp return NULL;
204 c34b20a2 2018-03-12 stsp static const struct got_error *
205 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
207 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
208 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
210 c34b20a2 2018-03-12 stsp len = strlen(path);
211 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
213 3c5b70f2 2018-12-27 stsp if (pad == 0)
214 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
216 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
217 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
218 c34b20a2 2018-03-12 stsp if (n != len)
219 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
220 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
221 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
222 c34b20a2 2018-03-12 stsp if (n != pad)
223 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
224 c34b20a2 2018-03-12 stsp return NULL;
227 c34b20a2 2018-03-12 stsp static const struct got_error *
228 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
229 c34b20a2 2018-03-12 stsp FILE *outfile)
231 c34b20a2 2018-03-12 stsp const struct got_error *err;
234 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
236 c34b20a2 2018-03-12 stsp return err;
237 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
239 c34b20a2 2018-03-12 stsp return err;
240 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
242 c34b20a2 2018-03-12 stsp return err;
243 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
245 c34b20a2 2018-03-12 stsp return err;
247 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
249 c34b20a2 2018-03-12 stsp return err;
250 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
252 c34b20a2 2018-03-12 stsp return err;
253 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
255 c34b20a2 2018-03-12 stsp return err;
257 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
259 c34b20a2 2018-03-12 stsp return err;
261 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
262 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
263 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
264 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
266 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
267 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
268 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
269 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
271 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
273 c34b20a2 2018-03-12 stsp return err;
275 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
276 c34b20a2 2018-03-12 stsp return err;
279 9d31a1d8 2018-03-11 stsp const struct got_error *
280 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
282 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
283 c34b20a2 2018-03-12 stsp struct got_fileindex_entry *entry;
284 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
285 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
287 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
288 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
289 c34b20a2 2018-03-12 stsp uint8_t buf[len];
291 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
293 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
294 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
295 c34b20a2 2018-03-12 stsp hdr.nentries = htobe32(fileindex->nentries);
297 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
298 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
299 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
300 c34b20a2 2018-03-12 stsp if (n != len)
301 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
303 c34b20a2 2018-03-12 stsp TAILQ_FOREACH(entry, &fileindex->entries, entry) {
304 c34b20a2 2018-03-12 stsp const struct got_error *err;
305 c34b20a2 2018-03-12 stsp err = write_fileindex_entry(&ctx, entry, outfile);
307 c34b20a2 2018-03-12 stsp return err;
310 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
311 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
312 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
313 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
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_val64(uint64_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 = be64toh(*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_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
336 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
337 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
338 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
339 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
340 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
341 52a74475 2018-12-24 stsp return NULL;
344 52a74475 2018-12-24 stsp static const struct got_error *
345 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
349 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
350 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
351 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
352 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
353 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
354 52a74475 2018-12-24 stsp return NULL;
357 52a74475 2018-12-24 stsp static const struct got_error *
358 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
360 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
361 52a74475 2018-12-24 stsp uint8_t buf[8];
362 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
364 52a74475 2018-12-24 stsp *path = malloc(totlen);
365 52a74475 2018-12-24 stsp if (*path == NULL)
366 52a74475 2018-12-24 stsp return got_error_from_errno();
369 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
370 52a74475 2018-12-24 stsp if (n != sizeof(buf))
371 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
372 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
373 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
374 52a74475 2018-12-24 stsp if (p == NULL) {
375 52a74475 2018-12-24 stsp err = got_error_from_errno();
378 52a74475 2018-12-24 stsp totlen += sizeof(buf);
381 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
382 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
383 52a74475 2018-12-24 stsp len += sizeof(buf);
384 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
387 52a74475 2018-12-24 stsp free(*path);
388 52a74475 2018-12-24 stsp *path = NULL;
390 52a74475 2018-12-24 stsp return err;
393 52a74475 2018-12-24 stsp static const struct got_error *
394 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
395 52a74475 2018-12-24 stsp FILE *infile)
397 52a74475 2018-12-24 stsp const struct got_error *err;
398 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
401 52a74475 2018-12-24 stsp *entryp = NULL;
403 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
404 52a74475 2018-12-24 stsp if (entry == NULL)
405 52a74475 2018-12-24 stsp return got_error_from_errno();
407 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
410 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
413 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
416 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
420 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
423 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
426 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
430 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
434 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
435 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
436 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
439 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
441 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
442 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
443 fc76cabb 2018-12-25 stsp err = got_ferror(infile, GOT_ERR_IO);
446 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
448 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
452 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
455 52a74475 2018-12-24 stsp free(entry);
457 52a74475 2018-12-24 stsp *entryp = entry;
458 52a74475 2018-12-24 stsp return err;
461 52a74475 2018-12-24 stsp const struct got_error *
462 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
464 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
465 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
466 52a74475 2018-12-24 stsp SHA1_CTX ctx;
467 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
468 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
469 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
471 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
472 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
473 52a74475 2018-12-24 stsp uint8_t buf[len];
476 52a74475 2018-12-24 stsp SHA1Init(&ctx);
478 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
479 51514078 2018-12-25 stsp if (n != len) {
480 51514078 2018-12-25 stsp if (n == 0) /* EOF */
481 51514078 2018-12-25 stsp return NULL;
482 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
485 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
487 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
488 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
489 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
490 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
492 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
493 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
494 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
495 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
497 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
498 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
500 52a74475 2018-12-24 stsp return err;
501 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
503 52a74475 2018-12-24 stsp return err;
506 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
507 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
508 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
509 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
510 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
511 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
513 9d31a1d8 2018-03-11 stsp return NULL;