Added mountpoint functions to sourcefs

This commit is contained in:
fedir 2025-05-01 16:16:09 +02:00
parent 07cb76f425
commit 31b70b6069
Signed by: fedir
GPG Key ID: C959EE85F0C9362C
3 changed files with 44 additions and 2 deletions

View File

@ -56,6 +56,7 @@ int main(int argc, char *argv[]) {
ret = fuse_main(argc - 1, argv, get_fuse_operations(), NULL); ret = fuse_main(argc - 1, argv, get_fuse_operations(), NULL);
free((void *)mountpoint); free((void *)mountpoint);
source_destroy();
destroy_ui_socket(); destroy_ui_socket();
return ret; return ret;
} }

7
src/real_filename.h Normal file
View File

@ -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

View File

@ -10,14 +10,14 @@
#include "sourcefs.h" #include "sourcefs.h"
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
static struct source_files_handle { static struct source_files_handle {
const char *mountpoint;
int root_fd; int root_fd;
} handle; } handle;
@ -30,6 +30,14 @@ const char *source_filename_translate(const char *filename) {
} }
int source_init(const char *root_path) { 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); int root_fd = open(root_path, O_PATH);
if (root_fd == -1) { if (root_fd == -1) {
@ -43,6 +51,32 @@ int source_init(const char *root_path) {
return 0; 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) { int source_mkdir(const char *filename, mode_t mode) {
const char *relative_filename = source_filename_translate(filename); const char *relative_filename = source_filename_translate(filename);
return mkdirat(handle.root_fd, relative_filename, mode); return mkdirat(handle.root_fd, relative_filename, mode);