diff --git a/.gitignore b/.gitignore index 845cda6..5b9425e 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ *.i*86 *.x86_64 *.hex +build/ # Debug files *.dSYM/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..878b34b --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +CC := cc + +CPPFLAGS := -Iinclude -MMD -MP +CFLAGS := -std=c11 -Wall -Wextra -Wpedantic -Wconversion +LDFLAGS := +LDLIBS := + +BUILD_DIR := build +TARGET := $(BUILD_DIR)/fsnap + +SOURCES := \ + src/main.c \ + src/walk.c \ + src/scan.c + +OBJECTS := $(SOURCES:%.c=$(BUILD_DIR)/%.o) +DEPENDENCIES := $(OBJECTS:.o=.d) + +.PHONY: all clean run + +all: $(TARGET) + +$(TARGET): $(OBJECTS) | $(BUILD_DIR) + $(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@ + +$(BUILD_DIR)/%.o: %.c + @mkdir -p $(dir $@) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +$(BUILD_DIR): + mkdir -p $@ + +run: $(TARGET) + ./$(TARGET) + +clean: + rm -rf $(BUILD_DIR) + +-include $(DEPENDENCIES) diff --git a/include/scan.h b/include/scan.h new file mode 100644 index 0000000..2fdc24f --- /dev/null +++ b/include/scan.h @@ -0,0 +1,6 @@ +#ifndef SCAN_H +#define SCAN_H + +int scan(const char *); + +#endif diff --git a/include/walk.h b/include/walk.h new file mode 100644 index 0000000..02a9c77 --- /dev/null +++ b/include/walk.h @@ -0,0 +1,8 @@ +#ifndef WALK_H +#define WALK_H + +typedef int(*operation)(const char *); + +int walk(const char *directory, operation op); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..401d46a --- /dev/null +++ b/src/main.c @@ -0,0 +1,29 @@ +#include "scan.h" +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc < 2) { + fprintf(stderr, "command not found [scan]\n"); + return EXIT_FAILURE; + } + + // I check the argument command exists + errno = 0; + const char *cmd = argv[1]; + if (strcmp(cmd, "scan") == 0) { + const char *directory = argv[2]; + if (scan(directory) == -1) { + const int err = errno; + fprintf(stderr, "scan failed: %s\n", strerror(err)); + return EXIT_FAILURE; + } + } else { + fprintf(stderr, "command %s is not valid\n", cmd); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/src/scan.c b/src/scan.c new file mode 100644 index 0000000..deae906 --- /dev/null +++ b/src/scan.c @@ -0,0 +1,31 @@ +#define _XOPEN_SOURCE 500 +#include "scan.h" +#include "walk.h" +#include +#include +#include + +#ifdef PATH_MAX +static long pathmax = PATH_MAX; +#else +static long pathmax = 1024; +#endif + +int print_file(const char *file) { + printf("[%s]\n", file); + return 0; +} + +/* Scan recursively directory, printing all entry */ +int scan(const char *directory) { + if (directory == NULL || strlen(directory) == 0) + return -1; + char path[pathmax]; + const char *dir = NULL; + if ((dir = realpath(directory, path)) == NULL) { + perror("realpath"); + return -1; + } + + return walk(dir, print_file); +} diff --git a/src/walk.c b/src/walk.c new file mode 100644 index 0000000..3f0c86e --- /dev/null +++ b/src/walk.c @@ -0,0 +1,110 @@ +#define _DEFAULT_SOURCE +#include "walk.h" +#include +#include +#include +#include +#include +#include + +static char *blacklist[] = {".", ".."}; + +char *strconcat(const char *src, int n, ...) { + va_list ap; + + /* First pass: total length. strcat never reallocates, so the buffer has to + be sized up front. */ + size_t len = strlen(src); + va_start(ap, n); + for (int i = 0; i < n; i++) { + len += strlen(va_arg(ap, char *)); + } + va_end(ap); + + char *path = malloc(len + 1); + if (path == NULL) + return NULL; + + size_t off = strlen(src); + memcpy(path, src, off); + va_start(ap, n); + for (int i = 0; i < n; i++) { + const char *s = va_arg(ap, char *); + size_t l = strlen(s); + memcpy(path + off, s, l); + off += l; + } + va_end(ap); + path[off] = '\0'; + + return path; +} + +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; + } + return 0; +} + +static int handle_unknown(const char *path, operation op) { + struct stat st; + + if (lstat(path, &st) == -1) { + return -1; + } + + if (S_ISDIR(st.st_mode)) { + return walk(path, op); + } + + op(path); + return 0; +} + +int walk(const char *directory, operation op) { + DIR *dirp = opendir(directory); + if (dirp == NULL) { + return -1; + } + + errno = 0; + char *curr = NULL; + struct dirent *direntp = NULL; + while ((direntp = readdir(dirp)) != NULL) { + curr = direntp->d_name; + if (ignore_file(curr)) + continue; + + char *path = strconcat(directory, 2, "/", curr); + if (path == NULL) + return -1; + + 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) == -1) { + return -1; + } + break; + case DT_DIR: + if (walk(path, op) == -1) { + return -1; + } + break; + default: + op(path); + break; + } + + if (path != NULL) + free(path); + } + + if (errno != 0) { + return -1; + } + return closedir(dirp); +}