mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 19:29:50 +00:00
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:
12
src/create.c
12
src/create.c
@@ -3,6 +3,7 @@
|
||||
#include "create.h"
|
||||
#include "utility.h"
|
||||
#include "walk.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
@@ -10,7 +11,7 @@
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <strings.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "walk.h"
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.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) {
|
||||
errno = 0;
|
||||
DIR *dirp = opendir(directory);
|
||||
if (dirp == NULL) {
|
||||
if (errno == EACCES) {
|
||||
fprintf(stderr, "access denied: %s\n", directory);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user