mirror of
https://github.com/fscotto/fsnap.git
synced 2026-11-26 19:29:50 +00:00
Refactor operation function definition
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#ifndef WALK_H
|
||||
#define WALK_H
|
||||
|
||||
typedef int(*operation)(const char *);
|
||||
typedef int(*operation)(const char *, void *);
|
||||
|
||||
int walk(const char *directory, operation op);
|
||||
int walk(const char *directory, operation op, void *context);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -11,7 +11,8 @@ static long pathmax = PATH_MAX;
|
||||
static long pathmax = 1024;
|
||||
#endif
|
||||
|
||||
int print_file(const char *file) {
|
||||
static int print_file(const char *file, void *context) {
|
||||
(void)context;
|
||||
printf("[%s]\n", file);
|
||||
return 0;
|
||||
}
|
||||
@@ -27,5 +28,5 @@ int scan(const char *directory) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return walk(dir, print_file);
|
||||
return walk(dir, print_file, NULL);
|
||||
}
|
||||
|
||||
18
src/walk.c
18
src/walk.c
@@ -9,7 +9,7 @@
|
||||
|
||||
static char *blacklist[] = {".", ".."};
|
||||
|
||||
char *strconcat(const char *src, int n, ...) {
|
||||
static char *strconcat(const char *src, int n, ...) {
|
||||
va_list ap;
|
||||
|
||||
/* First pass: total length. strcat never reallocates, so the buffer has to
|
||||
@@ -40,7 +40,7 @@ char *strconcat(const char *src, int n, ...) {
|
||||
return path;
|
||||
}
|
||||
|
||||
int ignore_file(const char *name) {
|
||||
static int ignore_file(const char *name) {
|
||||
for (long unsigned int i = 0; i < sizeof(blacklist) / sizeof(char *); i++) {
|
||||
if (strcmp(name, blacklist[i]) == 0)
|
||||
return 1;
|
||||
@@ -48,7 +48,7 @@ int ignore_file(const char *name) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int handle_unknown(const char *path, operation op) {
|
||||
static int handle_unknown(const char *path, operation op, void *context) {
|
||||
struct stat st;
|
||||
|
||||
if (lstat(path, &st) == -1) {
|
||||
@@ -56,14 +56,14 @@ static int handle_unknown(const char *path, operation op) {
|
||||
}
|
||||
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
return walk(path, op);
|
||||
return walk(path, op, context);
|
||||
}
|
||||
|
||||
op(path);
|
||||
op(path, context);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int walk(const char *directory, operation op) {
|
||||
int walk(const char *directory, operation op, void *context) {
|
||||
DIR *dirp = opendir(directory);
|
||||
if (dirp == NULL) {
|
||||
return -1;
|
||||
@@ -85,17 +85,17 @@ int walk(const char *directory, operation op) {
|
||||
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) == -1) {
|
||||
if (handle_unknown(path, op, context) == -1) {
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
case DT_DIR:
|
||||
if (walk(path, op) == -1) {
|
||||
if (walk(path, op, context) == -1) {
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
op(path);
|
||||
op(path, context);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user