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
46 struct got_fileindex {
47 struct got_fileindex_tree entries;
49 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
52 const struct got_error *
53 got_fileindex_entry_update(struct got_fileindex_entry *entry,
54 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
55 int update_timestamps)
59 if (lstat(ondisk_path, &sb) != 0)
60 return got_error_from_errno();
62 if (update_timestamps) {
63 entry->ctime_sec = sb.st_ctime;
64 entry->ctime_nsec = sb.st_ctimensec;
65 entry->mtime_sec = sb.st_mtime;
66 entry->mtime_nsec = sb.st_mtimensec;
68 entry->uid = sb.st_uid;
69 entry->gid = sb.st_gid;
70 entry->size = (sb.st_size & 0xffffffff);
71 if (sb.st_mode & S_IFLNK)
72 entry->mode = GOT_FILEIDX_MODE_SYMLINK;
74 entry->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
75 entry->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
76 GOT_FILEIDX_MODE_PERMS_SHIFT);
79 memcpy(entry->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
80 entry->flags &= ~GOT_FILEIDX_F_NO_BLOB;
82 entry->flags |= GOT_FILEIDX_F_NO_BLOB;
85 memcpy(entry->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
86 entry->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
88 entry->flags |= GOT_FILEIDX_F_NO_COMMIT;
93 const struct got_error *
94 got_fileindex_entry_alloc(struct got_fileindex_entry **entry,
95 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
100 *entry = calloc(1, sizeof(**entry));
102 return got_error_from_errno();
104 (*entry)->path = strdup(relpath);
105 if ((*entry)->path == NULL) {
106 const struct got_error *err = got_error_from_errno();
112 len = strlen(relpath);
113 if (len > GOT_FILEIDX_F_PATH_LEN)
114 len = GOT_FILEIDX_F_PATH_LEN;
115 (*entry)->flags |= len;
117 return got_fileindex_entry_update(*entry, ondisk_path, blob_sha1,
122 got_fileindex_entry_free(struct got_fileindex_entry *entry)
128 static const struct got_error *
129 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
131 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
132 return got_error(GOT_ERR_NO_SPACE);
134 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
135 fileindex->nentries++;
139 const struct got_error *
140 got_fileindex_entry_add(struct got_fileindex *fileindex,
141 struct got_fileindex_entry *entry)
143 /* Flag this entry until it gets written out to disk. */
144 entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
146 return add_entry(fileindex, entry);
150 got_fileindex_entry_remove(struct got_fileindex *fileindex,
151 struct got_fileindex_entry *entry)
153 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
154 fileindex->nentries--;
157 struct got_fileindex_entry *
158 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
160 struct got_fileindex_entry key;
161 memset(&key, 0, sizeof(key));
162 key.path = (char *)path;
163 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
166 const struct got_error *
167 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
168 got_fileindex_cb cb, void *cb_arg)
170 const struct got_error *err;
171 struct got_fileindex_entry *entry, *tmp;
173 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
174 err = (*cb)(cb_arg, entry);
181 struct got_fileindex *
182 got_fileindex_alloc(void)
184 struct got_fileindex *fileindex;
186 fileindex = calloc(1, sizeof(*fileindex));
187 if (fileindex == NULL)
190 RB_INIT(&fileindex->entries);
195 got_fileindex_free(struct got_fileindex *fileindex)
197 struct got_fileindex_entry *entry;
199 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
200 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
201 got_fileindex_entry_free(entry);
206 static const struct got_error *
207 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
212 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
213 n = fwrite(&val, 1, sizeof(val), outfile);
214 if (n != sizeof(val))
215 return got_ferror(outfile, GOT_ERR_IO);
219 static const struct got_error *
220 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
225 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
226 n = fwrite(&val, 1, sizeof(val), outfile);
227 if (n != sizeof(val))
228 return got_ferror(outfile, GOT_ERR_IO);
232 static const struct got_error *
233 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
238 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
239 n = fwrite(&val, 1, sizeof(val), outfile);
240 if (n != sizeof(val))
241 return got_ferror(outfile, GOT_ERR_IO);
245 static const struct got_error *
246 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
248 size_t n, len, pad = 0;
249 static const uint8_t zero[8] = { 0 };
252 while ((len + pad) % 8 != 0)
255 pad = 8; /* NUL-terminate */
257 SHA1Update(ctx, path, len);
258 n = fwrite(path, 1, len, outfile);
260 return got_ferror(outfile, GOT_ERR_IO);
261 SHA1Update(ctx, zero, pad);
262 n = fwrite(zero, 1, pad, outfile);
264 return got_ferror(outfile, GOT_ERR_IO);
268 static const struct got_error *
269 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
272 const struct got_error *err;
275 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
278 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
281 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
284 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
288 err = write_fileindex_val32(ctx, entry->uid, outfile);
291 err = write_fileindex_val32(ctx, entry->gid, outfile);
294 err = write_fileindex_val32(ctx, entry->size, outfile);
298 err = write_fileindex_val16(ctx, entry->mode, outfile);
302 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
303 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
304 if (n != SHA1_DIGEST_LENGTH)
305 return got_ferror(outfile, GOT_ERR_IO);
307 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
308 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
309 if (n != SHA1_DIGEST_LENGTH)
310 return got_ferror(outfile, GOT_ERR_IO);
312 err = write_fileindex_val32(ctx, entry->flags, outfile);
316 err = write_fileindex_path(ctx, entry->path, outfile);
320 const struct got_error *
321 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
323 const struct got_error *err = NULL;
324 struct got_fileindex_hdr hdr;
326 uint8_t sha1[SHA1_DIGEST_LENGTH];
328 struct got_fileindex_entry *entry;
332 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
333 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
334 hdr.nentries = htobe32(fileindex->nentries);
336 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
337 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
338 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
339 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
340 if (n != sizeof(hdr.signature))
341 return got_ferror(outfile, GOT_ERR_IO);
342 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
343 if (n != sizeof(hdr.version))
344 return got_ferror(outfile, GOT_ERR_IO);
345 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
346 if (n != sizeof(hdr.nentries))
347 return got_ferror(outfile, GOT_ERR_IO);
349 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
350 entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
351 err = write_fileindex_entry(&ctx, entry, outfile);
356 SHA1Final(sha1, &ctx);
357 n = fwrite(sha1, 1, sizeof(sha1), outfile);
358 if (n != sizeof(sha1))
359 return got_ferror(outfile, GOT_ERR_IO);
361 if (fflush(outfile) != 0)
362 return got_error_from_errno();
367 static const struct got_error *
368 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
372 n = fread(val, 1, sizeof(*val), infile);
373 if (n != sizeof(*val))
374 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
375 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
376 *val = be64toh(*val);
380 static const struct got_error *
381 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
385 n = fread(val, 1, sizeof(*val), infile);
386 if (n != sizeof(*val))
387 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
388 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
389 *val = be32toh(*val);
393 static const struct got_error *
394 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
398 n = fread(val, 1, sizeof(*val), infile);
399 if (n != sizeof(*val))
400 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
401 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
402 *val = be16toh(*val);
406 static const struct got_error *
407 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
409 const struct got_error *err = NULL;
411 size_t n, len = 0, totlen = sizeof(buf);
413 *path = malloc(totlen);
415 return got_error_from_errno();
418 n = fread(buf, 1, sizeof(buf), infile);
419 if (n != sizeof(buf))
420 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
421 if (len + sizeof(buf) > totlen) {
422 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
424 err = got_error_from_errno();
427 totlen += sizeof(buf);
430 SHA1Update(ctx, buf, sizeof(buf));
431 memcpy(*path + len, buf, sizeof(buf));
433 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
442 static const struct got_error *
443 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
446 const struct got_error *err;
447 struct got_fileindex_entry *entry;
452 entry = calloc(1, sizeof(*entry));
454 return got_error_from_errno();
456 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
459 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
462 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
465 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
469 err = read_fileindex_val32(&entry->uid, ctx, infile);
472 err = read_fileindex_val32(&entry->gid, ctx, infile);
475 err = read_fileindex_val32(&entry->size, ctx, infile);
479 err = read_fileindex_val16(&entry->mode, ctx, infile);
483 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
484 if (n != SHA1_DIGEST_LENGTH) {
485 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
488 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
490 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
491 if (n != SHA1_DIGEST_LENGTH) {
492 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
495 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
497 err = read_fileindex_val32(&entry->flags, ctx, infile);
501 err = read_fileindex_path(&entry->path, ctx, infile);
510 const struct got_error *
511 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
513 const struct got_error *err = NULL;
514 struct got_fileindex_hdr hdr;
516 struct got_fileindex_entry *entry;
517 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
518 uint8_t sha1[SHA1_DIGEST_LENGTH];
524 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
525 if (n != sizeof(hdr.signature)) {
526 if (n == 0) /* EOF */
528 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
530 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
531 if (n != sizeof(hdr.version)) {
532 if (n == 0) /* EOF */
534 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
536 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
537 if (n != sizeof(hdr.nentries)) {
538 if (n == 0) /* EOF */
540 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
543 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
544 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
545 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
547 hdr.signature = be32toh(hdr.signature);
548 hdr.version = be32toh(hdr.version);
549 hdr.nentries = be32toh(hdr.nentries);
551 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
552 return got_error(GOT_ERR_FILEIDX_SIG);
553 if (hdr.version != GOT_FILE_INDEX_VERSION)
554 return got_error(GOT_ERR_FILEIDX_VER);
556 for (i = 0; i < hdr.nentries; i++) {
557 err = read_fileindex_entry(&entry, &ctx, infile);
560 err = add_entry(fileindex, entry);
565 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
566 if (n != sizeof(sha1_expected))
567 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
568 SHA1Final(sha1, &ctx);
569 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
570 return got_error(GOT_ERR_FILEIDX_CSUM);
575 static struct got_fileindex_entry *
576 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
578 struct got_fileindex_entry *next;
580 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
582 /* Skip entries which were newly added by diff callbacks. */
583 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
584 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
589 static const struct got_error *
590 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
591 struct got_tree_object *, const char *, struct got_repository *,
592 struct got_fileindex_diff_tree_cb *, void *);
594 static const struct got_error *
595 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
596 struct got_fileindex_entry **ie, struct got_tree_entry *te,
597 const char *path, struct got_repository *repo,
598 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
600 const struct got_error *err = NULL;
602 if (S_ISDIR(te->mode)) {
604 struct got_tree_object *subtree;
606 if (asprintf(&subpath, "%s%s%s", path,
607 path[0] == '\0' ? "" : "/", te->name) == -1)
608 return got_error_from_errno();
610 err = got_object_open_as_tree(&subtree, repo, te->id);
616 err = diff_fileindex_tree(fileindex, ie, subtree,
617 subpath, repo, cb, cb_arg);
619 got_object_tree_close(subtree);
624 *next = SIMPLEQ_NEXT(te, entry);
628 static const struct got_error *
629 diff_fileindex_tree(struct got_fileindex *fileindex,
630 struct got_fileindex_entry **ie, struct got_tree_object *tree,
631 const char *path, struct got_repository *repo,
632 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
634 const struct got_error *err = NULL;
635 struct got_tree_entry *te = NULL;
636 size_t path_len = strlen(path);
637 const struct got_tree_entries *entries;
638 struct got_fileindex_entry *next;
640 entries = got_object_tree_get_entries(tree);
641 te = SIMPLEQ_FIRST(&entries->head);
642 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
646 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
647 err = got_error_from_errno();
650 cmp = got_path_cmp((*ie)->path, te_path);
653 err = cb->diff_old_new(cb_arg, *ie, te,
657 *ie = walk_fileindex(fileindex, *ie);
658 err = walk_tree(&te, fileindex, ie, te,
659 path, repo, cb, cb_arg);
660 } else if (cmp < 0 ) {
661 next = walk_fileindex(fileindex, *ie);
662 err = cb->diff_old(cb_arg, *ie, path);
667 err = cb->diff_new(cb_arg, te, path);
670 err = walk_tree(&te, fileindex, ie, te,
671 path, repo, cb, cb_arg);
676 next = walk_fileindex(fileindex, *ie);
677 err = cb->diff_old(cb_arg, *ie, path);
682 err = cb->diff_new(cb_arg, te, path);
685 err = walk_tree(&te, fileindex, ie, te, path, repo, cb,
695 const struct got_error *
696 got_fileindex_diff_tree(struct got_fileindex *fileindex,
697 struct got_tree_object *tree, struct got_repository *repo,
698 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
700 struct got_fileindex_entry *min;
701 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
702 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
705 static const struct got_error *
706 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
707 const char *, const char *, struct got_repository *,
708 struct got_fileindex_diff_dir_cb *, void *);
710 static const struct got_error *
711 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
712 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
713 const char *path, DIR *dir, const char *rootpath,
714 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
717 const struct got_error *err = NULL;
718 struct dirent *de = dle->data;
720 if (de->d_type == DT_DIR) {
725 if (asprintf(&subpath, "%s%s%s", path,
726 path[0] == '\0' ? "" : "/", de->d_name) == -1)
727 return got_error_from_errno();
729 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
731 return got_error_from_errno();
734 subdir = opendir(subdirpath);
735 if (subdir == NULL) {
738 return got_error_from_errno();
741 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
742 subpath, repo, cb, cb_arg);
750 *next = TAILQ_NEXT(dle, entry);
754 static const struct got_error *
755 diff_fileindex_dir(struct got_fileindex *fileindex,
756 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
757 const char *path, struct got_repository *repo,
758 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
760 const struct got_error *err = NULL;
761 struct dirent *de = NULL;
762 size_t path_len = strlen(path);
763 struct got_fileindex_entry *next;
764 struct got_pathlist_head dirlist;
765 struct got_pathlist_entry *dle;
767 TAILQ_INIT(&dirlist);
770 struct got_pathlist_entry *new = NULL;
771 struct dirent *dep = NULL;
773 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
775 err = got_error_from_errno();
779 if (readdir_r(dir, de, &dep) != 0) {
780 err = got_error_from_errno();
789 if (strcmp(de->d_name, ".") == 0 ||
790 strcmp(de->d_name, "..") == 0 ||
792 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
797 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
801 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
806 dle = TAILQ_FIRST(&dirlist);
807 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
812 if (asprintf(&de_path, "%s/%s", path,
814 err = got_error_from_errno();
817 cmp = got_path_cmp((*ie)->path, de_path);
820 err = cb->diff_old_new(cb_arg, *ie, de, path);
823 *ie = walk_fileindex(fileindex, *ie);
824 err = walk_dir(&dle, fileindex, ie, dle, path,
825 dir, rootpath, repo, cb, cb_arg);
826 } else if (cmp < 0 ) {
827 next = walk_fileindex(fileindex, *ie);
828 err = cb->diff_old(cb_arg, *ie, path);
833 err = cb->diff_new(cb_arg, de, path);
836 err = walk_dir(&dle, fileindex, ie, dle, path,
837 dir, rootpath, repo, cb, cb_arg);
842 next = walk_fileindex(fileindex, *ie);
843 err = cb->diff_old(cb_arg, *ie, path);
849 err = cb->diff_new(cb_arg, de, path);
852 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
853 rootpath, repo, cb, cb_arg);
859 TAILQ_FOREACH(dle, &dirlist, entry)
861 got_pathlist_free(&dirlist);
865 const struct got_error *
866 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
867 const char *rootpath, const char *path, struct got_repository *repo,
868 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
870 struct got_fileindex_entry *min;
871 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
872 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
876 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);