diff --git a/Makefile b/Makefile index 47180f2..9108fc1 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,7 @@ INSTALL ?= install SOURCES := \ src/main.c \ + src/stack.c \ src/utility.c \ src/types.c \ src/hash.c \ diff --git a/include/stack.h b/include/stack.h new file mode 100644 index 0000000..c835e21 --- /dev/null +++ b/include/stack.h @@ -0,0 +1,15 @@ +#ifndef STACK_H +#define STACK_H + +#include + +struct Stack; + +struct Stack *StackNew(void); +bool StackEmpty(const struct Stack *); +void *StackTop(const struct Stack *); +void *StackPop(struct Stack *); +int StackPush(struct Stack *, void *); +void StackRelease(struct Stack *); + +#endif diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..ecaa575 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,76 @@ +#include "stack.h" +#include + +struct Node { + void *data; + struct Node *next; +}; + +struct Stack { + size_t len; + struct Node *head; +}; + +struct Stack *StackNew(void) { + struct Stack *self = calloc(1, sizeof(*self)); + if (self == NULL) + return NULL; + self->head = NULL; + self->len = 0; + return self; +} + +bool StackEmpty(const struct Stack *self) { + if (self == NULL) + return true; + return self->len == 0; +} + +void *StackTop(const struct Stack *self) { + if (self == NULL) + return NULL; + if (self->head != NULL) { + return self->head->data; + } + return NULL; +} + +void *StackPop(struct Stack *self) { + if (self == NULL || self->head == NULL) + return NULL; + struct Node *head = self->head; + struct Node *next = self->head->next; + self->head = next; + void *data = head->data; + free(head); + self->len--; + return data; +} + +int StackPush(struct Stack *self, void *data) { + if (self == NULL) + return -1; + struct Node *n = calloc(1, sizeof(*n)); + if (n == NULL) + return -1; + n->data = data; + n->next = NULL; + if (self->head == NULL) { + self->head = n; + } else { + struct Node *head = self->head; + n->next = head; + self->head = n; + } + self->len++; + return 0; +} + +void StackRelease(struct Stack *self) { + if (self == NULL) + return; + while (!StackEmpty(self)) { + StackPop(self); + } + free(self); +} diff --git a/src/walk.c b/src/walk.c index 9155fd2..54480ed 100644 --- a/src/walk.c +++ b/src/walk.c @@ -1,3 +1,4 @@ +#include "stack.h" #include "utility.h" #include "walk.h" #include @@ -17,94 +18,126 @@ static int ignore_file(const char *name) { return 0; } -static int handle_unknown(const char *path, WalkOp op, void *context) { +/* Report whether path is a real directory. d_type is a fast path that neither + follows symbolic links nor needs another syscall; when the filesystem does + not provide it (DT_UNKNOWN), fall back to lstat, which also never follows + links. Returns 1 for a directory, 0 for anything else, -1 on error. */ +static int is_directory(const char *path, unsigned char type) { +#ifdef DT_UNKNOWN + if (type != DT_UNKNOWN) + return type == DT_DIR; +#endif struct stat st; - - if (lstat(path, &st) == -1) { + if (lstat(path, &st) == -1) return -1; - } - - if (S_ISDIR(st.st_mode)) { - if (op(path, context) == -1) - return -1; - return walk(path, op, context); - } - - if (op(path, context) == -1) - return -1; - return 0; + return S_ISDIR(st.st_mode) != 0; } int walk(const char *directory, WalkOp op, void *context) { - errno = 0; + char *base = NULL; + + struct Stack *stack = StackNew(); + if (stack == NULL) + goto failed; + DIR *dirp = opendir(directory); if (dirp == NULL) { if (errno == EACCES) { fprintf(stderr, "access denied: %s\n", directory); - return 0; + goto success; } - return -1; + goto failed; } - int rc = -1; - char *path = NULL; - struct dirent *direntp = NULL; - + /* The stack owns the char *paths of the subdirectories still to inspect. + At most one DIR is open at a time, so a deep tree cannot exhaust the + file-descriptor limit. base is the path of the directory currently being + read; NULL means the root was passed directly, so its storage is not + owned. */ for (;;) { - /* readdir returns NULL both at end of directory and on error, and is - allowed to set errno even when it succeeds. Reset it adjacent to the - call, so the test after the loop tells the two apart: resetting at the - bottom of the body instead would be skipped by the continue below. */ errno = 0; - direntp = readdir(dirp); - if (direntp == NULL) - break; + struct dirent *direntp; + while ((direntp = readdir(dirp)) != NULL) { + if (ignore_file(direntp->d_name)) + continue; - if (ignore_file(direntp->d_name)) - continue; + char *path = + strconcat(base == NULL ? directory : base, 2, "/", direntp->d_name); + if (path == NULL) + goto failed; - path = strconcat(directory, 2, "/", direntp->d_name); - if (path == NULL) - goto out; + const int type = is_directory(path, direntp->d_type); + if (type == -1) { + free(path); + goto failed; + } -#ifdef DT_UNKNOWN - switch (direntp->d_type) { - case DT_UNKNOWN: - /* operating system or filesystem do not support dirent->d_type. - In this case use alternative algorithm for walk directories. */ - if (handle_unknown(path, op, context) == -1) - goto out; - break; - case DT_DIR: - if (op(path, context) == -1) - goto out; - if (walk(path, op, context) == -1) - goto out; - break; - default: - if (op(path, context) == -1) - goto out; - break; + if (type == 1) { + /* Report the directory itself, then keep it pending for descent. + The stack now owns path; do not free it here. */ + if (op(path, context) == -1 || StackPush(stack, path) == -1) { + free(path); + goto failed; + } + } else { + if (op(path, context) == -1) { + free(path); + goto failed; + } + free(path); + } } -#else - if (handle_unknown(path, op, context) == -1) { - goto out; - } -#endif - free(path); - path = NULL; + if (errno != 0) + goto failed; + + if (closedir(dirp) == -1) + goto failed; + dirp = NULL; + free(base); + base = NULL; + + /* Open the next pending subdirectory, skipping the unreadable ones. + The previous handle is already closed, so only one stays open. */ + while (base == NULL) { + if (StackEmpty(stack)) + goto success; + base = StackPop(stack); + dirp = opendir(base); + if (dirp != NULL) + break; + if (errno != EACCES) { + free(base); + base = NULL; + goto failed; + } + fprintf(stderr, "access denied: %s\n", base); + free(base); + base = NULL; + } } - if (errno == 0) { - rc = 0; +success:; + if (dirp != NULL) + closedir(dirp); + free(base); + while (stack != NULL && !StackEmpty(stack)) { + free(StackPop(stack)); } + if (stack != NULL) + StackRelease(stack); + return 0; -out: - free(path); +failed:; const int saved_errno = errno; - if (closedir(dirp) == -1) - rc = -1; + if (dirp != NULL) + closedir(dirp); + free(base); + while (stack != NULL && !StackEmpty(stack)) { + free(StackPop(stack)); + } + if (stack != NULL) + StackRelease(stack); errno = saved_errno; - return rc; + return -1; }