mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 11:19:52 +00:00
1
Makefile
1
Makefile
@@ -31,6 +31,7 @@ INSTALL ?= install
|
|||||||
|
|
||||||
SOURCES := \
|
SOURCES := \
|
||||||
src/main.c \
|
src/main.c \
|
||||||
|
src/stack.c \
|
||||||
src/utility.c \
|
src/utility.c \
|
||||||
src/types.c \
|
src/types.c \
|
||||||
src/hash.c \
|
src/hash.c \
|
||||||
|
|||||||
15
include/stack.h
Normal file
15
include/stack.h
Normal 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
76
src/stack.c
Normal 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);
|
||||||
|
}
|
||||||
163
src/walk.c
163
src/walk.c
@@ -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;
|
||||||
|
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;
|
errno = saved_errno;
|
||||||
return rc;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user