diff --git a/src/main.c b/src/main.c index a4b2104..a871017 100644 --- a/src/main.c +++ b/src/main.c @@ -56,6 +56,7 @@ int main(int argc, char *argv[]) { ret = fuse_main(argc - 1, argv, get_fuse_operations(), NULL); free((void *)mountpoint); + source_destroy(); destroy_ui_socket(); return ret; } diff --git a/src/real_filename.h b/src/real_filename.h new file mode 100644 index 0000000..ee27b0a --- /dev/null +++ b/src/real_filename.h @@ -0,0 +1,7 @@ +#ifndef REAL_FILENAME_H +#define REAL_FILENAME_H + +const char *real_filename(const char *filename); +const char *get_mountpoint(void); + +#endif // !REAL_FILENAME_H diff --git a/src/sourcefs.c b/src/sourcefs.c index 5a209a2..66def02 100644 --- a/src/sourcefs.c +++ b/src/sourcefs.c @@ -10,14 +10,14 @@ #include "sourcefs.h" #include -#include #include #include +#include #include -#include #include static struct source_files_handle { + const char *mountpoint; int root_fd; } handle; @@ -30,6 +30,14 @@ const char *source_filename_translate(const char *filename) { } int source_init(const char *root_path) { + handle.mountpoint = malloc(strlen(root_path) + 1); + if (handle.mountpoint == NULL) { + perror("Malloc failed"); + return -1; + } + + strcpy(handle.mountpoint, root_path); + int root_fd = open(root_path, O_PATH); if (root_fd == -1) { @@ -43,6 +51,32 @@ int source_init(const char *root_path) { return 0; } +void source_destroy(void) { free(handle.mountpoint); } + +const char *get_mountpoint(void) { return handle.mountpoint; } + +const char *real_filename(const char *filename) { + const char *mountpoint = get_mountpoint(); + // Calculate required length + size_t len1 = strlen(mountpoint); + size_t len2 = strlen(filename); + size_t total_len = len1 + len2; + + // Allocate memory (+1 for null terminator) + char *result = malloc(total_len + 1); + if (result == NULL) { + fprintf(stderr, "Memory allocation failed"); + perror(""); + return NULL; + } + + // Copy strings + strcpy(result, mountpoint); + strcat(result, filename); + + return result; +} + int source_mkdir(const char *filename, mode_t mode) { const char *relative_filename = source_filename_translate(filename); return mkdirat(handle.root_fd, relative_filename, mode);