Merge pull request #1 from fscotto/feature/walk-dfs

Feature/walk dfs
This commit is contained in:
Fabio Scotto di Santolo
2026-09-13 23:34:13 +02:00
committed by GitHub
4 changed files with 190 additions and 65 deletions

View File

@@ -31,6 +31,7 @@ INSTALL ?= install
SOURCES := \
src/main.c \
src/stack.c \
src/utility.c \
src/types.c \
src/hash.c \

15
include/stack.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef STACK_H
#define STACK_H
#include <stdbool.h>
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

76
src/stack.c Normal file
View File

@@ -0,0 +1,76 @@
#include "stack.h"
#include <stdlib.h>
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);
}

View File

@@ -1,3 +1,4 @@
#include "stack.h"
#include "utility.h"
#include "walk.h"
#include <dirent.h>
@@ -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;
path = strconcat(directory, 2, "/", direntp->d_name);
char *path =
strconcat(base == NULL ? directory : base, 2, "/", direntp->d_name);
if (path == NULL)
goto out;
#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
goto failed;
const int type = is_directory(path, direntp->d_type);
if (type == -1) {
free(path);
path = NULL;
goto failed;
}
if (errno == 0) {
rc = 0;
}
out:
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);
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)
rc = -1;
errno = saved_errno;
return rc;
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;
}
}
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;
}