2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
29 #include "got_compat.h"
31 #include "got_error.h"
32 #include "got_object.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
56 got_fileindex_entry_perms_get(struct got_fileindex_entry *ie)
58 return ((ie->mode & GOT_FILEIDX_MODE_PERMS) >>
59 GOT_FILEIDX_MODE_PERMS_SHIFT);
63 fileindex_entry_perms_set(struct got_fileindex_entry *ie, mode_t mode)
65 ie->mode &= ~GOT_FILEIDX_MODE_PERMS;
66 ie->mode |= ((mode << GOT_FILEIDX_MODE_PERMS_SHIFT) &
67 GOT_FILEIDX_MODE_PERMS);
71 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
73 mode_t perms = got_fileindex_entry_perms_get(ie);
74 int type = got_fileindex_entry_filetype_get(ie);
77 if (type == GOT_FILEIDX_MODE_REGULAR_FILE ||
78 type == GOT_FILEIDX_MODE_BAD_SYMLINK)
83 return (ftype | (perms & (S_IRWXU | S_IRWXG | S_IRWXO)));
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)
93 if (fstatat(wt_fd, ondisk_path, &sb, AT_SYMLINK_NOFOLLOW) != 0) {
94 if (!((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) &&
96 return got_error_from_errno2("fstatat", ondisk_path);
97 sb.st_mode = GOT_DEFAULT_FILE_MODE;
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;
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);
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));
128 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
129 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
131 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
134 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
135 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
137 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
143 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
145 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
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,
160 *ie = calloc(1, sizeof(**ie));
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");
172 len = strlen(relpath);
173 if (len > GOT_FILEIDX_F_PATH_LEN)
174 len = GOT_FILEIDX_F_PATH_LEN;
181 got_fileindex_entry_free(struct got_fileindex_entry *ie)
188 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
190 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
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);
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);
208 got_fileindex_entry_filetype_get(struct got_fileindex_entry *ie)
210 return (ie->mode & GOT_FILEIDX_MODE_FILE_TYPE_ONDISK);
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);
221 got_fileindex_entry_staged_filetype_set(struct got_fileindex_entry *ie,
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);
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;
237 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
239 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
243 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
245 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
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;
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++;
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);
282 got_fileindex_entry_remove(struct got_fileindex *fileindex,
283 struct got_fileindex_entry *ie)
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.
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,
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))
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)
321 err = (*cb)(cb_arg, ie);
328 struct got_fileindex *
329 got_fileindex_alloc(void)
331 struct got_fileindex *fileindex;
333 fileindex = calloc(1, sizeof(*fileindex));
334 if (fileindex == NULL)
337 RB_INIT(&fileindex->entries);
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);
353 static const struct got_error *
354 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
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);
366 static const struct got_error *
367 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
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);
379 static const struct got_error *
380 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
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);
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 };
399 while ((len + pad) % 8 != 0)
402 pad = 8; /* NUL-terminate */
404 SHA1Update(ctx, path, len);
405 n = fwrite(path, 1, len, outfile);
407 return got_ferror(outfile, GOT_ERR_IO);
408 SHA1Update(ctx, zero, pad);
409 n = fwrite(zero, 1, pad, outfile);
411 return got_ferror(outfile, GOT_ERR_IO);
415 static const struct got_error *
416 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
419 const struct got_error *err;
423 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
426 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
429 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
432 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
436 err = write_fileindex_val32(ctx, ie->uid, outfile);
439 err = write_fileindex_val32(ctx, ie->gid, outfile);
442 err = write_fileindex_val32(ctx, ie->size, outfile);
446 err = write_fileindex_val16(ctx, ie->mode, outfile);
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);
464 err = write_fileindex_path(ctx, ie->path, outfile);
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,
474 if (n != SHA1_DIGEST_LENGTH)
475 return got_ferror(outfile, GOT_ERR_IO);
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;
487 uint8_t sha1[SHA1_DIGEST_LENGTH];
489 struct got_fileindex_entry *ie, *tmp;
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);
518 err = write_fileindex_entry(&ctx, ie, outfile);
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");
534 static const struct got_error *
535 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
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);
547 static const struct got_error *
548 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
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);
560 static const struct got_error *
561 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
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);
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);
582 return got_error_from_errno("malloc");
585 if (len + chunk_size > totlen) {
586 char *p = reallocarray(*path, totlen + chunk_size, 1);
588 err = got_error_from_errno("reallocarray");
591 totlen += chunk_size;
594 n = fread(*path + len, 1, chunk_size, infile);
595 if (n != chunk_size) {
596 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
599 SHA1Update(ctx, *path + len, chunk_size);
601 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
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;
620 ie = calloc(1, sizeof(*ie));
622 return got_error_from_errno("calloc");
624 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
627 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
630 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
633 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
637 err = read_fileindex_val32(&ie->uid, ctx, infile);
640 err = read_fileindex_val32(&ie->gid, ctx, infile);
643 err = read_fileindex_val32(&ie->size, ctx, infile);
647 err = read_fileindex_val16(&ie->mode, ctx, infile);
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);
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);
663 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
665 err = read_fileindex_val32(&ie->flags, ctx, infile);
669 err = read_fileindex_path(&ie->path, ctx, infile);
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,
679 if (n != SHA1_DIGEST_LENGTH) {
680 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
683 SHA1Update(ctx, ie->staged_blob_sha1, SHA1_DIGEST_LENGTH);
686 /* GOT_FILE_INDEX_VERSION 1 does not support staging. */
687 ie->flags &= ~GOT_FILEIDX_F_STAGE;
692 got_fileindex_entry_free(ie);
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;
704 struct got_fileindex_entry *ie;
705 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
706 uint8_t sha1[SHA1_DIGEST_LENGTH];
712 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
713 if (n != sizeof(hdr.signature)) {
714 if (n == 0) /* EOF */
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 */
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 */
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);
748 err = add_entry(fileindex, ie);
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);
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);
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))) {
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));
809 err = diff_fileindex_tree(fileindex, ie, subtree, subpath,
810 entry_name, repo, cb, cb_arg);
812 got_object_tree_close(subtree);
818 *next = got_object_tree_get_entry(tree, *tidx);
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;
834 te = got_object_tree_get_entry(tree, tidx);
835 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
838 const char *te_name = got_tree_entry_get_name(te);
840 if (asprintf(&te_path, "%s/%s", path, te_name) == -1) {
841 err = got_error_from_errno("asprintf");
844 cmp = got_path_cmp((*ie)->path, te_path,
845 got_fileindex_entry_path_len(*ie), strlen(te_path));
848 if (got_path_is_child((*ie)->path, path,
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,
855 if (err || entry_name)
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)
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)
877 err = walk_tree(&te, fileindex, ie, tree, &tidx,
878 path, entry_name, repo, cb, cb_arg);
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)
894 if (!got_object_tree_entry_is_submodule(te) &&
895 (entry_name == NULL ||
896 strcmp(got_tree_entry_get_name(te), entry_name)
898 err = cb->diff_new(cb_arg, te, path);
899 if (err || entry_name)
902 err = walk_tree(&te, fileindex, ie, tree, &tidx, path,
903 entry_name, repo, cb, cb_arg);
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,
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;
940 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
942 err = got_error_from_errno("malloc");
946 if (readdir_r(dir, de, &dep) != 0) {
947 err = got_error_from_errno("readdir_r");
956 if (strcmp(de->d_name, ".") == 0 ||
957 strcmp(de->d_name, "..") == 0 ||
959 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
964 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
970 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
980 free_dirlist(struct got_pathlist_head *dirlist)
982 struct got_pathlist_entry *dle;
984 TAILQ_FOREACH(dle, dirlist, entry)
986 got_pathlist_free(dirlist);
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);
996 ie = RB_ROOT(&fileindex->entries);
998 if (got_path_is_child(ie->path, path, path_len))
1000 cmp = got_path_cmp(path, ie->path, path_len,
1001 got_fileindex_entry_path_len(ie));
1003 ie = RB_LEFT(ie, entry);
1005 ie = RB_RIGHT(ie, entry);
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;
1026 /* Must traverse ignored directories if they contain tracked files. */
1027 if (de->d_type == DT_DIR && ignore &&
1028 have_tracked_file_in_dir(fileindex, path))
1031 if (de->d_type == DT_DIR && !ignore) {
1034 struct got_pathlist_head subdirlist;
1036 TAILQ_INIT(&subdirlist);
1038 if (asprintf(&subpath, "%s%s%s", path,
1039 path[0] == '\0' ? "" : "/", de->d_name) == -1)
1040 return got_error_from_errno("asprintf");
1042 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
1044 return got_error_from_errno("asprintf");
1047 subdirfd = openat(fd, de->d_name,
1048 O_RDONLY | O_NOFOLLOW | O_DIRECTORY | O_CLOEXEC);
1049 if (subdirfd == -1) {
1050 if (errno == EACCES) {
1051 *next = TAILQ_NEXT(dle, entry);
1054 err = got_error_from_errno2("openat", subdirpath);
1060 subdir = fdopendir(subdirfd);
1062 return got_error_from_errno2("fdopendir", path);
1064 err = read_dirlist(&subdirlist, subdir, subdirpath);
1071 err = diff_fileindex_dir(fileindex, ie, &subdirlist,
1072 dirfd(subdir), rootpath, subpath, repo, cb, cb_arg);
1073 if (subdir && closedir(subdir) == -1 && err == NULL)
1074 err = got_error_from_errno2("closedir", subdirpath);
1077 free_dirlist(&subdirlist);
1082 *next = TAILQ_NEXT(dle, entry);
1086 static const struct got_error *
1087 dirent_type_fixup(struct dirent *de, const char *rootpath, const char *path)
1089 const struct got_error *err;
1093 if (de->d_type != DT_UNKNOWN)
1096 /* DT_UNKNOWN occurs on NFS mounts without "readdir plus" RPC. */
1097 if (asprintf(&dir_path, "%s/%s", rootpath, path) == -1)
1098 return got_error_from_errno("asprintf");
1099 err = got_path_dirent_type(&type, dir_path, de);
1108 static const struct got_error *
1109 diff_fileindex_dir(struct got_fileindex *fileindex,
1110 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
1111 int dirfd, const char *rootpath, const char *path,
1112 struct got_repository *repo,
1113 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1115 const struct got_error *err = NULL;
1116 struct dirent *de = NULL;
1117 size_t path_len = strlen(path);
1118 struct got_pathlist_entry *dle;
1121 if (cb->diff_traverse) {
1122 err = cb->diff_traverse(cb_arg, path, dirfd);
1127 dle = TAILQ_FIRST(dirlist);
1128 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
1133 err = dirent_type_fixup(de, rootpath, path);
1136 if (asprintf(&de_path, "%s/%s", path,
1137 de->d_name) == -1) {
1138 err = got_error_from_errno("asprintf");
1141 cmp = got_path_cmp((*ie)->path, de_path,
1142 got_fileindex_entry_path_len(*ie),
1143 strlen(path) + 1 + strlen(de->d_name));
1146 err = cb->diff_old_new(cb_arg, *ie, de, path,
1150 *ie = walk_fileindex(fileindex, *ie);
1151 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1152 path, rootpath, repo, 0, cb, cb_arg);
1153 } else if (cmp < 0 ) {
1154 err = cb->diff_old(cb_arg, *ie, path);
1157 *ie = walk_fileindex(fileindex, *ie);
1159 err = cb->diff_new(&ignore, cb_arg, de, path,
1163 err = walk_dir(&dle, fileindex, ie, dle, dirfd,
1164 path, rootpath, repo, ignore, cb, cb_arg);
1169 err = cb->diff_old(cb_arg, *ie, path);
1172 *ie = walk_fileindex(fileindex, *ie);
1175 err = dirent_type_fixup(de, rootpath, path);
1178 err = cb->diff_new(&ignore, cb_arg, de, path, dirfd);
1181 err = walk_dir(&dle, fileindex, ie, dle, dirfd, path,
1182 rootpath, repo, ignore, cb, cb_arg);
1191 const struct got_error *
1192 got_fileindex_diff_dir(struct got_fileindex *fileindex, int fd,
1193 const char *rootpath, const char *path, struct got_repository *repo,
1194 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
1196 const struct got_error *err;
1197 struct got_fileindex_entry *ie;
1198 struct got_pathlist_head dirlist;
1202 TAILQ_INIT(&dirlist);
1205 * Duplicate the file descriptor so we can call closedir() below
1206 * without closing the file descriptor passed in by our caller.
1210 return got_error_from_errno2("dup", path);
1211 if (lseek(fd2, 0, SEEK_SET) == -1) {
1212 err = got_error_from_errno2("lseek", path);
1216 dir = fdopendir(fd2);
1218 err = got_error_from_errno2("fdopendir", path);
1222 err = read_dirlist(&dirlist, dir, path);
1228 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
1229 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
1230 ie = walk_fileindex(fileindex, ie);
1231 err = diff_fileindex_dir(fileindex, &ie, &dirlist, dirfd(dir),
1232 rootpath, path, repo, cb, cb_arg);
1234 if (closedir(dir) == -1 && err == NULL)
1235 err = got_error_from_errno2("closedir", path);
1236 free_dirlist(&dirlist);
1240 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);