Used the libfuse example as a foundation.
The main.c was completely replaced by one of the libfuse examples: [passthrough_fh.c](https://github.com/libfuse/libfuse/blob/master/example/passthrough_fh.c) . This choice is made based on that there is little point in spending time on reinventing the wheel by reimplementing passthrough all over again. Also, the [passthrough_hp.cc](https://github.com/libfuse/libfuse/blob/master/example/passthrough_hp.cc) wasn't used because it uses a vastly different and much more comlex API, even though the authors claim it to be faster.
This commit is contained in:
@@ -2,15 +2,24 @@
|
||||
|
||||
#include "sourcefs.h"
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static struct source_files_handle {
|
||||
int root_fd;
|
||||
} handle;
|
||||
|
||||
void source_fname_translate(const char *filename) {}
|
||||
const char *source_fname_translate(const char *filename) {
|
||||
if (strcmp("/", filename) == 0) {
|
||||
return ".";
|
||||
} else {
|
||||
return filename + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int source_init(const char *root_path) {
|
||||
int root_fd = open(root_path, O_PATH);
|
||||
@@ -24,17 +33,43 @@ int source_init(const char *root_path) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Currently this literally is a fstatat wrapper.
|
||||
int source_stat(const char *restrict pathname, struct stat *restrict statbuf) {
|
||||
int statret = fstatat(handle.root_fd, pathname, statbuf, 0);
|
||||
return statret;
|
||||
int source_mkdir(const char *filename, mode_t mode) {
|
||||
const char *relative_filename = source_fname_translate(filename);
|
||||
return mkdirat(handle.root_fd, relative_filename, mode);
|
||||
}
|
||||
|
||||
int source_unlink(const char *filename) {
|
||||
const char *relative_filename = source_fname_translate(filename);
|
||||
return unlinkat(handle.root_fd, relative_filename, 0);
|
||||
}
|
||||
|
||||
int source_stat(const char *restrict filename, struct stat *restrict statbuf) {
|
||||
const char *relative_filename = source_fname_translate(filename);
|
||||
return fstatat(handle.root_fd, relative_filename, statbuf, 0);
|
||||
}
|
||||
|
||||
int source_rmdir(const char *filename) {
|
||||
const char *relative_filename = source_fname_translate(filename);
|
||||
return unlinkat(handle.root_fd, relative_filename, AT_REMOVEDIR);
|
||||
}
|
||||
|
||||
int source_symlink(const char *target, const char *linkpath) {
|
||||
const char *relative_linkpath = source_fname_translate(linkpath);
|
||||
return symlinkat(target, handle.root_fd, relative_linkpath);
|
||||
}
|
||||
|
||||
DIR *source_opendir(const char *filename) {
|
||||
int fd = openat(handle.root_fd, filename, NULL);
|
||||
const char *relative_filename = source_fname_translate(filename);
|
||||
int fd = openat(handle.root_fd, relative_filename, NULL);
|
||||
if (fd < 0) {
|
||||
perror("Openat failed");
|
||||
return NULL;
|
||||
}
|
||||
DIR *dir_pointer = fdopendir(fd);
|
||||
return dir_pointer;
|
||||
}
|
||||
|
||||
// Just regular readdir in disguise.
|
||||
struct dirent *source_readdir(DIR *dirp) { return readdir(dirp); }
|
||||
int source_rename(const char *oldpath, const char *newpath) {
|
||||
printf("{\"%s\", \"%s\"}\n", oldpath, newpath);
|
||||
return -1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user