t / helper / test-dir-iterator.con commit Sync with maint (3034dab)
   1#include "test-tool.h"
   2#include "git-compat-util.h"
   3#include "strbuf.h"
   4#include "iterator.h"
   5#include "dir-iterator.h"
   6
   7/*
   8 * usage:
   9 * tool-test dir-iterator [--follow-symlinks] [--pedantic] directory_path
  10 */
  11int cmd__dir_iterator(int argc, const char **argv)
  12{
  13        struct strbuf path = STRBUF_INIT;
  14        struct dir_iterator *diter;
  15        unsigned int flags = 0;
  16        int iter_status;
  17
  18        for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
  19                if (strcmp(*argv, "--follow-symlinks") == 0)
  20                        flags |= DIR_ITERATOR_FOLLOW_SYMLINKS;
  21                else if (strcmp(*argv, "--pedantic") == 0)
  22                        flags |= DIR_ITERATOR_PEDANTIC;
  23                else
  24                        die("invalid option '%s'", *argv);
  25        }
  26
  27        if (!*argv || argc != 1)
  28                die("dir-iterator needs exactly one non-option argument");
  29
  30        strbuf_add(&path, *argv, strlen(*argv));
  31        diter = dir_iterator_begin(path.buf, flags);
  32
  33        if (!diter) {
  34                printf("dir_iterator_begin failure: %d\n", errno);
  35                exit(EXIT_FAILURE);
  36        }
  37
  38        while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) {
  39                if (S_ISDIR(diter->st.st_mode))
  40                        printf("[d] ");
  41                else if (S_ISREG(diter->st.st_mode))
  42                        printf("[f] ");
  43                else if (S_ISLNK(diter->st.st_mode))
  44                        printf("[s] ");
  45                else
  46                        printf("[?] ");
  47
  48                printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
  49                       diter->path.buf);
  50        }
  51
  52        if (iter_status != ITER_DONE) {
  53                printf("dir_iterator_advance failure\n");
  54                return 1;
  55        }
  56
  57        return 0;
  58}