From 1ddc085160e8abd19e6b5d4420a5b410175d50fd Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Sat, 1 Aug 2026 21:26:09 +0200 Subject: [PATCH] Skip unreadable directories instead of aborting the snapshot opendir() failing with EACCES now warns on stderr and lets the walk carry on with the rest of the tree. The record for the directory itself is still written, since the operation runs before the recursion, so its metadata survives even when its contents cannot be listed. Every other opendir error still propagates. Also check the return value of fprintf when writing a record, close the descriptor when fdopen fails, and move the strings.h include to the file that actually calls strcasecmp. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Mbj1ZPUEhxqMYKsK7Y7jkt --- src/create.c | 12 +++++++----- src/utility.c | 8 ++++---- src/walk.c | 6 ++++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/create.c b/src/create.c index 9b73c06..4ed2fe4 100644 --- a/src/create.c +++ b/src/create.c @@ -3,6 +3,7 @@ #include "create.h" #include "utility.h" #include "walk.h" +#include #include #include #include @@ -10,7 +11,7 @@ #include #include #include -#include +#include static char file_type(mode_t mode) { if (S_ISREG(mode)) @@ -53,10 +54,10 @@ static int write_record(const char *path, void *context) { intmax_t time = (intmax_t)st.st_mtime; // Write record in the stream file - fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, gid, size, - time, path); - - return 0; + return fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, gid, + size, time, path) < 0 + ? -1 + : 0; } int create(const char *directory, const char *output_file) { @@ -84,6 +85,7 @@ int create(const char *directory, const char *output_file) { FILE *tmp = fdopen(fd, "w+"); if (tmp == NULL) { + close(fd); rc = -1; goto out; } diff --git a/src/utility.c b/src/utility.c index 0ccdcbe..dfdd1f1 100644 --- a/src/utility.c +++ b/src/utility.c @@ -4,7 +4,6 @@ #include #include #include -#include char *strconcat(const char *src, int n, ...) { va_list ap; @@ -74,10 +73,11 @@ int copy(const char *src, const char *dst) { return -1; } + int rc = 0; if (fclose(file1) != 0) - return -1; + rc = -1; if (fclose(file2) != 0) - return -1; + rc = -1; - return 0; + return rc; } diff --git a/src/walk.c b/src/walk.c index a2a37ab..1647b87 100644 --- a/src/walk.c +++ b/src/walk.c @@ -3,6 +3,7 @@ #include "walk.h" #include #include +#include #include #include #include @@ -36,8 +37,13 @@ static int handle_unknown(const char *path, operation op, void *context) { } int walk(const char *directory, operation op, void *context) { + errno = 0; DIR *dirp = opendir(directory); if (dirp == NULL) { + if (errno == EACCES) { + fprintf(stderr, "access denied: %s\n", directory); + return 0; + } return -1; }