Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
19 #include <errno.h>
20 #include <dirent.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <limits.h>
26 #include <unistd.h>
27 #include <uuid.h>
29 #include "got_compat.h"
31 #include "got_error.h"
32 #include "got_object.h"
33 #include "got_path.h"
35 #include "got_lib_fileindex.h"
36 #include "got_lib_worktree.h"
38 /* got_fileindex_entry flags */
39 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
40 #define GOT_FILEIDX_F_STAGE 0x0000f000
41 #define GOT_FILEIDX_F_STAGE_SHIFT 12
42 #define GOT_FILEIDX_F_NOT_FLUSHED 0x00010000
43 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
44 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
45 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
46 #define GOT_FILEIDX_F_REMOVE_ON_FLUSH 0x00100000
47 #define GOT_FILEIDX_F_SKIPPED 0x00200000
49 struct got_fileindex {
50 struct got_fileindex_tree entries;
51 int nentries; /* Does not include entries marked for removal. */
52 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
53 };
55 mode_t
56 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
57 {
58 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
59 GOT_FILEIDX_MODE_PERMS_SHIFT);
60 }
62 static void
63 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
64 {
65 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
66 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
67 GOT_FILEIDX_MODE_PERMS);
68 }
70 mode_t
71 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
72 {
73 mode_t perms = got_fileindex_entry_perms_get(ie);
74 int type = got_fileindex_entry_filetype_get(ie);
75 uint32_t ftype;
77 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
78 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
79 ftype = S_IFREG;
80 else
81 ftype = S_IFLNK;
83 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
84 }
86 const struct got_error *
87 got_fileindex_entry_update(struct got_fileindex_entry *ie,
88 int wt_fd, const char *ondisk_path, uint8_t *blob_sha1,
89 uint8_t *commit_sha1, int update_timestamps)
90 {
91 struct stat sb;
93 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
94 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
95 errno == ENOENT))
96 return got_error_from_errno2("fstatat", ondisk_path);
97 sb.st_mode = GOT_DEFAULT_FILE_MODE;
98 } else {
99 if (sb.st_mode & S_IFDIR)
100 return got_error_set_errno(EISDIR, ondisk_path);
101 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
105 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
106 if (update_timestamps) {
107 ie->ctime_sec = sb.st_ctim.tv_sec;
108 ie->ctime_nsec = sb.st_ctim.tv_nsec;
109 ie->mtime_sec = sb.st_mtim.tv_sec;
110 ie->mtime_nsec = sb.st_mtim.tv_nsec;
112 ie->uid = sb.st_uid;
113 ie->gid = sb.st_gid;
114 ie->size = (sb.st_size & 0xffffffff);
115 if (S_ISLNK(sb.st_mode)) {
116 got_fileindex_entry_filetype_set(ie,
117 GOT_FILEIDX_MODE_SYMLINK);
118 fileindex_entry_perms_set(ie, 0);
119 } else {
120 got_fileindex_entry_filetype_set(ie,
121 GOT_FILEIDX_MODE_REGULAR_FILE);
122 fileindex_entry_perms_set(ie,
123 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
127 if (blob_sha1) {
128 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
129 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
130 } else
131 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
133 if (commit_sha1) {
134 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
135 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
136 } else
137 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
139 return NULL;
142 void
143 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
145 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
148 void
149 got_fileindex_entry_mark_skipped(struct got_fileindex_entry *ie)
151 ie->flags |= GOT_FILEIDX_F_SKIPPED;
154 const struct got_error *
155 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
156 const char *relpath)
158 size_t len;
160 *ie = calloc(1, sizeof(**ie));
161 if (*ie == NULL)
162 return got_error_from_errno("calloc");
164 (*ie)->path = strdup(relpath);
165 if ((*ie)->path == NULL) {
166 const struct got_error *err = got_error_from_errno("strdup");
167 free(*ie);
168 *ie = NULL;
169 return err;
172 len = strlen(relpath);
173 if (len > GOT_FILEIDX_F_PATH_LEN)
174 len = GOT_FILEIDX_F_PATH_LEN;
175 (*ie)->flags |= len;
177 return NULL;
180 void
181 got_fileindex_entry_free(struct got_fileindex_entry *ie)
183 free(ie->path);
184 free(ie);
187 size_t
188 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
190 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
193 uint32_t
194 got_fileindex_entry_stage_get(const struct got_fileindex_entry *ie)
196 return ((ie->flags & GOT_FILEIDX_F_STAGE) >> GOT_FILEIDX_F_STAGE_SHIFT);
199 void
200 got_fileindex_entry_stage_set(struct got_fileindex_entry *ie, uint32_t stage)
202 ie->flags &= ~GOT_FILEIDX_F_STAGE;
203 ie->flags |= ((stage << GOT_FILEIDX_F_STAGE_SHIFT) &
204 GOT_FILEIDX_F_STAGE);
207 int
208 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
210 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
213 void
214 got_fileindex_entry_filetype_set(struct got_fileindex_entry *ie, int type)
216 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_ONDISK;
217 ie->mode |= (type & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
220 void
221 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
222 int type)
224 ie->mode &= ~GOT_FILEIDX_MODE_FILE_TYPE_STAGED;
225 ie->mode |= ((type << GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT) &
226 GOT_FILEIDX_MODE_FILE_TYPE_STAGED);
229 int
230 got_fileindex_entry_staged_filetype_get(struct got_fileindex_entry *ie)
232 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_STAGED) >>
233 GOT_FILEIDX_MODE_FILE_TYPE_STAGED_SHIFT;
236 int
237 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
239 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
242 int
243 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
245 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
248 int
249 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
251 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
254 int
255 got_fileindex_entry_was_skipped(struct got_fileindex_entry *ie)
257 return (ie->flags & GOT_FILEIDX_F_SKIPPED) != 0;
260 static const struct got_error *
261 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
263 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
264 return got_error(GOT_ERR_NO_SPACE);
266 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
267 fileindex->nentries++;
268 return NULL;
271 const struct got_error *
272 got_fileindex_entry_add(struct got_fileindex *fileindex,
273 struct got_fileindex_entry *ie)
275 /* Flag this entry until it gets written out to disk. */
276 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
278 return add_entry(fileindex, ie);
281 void
282 got_fileindex_entry_remove(struct got_fileindex *fileindex,
283 struct got_fileindex_entry *ie)
285 /*
286 * Removing an entry from the RB tree immediately breaks
287 * in-progress iterations over file index entries.
288 * So flag this entry for removal and remove it once the index
289 * is written out to disk. Meanwhile, pretend this entry no longer
290 * exists if we get queried for it again before then.
291 */
292 ie->flags |= GOT_FILEIDX_F_REMOVE_ON_FLUSH;
293 fileindex->nentries--;
296 struct got_fileindex_entry *
297 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
298 size_t path_len)
300 struct got_fileindex_entry *ie;
301 struct got_fileindex_entry key;
302 memset(&key, 0, sizeof(key));
303 key.path = (char *)path;
304 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
305 ie = RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
306 if (ie && (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH))
307 return NULL;
308 return ie;
311 const struct got_error *
312 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
313 got_fileindex_cb cb, void *cb_arg)
315 const struct got_error *err;
316 struct got_fileindex_entry *ie, *tmp;
318 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
319 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH)
320 continue;
321 err = (*cb)(cb_arg, ie);
322 if (err)
323 return err;
325 return NULL;
328 struct got_fileindex *
329 got_fileindex_alloc(void)
331 struct got_fileindex *fileindex;
333 fileindex = calloc(1, sizeof(*fileindex));
334 if (fileindex == NULL)
335 return NULL;
337 RB_INIT(&fileindex->entries);
338 return fileindex;
341 void
342 got_fileindex_free(struct got_fileindex *fileindex)
344 struct got_fileindex_entry *ie;
346 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
347 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
348 got_fileindex_entry_free(ie);
350 free(fileindex);
353 static const struct got_error *
354 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
356 size_t n;
358 val = htobe64(val);
359 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
360 n = fwrite(&val, 1, sizeof(val), outfile);
361 if (n != sizeof(val))
362 return got_ferror(outfile, GOT_ERR_IO);
363 return NULL;
366 static const struct got_error *
367 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
369 size_t n;
371 val = htobe32(val);
372 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
373 n = fwrite(&val, 1, sizeof(val), outfile);
374 if (n != sizeof(val))
375 return got_ferror(outfile, GOT_ERR_IO);
376 return NULL;
379 static const struct got_error *
380 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
382 size_t n;
384 val = htobe16(val);
385 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
386 n = fwrite(&val, 1, sizeof(val), outfile);
387 if (n != sizeof(val))
388 return got_ferror(outfile, GOT_ERR_IO);
389 return NULL;
392 static const struct got_error *
393 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
395 size_t n, len, pad = 0;
396 static const uint8_t zero[8] = { 0 };
398 len = strlen(path);
399 while ((len + pad) % 8 != 0)
400 pad++;
401 if (pad == 0)
402 pad = 8; /* NUL-terminate */
404 SHA1Update(ctx, path, len);
405 n = fwrite(path, 1, len, outfile);
406 if (n != len)
407 return got_ferror(outfile, GOT_ERR_IO);
408 SHA1Update(ctx, zero, pad);
409 n = fwrite(zero, 1, pad, outfile);
410 if (n != pad)
411 return got_ferror(outfile, GOT_ERR_IO);
412 return NULL;
415 static const struct got_error *
416 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
417 FILE *outfile)
419 const struct got_error *err;
420 size_t n;
421 uint32_t stage;
423 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
424 if (err)
425 return err;
426 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
427 if (err)
428 return err;
429 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
430 if (err)
431 return err;
432 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
433 if (err)
434 return err;
436 err = write_fileindex_val32(ctx, ie->uid, outfile);
437 if (err)
438 return err;
439 err = write_fileindex_val32(ctx, ie->gid, outfile);
440 if (err)
441 return err;
442 err = write_fileindex_val32(ctx, ie->size, outfile);
443 if (err)
444 return err;
446 err = write_fileindex_val16(ctx, ie->mode, outfile);
447 if (err)
448 return err;
450 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
451 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
452 if (n != SHA1_DIGEST_LENGTH)
453 return got_ferror(outfile, GOT_ERR_IO);
455 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
456 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
457 if (n != SHA1_DIGEST_LENGTH)
458 return got_ferror(outfile, GOT_ERR_IO);
460 err = write_fileindex_val32(ctx, ie->flags, outfile);
461 if (err)
462 return err;
464 err = write_fileindex_path(ctx, ie->path, outfile);
465 if (err)
466 return err;
468 stage = got_fileindex_entry_stage_get(ie);
469 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
470 stage == GOT_FILEIDX_STAGE_ADD) {
471 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
472 n = fwrite(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
473 outfile);
474 if (n != SHA1_DIGEST_LENGTH)
475 return got_ferror(outfile, GOT_ERR_IO);
478 return NULL;
481 const struct got_error *
482 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
484 const struct got_error *err = NULL;
485 struct got_fileindex_hdr hdr;
486 SHA1_CTX ctx;
487 uint8_t sha1[SHA1_DIGEST_LENGTH];
488 size_t n;
489 struct got_fileindex_entry *ie, *tmp;
491 SHA1Init(&ctx);
493 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
494 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
495 hdr.nentries = htobe32(fileindex->nentries);
497 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
498 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
499 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
500 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
501 if (n != sizeof(hdr.signature))
502 return got_ferror(outfile, GOT_ERR_IO);
503 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
504 if (n != sizeof(hdr.version))
505 return got_ferror(outfile, GOT_ERR_IO);
506 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
507 if (n != sizeof(hdr.nentries))
508 return got_ferror(outfile, GOT_ERR_IO);
510 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
511 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
512 ie->flags &= ~GOT_FILEIDX_F_SKIPPED;
513 if (ie->flags & GOT_FILEIDX_F_REMOVE_ON_FLUSH) {
514 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
515 got_fileindex_entry_free(ie);
516 continue;
518 err = write_fileindex_entry(&ctx, ie, outfile);
519 if (err)
520 return err;
523 SHA1Final(sha1, &ctx);
524 n = fwrite(sha1, 1, sizeof(sha1), outfile);
525 if (n != sizeof(sha1))
526 return got_ferror(outfile, GOT_ERR_IO);
528 if (fflush(outfile) != 0)
529 return got_error_from_errno("fflush");
531 return NULL;
534 static const struct got_error *
535 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
537 size_t n;
539 n = fread(val, 1, sizeof(*val), infile);
540 if (n != sizeof(*val))
541 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
542 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
543 *val = be64toh(*val);
544 return NULL;
547 static const struct got_error *
548 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
550 size_t n;
552 n = fread(val, 1, sizeof(*val), infile);
553 if (n != sizeof(*val))
554 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
555 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
556 *val = be32toh(*val);
557 return NULL;
560 static const struct got_error *
561 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
563 size_t n;
565 n = fread(val, 1, sizeof(*val), infile);
566 if (n != sizeof(*val))
567 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
568 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
569 *val = be16toh(*val);
570 return NULL;
573 static const struct got_error *
574 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
576 const struct got_error *err = NULL;
577 const size_t chunk_size = 8;
578 size_t n, len = 0, totlen = chunk_size;
580 *path = malloc(totlen);
581 if (*path == NULL)
582 return got_error_from_errno("malloc");
584 do {
585 if (len + chunk_size > totlen) {
586 char *p = reallocarray(*path, totlen + chunk_size, 1);
587 if (p == NULL) {
588 err = got_error_from_errno("reallocarray");
589 break;
591 totlen += chunk_size;
592 *path = p;
594 n = fread(*path + len, 1, chunk_size, infile);
595 if (n != chunk_size) {
596 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
597 break;
599 SHA1Update(ctx, *path + len, chunk_size);
600 len += chunk_size;
601 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
603 if (err) {
604 free(*path);
605 *path = NULL;
607 return err;
610 static const struct got_error *
611 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
612 FILE *infile, uint32_t version)
614 const struct got_error *err;
615 struct got_fileindex_entry *ie;
616 size_t n;
618 *iep = NULL;
620 ie = calloc(1, sizeof(*ie));
621 if (ie == NULL)
622 return got_error_from_errno("calloc");
624 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
625 if (err)
626 goto done;
627 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
628 if (err)
629 goto done;
630 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
631 if (err)
632 goto done;
633 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
634 if (err)
635 goto done;
637 err = read_fileindex_val32(&ie->uid, ctx, infile);
638 if (err)
639 goto done;
640 err = read_fileindex_val32(&ie->gid, ctx, infile);
641 if (err)
642 goto done;
643 err = read_fileindex_val32(&ie->size, ctx, infile);
644 if (err)
645 goto done;
647 err = read_fileindex_val16(&ie->mode, ctx, infile);
648 if (err)
649 goto done;
651 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
652 if (n != SHA1_DIGEST_LENGTH) {
653 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
654 goto done;
656 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
658 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
659 if (n != SHA1_DIGEST_LENGTH) {
660 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
661 goto done;
663 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
665 err = read_fileindex_val32(&ie->flags, ctx, infile);
666 if (err)
667 goto done;
669 err = read_fileindex_path(&ie->path, ctx, infile);
670 if (err)
671 goto done;
673 if (version >= 2) {
674 uint32_t stage = got_fileindex_entry_stage_get(ie);
675 if (stage == GOT_FILEIDX_STAGE_MODIFY ||
676 stage == GOT_FILEIDX_STAGE_ADD) {
677 n = fread(ie->staged_blob_sha1, 1, SHA1_DIGEST_LENGTH,
678 infile);
679 if (n != SHA1_DIGEST_LENGTH) {
680 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
681 goto done;
683 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
685 } else {
686 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
687 ie->flags &= ~GOT_FILEIDX_F_STAGE;
690 done:
691 if (err)
692 got_fileindex_entry_free(ie);
693 else
694 *iep = ie;
695 return err;
698 const struct got_error *
699 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
701 const struct got_error *err = NULL;
702 struct got_fileindex_hdr hdr;
703 SHA1_CTX ctx;
704 struct got_fileindex_entry *ie;
705 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
706 uint8_t sha1[SHA1_DIGEST_LENGTH];
707 size_t n;
708 int i;
710 SHA1Init(&ctx);
712 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
713 if (n != sizeof(hdr.signature)) {
714 if (n == 0) /* EOF */
715 return NULL;
716 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
718 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
719 if (n != sizeof(hdr.version)) {
720 if (n == 0) /* EOF */
721 return NULL;
722 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
724 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
725 if (n != sizeof(hdr.nentries)) {
726 if (n == 0) /* EOF */
727 return NULL;
728 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
731 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
732 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
733 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
735 hdr.signature = be32toh(hdr.signature);
736 hdr.version = be32toh(hdr.version);
737 hdr.nentries = be32toh(hdr.nentries);
739 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
740 return got_error(GOT_ERR_FILEIDX_SIG);
741 if (hdr.version > GOT_FILE_INDEX_VERSION)
742 return got_error(GOT_ERR_FILEIDX_VER);
744 for (i = 0; i < hdr.nentries; i++) {
745 err = read_fileindex_entry(&ie, &ctx, infile, hdr.version);
746 if (err)
747 return err;
748 err = add_entry(fileindex, ie);
749 if (err)
750 return err;
753 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
754 if (n != sizeof(sha1_expected))
755 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
756 SHA1Final(sha1, &ctx);
757 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
758 return got_error(GOT_ERR_FILEIDX_CSUM);
760 return NULL;
763 static struct got_fileindex_entry *
764 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
766 struct got_fileindex_entry *next;
768 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
770 /* Skip entries which were added or removed by diff callbacks. */
771 while (next && (next->flags & (GOT_FILEIDX_F_NOT_FLUSHED |
772 GOT_FILEIDX_F_REMOVE_ON_FLUSH)))
773 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
775 return next;
778 static const struct got_error *
779 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **ie,
780 struct got_tree_object *tree, const char *, const char *,
781 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
783 static const struct got_error *
784 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
785 struct got_fileindex_entry **ie, struct got_tree_object *tree, int *tidx,
786 const char *path, const char *entry_name, struct got_repository *repo,
787 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
789 const struct got_error *err = NULL;
790 struct got_tree_entry *te = got_object_tree_get_entry(tree, *tidx);
792 if (!got_object_tree_entry_is_submodule(te) &&
793 S_ISDIR(got_tree_entry_get_mode(te))) {
794 char *subpath;
795 struct got_tree_object *subtree;
797 if (asprintf(&subpath, "%s%s%s", path,
798 path[0] == '\0' ? "" : "/",
799 got_tree_entry_get_name(te)) == -1)
800 return got_error_from_errno("asprintf");
802 err = got_object_open_as_tree(&subtree, repo,
803 got_tree_entry_get_id(te));
804 if (err) {
805 free(subpath);
806 return err;
809 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
810 entry_name, repo, cb, cb_arg);
811 free(subpath);
812 got_object_tree_close(subtree);
813 if (err)
814 return err;
817 (*tidx)++;
818 *next = got_object_tree_get_entry(tree, *tidx);
819 return NULL;
822 static const struct got_error *
823 diff_fileindex_tree(struct got_fileindex *fileindex,
824 struct got_fileindex_entry **ie, struct got_tree_object *tree,
825 const char *path, const char *entry_name, struct got_repository *repo,
826 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
828 const struct got_error *err = NULL;
829 struct got_tree_entry *te = NULL;
830 size_t path_len = strlen(path);
831 struct got_fileindex_entry *next;
832 int tidx = 0;
834 te = got_object_tree_get_entry(tree, tidx);
835 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
836 if (te && *ie) {
837 char *te_path;
838 const char *te_name = got_tree_entry_get_name(te);
839 int cmp;
840 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
841 err = got_error_from_errno("asprintf");
842 break;
844 cmp = got_path_cmp((*ie)->path, te_path,
845 got_fileindex_entry_path_len(*ie), strlen(te_path));
846 free(te_path);
847 if (cmp == 0) {
848 if (got_path_is_child((*ie)->path, path,
849 path_len) &&
850 !got_object_tree_entry_is_submodule(te) &&
851 (entry_name == NULL ||
852 strcmp(te_name, entry_name) == 0)) {
853 err = cb->diff_old_new(cb_arg, *ie, te,
854 path);
855 if (err || entry_name)
856 break;
858 *ie = walk_fileindex(fileindex, *ie);
859 err = walk_tree(&te, fileindex, ie, tree, &tidx,
860 path, entry_name, repo, cb, cb_arg);
861 } else if (cmp < 0) {
862 next = walk_fileindex(fileindex, *ie);
863 if (got_path_is_child((*ie)->path, path,
864 path_len) && entry_name == NULL) {
865 err = cb->diff_old(cb_arg, *ie, path);
866 if (err || entry_name)
867 break;
869 *ie = next;
870 } else {
871 if ((entry_name == NULL ||
872 strcmp(te_name, entry_name) == 0)) {
873 err = cb->diff_new(cb_arg, te, path);
874 if (err || entry_name)
875 break;
877 err = walk_tree(&te, fileindex, ie, tree, &tidx,
878 path, entry_name, repo, cb, cb_arg);
880 if (err)
881 break;
882 } else if (*ie) {
883 next = walk_fileindex(fileindex, *ie);
884 if (got_path_is_child((*ie)->path, path, path_len) &&
885 (entry_name == NULL ||
886 (te && strcmp(got_tree_entry_get_name(te),
887 entry_name) == 0))) {
888 err = cb->diff_old(cb_arg, *ie, path);
889 if (err || entry_name)
890 break;
892 *ie = next;
893 } else if (te) {
894 if (!got_object_tree_entry_is_submodule(te) &&
895 (entry_name == NULL ||
896 strcmp(got_tree_entry_get_name(te), entry_name)
897 == 0)) {
898 err = cb->diff_new(cb_arg, te, path);
899 if (err || entry_name)
900 break;
902 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
903 entry_name, repo, cb, cb_arg);
904 if (err)
905 break;
909 return err;
912 const struct got_error *
913 got_fileindex_diff_tree(struct got_fileindex *fileindex,
914 struct got_tree_object *tree, const char *path, const char *entry_name,
915 struct got_repository *repo,
916 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
918 struct got_fileindex_entry *ie;
919 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
920 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
921 ie = walk_fileindex(fileindex, ie);
922 return diff_fileindex_tree(fileindex, &ie, tree, path, entry_name, repo,
923 cb, cb_arg);
926 static const struct got_error *
927 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
928 struct got_pathlist_head *, int, const char *, const char *,
929 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
931 static const struct got_error *
932 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
934 const struct got_error *err = NULL;
935 struct got_pathlist_entry *new = NULL;
936 struct dirent *dep = NULL;
937 struct dirent *de = NULL;
939 for (;;) {
940 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
941 if (de == NULL) {
942 err = got_error_from_errno("malloc");
943 break;
946 if (readdir_r(dir, de, &dep) != 0) {
947 err = got_error_from_errno("readdir_r");
948 free(de);
949 break;
951 if (dep == NULL) {
952 free(de);
953 break;
956 if (strcmp(de->d_name, ".") == 0 ||
957 strcmp(de->d_name, "..") == 0 ||
958 (path[0] == '\0' &&
959 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
960 free(de);
961 continue;
964 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
965 if (err) {
966 free(de);
967 break;
969 if (new == NULL) {
970 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
971 free(de);
972 break;
976 return err;
979 void
980 free_dirlist(struct got_pathlist_head *dirlist)
982 struct got_pathlist_entry *dle;
984 TAILQ_FOREACH(dle, dirlist, entry)
985 free(dle->data);
986 got_pathlist_free(dirlist);
989 static int
990 have_tracked_file_in_dir(struct got_fileindex *fileindex, const char *path)
992 struct got_fileindex_entry *ie;
993 size_t path_len = strlen(path);
994 int cmp;
996 ie = RB_ROOT(&fileindex->entries);
997 while (ie) {
998 if (got_path_is_child(ie->path, path, path_len))
999 return 1;
1000 cmp = got_path_cmp(path, ie->path, path_len,
1001 got_fileindex_entry_path_len(ie));
1002 if (cmp < 0)
1003 ie = RB_LEFT(ie, entry);
1004 else if (cmp > 0)
1005 ie = RB_RIGHT(ie, entry);
1006 else
1007 break;
1010 return 0;
1013 static const struct got_error *
1014 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
1015 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle, int fd,
1016 const char *path, const char *rootpath, struct got_repository *repo,
1017 int ignore, struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1019 const struct got_error *err = NULL;
1020 struct dirent *de = dle->data;
1021 DIR *subdir = NULL;
1022 int subdirfd = -1;
1023 int type;
1025 *next = NULL;
1027 if (de->d_type == DT_UNKNOWN) {
1028 /* Occurs on NFS mounts without "readdir plus" RPC. */
1029 char *dir_path;
1030 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1031 return got_error_from_errno("asprintf");
1032 err = got_path_dirent_type(&type, dir_path, de);
1033 free(dir_path);
1034 if (err)
1035 return err;
1036 } else
1037 type = de->d_type;
1039 /* Must traverse ignored directories if they contain tracked files. */
1040 if (type == DT_DIR && ignore &&
1041 have_tracked_file_in_dir(fileindex, path))
1042 ignore = 0;
1044 if (type == DT_DIR && !ignore) {
1045 char *subpath;
1046 char *subdirpath;
1047 struct got_pathlist_head subdirlist;
1049 TAILQ_INIT(&subdirlist);
1051 if (asprintf(&subpath, "%s%s%s", path,
1052 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1053 return got_error_from_errno("asprintf");
1055 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1056 free(subpath);
1057 return got_error_from_errno("asprintf");
1060 subdirfd = openat(fd, de->d_name,
1061 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1062 if (subdirfd == -1) {
1063 if (errno == EACCES) {
1064 *next = TAILQ_NEXT(dle, entry);
1065 return NULL;
1067 err = got_error_from_errno2("openat", subdirpath);
1068 free(subpath);
1069 free(subdirpath);
1070 return err;
1073 subdir = fdopendir(subdirfd);
1074 if (subdir == NULL)
1075 return got_error_from_errno2("fdopendir", path);
1076 subdirfd = -1;
1077 err = read_dirlist(&subdirlist, subdir, subdirpath);
1078 if (err) {
1079 free(subpath);
1080 free(subdirpath);
1081 closedir(subdir);
1082 return err;
1084 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1085 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1086 if (subdir && closedir(subdir) == -1 && err == NULL)
1087 err = got_error_from_errno2("closedir", subdirpath);
1088 free(subpath);
1089 free(subdirpath);
1090 free_dirlist(&subdirlist);
1091 if (err)
1092 return err;
1095 *next = TAILQ_NEXT(dle, entry);
1096 return NULL;
1099 static const struct got_error *
1100 diff_fileindex_dir(struct got_fileindex *fileindex,
1101 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1102 int dirfd, const char *rootpath, const char *path,
1103 struct got_repository *repo,
1104 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1106 const struct got_error *err = NULL;
1107 struct dirent *de = NULL;
1108 size_t path_len = strlen(path);
1109 struct got_pathlist_entry *dle;
1110 int ignore;
1112 if (cb->diff_traverse) {
1113 err = cb->diff_traverse(cb_arg, path, dirfd);
1114 if (err)
1115 return err;
1118 dle = TAILQ_FIRST(dirlist);
1119 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1120 if (dle && *ie) {
1121 char *de_path;
1122 int cmp;
1123 de = dle->data;
1124 if (asprintf(&de_path, "%s/%s", path,
1125 de->d_name) == -1) {
1126 err = got_error_from_errno("asprintf");
1127 break;
1129 cmp = got_path_cmp((*ie)->path, de_path,
1130 got_fileindex_entry_path_len(*ie),
1131 strlen(path) + 1 + strlen(de->d_name));
1132 free(de_path);
1133 if (cmp == 0) {
1134 err = cb->diff_old_new(cb_arg, *ie, de, path,
1135 dirfd);
1136 if (err)
1137 break;
1138 *ie = walk_fileindex(fileindex, *ie);
1139 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1140 path, rootpath, repo, 0, cb, cb_arg);
1141 } else if (cmp < 0 ) {
1142 err = cb->diff_old(cb_arg, *ie, path);
1143 if (err)
1144 break;
1145 *ie = walk_fileindex(fileindex, *ie);
1146 } else {
1147 err = cb->diff_new(&ignore, cb_arg, de, path,
1148 dirfd);
1149 if (err)
1150 break;
1151 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1152 path, rootpath, repo, ignore, cb, cb_arg);
1154 if (err)
1155 break;
1156 } else if (*ie) {
1157 err = cb->diff_old(cb_arg, *ie, path);
1158 if (err)
1159 break;
1160 *ie = walk_fileindex(fileindex, *ie);
1161 } else if (dle) {
1162 de = dle->data;
1163 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1164 if (err)
1165 break;
1166 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1167 rootpath, repo, ignore, cb, cb_arg);
1168 if (err)
1169 break;
1173 return err;
1176 const struct got_error *
1177 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1178 const char *rootpath, const char *path, struct got_repository *repo,
1179 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1181 const struct got_error *err;
1182 struct got_fileindex_entry *ie;
1183 struct got_pathlist_head dirlist;
1184 int fd2;
1185 DIR *dir;
1187 TAILQ_INIT(&dirlist);
1190 * Duplicate the file descriptor so we can call closedir() below
1191 * without closing the file descriptor passed in by our caller.
1193 fd2 = dup(fd);
1194 if (fd2 == -1)
1195 return got_error_from_errno2("dup", path);
1196 if (lseek(fd2, 0, SEEK_SET) == -1) {
1197 err = got_error_from_errno2("lseek", path);
1198 close(fd2);
1199 return err;
1201 dir = fdopendir(fd2);
1202 if (dir == NULL) {
1203 err = got_error_from_errno2("fdopendir", path);
1204 close(fd2);
1205 return err;
1207 err = read_dirlist(&dirlist, dir, path);
1208 if (err) {
1209 closedir(dir);
1210 return err;
1213 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1214 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1215 ie = walk_fileindex(fileindex, ie);
1216 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1217 rootpath, path, repo, cb, cb_arg);
1219 if (closedir(dir) == -1 && err == NULL)
1220 err = got_error_from_errno2("closedir", path);
1221 free_dirlist(&dirlist);
1222 return err;
1225 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);