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>
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_NOT_FLUSHED 0x00010000
41 #define GOT_FILEIDX_F_NO_BLOB 0x00020000
42 #define GOT_FILEIDX_F_NO_COMMIT 0x00040000
43 #define GOT_FILEIDX_F_NO_FILE_ON_DISK 0x00080000
45 struct got_fileindex {
46 struct got_fileindex_tree entries;
48 #define GOT_FILEIDX_MAX_ENTRIES INT_MAX
52 got_fileindex_perms_from_st(struct stat *sb)
54 uint16_t perms = (sb->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO));
55 return (perms << GOT_FILEIDX_MODE_PERMS_SHIFT);
59 got_fileindex_perms_to_st(struct got_fileindex_entry *ie)
61 mode_t perms = (ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT);
62 return (perms & (S_IRWXU | S_IRWXG | S_IRWXO));
65 const struct got_error *
66 got_fileindex_entry_update(struct got_fileindex_entry *ie,
67 const char *ondisk_path, uint8_t *blob_sha1, uint8_t *commit_sha1,
68 int update_timestamps)
72 if (lstat(ondisk_path, &sb) != 0) {
73 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0)
74 return got_error_from_errno2("lstat", ondisk_path);
76 if (sb.st_mode & S_IFDIR)
77 return got_error_set_errno(EISDIR, ondisk_path);
78 ie->flags &= ~GOT_FILEIDX_F_NO_FILE_ON_DISK;
82 if ((ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0) {
83 if (update_timestamps) {
84 ie->ctime_sec = sb.st_ctime;
85 ie->ctime_nsec = sb.st_ctimensec;
86 ie->mtime_sec = sb.st_mtime;
87 ie->mtime_nsec = sb.st_mtimensec;
91 ie->size = (sb.st_size & 0xffffffff);
92 if (sb.st_mode & S_IFLNK)
93 ie->mode = GOT_FILEIDX_MODE_SYMLINK;
95 ie->mode = GOT_FILEIDX_MODE_REGULAR_FILE;
96 ie->mode |= got_fileindex_perms_from_st(&sb);
100 memcpy(ie->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
101 ie->flags &= ~GOT_FILEIDX_F_NO_BLOB;
103 ie->flags |= GOT_FILEIDX_F_NO_BLOB;
106 memcpy(ie->commit_sha1, commit_sha1, SHA1_DIGEST_LENGTH);
107 ie->flags &= ~GOT_FILEIDX_F_NO_COMMIT;
109 ie->flags |= GOT_FILEIDX_F_NO_COMMIT;
115 got_fileindex_entry_mark_deleted_from_disk(struct got_fileindex_entry *ie)
117 ie->flags |= GOT_FILEIDX_F_NO_FILE_ON_DISK;
120 const struct got_error *
121 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
122 const char *ondisk_path, const char *relpath, uint8_t *blob_sha1,
123 uint8_t *commit_sha1)
127 *ie = calloc(1, sizeof(**ie));
129 return got_error_from_errno("calloc");
131 (*ie)->path = strdup(relpath);
132 if ((*ie)->path == NULL) {
133 const struct got_error *err = got_error_from_errno("strdup");
139 len = strlen(relpath);
140 if (len > GOT_FILEIDX_F_PATH_LEN)
141 len = GOT_FILEIDX_F_PATH_LEN;
144 return got_fileindex_entry_update(*ie, ondisk_path, blob_sha1,
149 got_fileindex_entry_free(struct got_fileindex_entry *ie)
156 got_fileindex_entry_path_len(const struct got_fileindex_entry *ie)
158 return (size_t)(ie->flags & GOT_FILEIDX_F_PATH_LEN);
162 got_fileindex_entry_has_blob(struct got_fileindex_entry *ie)
164 return (ie->flags & GOT_FILEIDX_F_NO_BLOB) == 0;
168 got_fileindex_entry_has_commit(struct got_fileindex_entry *ie)
170 return (ie->flags & GOT_FILEIDX_F_NO_COMMIT) == 0;
174 got_fileindex_entry_has_file_on_disk(struct got_fileindex_entry *ie)
176 return (ie->flags & GOT_FILEIDX_F_NO_FILE_ON_DISK) == 0;
179 static const struct got_error *
180 add_entry(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
182 if (fileindex->nentries >= GOT_FILEIDX_MAX_ENTRIES)
183 return got_error(GOT_ERR_NO_SPACE);
185 RB_INSERT(got_fileindex_tree, &fileindex->entries, ie);
186 fileindex->nentries++;
190 const struct got_error *
191 got_fileindex_entry_add(struct got_fileindex *fileindex,
192 struct got_fileindex_entry *ie)
194 /* Flag this entry until it gets written out to disk. */
195 ie->flags |= GOT_FILEIDX_F_NOT_FLUSHED;
197 return add_entry(fileindex, ie);
201 got_fileindex_entry_remove(struct got_fileindex *fileindex,
202 struct got_fileindex_entry *ie)
204 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
205 fileindex->nentries--;
208 struct got_fileindex_entry *
209 got_fileindex_entry_get(struct got_fileindex *fileindex, const char *path,
212 struct got_fileindex_entry key;
213 memset(&key, 0, sizeof(key));
214 key.path = (char *)path;
215 key.flags = (path_len & GOT_FILEIDX_F_PATH_LEN);
216 return RB_FIND(got_fileindex_tree, &fileindex->entries, &key);
219 const struct got_error *
220 got_fileindex_for_each_entry_safe(struct got_fileindex *fileindex,
221 got_fileindex_cb cb, void *cb_arg)
223 const struct got_error *err;
224 struct got_fileindex_entry *ie, *tmp;
226 RB_FOREACH_SAFE(ie, got_fileindex_tree, &fileindex->entries, tmp) {
227 err = (*cb)(cb_arg, ie);
234 struct got_fileindex *
235 got_fileindex_alloc(void)
237 struct got_fileindex *fileindex;
239 fileindex = calloc(1, sizeof(*fileindex));
240 if (fileindex == NULL)
243 RB_INIT(&fileindex->entries);
248 got_fileindex_free(struct got_fileindex *fileindex)
250 struct got_fileindex_entry *ie;
252 while ((ie = RB_MIN(got_fileindex_tree, &fileindex->entries))) {
253 RB_REMOVE(got_fileindex_tree, &fileindex->entries, ie);
254 got_fileindex_entry_free(ie);
259 static const struct got_error *
260 write_fileindex_val64(SHA1_CTX *ctx, uint64_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_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile)
278 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
279 n = fwrite(&val, 1, sizeof(val), outfile);
280 if (n != sizeof(val))
281 return got_ferror(outfile, GOT_ERR_IO);
285 static const struct got_error *
286 write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile)
291 SHA1Update(ctx, (uint8_t *)&val, sizeof(val));
292 n = fwrite(&val, 1, sizeof(val), outfile);
293 if (n != sizeof(val))
294 return got_ferror(outfile, GOT_ERR_IO);
298 static const struct got_error *
299 write_fileindex_path(SHA1_CTX *ctx, const char *path, FILE *outfile)
301 size_t n, len, pad = 0;
302 static const uint8_t zero[8] = { 0 };
305 while ((len + pad) % 8 != 0)
308 pad = 8; /* NUL-terminate */
310 SHA1Update(ctx, path, len);
311 n = fwrite(path, 1, len, outfile);
313 return got_ferror(outfile, GOT_ERR_IO);
314 SHA1Update(ctx, zero, pad);
315 n = fwrite(zero, 1, pad, outfile);
317 return got_ferror(outfile, GOT_ERR_IO);
321 static const struct got_error *
322 write_fileindex_entry(SHA1_CTX *ctx, struct got_fileindex_entry *ie,
325 const struct got_error *err;
328 err = write_fileindex_val64(ctx, ie->ctime_sec, outfile);
331 err = write_fileindex_val64(ctx, ie->ctime_nsec, outfile);
334 err = write_fileindex_val64(ctx, ie->mtime_sec, outfile);
337 err = write_fileindex_val64(ctx, ie->mtime_nsec, outfile);
341 err = write_fileindex_val32(ctx, ie->uid, outfile);
344 err = write_fileindex_val32(ctx, ie->gid, outfile);
347 err = write_fileindex_val32(ctx, ie->size, outfile);
351 err = write_fileindex_val16(ctx, ie->mode, outfile);
355 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
356 n = fwrite(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
357 if (n != SHA1_DIGEST_LENGTH)
358 return got_ferror(outfile, GOT_ERR_IO);
360 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
361 n = fwrite(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, outfile);
362 if (n != SHA1_DIGEST_LENGTH)
363 return got_ferror(outfile, GOT_ERR_IO);
365 err = write_fileindex_val32(ctx, ie->flags, outfile);
369 err = write_fileindex_path(ctx, ie->path, outfile);
373 const struct got_error *
374 got_fileindex_write(struct got_fileindex *fileindex, FILE *outfile)
376 const struct got_error *err = NULL;
377 struct got_fileindex_hdr hdr;
379 uint8_t sha1[SHA1_DIGEST_LENGTH];
381 struct got_fileindex_entry *ie;
385 hdr.signature = htobe32(GOT_FILE_INDEX_SIGNATURE);
386 hdr.version = htobe32(GOT_FILE_INDEX_VERSION);
387 hdr.nentries = htobe32(fileindex->nentries);
389 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
390 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
391 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
392 n = fwrite(&hdr.signature, 1, sizeof(hdr.signature), outfile);
393 if (n != sizeof(hdr.signature))
394 return got_ferror(outfile, GOT_ERR_IO);
395 n = fwrite(&hdr.version, 1, sizeof(hdr.version), outfile);
396 if (n != sizeof(hdr.version))
397 return got_ferror(outfile, GOT_ERR_IO);
398 n = fwrite(&hdr.nentries, 1, sizeof(hdr.nentries), outfile);
399 if (n != sizeof(hdr.nentries))
400 return got_ferror(outfile, GOT_ERR_IO);
402 RB_FOREACH(ie, got_fileindex_tree, &fileindex->entries) {
403 ie->flags &= ~GOT_FILEIDX_F_NOT_FLUSHED;
404 err = write_fileindex_entry(&ctx, ie, outfile);
409 SHA1Final(sha1, &ctx);
410 n = fwrite(sha1, 1, sizeof(sha1), outfile);
411 if (n != sizeof(sha1))
412 return got_ferror(outfile, GOT_ERR_IO);
414 if (fflush(outfile) != 0)
415 return got_error_from_errno("fflush");
420 static const struct got_error *
421 read_fileindex_val64(uint64_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 = be64toh(*val);
433 static const struct got_error *
434 read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile)
438 n = fread(val, 1, sizeof(*val), infile);
439 if (n != sizeof(*val))
440 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
441 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
442 *val = be32toh(*val);
446 static const struct got_error *
447 read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile)
451 n = fread(val, 1, sizeof(*val), infile);
452 if (n != sizeof(*val))
453 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
454 SHA1Update(ctx, (uint8_t *)val, sizeof(*val));
455 *val = be16toh(*val);
459 static const struct got_error *
460 read_fileindex_path(char **path, SHA1_CTX *ctx, FILE *infile)
462 const struct got_error *err = NULL;
463 const size_t chunk_size = 8;
464 size_t n, len = 0, totlen = chunk_size;
466 *path = malloc(totlen);
468 return got_error_from_errno("malloc");
471 if (len + chunk_size > totlen) {
472 char *p = reallocarray(*path, totlen + chunk_size, 1);
474 err = got_error_from_errno("reallocarray");
477 totlen += chunk_size;
480 n = fread(*path + len, 1, chunk_size, infile);
481 if (n != chunk_size) {
482 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
485 SHA1Update(ctx, *path + len, chunk_size);
487 } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL);
496 static const struct got_error *
497 read_fileindex_entry(struct got_fileindex_entry **iep, SHA1_CTX *ctx,
500 const struct got_error *err;
501 struct got_fileindex_entry *ie;
506 ie = calloc(1, sizeof(*ie));
508 return got_error_from_errno("calloc");
510 err = read_fileindex_val64(&ie->ctime_sec, ctx, infile);
513 err = read_fileindex_val64(&ie->ctime_nsec, ctx, infile);
516 err = read_fileindex_val64(&ie->mtime_sec, ctx, infile);
519 err = read_fileindex_val64(&ie->mtime_nsec, ctx, infile);
523 err = read_fileindex_val32(&ie->uid, ctx, infile);
526 err = read_fileindex_val32(&ie->gid, ctx, infile);
529 err = read_fileindex_val32(&ie->size, ctx, infile);
533 err = read_fileindex_val16(&ie->mode, ctx, infile);
537 n = fread(ie->blob_sha1, 1, SHA1_DIGEST_LENGTH, infile);
538 if (n != SHA1_DIGEST_LENGTH) {
539 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
542 SHA1Update(ctx, ie->blob_sha1, SHA1_DIGEST_LENGTH);
544 n = fread(ie->commit_sha1, 1, SHA1_DIGEST_LENGTH, infile);
545 if (n != SHA1_DIGEST_LENGTH) {
546 err = got_ferror(infile, GOT_ERR_FILEIDX_BAD);
549 SHA1Update(ctx, ie->commit_sha1, SHA1_DIGEST_LENGTH);
551 err = read_fileindex_val32(&ie->flags, ctx, infile);
555 err = read_fileindex_path(&ie->path, ctx, infile);
558 got_fileindex_entry_free(ie);
564 const struct got_error *
565 got_fileindex_read(struct got_fileindex *fileindex, FILE *infile)
567 const struct got_error *err = NULL;
568 struct got_fileindex_hdr hdr;
570 struct got_fileindex_entry *ie;
571 uint8_t sha1_expected[SHA1_DIGEST_LENGTH];
572 uint8_t sha1[SHA1_DIGEST_LENGTH];
578 n = fread(&hdr.signature, 1, sizeof(hdr.signature), infile);
579 if (n != sizeof(hdr.signature)) {
580 if (n == 0) /* EOF */
582 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
584 n = fread(&hdr.version, 1, sizeof(hdr.version), infile);
585 if (n != sizeof(hdr.version)) {
586 if (n == 0) /* EOF */
588 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
590 n = fread(&hdr.nentries, 1, sizeof(hdr.nentries), infile);
591 if (n != sizeof(hdr.nentries)) {
592 if (n == 0) /* EOF */
594 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
597 SHA1Update(&ctx, (uint8_t *)&hdr.signature, sizeof(hdr.signature));
598 SHA1Update(&ctx, (uint8_t *)&hdr.version, sizeof(hdr.version));
599 SHA1Update(&ctx, (uint8_t *)&hdr.nentries, sizeof(hdr.nentries));
601 hdr.signature = be32toh(hdr.signature);
602 hdr.version = be32toh(hdr.version);
603 hdr.nentries = be32toh(hdr.nentries);
605 if (hdr.signature != GOT_FILE_INDEX_SIGNATURE)
606 return got_error(GOT_ERR_FILEIDX_SIG);
607 if (hdr.version != GOT_FILE_INDEX_VERSION)
608 return got_error(GOT_ERR_FILEIDX_VER);
610 for (i = 0; i < hdr.nentries; i++) {
611 err = read_fileindex_entry(&ie, &ctx, infile);
614 err = add_entry(fileindex, ie);
619 n = fread(sha1_expected, 1, sizeof(sha1_expected), infile);
620 if (n != sizeof(sha1_expected))
621 return got_ferror(infile, GOT_ERR_FILEIDX_BAD);
622 SHA1Final(sha1, &ctx);
623 if (memcmp(sha1, sha1_expected, SHA1_DIGEST_LENGTH) != 0)
624 return got_error(GOT_ERR_FILEIDX_CSUM);
629 static struct got_fileindex_entry *
630 walk_fileindex(struct got_fileindex *fileindex, struct got_fileindex_entry *ie)
632 struct got_fileindex_entry *next;
634 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, ie);
636 /* Skip entries which were newly added by diff callbacks. */
637 while (next && (next->flags & GOT_FILEIDX_F_NOT_FLUSHED))
638 next = RB_NEXT(got_fileindex_tree, &fileindex->entries, next);
643 static const struct got_error *
644 diff_fileindex_tree(struct got_fileindex *, struct got_fileindex_entry **,
645 const struct got_tree_entries *, const char *, const char *,
646 struct got_repository *, struct got_fileindex_diff_tree_cb *, void *);
648 static const struct got_error *
649 walk_tree(struct got_tree_entry **next, struct got_fileindex *fileindex,
650 struct got_fileindex_entry **ie, struct got_tree_entry *te,
651 const char *path, const char *entry_name, struct got_repository *repo,
652 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
654 const struct got_error *err = NULL;
656 if (S_ISDIR(te->mode)) {
658 struct got_tree_object *subtree;
660 if (asprintf(&subpath, "%s%s%s", path,
661 path[0] == '\0' ? "" : "/", te->name) == -1)
662 return got_error_from_errno("asprintf");
664 err = got_object_open_as_tree(&subtree, repo, te->id);
670 err = diff_fileindex_tree(fileindex, ie,
671 got_object_tree_get_entries(subtree), subpath, entry_name,
674 got_object_tree_close(subtree);
679 *next = SIMPLEQ_NEXT(te, entry);
683 static const struct got_error *
684 diff_fileindex_tree(struct got_fileindex *fileindex,
685 struct got_fileindex_entry **ie, const struct got_tree_entries *entries,
686 const char *path, const char *entry_name, struct got_repository *repo,
687 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
689 const struct got_error *err = NULL;
690 struct got_tree_entry *te = NULL;
691 size_t path_len = strlen(path);
692 struct got_fileindex_entry *next;
694 te = SIMPLEQ_FIRST(&entries->head);
695 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || te) {
699 if (asprintf(&te_path, "%s/%s", path, te->name) == -1) {
700 err = got_error_from_errno("asprintf");
703 cmp = got_path_cmp((*ie)->path, te_path,
704 got_fileindex_entry_path_len(*ie), strlen(te_path));
707 if (got_path_is_child((*ie)->path, path,
708 path_len) && (entry_name == NULL ||
709 strcmp(te->name, entry_name) == 0)) {
710 err = cb->diff_old_new(cb_arg, *ie, te,
712 if (err || entry_name)
715 *ie = walk_fileindex(fileindex, *ie);
716 err = walk_tree(&te, fileindex, ie, te,
717 path, entry_name, repo, cb, cb_arg);
718 } else if (cmp < 0) {
719 next = walk_fileindex(fileindex, *ie);
720 if (got_path_is_child((*ie)->path, path,
721 path_len) && (entry_name == NULL ||
722 strcmp(te->name, entry_name) == 0)) {
723 err = cb->diff_old(cb_arg, *ie, path);
724 if (err || entry_name)
729 if ((entry_name == NULL ||
730 strcmp(te->name, entry_name) == 0)) {
731 err = cb->diff_new(cb_arg, te, path);
732 if (err || entry_name)
735 err = walk_tree(&te, fileindex, ie, te,
736 path, entry_name, repo, cb, cb_arg);
741 next = walk_fileindex(fileindex, *ie);
742 if (got_path_is_child((*ie)->path, path, path_len) &&
743 (entry_name == NULL ||
744 strcmp(te->name, entry_name) == 0)) {
745 err = cb->diff_old(cb_arg, *ie, path);
746 if (err || entry_name)
751 if (entry_name == NULL ||
752 strcmp(te->name, entry_name) == 0) {
753 err = cb->diff_new(cb_arg, te, path);
754 if (err || entry_name)
757 err = walk_tree(&te, fileindex, ie, te, path,
758 entry_name, repo, cb, cb_arg);
767 const struct got_error *
768 got_fileindex_diff_tree(struct got_fileindex *fileindex,
769 struct got_tree_object *tree, const char *path, const char *entry_name,
770 struct got_repository *repo,
771 struct got_fileindex_diff_tree_cb *cb, void *cb_arg)
773 struct got_fileindex_entry *ie;
774 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
775 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
776 ie = walk_fileindex(fileindex, ie);
777 return diff_fileindex_tree(fileindex, &ie,
778 got_object_tree_get_entries(tree), path, entry_name, repo,
782 static const struct got_error *
783 diff_fileindex_dir(struct got_fileindex *, struct got_fileindex_entry **,
784 struct got_pathlist_head *, const char *, const char *,
785 struct got_repository *, struct got_fileindex_diff_dir_cb *, void *);
787 static const struct got_error *
788 read_dirlist(struct got_pathlist_head *dirlist, DIR *dir, const char *path)
790 const struct got_error *err = NULL;
791 struct got_pathlist_entry *new = NULL;
792 struct dirent *dep = NULL;
793 struct dirent *de = NULL;
796 de = malloc(sizeof(struct dirent) + NAME_MAX + 1);
798 err = got_error_from_errno("malloc");
802 if (readdir_r(dir, de, &dep) != 0) {
803 err = got_error_from_errno("readdir_r");
812 if (strcmp(de->d_name, ".") == 0 ||
813 strcmp(de->d_name, "..") == 0 ||
815 strcmp(de->d_name, GOT_WORKTREE_GOT_DIR) == 0)) {
820 err = got_pathlist_insert(&new, dirlist, de->d_name, de);
826 err = got_error(GOT_ERR_DIR_DUP_ENTRY);
836 free_dirlist(struct got_pathlist_head *dirlist)
838 struct got_pathlist_entry *dle;
840 TAILQ_FOREACH(dle, dirlist, entry)
842 got_pathlist_free(dirlist);
845 static const struct got_error *
846 walk_dir(struct got_pathlist_entry **next, struct got_fileindex *fileindex,
847 struct got_fileindex_entry **ie, struct got_pathlist_entry *dle,
848 const char *path, const char *rootpath, struct got_repository *repo,
849 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
851 const struct got_error *err = NULL;
852 struct dirent *de = dle->data;
856 if (de->d_type == DT_DIR) {
860 struct got_pathlist_head subdirlist;
862 TAILQ_INIT(&subdirlist);
864 if (asprintf(&subpath, "%s%s%s", path,
865 path[0] == '\0' ? "" : "/", de->d_name) == -1)
866 return got_error_from_errno("asprintf");
868 if (asprintf(&subdirpath, "%s/%s", rootpath, subpath) == -1) {
870 return got_error_from_errno("asprintf");
873 subdir = opendir(subdirpath);
874 if (subdir == NULL) {
877 return got_error_from_errno2("opendir", subdirpath);
880 err = read_dirlist(&subdirlist, subdir, subdirpath);
887 err = diff_fileindex_dir(fileindex, ie, &subdirlist, rootpath,
888 subpath, repo, cb, cb_arg);
892 free_dirlist(&subdirlist);
897 *next = TAILQ_NEXT(dle, entry);
901 static const struct got_error *
902 diff_fileindex_dir(struct got_fileindex *fileindex,
903 struct got_fileindex_entry **ie, struct got_pathlist_head *dirlist,
904 const char *rootpath, const char *path, struct got_repository *repo,
905 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
907 const struct got_error *err = NULL;
908 struct dirent *de = NULL;
909 size_t path_len = strlen(path);
910 struct got_pathlist_entry *dle;
912 dle = TAILQ_FIRST(dirlist);
913 while ((*ie && got_path_is_child((*ie)->path, path, path_len)) || dle) {
918 if (asprintf(&de_path, "%s/%s", path,
920 err = got_error_from_errno("asprintf");
923 cmp = got_path_cmp((*ie)->path, de_path,
924 got_fileindex_entry_path_len(*ie),
925 strlen(path) + 1 + de->d_namlen);
928 err = cb->diff_old_new(cb_arg, *ie, de, path);
931 *ie = walk_fileindex(fileindex, *ie);
932 err = walk_dir(&dle, fileindex, ie, dle, path,
933 rootpath, repo, cb, cb_arg);
934 } else if (cmp < 0 ) {
935 err = cb->diff_old(cb_arg, *ie, path);
938 *ie = walk_fileindex(fileindex, *ie);
940 err = cb->diff_new(cb_arg, de, path);
943 err = walk_dir(&dle, fileindex, ie, dle, path,
944 rootpath, repo, cb, cb_arg);
949 err = cb->diff_old(cb_arg, *ie, path);
952 *ie = walk_fileindex(fileindex, *ie);
955 err = cb->diff_new(cb_arg, de, path);
958 err = walk_dir(&dle, fileindex, ie, dle, path,
959 rootpath, repo, cb, cb_arg);
968 const struct got_error *
969 got_fileindex_diff_dir(struct got_fileindex *fileindex, DIR *rootdir,
970 const char *rootpath, const char *path, struct got_repository *repo,
971 struct got_fileindex_diff_dir_cb *cb, void *cb_arg)
973 const struct got_error *err;
974 struct got_fileindex_entry *ie;
975 struct got_pathlist_head dirlist;
977 TAILQ_INIT(&dirlist);
978 err = read_dirlist(&dirlist, rootdir, path);
981 ie = RB_MIN(got_fileindex_tree, &fileindex->entries);
982 while (ie && !got_path_is_child(ie->path, path, strlen(path)))
983 ie = walk_fileindex(fileindex, ie);
984 err = diff_fileindex_dir(fileindex, &ie, &dirlist, rootpath, path,
986 free_dirlist(&dirlist);
990 RB_GENERATE(got_fileindex_tree, got_fileindex_entry, entry, got_fileindex_cmp);