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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mbj1ZPUEhxqMYKsK7Y7jkt
This commit is contained in:
Fabio Scotto di Santolo
2026-08-01 21:26:09 +02:00
parent 777f14a9e9
commit 1ddc085160
3 changed files with 17 additions and 9 deletions

View File

@@ -3,6 +3,7 @@
#include "create.h" #include "create.h"
#include "utility.h" #include "utility.h"
#include "walk.h" #include "walk.h"
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
@@ -10,7 +11,7 @@
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <strings.h>
static char file_type(mode_t mode) { static char file_type(mode_t mode) {
if (S_ISREG(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; intmax_t time = (intmax_t)st.st_mtime;
// Write record in the stream file // Write record in the stream file
fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, gid, size, return fprintf(snapshot, "%c|%04o|%ju|%ju|%jd|%jd|%s\n", type, perm, uid, gid,
time, path); size, time, path) < 0
? -1
return 0; : 0;
} }
int create(const char *directory, const char *output_file) { 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+"); FILE *tmp = fdopen(fd, "w+");
if (tmp == NULL) { if (tmp == NULL) {
close(fd);
rc = -1; rc = -1;
goto out; goto out;
} }

View File

@@ -4,7 +4,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
char *strconcat(const char *src, int n, ...) { char *strconcat(const char *src, int n, ...) {
va_list ap; va_list ap;
@@ -74,10 +73,11 @@ int copy(const char *src, const char *dst) {
return -1; return -1;
} }
int rc = 0;
if (fclose(file1) != 0) if (fclose(file1) != 0)
return -1; rc = -1;
if (fclose(file2) != 0) if (fclose(file2) != 0)
return -1; rc = -1;
return 0; return rc;
} }

View File

@@ -3,6 +3,7 @@
#include "walk.h" #include "walk.h"
#include <dirent.h> #include <dirent.h>
#include <errno.h> #include <errno.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -36,8 +37,13 @@ static int handle_unknown(const char *path, operation op, void *context) {
} }
int walk(const char *directory, operation op, void *context) { int walk(const char *directory, operation op, void *context) {
errno = 0;
DIR *dirp = opendir(directory); DIR *dirp = opendir(directory);
if (dirp == NULL) { if (dirp == NULL) {
if (errno == EACCES) {
fprintf(stderr, "access denied: %s\n", directory);
return 0;
}
return -1; return -1;
} }