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)
129 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
131 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
135 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
137 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
140 static const struct got_error *
141 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *entry)
143 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
144 return got_error(GOT_ERR_NO_SPACE);
146 RB_INSERT(got_fileindex_tree, &fileindex->entries, entry);
147 fileindex->nentries++;
151 const struct got_error *
152 got_fileindex_entry_add(struct got_fileindex *fileindex,
153 struct got_fileindex_entry *entry)
155 /* Flag this entry until it gets written out to disk. */
156 entry->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
158 return add_entry(fileindex, entry);
162 got_fileindex_entry_remove(struct got_fileindex *fileindex,
163 struct got_fileindex_entry *entry)
165 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
166 fileindex->nentries--;
169 struct got_fileindex_entry *
170 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path)
172 struct got_fileindex_entry key;
173 memset(&key, 0, sizeof(key));
174 key.path = (char *)path;
175 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
178 const struct got_error *
179 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
180 got_fileindex_cb cb, void *cb_arg)
182 const struct got_error *err;
183 struct got_fileindex_entry *entry, *tmp;
185 RB_FOREACH_SAFE(entry, got_fileindex_tree, &fileindex->entries, tmp) {
186 err = (*cb)(cb_arg, entry);
193 struct got_fileindex *
194 got_fileindex_alloc(void)
196 struct got_fileindex *fileindex;
198 fileindex = calloc(1, sizeof(*fileindex));
199 if (fileindex == NULL)
202 RB_INIT(&fileindex->entries);
207 got_fileindex_free(struct got_fileindex *fileindex)
209 struct got_fileindex_entry *entry;
211 while ((entry = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
212 RB_REMOVE(got_fileindex_tree, &fileindex->entries, entry);
213 got_fileindex_entry_free(entry);
218 static const struct got_error *
219 write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile)
224 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
225 n = fwrite(&val, 1, sizeof(val), outfile);
226 if (n != sizeof(val))
227 return got_ferror(outfile, GOT_ERR_IO);
231 static const struct got_error *
232 write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
237 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
238 n = fwrite(&val, 1, sizeof(val), outfile);
239 if (n != sizeof(val))
240 return got_ferror(outfile, GOT_ERR_IO);
244 static const struct got_error *
245 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
250 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
251 n = fwrite(&val, 1, sizeof(val), outfile);
252 if (n != sizeof(val))
253 return got_ferror(outfile, GOT_ERR_IO);
257 static const struct got_error *
258 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
260 size_t n, len, pad = 0;
261 static const uint8_t zero[8] = { 0 };
264 while ((len + pad) % 8 != 0)
267 pad = 8; /* NUL-terminate */
269 SHA1Update(ctx, path, len);
270 n = fwrite(path, 1, len, outfile);
272 return got_ferror(outfile, GOT_ERR_IO);
273 SHA1Update(ctx, zero, pad);
274 n = fwrite(zero, 1, pad, outfile);
276 return got_ferror(outfile, GOT_ERR_IO);
280 static const struct got_error *
281 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *entry,
284 const struct got_error *err;
287 err = write_fileindex_val64(ctx, entry->ctime_sec, outfile);
290 err = write_fileindex_val64(ctx, entry->ctime_nsec, outfile);
293 err = write_fileindex_val64(ctx, entry->mtime_sec, outfile);
296 err = write_fileindex_val64(ctx, entry->mtime_nsec, outfile);
300 err = write_fileindex_val32(ctx, entry->uid, outfile);
303 err = write_fileindex_val32(ctx, entry->gid, outfile);
306 err = write_fileindex_val32(ctx, entry->size, outfile);
310 err = write_fileindex_val16(ctx, entry->mode, outfile);
314 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
315 n = fwrite(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
316 if (n != SHA1_DIGEST_LENGTH)
317 return got_ferror(outfile, GOT_ERR_IO);
319 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
320 n = fwrite(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
321 if (n != SHA1_DIGEST_LENGTH)
322 return got_ferror(outfile, GOT_ERR_IO);
324 err = write_fileindex_val32(ctx, entry->flags, outfile);
328 err = write_fileindex_path(ctx, entry->path, outfile);
332 const struct got_error *
333 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
335 const struct got_error *err = NULL;
336 struct got_fileindex_hdr hdr;
338 uint8_t sha1[SHA1_DIGEST_LENGTH];
340 struct got_fileindex_entry *entry;
344 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
345 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
346 hdr.nentries = htobe32(fileindex->nentries);
348 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
349 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
350 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
351 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
352 if (n != sizeof(hdr.signature))
353 return got_ferror(outfile, GOT_ERR_IO);
354 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
355 if (n != sizeof(hdr.version))
356 return got_ferror(outfile, GOT_ERR_IO);
357 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
358 if (n != sizeof(hdr.nentries))
359 return got_ferror(outfile, GOT_ERR_IO);
361 RB_FOREACH(entry, got_fileindex_tree, &fileindex->entries) {
362 entry->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
363 err = write_fileindex_entry(&ctx, entry, outfile);
368 SHA1Final(sha1, &ctx);
369 n = fwrite(sha1, 1, sizeof(sha1), outfile);
370 if (n != sizeof(sha1))
371 return got_ferror(outfile, GOT_ERR_IO);
373 if (fflush(outfile) != 0)
374 return got_error_from_errno();
379 static const struct got_error *
380 read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile)
384 n = fread(val, 1, sizeof(*val), infile);
385 if (n != sizeof(*val))
386 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
387 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
388 *val = be64toh(*val);
392 static const struct got_error *
393 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
397 n = fread(val, 1, sizeof(*val), infile);
398 if (n != sizeof(*val))
399 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
400 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
401 *val = be32toh(*val);
405 static const struct got_error *
406 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
410 n = fread(val, 1, sizeof(*val), infile);
411 if (n != sizeof(*val))
412 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
413 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
414 *val = be16toh(*val);
418 static const struct got_error *
419 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
421 const struct got_error *err = NULL;
423 size_t n, len = 0, totlen = sizeof(buf);
425 *path = malloc(totlen);
427 return got_error_from_errno();
430 n = fread(buf, 1, sizeof(buf), infile);
431 if (n != sizeof(buf))
432 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
433 if (len + sizeof(buf) > totlen) {
434 char *p = reallocarray(*path, totlen + sizeof(buf), 1);
436 err = got_error_from_errno();
439 totlen += sizeof(buf);
442 SHA1Update(ctx, buf, sizeof(buf));
443 memcpy(*path + len, buf, sizeof(buf));
445 } while (memchr(buf, '\0', sizeof(buf)) == NULL);
454 static const struct got_error *
455 read_fileindex_entry(struct got_fileindex_entry **entryp, SHA1_CTX *ctx,
458 const struct got_error *err;
459 struct got_fileindex_entry *entry;
464 entry = calloc(1, sizeof(*entry));
466 return got_error_from_errno();
468 err = read_fileindex_val64(&entry->ctime_sec, ctx, infile);
471 err = read_fileindex_val64(&entry->ctime_nsec, ctx, infile);
474 err = read_fileindex_val64(&entry->mtime_sec, ctx, infile);
477 err = read_fileindex_val64(&entry->mtime_nsec, ctx, infile);
481 err = read_fileindex_val32(&entry->uid, ctx, infile);
484 err = read_fileindex_val32(&entry->gid, ctx, infile);
487 err = read_fileindex_val32(&entry->size, ctx, infile);
491 err = read_fileindex_val16(&entry->mode, ctx, infile);
495 n = fread(entry->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
496 if (n != SHA1_DIGEST_LENGTH) {
497 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
500 SHA1Update(ctx, entry->blob_sha1, SHA1_DIGEST_LENGTH);
502 n = fread(entry->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
503 if (n != SHA1_DIGEST_LENGTH) {
504 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
507 SHA1Update(ctx, entry->commit_sha1, SHA1_DIGEST_LENGTH);
509 err = read_fileindex_val32(&entry->flags, ctx, infile);
513 err = read_fileindex_path(&entry->path, ctx, infile);
522 const struct got_error *
523 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
525 const struct got_error *err = NULL;
526 struct got_fileindex_hdr hdr;
528 struct got_fileindex_entry *entry;
529 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
530 uint8_t sha1[SHA1_DIGEST_LENGTH];
536 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
537 if (n != sizeof(hdr.signature)) {
538 if (n == 0) /* EOF */
540 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
542 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
543 if (n != sizeof(hdr.version)) {
544 if (n == 0) /* EOF */
546 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
548 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
549 if (n != sizeof(hdr.nentries)) {
550 if (n == 0) /* EOF */
552 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
555 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
556 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
557 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
559 hdr.signature = be32toh(hdr.signature);
560 hdr.version = be32toh(hdr.version);
561 hdr.nentries = be32toh(hdr.nentries);
563 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
564 return got_error(GOT_ERR_FILEIDX_SIG);
565 if (hdr.version != GOT_FILE_INDEX_VERSION)
566 return got_error(GOT_ERR_FILEIDX_VER);
568 for (i = 0; i < hdr.nentries; i++) {
569 err = read_fileindex_entry(&entry, &ctx, infile);
572 err = add_entry(fileindex, entry);
577 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
578 if (n != sizeof(sha1_expected))
579 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
580 SHA1Final(sha1, &ctx);
581 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
582 return got_error(GOT_ERR_FILEIDX_CSUM);
587 static struct got_fileindex_entry *
588 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
590 struct got_fileindex_entry *next;
592 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
594 /* Skip entries which were newly added by diff callbacks. */
595 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
596 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
601 static const struct got_error *
602 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
603 struct got_tree_object *, const char *, struct got_repository *,
604 struct got_fileindex_diff_tree_cb *, void *);
606 static const struct got_error *
607 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
608 struct got_fileindex_entry **ie, struct got_tree_entry *te,
609 const char *path, struct got_repository *repo,
610 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
612 const struct got_error *err = NULL;
614 if (S_ISDIR(te->mode)) {
616 struct got_tree_object *subtree;
618 if (asprintf(&subpath, "%s%s%s", path,
619 path[0] == '\0' ? "" : "/", te->name) == -1)
620 return got_error_from_errno();
622 err = got_object_open_as_tree(&subtree, repo, te->id);
628 err = diff_fileindex_tree(fileindex, ie, subtree,
629 subpath, repo, cb, cb_arg);
631 got_object_tree_close(subtree);
636 *next = SIMPLEQ_NEXT(te, entry);
640 static const struct got_error *
641 diff_fileindex_tree(struct got_fileindex *fileindex,
642 struct got_fileindex_entry **ie, struct got_tree_object *tree,
643 const char *path, struct got_repository *repo,
644 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
646 const struct got_error *err = NULL;
647 struct got_tree_entry *te = NULL;
648 size_t path_len = strlen(path);
649 const struct got_tree_entries *entries;
650 struct got_fileindex_entry *next;
652 entries = got_object_tree_get_entries(tree);
653 te = SIMPLEQ_FIRST(&entries->head);
654 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
658 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
659 err = got_error_from_errno();
662 cmp = got_path_cmp((*ie)->path, te_path);
665 err = cb->diff_old_new(cb_arg, *ie, te,
669 *ie = walk_fileindex(fileindex, *ie);
670 err = walk_tree(&te, fileindex, ie, te,
671 path, repo, cb, cb_arg);
672 } else if (cmp < 0 ) {
673 next = walk_fileindex(fileindex, *ie);
674 err = cb->diff_old(cb_arg, *ie, path);
679 err = cb->diff_new(cb_arg, te, path);
682 err = walk_tree(&te, fileindex, ie, te,
683 path, repo, cb, cb_arg);
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, path, repo, cb,
707 const struct got_error *
708 got_fileindex_diff_tree(struct got_fileindex *fileindex,
709 struct got_tree_object *tree, struct got_repository *repo,
710 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
712 struct got_fileindex_entry *min;
713 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
714 return diff_fileindex_tree(fileindex, &min, tree, "", repo, cb, cb_arg);
717 static const struct got_error *
718 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **, DIR *,
719 const char *, const char *, struct got_repository *,
720 struct got_fileindex_diff_dir_cb *, void *);
722 static const struct got_error *
723 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
724 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
725 const char *path, DIR *dir, const char *rootpath,
726 struct got_repository *repo, struct got_fileindex_diff_dir_cb *cb,
729 const struct got_error *err = NULL;
730 struct dirent *de = dle->data;
732 if (de->d_type == DT_DIR) {
737 if (asprintf(&subpath, "%s%s%s", path,
738 path[0] == '\0' ? "" : "/", de->d_name) == -1)
739 return got_error_from_errno();
741 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
743 return got_error_from_errno();
746 subdir = opendir(subdirpath);
747 if (subdir == NULL) {
750 return got_error_from_errno();
753 err = diff_fileindex_dir(fileindex, ie, subdir, rootpath,
754 subpath, repo, cb, cb_arg);
762 *next = TAILQ_NEXT(dle, entry);
766 static const struct got_error *
767 diff_fileindex_dir(struct got_fileindex *fileindex,
768 struct got_fileindex_entry **ie, DIR *dir, const char *rootpath,
769 const char *path, struct got_repository *repo,
770 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
772 const struct got_error *err = NULL;
773 struct dirent *de = NULL;
774 size_t path_len = strlen(path);
775 struct got_fileindex_entry *next;
776 struct got_pathlist_head dirlist;
777 struct got_pathlist_entry *dle;
779 TAILQ_INIT(&dirlist);
782 struct got_pathlist_entry *new = NULL;
783 struct dirent *dep = NULL;
785 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
787 err = got_error_from_errno();
791 if (readdir_r(dir, de, &dep) != 0) {
792 err = got_error_from_errno();
801 if (strcmp(de->d_name, ".") == 0 ||
802 strcmp(de->d_name, "..") == 0 ||
804 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
809 err = got_pathlist_insert(&new, &dirlist, de->d_name, de);
813 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
818 dle = TAILQ_FIRST(&dirlist);
819 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
824 if (asprintf(&de_path, "%s/%s", path,
826 err = got_error_from_errno();
829 cmp = got_path_cmp((*ie)->path, de_path);
832 err = cb->diff_old_new(cb_arg, *ie, de, path);
835 *ie = walk_fileindex(fileindex, *ie);
836 err = walk_dir(&dle, fileindex, ie, dle, path,
837 dir, rootpath, repo, cb, cb_arg);
838 } else if (cmp < 0 ) {
839 next = walk_fileindex(fileindex, *ie);
840 err = cb->diff_old(cb_arg, *ie, path);
845 err = cb->diff_new(cb_arg, de, path);
848 err = walk_dir(&dle, fileindex, ie, dle, path,
849 dir, rootpath, repo, cb, cb_arg);
854 next = walk_fileindex(fileindex, *ie);
855 err = cb->diff_old(cb_arg, *ie, path);
861 err = cb->diff_new(cb_arg, de, path);
864 err = walk_dir(&dle, fileindex, ie, dle, path, dir,
865 rootpath, repo, cb, cb_arg);
871 TAILQ_FOREACH(dle, &dirlist, entry)
873 got_pathlist_free(&dirlist);
877 const struct got_error *
878 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
879 const char *rootpath, const char *path, struct got_repository *repo,
880 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
882 struct got_fileindex_entry *min;
883 min = RB_MIN(got_fileindex_tree, &fileindex->entries);
884 return diff_fileindex_dir(fileindex, &min, rootdir, rootpath, path,
888 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);