2019-07-10 23:58:57 +00:00
|
|
|
#include "test-tool.h"
|
|
|
|
#include "git-compat-util.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "iterator.h"
|
|
|
|
#include "dir-iterator.h"
|
|
|
|
|
2019-07-30 17:45:48 +00:00
|
|
|
static const char *error_name(int error_number)
|
|
|
|
{
|
|
|
|
switch (error_number) {
|
|
|
|
case ENOENT: return "ENOENT";
|
|
|
|
case ENOTDIR: return "ENOTDIR";
|
|
|
|
default: return "ESOMETHINGELSE";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 23:59:00 +00:00
|
|
|
/*
|
|
|
|
* usage:
|
2023-02-16 18:27:14 +00:00
|
|
|
* tool-test dir-iterator [--pedantic] directory_path
|
2019-07-10 23:59:00 +00:00
|
|
|
*/
|
2019-07-10 23:58:57 +00:00
|
|
|
int cmd__dir_iterator(int argc, const char **argv)
|
|
|
|
{
|
|
|
|
struct dir_iterator *diter;
|
2019-07-10 23:59:00 +00:00
|
|
|
unsigned int flags = 0;
|
|
|
|
int iter_status;
|
|
|
|
|
|
|
|
for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
|
2023-02-16 18:27:14 +00:00
|
|
|
if (strcmp(*argv, "--pedantic") == 0)
|
2019-07-10 23:59:00 +00:00
|
|
|
flags |= DIR_ITERATOR_PEDANTIC;
|
|
|
|
else
|
|
|
|
die("invalid option '%s'", *argv);
|
|
|
|
}
|
2019-07-10 23:58:57 +00:00
|
|
|
|
2019-07-10 23:59:00 +00:00
|
|
|
if (!*argv || argc != 1)
|
|
|
|
die("dir-iterator needs exactly one non-option argument");
|
2019-07-10 23:58:57 +00:00
|
|
|
|
2019-08-07 11:15:20 +00:00
|
|
|
diter = dir_iterator_begin(*argv, flags);
|
2019-07-10 23:58:57 +00:00
|
|
|
|
2019-07-10 23:58:59 +00:00
|
|
|
if (!diter) {
|
2019-07-30 17:45:48 +00:00
|
|
|
printf("dir_iterator_begin failure: %s\n", error_name(errno));
|
2019-07-10 23:58:59 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2019-07-10 23:59:00 +00:00
|
|
|
while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) {
|
2019-07-10 23:58:57 +00:00
|
|
|
if (S_ISDIR(diter->st.st_mode))
|
|
|
|
printf("[d] ");
|
|
|
|
else if (S_ISREG(diter->st.st_mode))
|
|
|
|
printf("[f] ");
|
2019-07-10 23:59:00 +00:00
|
|
|
else if (S_ISLNK(diter->st.st_mode))
|
|
|
|
printf("[s] ");
|
2019-07-10 23:58:57 +00:00
|
|
|
else
|
|
|
|
printf("[?] ");
|
|
|
|
|
|
|
|
printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
|
|
|
|
diter->path.buf);
|
|
|
|
}
|
|
|
|
|
2019-07-10 23:59:00 +00:00
|
|
|
if (iter_status != ITER_DONE) {
|
|
|
|
printf("dir_iterator_advance failure\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-07-10 23:58:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|