diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 14:47:53 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 14:47:53 +0000 |
commit | c8bae7493d2f2910b57f13ded012e86bdcfb0532 (patch) | |
tree | 24e09d9f84dec336720cf393e156089ca2835791 /t/helper/test-dir-iterator.c | |
parent | Initial commit. (diff) | |
download | git-c8bae7493d2f2910b57f13ded012e86bdcfb0532.tar.xz git-c8bae7493d2f2910b57f13ded012e86bdcfb0532.zip |
Adding upstream version 1:2.39.2.upstream/1%2.39.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 't/helper/test-dir-iterator.c')
-rw-r--r-- | t/helper/test-dir-iterator.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/t/helper/test-dir-iterator.c b/t/helper/test-dir-iterator.c new file mode 100644 index 0000000..659b6bf --- /dev/null +++ b/t/helper/test-dir-iterator.c @@ -0,0 +1,65 @@ +#include "test-tool.h" +#include "git-compat-util.h" +#include "strbuf.h" +#include "iterator.h" +#include "dir-iterator.h" + +static const char *error_name(int error_number) +{ + switch (error_number) { + case ENOENT: return "ENOENT"; + case ENOTDIR: return "ENOTDIR"; + default: return "ESOMETHINGELSE"; + } +} + +/* + * usage: + * tool-test dir-iterator [--follow-symlinks] [--pedantic] directory_path + */ +int cmd__dir_iterator(int argc, const char **argv) +{ + struct dir_iterator *diter; + unsigned int flags = 0; + int iter_status; + + for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) { + if (strcmp(*argv, "--follow-symlinks") == 0) + flags |= DIR_ITERATOR_FOLLOW_SYMLINKS; + else if (strcmp(*argv, "--pedantic") == 0) + flags |= DIR_ITERATOR_PEDANTIC; + else + die("invalid option '%s'", *argv); + } + + if (!*argv || argc != 1) + die("dir-iterator needs exactly one non-option argument"); + + diter = dir_iterator_begin(*argv, flags); + + if (!diter) { + printf("dir_iterator_begin failure: %s\n", error_name(errno)); + exit(EXIT_FAILURE); + } + + while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) { + if (S_ISDIR(diter->st.st_mode)) + printf("[d] "); + else if (S_ISREG(diter->st.st_mode)) + printf("[f] "); + else if (S_ISLNK(diter->st.st_mode)) + printf("[s] "); + else + printf("[?] "); + + printf("(%s) [%s] %s\n", diter->relative_path, diter->basename, + diter->path.buf); + } + + if (iter_status != ITER_DONE) { + printf("dir_iterator_advance failure\n"); + return 1; + } + + return 0; +} |