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.
17 #include <sys/queue.h>
30 #include "got_error.h"
31 #include "got_object.h"
33 #include "got_lib_path.h"
34 #include "got_lib_fileindex.h"
35 #include "got_lib_worktree.h"
37 /* got_fileindex_entry flags */
38 #define GOT_FILEIDX_F_PATH_LEN 0x00000fff
39 #define GOT_FILEIDX_F_STAGE 0x00003000
40 #define GOT_FILEIDX_F_EXTENDED 0x00004000
41 #define GOT_FILEIDX_F_ASSUME_VALID 0x00008000
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
47 struct got_fileindex {
48 struct got_fileindex_tree entries;
50 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
53 const struct got_error *
54 got_fileindex_entry_update(struct got_fileindex_entry *entry,
55 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
56 int update_timestamps)
60 if (lstat(ondisk_path, &sb) != 0)
61 return got_error_from_errno();
63 entry->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
65 if (update_timestamps) {
66 entry->ctime_sec = sb.st_ctime;
67 entry->ctime_nsec = sb.st_ctimensec;
68 entry->mtime_sec = sb.st_mtime;
69 entry->mtime_nsec = sb.st_mtimensec;
71 entry->uid = sb.st_uid;
72 entry->gid = sb.st_gid;
73 entry->size = (sb.st_size & 0xffffffff);
74 if (sb.st_mode & S_IFLNK)
75 entry->mode = GOT_FILEIDX_MODE_SYMLINK;
77 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
78 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
79 GOT_FILEIDX_MODE_PERMS_SHIFT);
82 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
83 entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
85 entry->flags |= GOT_FILEIDX_F_NO_BLOB;
88 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
89 entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
91 entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
97 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *entry)
99 entry->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
102 const struct got_error *
103 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
104 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
105 uint8_t *commit_sha1)
109 *entry = calloc(1, sizeof(**entry));
111 return got_error_from_errno();
113 (*entry)->path = strdup(relpath);
114 if ((*entry)->path == NULL) {
115 const struct got_error *err = got_error_from_errno();
121 len = strlen(relpath);
122 if (len > GOT_FILEIDX_F_PATH_LEN)
123 len = GOT_FILEIDX_F_PATH_LEN;
124 (*entry)->flags |= len;
126 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
131 got_fileindex_entry_free(struct got_fileindex_entry *entry)
138 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
140 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
144 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
146 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
150 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
152 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
155 static const struct got_error *
156 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
158 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
159 return got_error(GOT_ERR_NO_SPACE);
161 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
162 fileindex->nentries++;
166 const struct got_error *
167 got_fileindex_entry_add(struct got_fileindex *fileindex,
168 struct got_fileindex_entry *entry)
170 /* Flag this entry until it gets written out to disk. */
171 entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
173 return add_entry(fileindex, entry);
177 got_fileindex_entry_remove(struct got_fileindex *fileindex,
178 struct got_fileindex_entry *entry)
180 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
181 fileindex->nentries--;
184 struct got_fileindex_entry *
185 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
187 struct got_fileindex_entry key;
188 memset(&key, 0, sizeof(key));
189 key.path = (char *)path;
190 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
193 const struct got_error *
194 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
195 got_fileindex_cb cb, void *cb_arg)
197 const struct got_error *err;
198 struct got_fileindex_entry *entry, *tmp;
200 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
201 err = (*cb)(cb_arg, entry);
208 struct got_fileindex *
209 got_fileindex_alloc(void)
211 struct got_fileindex *fileindex;
213 fileindex = calloc(1, sizeof(*fileindex));
214 if (fileindex == NULL)
217 RB_INIT(&fileindex->entries);
222 got_fileindex_free(struct got_fileindex *fileindex)
224 struct got_fileindex_entry *entry;
226 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
227 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
228 got_fileindex_entry_free(entry);
233 static const struct got_error *
234 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
239 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
240 n = fwrite(&val, 1, sizeof(val), outfile);
241 if (n != sizeof(val))
242 return got_ferror(outfile, GOT_ERR_IO);
246 static const struct got_error *
247 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
252 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
253 n = fwrite(&val, 1, sizeof(val), outfile);
254 if (n != sizeof(val))
255 return got_ferror(outfile, GOT_ERR_IO);
259 static const struct got_error *
260 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
265 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
266 n = fwrite(&val, 1, sizeof(val), outfile);
267 if (n != sizeof(val))
268 return got_ferror(outfile, GOT_ERR_IO);
272 static const struct got_error *
273 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
275 size_t n, len, pad = 0;
276 static const uint8_t zero[8] = { 0 };
279 while ((len + pad) % 8 != 0)
282 pad = 8; /* NUL-terminate */
284 SHA1Update(ctx, path, len);
285 n = fwrite(path, 1, len, outfile);
287 return got_ferror(outfile, GOT_ERR_IO);
288 SHA1Update(ctx, zero, pad);
289 n = fwrite(zero, 1, pad, outfile);
291 return got_ferror(outfile, GOT_ERR_IO);
295 static const struct got_error *
296 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
299 const struct got_error *err;
302 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
305 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
308 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
311 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
315 err = write_fileindex_val32(ctx, entry->uid, outfile);
318 err = write_fileindex_val32(ctx, entry->gid, outfile);
321 err = write_fileindex_val32(ctx, entry->size, outfile);
325 err = write_fileindex_val16(ctx, entry->mode, outfile);
329 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
330 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
331 if (n != SHA1_DIGEST_LENGTH)
332 return got_ferror(outfile, GOT_ERR_IO);
334 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
335 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
336 if (n != SHA1_DIGEST_LENGTH)
337 return got_ferror(outfile, GOT_ERR_IO);
339 err = write_fileindex_val32(ctx, entry->flags, outfile);
343 err = write_fileindex_path(ctx, entry->path, outfile);
347 const struct got_error *
348 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
350 const struct got_error *err = NULL;
351 struct got_fileindex_hdr hdr;
353 uint8_t sha1[SHA1_DIGEST_LENGTH];
355 struct got_fileindex_entry *entry;
359 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
360 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
361 hdr.nentries = htobe32(fileindex->nentries);
363 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
364 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
365 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
366 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
367 if (n != sizeof(hdr.signature))
368 return got_ferror(outfile, GOT_ERR_IO);
369 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
370 if (n != sizeof(hdr.version))
371 return got_ferror(outfile, GOT_ERR_IO);
372 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
373 if (n != sizeof(hdr.nentries))
374 return got_ferror(outfile, GOT_ERR_IO);
376 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
377 entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
378 err = write_fileindex_entry(&ctx, entry, outfile);
383 SHA1Final(sha1, &ctx);
384 n = fwrite(sha1, 1, sizeof(sha1), outfile);
385 if (n != sizeof(sha1))
386 return got_ferror(outfile, GOT_ERR_IO);
388 if (fflush(outfile) != 0)
389 return got_error_from_errno();
394 static const struct got_error *
395 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
399 n = fread(val, 1, sizeof(*val), infile);
400 if (n != sizeof(*val))
401 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
402 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
403 *val = be64toh(*val);
407 static const struct got_error *
408 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
412 n = fread(val, 1, sizeof(*val), infile);
413 if (n != sizeof(*val))
414 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
415 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
416 *val = be32toh(*val);
420 static const struct got_error *
421 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
425 n = fread(val, 1, sizeof(*val), infile);
426 if (n != sizeof(*val))
427 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
428 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
429 *val = be16toh(*val);
433 static const struct got_error *
434 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
436 const struct got_error *err = NULL;
438 size_t n, len = 0, totlen = sizeof(buf);
440 *path = malloc(totlen);
442 return got_error_from_errno();
445 n = fread(buf, 1, sizeof(buf), infile);
446 if (n != sizeof(buf))
447 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
448 if (len + sizeof(buf) > totlen) {
449 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
451 err = got_error_from_errno();
454 totlen += sizeof(buf);
457 SHA1Update(ctx, buf, sizeof(buf));
458 memcpy(*path + len, buf, sizeof(buf));
460 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
469 static const struct got_error *
470 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
473 const struct got_error *err;
474 struct got_fileindex_entry *entry;
479 entry = calloc(1, sizeof(*entry));
481 return got_error_from_errno();
483 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
486 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
489 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
492 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
496 err = read_fileindex_val32(&entry->uid, ctx, infile);
499 err = read_fileindex_val32(&entry->gid, ctx, infile);
502 err = read_fileindex_val32(&entry->size, ctx, infile);
506 err = read_fileindex_val16(&entry->mode, ctx, infile);
510 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
511 if (n != SHA1_DIGEST_LENGTH) {
512 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
515 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
517 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
518 if (n != SHA1_DIGEST_LENGTH) {
519 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
522 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
524 err = read_fileindex_val32(&entry->flags, ctx, infile);
528 err = read_fileindex_path(&entry->path, ctx, infile);
537 const struct got_error *
538 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
540 const struct got_error *err = NULL;
541 struct got_fileindex_hdr hdr;
543 struct got_fileindex_entry *entry;
544 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
545 uint8_t sha1[SHA1_DIGEST_LENGTH];
551 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
552 if (n != sizeof(hdr.signature)) {
553 if (n == 0) /* EOF */
555 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
557 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
558 if (n != sizeof(hdr.version)) {
559 if (n == 0) /* EOF */
561 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
563 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
564 if (n != sizeof(hdr.nentries)) {
565 if (n == 0) /* EOF */
567 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
570 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
571 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
572 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
574 hdr.signature = be32toh(hdr.signature);
575 hdr.version = be32toh(hdr.version);
576 hdr.nentries = be32toh(hdr.nentries);
578 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
579 return got_error(GOT_ERR_FILEIDX_SIG);
580 if (hdr.version != GOT_FILE_INDEX_VERSION)
581 return got_error(GOT_ERR_FILEIDX_VER);
583 for (i = 0; i < hdr.nentries; i++) {
584 err = read_fileindex_entry(&entry, &ctx, infile);
587 err = add_entry(fileindex, entry);
592 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
593 if (n != sizeof(sha1_expected))
594 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
595 SHA1Final(sha1, &ctx);
596 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
597 return got_error(GOT_ERR_FILEIDX_CSUM);
602 static struct got_fileindex_entry *
603 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
605 struct got_fileindex_entry *next;
607 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
609 /* Skip entries which were newly added by diff callbacks. */
610 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
611 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
616 static const struct got_error *
617 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
618 struct got_tree_object *, const char *, struct got_repository *,
619 struct got_fileindex_diff_tree_cb *, void *);
621 static const struct got_error *
622 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
623 struct got_fileindex_entry **ie, struct got_tree_entry *te,
624 const char *path, struct got_repository *repo,
625 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
627 const struct got_error *err = NULL;
629 if (S_ISDIR(te->mode)) {
631 struct got_tree_object *subtree;
633 if (asprintf(&subpath, "%s%s%s", path,
634 path[0] == '\0' ? "" : "/", te->name) == -1)
635 return got_error_from_errno();
637 err = got_object_open_as_tree(&subtree, repo, te->id);
643 err = diff_fileindex_tree(fileindex, ie, subtree,
644 subpath, repo, cb, cb_arg);
646 got_object_tree_close(subtree);
651 *next = SIMPLEQ_NEXT(te, entry);
655 static const struct got_error *
656 diff_fileindex_tree(struct got_fileindex *fileindex,
657 struct got_fileindex_entry **ie, struct got_tree_object *tree,
658 const char *path, struct got_repository *repo,
659 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
661 const struct got_error *err = NULL;
662 struct got_tree_entry *te = NULL;
663 size_t path_len = strlen(path);
664 const struct got_tree_entries *entries;
665 struct got_fileindex_entry *next;
667 entries = got_object_tree_get_entries(tree);
668 te = SIMPLEQ_FIRST(&entries->head);
669 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
673 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
674 err = got_error_from_errno();
677 cmp = got_path_cmp((*ie)->path, te_path);
680 err = cb->diff_old_new(cb_arg, *ie, te,
684 *ie = walk_fileindex(fileindex, *ie);
685 err = walk_tree(&te, fileindex, ie, te,
686 path, repo, cb, cb_arg);
687 } else if (cmp < 0 ) {
688 next = walk_fileindex(fileindex, *ie);
689 err = cb->diff_old(cb_arg, *ie, path);
694 err = cb->diff_new(cb_arg, te, path);
697 err = walk_tree(&te, fileindex, ie, te,
698 path, repo, cb, cb_arg);
703 next = walk_fileindex(fileindex, *ie);
704 err = cb->diff_old(cb_arg, *ie, path);
709 err = cb->diff_new(cb_arg, te, path);
712 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
722 const struct got_error *
723 got_fileindex_diff_tree(struct got_fileindex *fileindex,
724 struct got_tree_object *tree, struct got_repository *repo,
725 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
727 struct got_fileindex_entry *min;
728 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
729 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
732 static const struct got_error *
733 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
734 const char *, const char *, struct got_repository *,
735 struct got_fileindex_diff_dir_cb *, void *);
737 static const struct got_error *
738 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
739 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
740 const char *path, DIR *dir, const char *rootpath,
741 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
744 const struct got_error *err = NULL;
745 struct dirent *de = dle->data;
747 if (de->d_type == DT_DIR) {
752 if (asprintf(&subpath, "%s%s%s", path,
753 path[0] == '\0' ? "" : "/", de->d_name) == -1)
754 return got_error_from_errno();
756 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
758 return got_error_from_errno();
761 subdir = opendir(subdirpath);
762 if (subdir == NULL) {
765 return got_error_from_errno();
768 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
769 subpath, repo, cb, cb_arg);
777 *next = TAILQ_NEXT(dle, entry);
781 static const struct got_error *
782 diff_fileindex_dir(struct got_fileindex *fileindex,
783 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
784 const char *path, struct got_repository *repo,
785 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
787 const struct got_error *err = NULL;
788 struct dirent *de = NULL;
789 size_t path_len = strlen(path);
790 struct got_fileindex_entry *next;
791 struct got_pathlist_head dirlist;
792 struct got_pathlist_entry *dle;
794 TAILQ_INIT(&dirlist);
797 struct got_pathlist_entry *new = NULL;
798 struct dirent *dep = NULL;
800 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
802 err = got_error_from_errno();
806 if (readdir_r(dir, de, &dep) != 0) {
807 err = got_error_from_errno();
816 if (strcmp(de->d_name, ".") == 0 ||
817 strcmp(de->d_name, "..") == 0 ||
819 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
824 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
828 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
833 dle = TAILQ_FIRST(&dirlist);
834 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
839 if (asprintf(&de_path, "%s/%s", path,
841 err = got_error_from_errno();
844 cmp = got_path_cmp((*ie)->path, de_path);
847 err = cb->diff_old_new(cb_arg, *ie, de, path);
850 *ie = walk_fileindex(fileindex, *ie);
851 err = walk_dir(&dle, fileindex, ie, dle, path,
852 dir, rootpath, repo, cb, cb_arg);
853 } else if (cmp < 0 ) {
854 next = walk_fileindex(fileindex, *ie);
855 err = cb->diff_old(cb_arg, *ie, path);
860 err = cb->diff_new(cb_arg, de, path);
863 err = walk_dir(&dle, fileindex, ie, dle, path,
864 dir, rootpath, repo, cb, cb_arg);
869 next = walk_fileindex(fileindex, *ie);
870 err = cb->diff_old(cb_arg, *ie, path);
876 err = cb->diff_new(cb_arg, de, path);
879 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
880 rootpath, repo, cb, cb_arg);
886 TAILQ_FOREACH(dle, &dirlist, entry)
888 got_pathlist_free(&dirlist);
892 const struct got_error *
893 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
894 const char *rootpath, const char *path, struct got_repository *repo,
895 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
897 struct got_fileindex_entry *min;
898 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
899 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
903 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);