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 133d2798 2019-01-08 stsp #include <sys/tree.h>
19 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
21 c48c4a9c 2018-03-11 stsp #include <stdio.h>
22 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
23 c48c4a9c 2018-03-11 stsp #include <string.h>
24 c48c4a9c 2018-03-11 stsp #include <sha1.h>
25 c34b20a2 2018-03-12 stsp #include <endian.h>
26 133d2798 2019-01-08 stsp #include <limits.h>
28 c48c4a9c 2018-03-11 stsp #include "got_error.h"
30 133d2798 2019-01-08 stsp #include "got_lib_path.h"
31 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
33 133d2798 2019-01-08 stsp struct got_fileindex {
34 133d2798 2019-01-08 stsp struct got_fileindex_tree entries;
35 133d2798 2019-01-08 stsp int nentries;
36 133d2798 2019-01-08 stsp #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
39 c48c4a9c 2018-03-11 stsp const struct got_error *
40 51514078 2018-12-25 stsp got_fileindex_entry_update(struct got_fileindex_entry *entry,
41 51514078 2018-12-25 stsp const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1)
43 c48c4a9c 2018-03-11 stsp struct stat sb;
45 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) != 0)
46 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
48 51514078 2018-12-25 stsp entry->ctime_sec = sb.st_ctime;
49 51514078 2018-12-25 stsp entry->ctime_nsec = sb.st_ctimensec;
50 51514078 2018-12-25 stsp entry->mtime_sec = sb.st_mtime;
51 51514078 2018-12-25 stsp entry->mtime_nsec = sb.st_mtimensec;
52 51514078 2018-12-25 stsp entry->uid = sb.st_uid;
53 51514078 2018-12-25 stsp entry->gid = sb.st_gid;
54 51514078 2018-12-25 stsp entry->size = (sb.st_size & 0xffffffff);
55 51514078 2018-12-25 stsp if (sb.st_mode & S_IFLNK)
56 51514078 2018-12-25 stsp entry->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
58 51514078 2018-12-25 stsp entry->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
59 51514078 2018-12-25 stsp entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
60 51514078 2018-12-25 stsp GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
61 51514078 2018-12-25 stsp memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
62 51514078 2018-12-25 stsp memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
64 51514078 2018-12-25 stsp return NULL;
67 51514078 2018-12-25 stsp const struct got_error *
68 51514078 2018-12-25 stsp got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
69 51514078 2018-12-25 stsp const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
70 51514078 2018-12-25 stsp uint8_t *commit_sha1)
74 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
75 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
76 0a585a0d 2018-03-17 stsp return got_error_from_errno();
78 c34b20a2 2018-03-12 stsp (*entry)->path = strdup(relpath);
79 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
80 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
81 c48c4a9c 2018-03-11 stsp free(*entry);
82 c48c4a9c 2018-03-11 stsp *entry = NULL;
86 c34b20a2 2018-03-12 stsp len = strlen(relpath);
87 c48c4a9c 2018-03-11 stsp if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
88 c48c4a9c 2018-03-11 stsp len = GOT_INDEX_ENTRY_F_PATH_LEN;
89 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
91 51514078 2018-12-25 stsp return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
92 51514078 2018-12-25 stsp commit_sha1);
96 7426bbfd 2018-12-24 stsp got_fileindex_entry_free(struct got_fileindex_entry *entry)
98 c48c4a9c 2018-03-11 stsp free(entry->path);
99 c48c4a9c 2018-03-11 stsp free(entry);
102 9d31a1d8 2018-03-11 stsp const struct got_error *
103 9d31a1d8 2018-03-11 stsp got_fileindex_entry_add(struct got_fileindex *fileindex,
104 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry)
106 133d2798 2019-01-08 stsp if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
107 133d2798 2019-01-08 stsp return got_error(GOT_ERR_NO_SPACE);
109 133d2798 2019-01-08 stsp RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
110 133d2798 2019-01-08 stsp fileindex->nentries++;
111 133d2798 2019-01-08 stsp return NULL;
115 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(struct got_fileindex *fileindex,
116 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry)
118 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
119 133d2798 2019-01-08 stsp fileindex->nentries--;
122 51514078 2018-12-25 stsp struct got_fileindex_entry *
123 51514078 2018-12-25 stsp got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
125 133d2798 2019-01-08 stsp struct got_fileindex_entry key;
126 133d2798 2019-01-08 stsp memset(&key, 0, sizeof(key));
127 133d2798 2019-01-08 stsp key.path = (char *)path;
128 133d2798 2019-01-08 stsp return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
131 512f0d0e 2019-01-02 stsp const struct got_error *
132 e1ed7f77 2019-01-06 stsp got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
133 b504a804 2019-01-08 stsp got_fileindex_cb cb, void *cb_arg)
135 133d2798 2019-01-08 stsp const struct got_error *err;
136 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry, *tmp;
138 133d2798 2019-01-08 stsp RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
139 133d2798 2019-01-08 stsp err = (*cb)(cb_arg, entry);
141 133d2798 2019-01-08 stsp return err;
143 b504a804 2019-01-08 stsp return NULL;
146 133d2798 2019-01-08 stsp struct got_fileindex *
147 133d2798 2019-01-08 stsp got_fileindex_alloc(void)
149 133d2798 2019-01-08 stsp struct got_fileindex *fileindex;
151 133d2798 2019-01-08 stsp fileindex = calloc(1, sizeof(*fileindex));
152 133d2798 2019-01-08 stsp if (fileindex == NULL)
153 133d2798 2019-01-08 stsp return NULL;
155 133d2798 2019-01-08 stsp RB_INIT(&fileindex->entries);
156 133d2798 2019-01-08 stsp return fileindex;
160 7426bbfd 2018-12-24 stsp got_fileindex_free(struct got_fileindex *fileindex)
162 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
164 133d2798 2019-01-08 stsp while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
165 133d2798 2019-01-08 stsp RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
166 133d2798 2019-01-08 stsp got_fileindex_entry_free(entry);
168 9d31a1d8 2018-03-11 stsp free(fileindex);
171 c34b20a2 2018-03-12 stsp static const struct got_error *
172 c34b20a2 2018-03-12 stsp write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
176 c34b20a2 2018-03-12 stsp val = htobe64(val);
177 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
178 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
179 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
180 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
181 c34b20a2 2018-03-12 stsp return NULL;
184 c34b20a2 2018-03-12 stsp static const struct got_error *
185 c34b20a2 2018-03-12 stsp write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
189 c34b20a2 2018-03-12 stsp val = htobe32(val);
190 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
191 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
192 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
193 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
194 c34b20a2 2018-03-12 stsp return NULL;
197 c34b20a2 2018-03-12 stsp static const struct got_error *
198 c34b20a2 2018-03-12 stsp write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
202 c34b20a2 2018-03-12 stsp val = htobe16(val);
203 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
204 3fe2daf1 2018-12-24 stsp n = fwrite(&val, 1, sizeof(val), outfile);
205 c34b20a2 2018-03-12 stsp if (n != sizeof(val))
206 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
207 c34b20a2 2018-03-12 stsp return NULL;
210 c34b20a2 2018-03-12 stsp static const struct got_error *
211 c34b20a2 2018-03-12 stsp write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
213 3c5b70f2 2018-12-27 stsp size_t n, len, pad = 0;
214 c34b20a2 2018-03-12 stsp static const uint8_t zero[8] = { 0 };
216 c34b20a2 2018-03-12 stsp len = strlen(path);
217 3c5b70f2 2018-12-27 stsp while ((len + pad) % 8 != 0)
219 3c5b70f2 2018-12-27 stsp if (pad == 0)
220 3c5b70f2 2018-12-27 stsp pad = 8; /* NUL-terminate */
222 c34b20a2 2018-03-12 stsp SHA1Update(ctx, path, len);
223 c34b20a2 2018-03-12 stsp n = fwrite(path, 1, len, outfile);
224 c34b20a2 2018-03-12 stsp if (n != len)
225 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
226 c34b20a2 2018-03-12 stsp SHA1Update(ctx, zero, pad);
227 c34b20a2 2018-03-12 stsp n = fwrite(zero, 1, pad, outfile);
228 c34b20a2 2018-03-12 stsp if (n != pad)
229 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
230 c34b20a2 2018-03-12 stsp return NULL;
233 c34b20a2 2018-03-12 stsp static const struct got_error *
234 c34b20a2 2018-03-12 stsp write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
235 c34b20a2 2018-03-12 stsp FILE *outfile)
237 c34b20a2 2018-03-12 stsp const struct got_error *err;
240 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
242 c34b20a2 2018-03-12 stsp return err;
243 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
245 c34b20a2 2018-03-12 stsp return err;
246 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
248 c34b20a2 2018-03-12 stsp return err;
249 c34b20a2 2018-03-12 stsp err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
251 c34b20a2 2018-03-12 stsp return err;
253 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->uid, outfile);
255 c34b20a2 2018-03-12 stsp return err;
256 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->gid, outfile);
258 c34b20a2 2018-03-12 stsp return err;
259 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->size, outfile);
261 c34b20a2 2018-03-12 stsp return err;
263 c34b20a2 2018-03-12 stsp err = write_fileindex_val16(ctx, entry->mode, outfile);
265 c34b20a2 2018-03-12 stsp return err;
267 c34b20a2 2018-03-12 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
268 c34b20a2 2018-03-12 stsp n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
269 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH)
270 fc76cabb 2018-12-25 stsp return got_ferror(outfile, GOT_ERR_IO);
272 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
273 fc76cabb 2018-12-25 stsp n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
274 c34b20a2 2018-03-12 stsp if (n != SHA1_DIGEST_LENGTH)
275 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
277 c34b20a2 2018-03-12 stsp err = write_fileindex_val32(ctx, entry->flags, outfile);
279 c34b20a2 2018-03-12 stsp return err;
281 c34b20a2 2018-03-12 stsp err = write_fileindex_path(ctx, entry->path, outfile);
282 c34b20a2 2018-03-12 stsp return err;
285 9d31a1d8 2018-03-11 stsp const struct got_error *
286 9d31a1d8 2018-03-11 stsp got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
288 b504a804 2019-01-08 stsp const struct got_error *err = NULL;
289 c34b20a2 2018-03-12 stsp struct got_fileindex_hdr hdr;
290 c34b20a2 2018-03-12 stsp SHA1_CTX ctx;
291 c34b20a2 2018-03-12 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
293 c34b20a2 2018-03-12 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
294 c34b20a2 2018-03-12 stsp sizeof(hdr.nentries);
295 c34b20a2 2018-03-12 stsp uint8_t buf[len];
296 133d2798 2019-01-08 stsp struct got_fileindex_entry *entry;
298 c34b20a2 2018-03-12 stsp SHA1Init(&ctx);
300 c34b20a2 2018-03-12 stsp hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
301 c34b20a2 2018-03-12 stsp hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
302 133d2798 2019-01-08 stsp hdr.nentries = htobe32(fileindex->nentries);
304 c34b20a2 2018-03-12 stsp memcpy(buf, &hdr, len);
305 c34b20a2 2018-03-12 stsp SHA1Update(&ctx, buf, len);
306 c34b20a2 2018-03-12 stsp n = fwrite(buf, 1, len, outfile);
307 c34b20a2 2018-03-12 stsp if (n != len)
308 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
310 133d2798 2019-01-08 stsp RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
311 133d2798 2019-01-08 stsp err = write_fileindex_entry(&ctx, entry, outfile);
313 133d2798 2019-01-08 stsp return err;
316 c34b20a2 2018-03-12 stsp SHA1Final(sha1, &ctx);
317 c34b20a2 2018-03-12 stsp n = fwrite(sha1, 1, sizeof(sha1), outfile);
318 c34b20a2 2018-03-12 stsp if (n != sizeof(sha1))
319 c34b20a2 2018-03-12 stsp return got_ferror(outfile, GOT_ERR_IO);
321 52a74475 2018-12-24 stsp return NULL;
324 52a74475 2018-12-24 stsp static const struct got_error *
325 52a74475 2018-12-24 stsp read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
329 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
330 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
331 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
332 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
333 9eb6a6b2 2018-12-24 stsp *val = be64toh(*val);
334 52a74475 2018-12-24 stsp return NULL;
337 52a74475 2018-12-24 stsp static const struct got_error *
338 52a74475 2018-12-24 stsp read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
342 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
343 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
344 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
345 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
346 9eb6a6b2 2018-12-24 stsp *val = be32toh(*val);
347 52a74475 2018-12-24 stsp return NULL;
350 52a74475 2018-12-24 stsp static const struct got_error *
351 52a74475 2018-12-24 stsp read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
355 3fe2daf1 2018-12-24 stsp n = fread(val, 1, sizeof(*val), infile);
356 3fe2daf1 2018-12-24 stsp if (n != sizeof(*val))
357 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
358 3fe2daf1 2018-12-24 stsp SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
359 9eb6a6b2 2018-12-24 stsp *val = be16toh(*val);
360 52a74475 2018-12-24 stsp return NULL;
363 52a74475 2018-12-24 stsp static const struct got_error *
364 52a74475 2018-12-24 stsp read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
366 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
367 52a74475 2018-12-24 stsp uint8_t buf[8];
368 52a74475 2018-12-24 stsp size_t n, len = 0, totlen = sizeof(buf);
370 52a74475 2018-12-24 stsp *path = malloc(totlen);
371 52a74475 2018-12-24 stsp if (*path == NULL)
372 52a74475 2018-12-24 stsp return got_error_from_errno();
375 52a74475 2018-12-24 stsp n = fread(buf, 1, sizeof(buf), infile);
376 52a74475 2018-12-24 stsp if (n != sizeof(buf))
377 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
378 52a74475 2018-12-24 stsp if (len + sizeof(buf) > totlen) {
379 52a74475 2018-12-24 stsp char *p = reallocarray(*path, totlen + sizeof(buf), 1);
380 52a74475 2018-12-24 stsp if (p == NULL) {
381 52a74475 2018-12-24 stsp err = got_error_from_errno();
384 52a74475 2018-12-24 stsp totlen += sizeof(buf);
387 52a74475 2018-12-24 stsp SHA1Update(ctx, buf, sizeof(buf));
388 52a74475 2018-12-24 stsp memcpy(*path + len, buf, sizeof(buf));
389 52a74475 2018-12-24 stsp len += sizeof(buf);
390 60619907 2018-12-27 stsp } while (memchr(buf, '\0', sizeof(buf)) == NULL);
393 52a74475 2018-12-24 stsp free(*path);
394 52a74475 2018-12-24 stsp *path = NULL;
396 52a74475 2018-12-24 stsp return err;
399 52a74475 2018-12-24 stsp static const struct got_error *
400 52a74475 2018-12-24 stsp read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
401 52a74475 2018-12-24 stsp FILE *infile)
403 52a74475 2018-12-24 stsp const struct got_error *err;
404 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
407 52a74475 2018-12-24 stsp *entryp = NULL;
409 52a74475 2018-12-24 stsp entry = calloc(1, sizeof(*entry));
410 52a74475 2018-12-24 stsp if (entry == NULL)
411 52a74475 2018-12-24 stsp return got_error_from_errno();
413 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
416 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
419 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
422 52a74475 2018-12-24 stsp err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
426 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->uid, ctx, infile);
429 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->gid, ctx, infile);
432 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->size, ctx, infile);
436 52a74475 2018-12-24 stsp err = read_fileindex_val16(&entry->mode, ctx, infile);
440 52a74475 2018-12-24 stsp n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
441 52a74475 2018-12-24 stsp if (n != SHA1_DIGEST_LENGTH) {
442 52a74475 2018-12-24 stsp err = got_ferror(infile, GOT_ERR_IO);
445 52a74475 2018-12-24 stsp SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
447 fc76cabb 2018-12-25 stsp n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
448 fc76cabb 2018-12-25 stsp if (n != SHA1_DIGEST_LENGTH) {
449 fc76cabb 2018-12-25 stsp err = got_ferror(infile, GOT_ERR_IO);
452 fc76cabb 2018-12-25 stsp SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
454 52a74475 2018-12-24 stsp err = read_fileindex_val32(&entry->flags, ctx, infile);
458 52a74475 2018-12-24 stsp err = read_fileindex_path(&entry->path, ctx, infile);
461 52a74475 2018-12-24 stsp free(entry);
463 52a74475 2018-12-24 stsp *entryp = entry;
464 52a74475 2018-12-24 stsp return err;
467 52a74475 2018-12-24 stsp const struct got_error *
468 52a74475 2018-12-24 stsp got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
470 52a74475 2018-12-24 stsp const struct got_error *err = NULL;
471 52a74475 2018-12-24 stsp struct got_fileindex_hdr hdr;
472 52a74475 2018-12-24 stsp SHA1_CTX ctx;
473 52a74475 2018-12-24 stsp struct got_fileindex_entry *entry;
474 52a74475 2018-12-24 stsp uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
475 52a74475 2018-12-24 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
477 52a74475 2018-12-24 stsp const size_t len = sizeof(hdr.signature) + sizeof(hdr.version) +
478 52a74475 2018-12-24 stsp sizeof(hdr.nentries);
479 52a74475 2018-12-24 stsp uint8_t buf[len];
482 52a74475 2018-12-24 stsp SHA1Init(&ctx);
484 52a74475 2018-12-24 stsp n = fread(buf, 1, len, infile);
485 51514078 2018-12-25 stsp if (n != len) {
486 51514078 2018-12-25 stsp if (n == 0) /* EOF */
487 51514078 2018-12-25 stsp return NULL;
488 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
491 52a74475 2018-12-24 stsp SHA1Update(&ctx, buf, len);
493 52a74475 2018-12-24 stsp memcpy(&hdr, buf, len);
494 9eb6a6b2 2018-12-24 stsp hdr.signature = be32toh(hdr.signature);
495 9eb6a6b2 2018-12-24 stsp hdr.version = be32toh(hdr.version);
496 9eb6a6b2 2018-12-24 stsp hdr.nentries = be32toh(hdr.nentries);
498 52a74475 2018-12-24 stsp if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
499 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_SIG);
500 52a74475 2018-12-24 stsp if (hdr.version != GOT_FILE_INDEX_VERSION)
501 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_VER);
503 52a74475 2018-12-24 stsp for (i = 0; i < hdr.nentries; i++) {
504 52a74475 2018-12-24 stsp err = read_fileindex_entry(&entry, &ctx, infile);
506 52a74475 2018-12-24 stsp return err;
507 52a74475 2018-12-24 stsp err = got_fileindex_entry_add(fileindex, entry);
509 52a74475 2018-12-24 stsp return err;
512 52a74475 2018-12-24 stsp n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
513 52a74475 2018-12-24 stsp if (n != sizeof(sha1_expected))
514 52a74475 2018-12-24 stsp return got_ferror(infile, GOT_ERR_IO);
515 52a74475 2018-12-24 stsp SHA1Final(sha1, &ctx);
516 52a74475 2018-12-24 stsp if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
517 52a74475 2018-12-24 stsp return got_error(GOT_ERR_FILEIDX_CSUM);
519 9d31a1d8 2018-03-11 stsp return NULL;
522 7a9df742 2019-01-08 stsp RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);