Walk iteratively with an explicit stack

This commit is contained in:
Fabio Scotto di Santolo
2026-09-13 23:33:29 +02:00
parent f2817f3ca5
commit 3c6785532a

View File

@@ -1,3 +1,4 @@
#include "stack.h"
#include "utility.h" #include "utility.h"
#include "walk.h" #include "walk.h"
#include <dirent.h> #include <dirent.h>
@@ -17,94 +18,126 @@ static int ignore_file(const char *name) {
return 0; 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; struct stat st;
if (lstat(path, &st) == -1)
if (lstat(path, &st) == -1) {
return -1; return -1;
} return S_ISDIR(st.st_mode) != 0;
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;
} }
int walk(const char *directory, WalkOp op, void *context) { 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); DIR *dirp = opendir(directory);
if (dirp == NULL) { if (dirp == NULL) {
if (errno == EACCES) { if (errno == EACCES) {
fprintf(stderr, "access denied: %s\n", directory); fprintf(stderr, "access denied: %s\n", directory);
return 0; goto success;
} }
return -1; goto failed;
} }
int rc = -1; /* The stack owns the char *paths of the subdirectories still to inspect.
char *path = NULL; At most one DIR is open at a time, so a deep tree cannot exhaust the
struct dirent *direntp = NULL; 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 (;;) { 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; errno = 0;
direntp = readdir(dirp); struct dirent *direntp;
if (direntp == NULL) while ((direntp = readdir(dirp)) != NULL) {
break;
if (ignore_file(direntp->d_name)) if (ignore_file(direntp->d_name))
continue; continue;
path = strconcat(directory, 2, "/", direntp->d_name); char *path =
strconcat(base == NULL ? directory : base, 2, "/", direntp->d_name);
if (path == NULL) if (path == NULL)
goto out; 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;
}
#else
if (handle_unknown(path, op, context) == -1) {
goto out;
}
#endif
const int type = is_directory(path, direntp->d_type);
if (type == -1) {
free(path); free(path);
path = NULL; goto failed;
} }
if (errno == 0) { if (type == 1) {
rc = 0; /* 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) {
out:
free(path); free(path);
const int saved_errno = errno; goto failed;
}
} else {
if (op(path, context) == -1) {
free(path);
goto failed;
}
free(path);
}
}
if (errno != 0)
goto failed;
if (closedir(dirp) == -1) if (closedir(dirp) == -1)
rc = -1; goto failed;
errno = saved_errno; dirp = NULL;
return rc; 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;
}
}
success:;
if (dirp != NULL)
closedir(dirp);
free(base);
while (stack != NULL && !StackEmpty(stack)) {
free(StackPop(stack));
}
if (stack != NULL)
StackRelease(stack);
return 0;
failed:;
const int saved_errno = errno;
if (dirp != NULL)
closedir(dirp);
free(base);
while (stack != NULL && !StackEmpty(stack)) {
free(StackPop(stack));
}
if (stack != NULL)
StackRelease(stack);
errno = saved_errno;
return -1;
} }