From 6e44f4a32631cab2a9081b10e7f17b22b90d988a Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Tue, 4 Nov 2014 17:40:31 +0100 Subject: [PATCH] Implemented basic fuse interface --- src/cryfs_lib/CMakeLists.txt | 4 + src/cryfs_lib/CryDevice.h | 4 + src/cryfs_lib/fuse/CMakeLists.txt | 3 + src/cryfs_lib/fuse/Fuse.cpp | 198 ++++++++++++++++++++++++++++++ src/cryfs_lib/fuse/Fuse.h | 168 +++++++++++++++++++++++++ src/cryfs_lib/fuse/params.h | 7 ++ src/main.cpp | 10 +- 7 files changed, 387 insertions(+), 7 deletions(-) create mode 100644 src/cryfs_lib/fuse/CMakeLists.txt create mode 100644 src/cryfs_lib/fuse/Fuse.cpp create mode 100644 src/cryfs_lib/fuse/Fuse.h create mode 100644 src/cryfs_lib/fuse/params.h diff --git a/src/cryfs_lib/CMakeLists.txt b/src/cryfs_lib/CMakeLists.txt index 4dc2b452..17500cbd 100644 --- a/src/cryfs_lib/CMakeLists.txt +++ b/src/cryfs_lib/CMakeLists.txt @@ -1 +1,5 @@ +add_subdirectory(fuse) + add_library(cryfs_lib CryDevice.cpp) + +target_link_libraries(cryfs_lib cryfs_fuse) diff --git a/src/cryfs_lib/CryDevice.h b/src/cryfs_lib/CryDevice.h index 27076ce6..22822db8 100644 --- a/src/cryfs_lib/CryDevice.h +++ b/src/cryfs_lib/CryDevice.h @@ -1,12 +1,16 @@ #ifndef CRYFS_LIB_CRYDEVICE_H_ #define CRYFS_LIB_CRYDEVICE_H_ +#include "fuse/Fuse.h" + namespace cryfs { class CryDevice { public: CryDevice(); virtual ~CryDevice(); +private: + Fuse _fuse; }; } diff --git a/src/cryfs_lib/fuse/CMakeLists.txt b/src/cryfs_lib/fuse/CMakeLists.txt new file mode 100644 index 00000000..7b2999d2 --- /dev/null +++ b/src/cryfs_lib/fuse/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(cryfs_fuse Fuse.cpp) + +target_link_libraries(cryfs_fuse fuse) diff --git a/src/cryfs_lib/fuse/Fuse.cpp b/src/cryfs_lib/fuse/Fuse.cpp new file mode 100644 index 00000000..7dd0eb8e --- /dev/null +++ b/src/cryfs_lib/fuse/Fuse.cpp @@ -0,0 +1,198 @@ +#include "Fuse.h" + +#include + +using std::unique_ptr; +using std::make_unique; +using std::string; + +#define FUSE_OBJ ((Fuse *) fuse_get_context()->private_data) + +namespace { +int getattr(const char *path, struct stat *stbuf) { + return FUSE_OBJ->getattr(path, stbuf); +} + +int fgetattr(const char *path, struct stat *stbuf, fuse_file_info *fileinfo) { + return FUSE_OBJ->fgetattr(path, stbuf, fileinfo); +} + +int readlink(const char *path, char *buf, size_t size) { + return FUSE_OBJ->readlink(path, buf, size); +} + +int mknod(const char *path, mode_t mode, dev_t rdev) { + return FUSE_OBJ->mknod(path, mode, rdev); +} + +int mkdir(const char *path, mode_t mode) { + return FUSE_OBJ->mkdir(path, mode); +} + +int unlink(const char *path) { + return FUSE_OBJ->unlink(path); +} + +int rmdir(const char *path) { + return FUSE_OBJ->rmdir(path); +} + +int symlink(const char *from, const char *to) { + return FUSE_OBJ->symlink(from, to); +} + +int rename(const char *from, const char *to) { + return FUSE_OBJ->rename(from, to); +} + +int link(const char *from, const char *to) { + return FUSE_OBJ->link(from, to); +} + +int chmod(const char *path, mode_t mode) { + return FUSE_OBJ->chmod(path, mode); +} + +int chown(const char *path, uid_t uid, gid_t gid) { + return FUSE_OBJ->chown(path, uid, gid); +} + +int truncate(const char *path, off_t size) { + return FUSE_OBJ->truncate(path, size); +} + +int ftruncate(const char *path, off_t size, fuse_file_info *fileinfo) { + return FUSE_OBJ->ftruncate(path, size, fileinfo); +} + +int utimens(const char *path, const timespec times[2]) { + return FUSE_OBJ->utimens(path, times); +} + +int open(const char *path, fuse_file_info *fileinfo) { + return FUSE_OBJ->open(path, fileinfo); +} + +int release(const char *path, fuse_file_info *fileinfo) { + return FUSE_OBJ->release(path, fileinfo); +} + +int read(const char *path, char *buf, size_t size, off_t offset, fuse_file_info *fileinfo) { + return FUSE_OBJ->read(path, buf, size, offset, fileinfo); +} + +int write(const char *path, const char *buf, size_t size, off_t offset, fuse_file_info *fileinfo) { + return FUSE_OBJ->write(path, buf, size, offset, fileinfo); +} + +int statfs(const char *path, struct statvfs *fsstat) { + return FUSE_OBJ->statfs(path, fsstat); +} + +int flush(const char *path, fuse_file_info *fileinfo) { + return FUSE_OBJ->flush(path, fileinfo); +} + +int fsync(const char *path, int flags, fuse_file_info *fileinfo) { + return FUSE_OBJ->fsync(path, flags, fileinfo); +} + +//int setxattr(const char*, const char*, const char*, size_t, int) +//int getxattr(const char*, const char*, char*, size_t) +//int listxattr(const char*, char*, size_t) +//int removexattr(const char*, const char*) + +int opendir(const char *path, fuse_file_info *fileinfo) { + return FUSE_OBJ->opendir(path, fileinfo); +} + +int readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, fuse_file_info *fileinfo) { + return FUSE_OBJ->readdir(path, buf, filler, offset, fileinfo); +} + +int releasedir(const char *path, fuse_file_info *fileinfo) { + return FUSE_OBJ->releasedir(path, fileinfo); +} + +int fsyncdir(const char *path, int datasync, fuse_file_info *fileinfo) { + return FUSE_OBJ->fsyncdir(path, datasync, fileinfo); +} + +void* init(fuse_conn_info *conn) { + return FUSE_OBJ->init(conn); +} + +void destroy(void *userdata) { + return FUSE_OBJ->destroy(userdata); +} + +int access(const char *path, int mask) { + return FUSE_OBJ->access(path, mask); +} + +int create(const char *path, mode_t mode, fuse_file_info *fileinfo) { + return FUSE_OBJ->create(path, mode, fileinfo); +} + +/*int lock(const char*, fuse_file_info*, int cmd, flock*) +int bmap(const char*, size_t blocksize, uint64_t *idx) +int ioctl(const char*, int cmd, void *arg, fuse_file_info*, unsigned int flags, void *data) +int poll(const char*, fuse_file_info*, fuse_pollhandle *ph, unsigned *reventsp) +int write_buf(const char*, fuse_bufvec *buf, off_t off, fuse_file_info*) +int read_buf(const chas*, struct fuse_bufvec **bufp, size_t size, off_T off, fuse_file_info*) +int flock(const char*, fuse_file_info*, int op) +int fallocate(const char*, int, off_t, off_t, fuse_file_info*)*/ + +fuse_operations *operations() { + static unique_ptr singleton(nullptr); + + if (!singleton) { + singleton = make_unique(); + singleton->getattr = &getattr; + singleton->fgetattr = &fgetattr; + singleton->readlink = &readlink; + singleton->mknod = &mknod; + singleton->mkdir = &mkdir; + singleton->unlink = &unlink; + singleton->rmdir = &rmdir; + singleton->symlink = &symlink; + singleton->rename = &rename; + singleton->link = &link; + singleton->chmod = &chmod; + singleton->chown = &chown; + singleton->truncate = &truncate; + singleton->utimens = &utimens; + singleton->open = &open; + singleton->read = &read; + singleton->write = &write; + singleton->statfs = &statfs; + singleton->flush = &flush; + singleton->release = &release; + singleton->fsync = &fsync; + /*#ifdef HAVE_SYS_XATTR_H + singleton->setxattr = &setxattr; + singleton->getxattr = &getxattr; + singleton->listxattr = &listxattr; + singleton->removexattr = &removexattr; + #endif*/ + singleton->opendir = &opendir; + singleton->readdir = &readdir; + singleton->releasedir = &releasedir; + singleton->fsyncdir = &fsyncdir; + singleton->init = &init; + singleton->destroy = &destroy; + singleton->access = &access; + singleton->create = &create; + singleton->ftruncate = &ftruncate; + } + + return singleton.get(); +} +} + +Fuse::~Fuse() { +} + +void Fuse::run(int argc, char **argv) { + fuse_main(argc, argv, operations(), (void*)this); +} diff --git a/src/cryfs_lib/fuse/Fuse.h b/src/cryfs_lib/fuse/Fuse.h new file mode 100644 index 00000000..5919961b --- /dev/null +++ b/src/cryfs_lib/fuse/Fuse.h @@ -0,0 +1,168 @@ +#pragma once +#ifndef CRYFS_LIB_FUSE_FUSE_H_ +#define CRYFS_LIB_FUSE_FUSE_H_ + +#include "params.h" +#include +#include +#include +#include + +class Fuse { +public: + virtual ~Fuse(); + + void run(int argc, char **argv); + + int getattr(const char *path, struct stat *stbuf) { + printf("Called getattr(%s, _)\n", path); + return 0; + } + + int fgetattr(const char *path, struct stat *stbuf, fuse_file_info *fileinfo) { + printf("Called fgetattr(%s, _, _)\n", path); + return 0; + } + + int readlink(const char *path, char *buf, size_t size) { + printf("Called readlink(%s, _, %d)\n", path, size); + return 0; + } + + int mknod(const char *path, mode_t mode, dev_t rdev) { + printf("Called mknod(%s, %d, _)\n", path, mode); + return 0; + } + + int mkdir(const char *path, mode_t mode) { + printf("Called mkdir(%s, %d)\n", path, mode); + return 0; + } + + int unlink(const char *path) { + printf("Called unlink(%s)\n", path); + return 0; + } + + int rmdir(const char *path) { + printf("Called rmdir(%s)\n", path); + return 0; + } + + int symlink(const char *from, const char *to) { + printf("Called symlink(%s, %s)\n", from, to); + return 0; + } + + int rename(const char *from, const char *to) { + printf("Called rename(%s, %s)\n", from, to); + return 0; + } + + int link(const char *from, const char *to) { + printf("Called link(%s, %s)\n", from, to); + return 0; + } + + int chmod(const char *path, mode_t mode) { + printf("Called chmod(%s, %d)\n", path, mode); + return 0; + } + + int chown(const char *path, uid_t uid, gid_t gid) { + printf("Called chown(%s, %d, %d)\n", path, uid, gid); + return 0; + } + + int truncate(const char *path, off_t size) { + printf("Called truncate(%s, %d)\n", path, size); + return 0; + } + + int ftruncate(const char *path, off_t size, fuse_file_info *fileinfo) { + printf("Called ftruncate(%s, %d)\n", path, size); + return 0; + } + + int utimens(const char *path, const timespec times[2]) { + printf("Called utimens(%s, _)\n", path); + return 0; + } + + int open(const char *path, fuse_file_info *fileinfo) { + printf("Called open(%s)\n", path); + return 0; + } + + int release(const char *path, fuse_file_info *fileinfo) { + printf("Called release(%s)\n", path); + return 0; + } + + int read(const char *path, char *buf, size_t size, off_t offset, fuse_file_info *fileinfo) { + printf("Called read(%s, _, %d, %d, _)\n", path, size, offset); + return 0; + } + + int write(const char *path, const char *buf, size_t size, off_t offset, fuse_file_info *fileinfo) { + printf("Called write(%s, _, %d, %d, _)\n", path, size, offset); + return 0; + } + + int statfs(const char *path, struct statvfs *fsstat) { + printf("Called statfs(%s)\n", path); + return 0; + } + + int flush(const char *path, fuse_file_info *fileinfo) { + printf("Called flush(%s)\n", path); + return 0; + } + + int fsync(const char *path, int flags, fuse_file_info *fileinfo) { + printf("Called fsync(%s, %d, _)\n", path, flags); + return 0; + } + + int opendir(const char *path, fuse_file_info *fileinfo) { + printf("Called opendir(%s, _)\n", path); + return 0; + } + + int readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, fuse_file_info *fileinfo) { + printf("Called readdir(%s, _, _, %d, _)\n", path, offset); + return 0; + } + + int releasedir(const char *path, fuse_file_info *fileinfo) { + printf("Called releasedir(%s, _)\n", path); + return 0; + } + + int fsyncdir(const char *path, int datasync, fuse_file_info *fileinfo) { + printf("Called fsyncdir(%s, %d, _)\n", path, datasync); + return 0; + } + + void* init(fuse_conn_info *conn) { + printf("Called init()\n"); + return this; + } + + void destroy(void *userdata) { + printf("Called destroy()\n"); + } + + int access(const char *path, int mask) { + printf("Called access(%s, %d)\n", path, mask); + return 0; + } + + int create(const char *path, mode_t mode, fuse_file_info *fileinfo) { + printf("Called create(%s, %d, _)\n", path, mode); + return 0; + } + +}; + +#endif /* CRYFS_LIB_FUSE_FUSE_H_ */ diff --git a/src/cryfs_lib/fuse/params.h b/src/cryfs_lib/fuse/params.h new file mode 100644 index 00000000..5a8a9e40 --- /dev/null +++ b/src/cryfs_lib/fuse/params.h @@ -0,0 +1,7 @@ +#pragma once +#ifndef CRYFS_LIB_FUSE_PARAMS_H_ +#define CRYFS_LIB_FUSE_PARAMS_H_ + +#define FUSE_USE_VERSION 26 + +#endif /* CRYFS_LIB_FUSE_PARAMS_H_ */ diff --git a/src/main.cpp b/src/main.cpp index 26fee77a..a8a0a1dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,14 +6,10 @@ #include "cryfs_lib/CryDevice.h" -int main () +int main (int argc, char *argv[]) { printf("Version: %d\n", buildconfig::VERSION::MAJOR); - cryfs::CryDevice device; - #ifdef NDEBUG - printf("Release build"); - #else - printf("Debug build"); - #endif + Fuse fuse; + fuse.run(argc, argv); return 0; }