From e48cbc62f15d7117d9fa71f3b6c82a5f2b6c0af8 Mon Sep 17 00:00:00 2001 From: fedir Date: Wed, 20 Nov 2024 09:28:20 +0100 Subject: [PATCH] Added source files initialization. Now source files handling is delegated to functions defined in `sourcefs.c` file. By encapsulating methods for managing the underlying source files structure we ensure that all code changes to the source file protection strategy will be localized to `sourcefs.c` file. Also changed Makefile to use more sane way of handling options and build flags. --- Makefile | 35 +++++++++++++++++++++++------------ sources/main.c | 1 + sources/sourcefs.c | 21 +++++++++++++++++++++ sources/sourcefs.h | 14 ++++++++++++++ 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 sources/sourcefs.c create mode 100644 sources/sourcefs.h diff --git a/Makefile b/Makefile index b45d4b9..2b70e42 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,35 @@ +SHELL=/bin/bash CC := gcc CXX := g++ -O_LDFLAGS := -O_CFLAGS := -O3 +CFLAGS := -I/usr/include/fuse -D_FILE_OFFSET_BITS=64 +LDFLAGS := -lfuse3 -pthread -O_LDFLAGS_DEBUG := -O_CFLAGS_DEBUG := -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Wno-unused -Weffc++ - -I_LDFLAGS := -lfuse3 -pthread -I_CFLAGS := -I/usr/include/fuse -D_FILE_OFFSET_BITS=64 +ifdef DEBUG + CFLAGS += -O0 -pedantic -Wall -Wextra -Wcast-align \ + -Wcast-qual -Wdisabled-optimization -Wformat=2 \ + -Winit-self -Wlogical-op -Wmissing-declarations \ + -Wmissing-include-dirs -Wredundant-decls -Wshadow \ + -Wsign-conversion -Wstrict-overflow=5 \ + -Wswitch-default -Wundef -Wno-unused + LDFLAGS += +else + CFLAGS += -O3 + LDFLAGS += +endif SOURCES_DIR := ./sources BUILD_DIR := ./build -build: $(SOURCES_DIR)/main.cpp - $(CC) $(O_CFLAGS) $(I_CFLAGS) $(SOURCES_DIR)/main.cpp $(O_LDFLAGS) $(I_LDFLAGS) -o $(BUILD_DIR)/icfs +build: $(BUILD_DIR)/main.o $(BUILD_DIR)/sourcefs.o + $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $(BUILD_DIR)/icfs -dev-build: $(SOURCES_DIR)/main.cpp - $(CC) $(O_CFLAGS_DEBUG) $(I_CFLAGS) $(SOURCES_DIR)/main.cpp $(O_LDFLAGS_DEBUG) $(I_LDFLAGS) -o $(BUILD_DIR)/icfs +$(BUILD_DIR)/main.o: $(SOURCES_DIR)/main.c + $(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $(BUILD_DIR)/main.o + +$(BUILD_DIR)/sourcefs.o: $(SOURCES_DIR)/sourcefs.c $(SOURCES_DIR)/sourcefs.h + $(CC) $(CFLAGS) -c $< $(LDFLAGS) -o $@ clean: - rm $(BUILD_DIR)/icfs + rm $(BUILD_DIR)/* diff --git a/sources/main.c b/sources/main.c index be7d438..133f13d 100644 --- a/sources/main.c +++ b/sources/main.c @@ -16,6 +16,7 @@ static void *icfs_init(struct fuse_conn_info *conn, struct fuse_config *cfg) { (void)conn; /* Explicit way to tell we ignore these arguments. */ (void)cfg; /* This also silences the compiler warnings. */ + source_init(mountpoint); return NULL; } diff --git a/sources/sourcefs.c b/sources/sourcefs.c new file mode 100644 index 0000000..0d26955 --- /dev/null +++ b/sources/sourcefs.c @@ -0,0 +1,21 @@ +#define _GNU_SOURCE + +#include "sourcefs.h" + +#include + +static struct source_files_handle { + int root_fd; +} handle; + +int source_init(const char *root_path) { + int root_fd = open(root_path, O_PATH); + + if (root_fd == -1) { + return -1; + } + + handle.root_fd = root_fd; + + return 0; +} diff --git a/sources/sourcefs.h b/sources/sourcefs.h new file mode 100644 index 0000000..e5ff4dc --- /dev/null +++ b/sources/sourcefs.h @@ -0,0 +1,14 @@ + + +#ifndef SOURCEFS_H +#define SOURCEFS_H + +/** + * Initializes the source file handling. + * + * @param root_path The root of the source files folder. + * @return 0 on success, 1 on failure. + */ +int source_init(const char *root_path); + +#endif // !SOURCEFS_H