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; }