97 lines
2.2 KiB
C
97 lines
2.2 KiB
C
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#define FUSE_USE_VERSION 31
|
|
|
|
#include "sourcefs.h"
|
|
#include <fuse3/fuse.h>
|
|
|
|
const char *mountpoint = NULL;
|
|
|
|
/*
|
|
* #In this function we need to:
|
|
*
|
|
* * Open the folder we are mounting over (and remember it).
|
|
* * Initialize the process table.
|
|
*/
|
|
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. */
|
|
|
|
int ret = source_init(mountpoint);
|
|
if (ret != 0) {
|
|
perror("Failed to initialize filesystem.");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static int icfs_getattr(const char *path, struct stat *stbuf,
|
|
struct fuse_file_info *fi) {
|
|
(void)stbuf;
|
|
(void)fi;
|
|
|
|
int statret = source_stat(path, stbuf);
|
|
if (statret != 0)
|
|
perror("Stat failed.");
|
|
|
|
return statret;
|
|
}
|
|
|
|
static int icfs_opendir(const char *path, struct fuse_file_info *fi) {
|
|
DIR *dir_stream = source_opendir(path);
|
|
|
|
if (dir_stream == NULL) {
|
|
perror("Opendir failed.");
|
|
return 1;
|
|
}
|
|
|
|
fi->fh = (intptr_t)dir_stream;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int icfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
|
|
off_t offset, struct fuse_file_info *fi,
|
|
enum fuse_readdir_flags flags) {
|
|
DIR *dir_stream = (DIR *)fi->fh;
|
|
struct dirent *dir_entry = source_readdir(dir_stream);
|
|
if (dir_entry == NULL) {
|
|
perror("Readdir failed.");
|
|
return 1;
|
|
}
|
|
|
|
do {
|
|
int filler_ret = filler(buf, dir_entry->d_name, NULL, 0, 0);
|
|
if (filler_ret != 0) {
|
|
return -ENOMEM;
|
|
}
|
|
} while ((dir_entry = source_readdir(dir_stream)) != NULL);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct fuse_operations icfs_oper = {
|
|
.init = icfs_init,
|
|
.getattr = icfs_getattr,
|
|
.opendir = icfs_opendir,
|
|
.readdir = icfs_readdir,
|
|
};
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int ret;
|
|
|
|
// FUSE won't tell us the mountpoint on it's own, so we need to extract it
|
|
// ourselves.
|
|
mountpoint = realpath(argv[argc - 1], NULL);
|
|
|
|
ret = fuse_main(argc, argv, &icfs_oper, NULL);
|
|
|
|
return ret;
|
|
}
|