Merge branch 'jk/sort-iter-test-output'
[gitweb.git] / t / helper / test-dir-iterator.c
index 84f50bed8c9078f4678c9be92a76f5a762ee6185..c7c30664dad2a027c4cf6b0bac7ecb78080cbcf1 100644 (file)
@@ -4,24 +4,53 @@
 #include "iterator.h"
 #include "dir-iterator.h"
 
-/* Argument is a directory path to iterate over */
+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 strbuf path = STRBUF_INIT;
        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 (argc < 2)
-               die("BUG: test-dir-iterator needs one argument");
+       if (!*argv || argc != 1)
+               die("dir-iterator needs exactly one non-option argument");
 
-       strbuf_add(&path, argv[1], strlen(argv[1]));
+       strbuf_add(&path, *argv, strlen(*argv));
+       diter = dir_iterator_begin(path.buf, flags);
 
-       diter = dir_iterator_begin(path.buf);
+       if (!diter) {
+               printf("dir_iterator_begin failure: %s\n", error_name(errno));
+               exit(EXIT_FAILURE);
+       }
 
-       while (dir_iterator_advance(diter) == ITER_OK) {
+       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("[?] ");
 
@@ -29,5 +58,10 @@ int cmd__dir_iterator(int argc, const char **argv)
                       diter->path.buf);
        }
 
+       if (iter_status != ITER_DONE) {
+               printf("dir_iterator_advance failure\n");
+               return 1;
+       }
+
        return 0;
 }