t / helper / test-dir-iterator.con commit test-dir-iterator: do not assume errno values (9042140)
   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
   7static const char *error_name(int error_number)
   8{
   9        switch (error_number) {
  10        case ENOENT: return "ENOENT";
  11        case ENOTDIR: return "ENOTDIR";
  12        default: return "ESOMETHINGELSE";
  13        }
  14}
  15
  16/*
  17 * usage:
  18 * tool-test dir-iterator [--follow-symlinks] [--pedantic] directory_path
  19 */
  20int cmd__dir_iterator(int argc, const char **argv)
  21{
  22        struct strbuf path = STRBUF_INIT;
  23        struct dir_iterator *diter;
  24        unsigned int flags = 0;
  25        int iter_status;
  26
  27        for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
  28                if (strcmp(*argv, "--follow-symlinks") == 0)
  29                        flags |= DIR_ITERATOR_FOLLOW_SYMLINKS;
  30                else if (strcmp(*argv, "--pedantic") == 0)
  31                        flags |= DIR_ITERATOR_PEDANTIC;
  32                else
  33                        die("invalid option '%s'", *argv);
  34        }
  35
  36        if (!*argv || argc != 1)
  37                die("dir-iterator needs exactly one non-option argument");
  38
  39        strbuf_add(&path, *argv, strlen(*argv));
  40        diter = dir_iterator_begin(path.buf, flags);
  41
  42        if (!diter) {
  43                printf("dir_iterator_begin failure: %s\n", error_name(errno));
  44                exit(EXIT_FAILURE);
  45        }
  46
  47        while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) {
  48                if (S_ISDIR(diter->st.st_mode))
  49                        printf("[d] ");
  50                else if (S_ISREG(diter->st.st_mode))
  51                        printf("[f] ");
  52                else if (S_ISLNK(diter->st.st_mode))
  53                        printf("[s] ");
  54                else
  55                        printf("[?] ");
  56
  57                printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
  58                       diter->path.buf);
  59        }
  60
  61        if (iter_status != ITER_DONE) {
  62                printf("dir_iterator_advance failure\n");
  63                return 1;
  64        }
  65
  66        return 0;
  67}