ICFS/sources/main.c

97 lines
2.2 KiB
C
Raw Normal View History

2024-12-03 18:10:50 +01:00
#include <dirent.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
2024-12-03 18:10:50 +01:00
#include <string.h>
#define FUSE_USE_VERSION 31
#include "sourcefs.h"
#include <fuse3/fuse.h>
2024-12-03 18:10:50 +01:00
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. */
2024-12-03 18:10:50 +01:00
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) {
2024-12-03 18:10:50 +01:00
(void)stbuf;
(void)fi;
int statret = source_stat(path, stbuf);
2024-12-03 18:10:50 +01:00
if (statret != 0)
perror("Stat failed.");
return statret;
}
2024-12-03 18:10:50 +01:00
static int icfs_opendir(const char *path, struct fuse_file_info *fi) {
DIR *dir_stream = source_opendir(path);
2024-12-03 18:10:50 +01:00
if (dir_stream == NULL) {
perror("Opendir failed.");
return 1;
}
2024-12-03 18:10:50 +01:00
fi->fh = (intptr_t)dir_stream;
2024-12-03 18:10:50 +01:00
return 0;
}
2024-12-03 18:10:50 +01:00
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,
2024-12-03 18:10:50 +01:00
.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.
2024-12-03 18:10:50 +01:00
mountpoint = realpath(argv[argc - 1], NULL);
ret = fuse_main(argc, argv, &icfs_oper, NULL);
return ret;
}