Added mountpoint functions to sourcefs
This commit is contained in:
parent
07cb76f425
commit
31b70b6069
@ -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;
|
||||
}
|
||||
|
7
src/real_filename.h
Normal file
7
src/real_filename.h
Normal 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
|
@ -10,14 +10,14 @@
|
||||
|
||||
#include "sourcefs.h"
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user