Renamed cryfs::CryXXX classes to fusepp::FuseXXX classes

This commit is contained in:
Sebastian Messmer 2014-11-15 15:16:18 +01:00
parent 9af5f72665
commit da7debdf9d
23 changed files with 800 additions and 34 deletions

View File

@ -3,18 +3,18 @@
#include <sys/types.h>
#include <sys/time.h>
#include <dirent.h>
#include <fusepp/FuseErrnoException.h>
#include <fusepp/FuseNode.h>
#include <cassert>
#include "cryfs_lib/CryNode.h"
#include "cryfs_lib/CryErrnoException.h"
#define UNUSED(expr) (void)(expr)
using fusepp::path;
namespace cryfs {
namespace fusepp {
CryFuse::CryFuse(CryDevice *device)
CryFuse::CryFuse(FuseDevice *device)
:_device(device) {
}
@ -23,7 +23,7 @@ int CryFuse::getattr(const path &path, struct stat *stbuf) {
try {
_device->lstat(path, stbuf);
return 0;
} catch(cryfs::CryErrnoException &e) {
} catch(fusepp::FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -43,7 +43,7 @@ int CryFuse::fgetattr(const path &path, struct stat *stbuf, fuse_file_info *file
try {
_device->fstat(fileinfo->fh, stbuf);
return 0;
} catch(cryfs::CryErrnoException &e) {
} catch(fusepp::FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -77,7 +77,7 @@ int CryFuse::mkdir(const path &path, mode_t mode) {
try {
_device->mkdir(path, mode);
return 0;
} catch(cryfs::CryErrnoException &e) {
} catch(fusepp::FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -87,7 +87,7 @@ int CryFuse::unlink(const path &path) {
try {
_device->unlink(path);
return 0;
} catch(cryfs::CryErrnoException &e) {
} catch(fusepp::FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -96,7 +96,7 @@ int CryFuse::rmdir(const path &path) {
try {
_device->rmdir(path);
return 0;
} catch(cryfs::CryErrnoException &e) {
} catch(fusepp::FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -116,7 +116,7 @@ int CryFuse::rename(const path &from, const path &to) {
try {
_device->rename(from, to);
return 0;
} catch(cryfs::CryErrnoException &e) {
} catch(fusepp::FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -154,7 +154,7 @@ int CryFuse::truncate(const path &path, off_t size) {
try {
_device->truncate(path, size);
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -165,7 +165,7 @@ int CryFuse::ftruncate(const path &path, off_t size, fuse_file_info *fileinfo) {
try {
_device->ftruncate(fileinfo->fh, size);
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -176,7 +176,7 @@ int CryFuse::utimens(const path &path, const timespec times[2]) {
try {
_device->utimens(path, times);
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -186,7 +186,7 @@ int CryFuse::open(const path &path, fuse_file_info *fileinfo) {
try {
fileinfo->fh = _device->openFile(path, fileinfo->flags);
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -197,7 +197,7 @@ int CryFuse::release(const path &path, fuse_file_info *fileinfo) {
try {
_device->closeFile(fileinfo->fh);
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -209,7 +209,7 @@ int CryFuse::read(const path &path, char *buf, size_t size, off_t offset, fuse_f
//printf("Reading from file %d\n", fileinfo->fh);
//fflush(stdout);
return _device->read(fileinfo->fh, buf, size, offset);
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -220,7 +220,7 @@ int CryFuse::write(const path &path, const char *buf, size_t size, off_t offset,
try {
_device->write(fileinfo->fh, buf, size, offset);
return size;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -231,7 +231,7 @@ int CryFuse::statfs(const path &path, struct statvfs *fsstat) {
try {
_device->statfs(path, fsstat);
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -254,7 +254,7 @@ int CryFuse::fsync(const path &path, int datasync, fuse_file_info *fileinfo) {
_device->fsync(fileinfo->fh);
}
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -282,7 +282,7 @@ int CryFuse::readdir(const path &path, void *buf, fuse_fill_dir_t filler, off_t
}
}
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -318,7 +318,7 @@ int CryFuse::access(const path &path, int mask) {
try {
_device->access(path, mask);
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}
@ -328,7 +328,7 @@ int CryFuse::create(const path &path, mode_t mode, fuse_file_info *fileinfo) {
try {
fileinfo->fh = _device->createAndOpenFile(path, mode);
return 0;
} catch (CryErrnoException &e) {
} catch (FuseErrnoException &e) {
return -e.getErrno();
}
}

View File

@ -3,14 +3,14 @@
#define CRYFS_LIB_CRYFUSE_H_
#include "fusepp/Fuse.h"
#include "cryfs_lib/CryDevice.h"
#include "cryfs_lib/utils/macros.h"
#include "fusepp/FuseDevice.h"
#include "fusepp/utils/macros.h"
namespace cryfs {
namespace fusepp {
class CryFuse: public fusepp::Fuse {
public:
CryFuse(CryDevice *device);
CryFuse(FuseDevice *device);
int getattr(const fusepp::path &path, struct stat *stbuf) override;
int fgetattr(const fusepp::path &path, struct stat *stbuf, fuse_file_info *fileinfo) override;
@ -44,7 +44,7 @@ public:
int create(const fusepp::path &path, mode_t mode, fuse_file_info *fileinfo) override;
private:
CryDevice *_device;
FuseDevice *_device;
DISALLOW_COPY_AND_ASSIGN(CryFuse);
};

View File

@ -1,3 +1,3 @@
add_library(fusepp Fuse.cpp)
add_library(fusepp Fuse.cpp FuseDevice.cpp FuseDir.cpp FuseErrnoException.cpp FuseFile.cpp FuseNode.cpp FuseOpenFile.cpp FuseOpenFileList.cpp IdList.cpp)
target_link_libraries(fusepp fuse boost_filesystem boost_system)

View File

@ -1,6 +1,6 @@
#pragma once
#ifndef CRYFS_LIB_FUSEPP_FUSE_H_
#define CRYFS_LIB_FUSEPP_FUSE_H_
#ifndef FUSEPP_FUSE_H_
#define FUSEPP_FUSE_H_
#include "params.h"
#include <fuse.h>
@ -55,4 +55,4 @@ public:
};
}
#endif /* CRYFS_LIB_FUSEPP_FUSE_H_ */
#endif /* FUSEPP_FUSE_H_ */

145
src/fusepp/FuseDevice.cpp Normal file
View File

@ -0,0 +1,145 @@
#include "FuseDevice.h"
#include <memory>
#include <fcntl.h>
#include <fusepp/FuseDir.h>
#include <fusepp/FuseErrnoException.h>
#include <fusepp/FuseFile.h>
#include <fusepp/FuseOpenFile.h>
#include "utils/pointer.h"
using namespace fusepp;
using std::unique_ptr;
using std::make_unique;
using std::vector;
using std::string;
FuseDevice::FuseDevice(const bf::path &rootdir)
:_rootdir(rootdir), _open_files() {
}
FuseDevice::~FuseDevice() {
}
unique_ptr<FuseNode> FuseDevice::Load(const bf::path &path) {
auto real_path = RootDir() / path;
if(bf::is_directory(real_path)) {
return make_unique<FuseDir>(this, path);
} else if(bf::is_regular_file(real_path)) {
return make_unique<FuseFile>(this, path);
}
throw FuseErrnoException(ENOENT);
}
unique_ptr<FuseFile> FuseDevice::LoadFile(const bf::path &path) {
auto node = Load(path);
auto file = dynamic_pointer_move<FuseFile>(node);
if (!file) {
throw FuseErrnoException(EISDIR);
}
return file;
}
unique_ptr<FuseDir> FuseDevice::LoadDir(const bf::path &path) {
auto node = Load(path);
auto dir = dynamic_pointer_move<FuseDir>(node);
if (!dir) {
throw FuseErrnoException(ENOTDIR);
}
return dir;
}
int FuseDevice::openFile(const bf::path &path, int flags) {
auto file = LoadFile(path);
return openFile(*file, flags);
}
int FuseDevice::openFile(const FuseFile &file, int flags) {
return _open_files.open(file, flags);
}
void FuseDevice::closeFile(int descriptor) {
_open_files.close(descriptor);
}
void FuseDevice::lstat(const bf::path &path, struct ::stat *stbuf) {
Load(path)->stat(stbuf);
}
void FuseDevice::fstat(int descriptor, struct ::stat *stbuf) {
_open_files.get(descriptor)->stat(stbuf);
}
void FuseDevice::truncate(const bf::path &path, off_t size) {
LoadFile(path)->truncate(size);
}
void FuseDevice::ftruncate(int descriptor, off_t size) {
_open_files.get(descriptor)->truncate(size);
}
int FuseDevice::read(int descriptor, void *buf, size_t count, off_t offset) {
return _open_files.get(descriptor)->read(buf, count, offset);
}
void FuseDevice::write(int descriptor, const void *buf, size_t count, off_t offset) {
_open_files.get(descriptor)->write(buf, count, offset);
}
void FuseDevice::fsync(int descriptor) {
_open_files.get(descriptor)->fsync();
}
void FuseDevice::fdatasync(int descriptor) {
_open_files.get(descriptor)->fdatasync();
}
void FuseDevice::access(const bf::path &path, int mask) {
Load(path)->access(mask);
}
int FuseDevice::createAndOpenFile(const bf::path &path, mode_t mode) {
//TODO Creating the file opens and closes it. We then reopen it afterwards.
// This is slow. Improve!
auto dir = LoadDir(path.parent_path());
auto file = dir->createFile(path.filename().native(), mode);
return openFile(*file, O_WRONLY | O_TRUNC);
}
void FuseDevice::mkdir(const bf::path &path, mode_t mode) {
auto dir = LoadDir(path.parent_path());
dir->createDir(path.filename().native(), mode);
}
void FuseDevice::rmdir(const bf::path &path) {
auto dir = LoadDir(path);
dir->rmdir();
}
void FuseDevice::unlink(const bf::path &path) {
auto file = LoadFile(path);
file->unlink();
}
void FuseDevice::rename(const bf::path &from, const bf::path &to) {
auto node = Load(from);
node->rename(to);
}
unique_ptr<vector<string>> FuseDevice::readDir(const bf::path &path) {
auto dir = LoadDir(path);
return dir->children();
}
void FuseDevice::utimens(const bf::path &path, const timespec times[2]) {
auto node = Load(path);
node->utimens(times);
}
void FuseDevice::statfs(const bf::path &path, struct statvfs *fsstat) {
int retval = ::statvfs(path.c_str(), fsstat);
CHECK_RETVAL(retval);
}

64
src/fusepp/FuseDevice.h Normal file
View File

@ -0,0 +1,64 @@
#pragma once
#ifndef FUSEPP_FUSEDEVICE_H_
#define FUSEPP_FUSEDEVICE_H_
#include <boost/filesystem.hpp>
#include <fusepp/FuseOpenFileList.h>
#include <memory>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include "utils/macros.h"
namespace fusepp {
class FuseNode;
class FuseFile;
class FuseOpenFile;
class FuseDir;
namespace bf = boost::filesystem;
class FuseDevice {
public:
FuseDevice(const bf::path &rootdir);
virtual ~FuseDevice();
int openFile(const bf::path &path, int flags);
void closeFile(int descriptor);
void lstat(const bf::path &path, struct ::stat *stbuf);
void fstat(int descriptor, struct ::stat *stbuf);
void truncate(const bf::path &path, off_t size);
void ftruncate(int descriptor, off_t size);
int read(int descriptor, void *buf, size_t count, off_t offset);
void write(int descriptor, const void *buf, size_t count, off_t offset);
void fsync(int descriptor);
void fdatasync(int descriptor);
void access(const bf::path &path, int mask);
int createAndOpenFile(const bf::path &path, mode_t mode);
void mkdir(const bf::path &path, mode_t mode);
void rmdir(const bf::path &path);
void unlink(const bf::path &path);
void rename(const bf::path &from, const bf::path &to);
std::unique_ptr<std::vector<std::string>> readDir(const bf::path &path);
void utimens(const bf::path &path, const timespec times[2]);
void statfs(const bf::path &path, struct statvfs *fsstat);
const bf::path &RootDir() const;
private:
std::unique_ptr<FuseNode> Load(const bf::path &path);
std::unique_ptr<FuseFile> LoadFile(const bf::path &path);
std::unique_ptr<FuseDir> LoadDir(const bf::path &path);
int openFile(const FuseFile &file, int flags);
const bf::path _rootdir;
FuseOpenFileList _open_files;
DISALLOW_COPY_AND_ASSIGN(FuseDevice);
};
inline const bf::path &FuseDevice::RootDir() const {
return _rootdir;
}
}
#endif /* FUSEPP_FUSEDEVICE_H_ */

75
src/fusepp/FuseDir.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <fusepp/FuseDevice.h>
#include <fusepp/FuseDir.h>
#include <fusepp/FuseErrnoException.h>
#include <fusepp/FuseFile.h>
using std::string;
using std::unique_ptr;
using std::make_unique;
using std::vector;
namespace fusepp {
FuseDir::FuseDir(FuseDevice *device, const bf::path &path)
:FuseNode(device, path) {
assert(bf::is_directory(base_path()));
}
FuseDir::~FuseDir() {
}
unique_ptr<FuseFile> FuseDir::createFile(const string &name, mode_t mode) {
auto file_path = base_path() / name;
//Create file
int fd = ::creat(file_path.c_str(), mode);
CHECK_RETVAL(fd);
::close(fd);
return make_unique<FuseFile>(device(), path() / name);
}
unique_ptr<FuseDir> FuseDir::createDir(const string &name, mode_t mode) {
auto dir_path = base_path() / name;
//Create dir
int retval = ::mkdir(dir_path.c_str(), mode);
CHECK_RETVAL(retval);
return make_unique<FuseDir>(device(), path() / name);
}
void FuseDir::rmdir() {
int retval = ::rmdir(base_path().c_str());
CHECK_RETVAL(retval);
}
unique_ptr<vector<string>> FuseDir::children() const {
DIR *dir = ::opendir(base_path().c_str());
if (dir == nullptr) {
throw FuseErrnoException(errno);
}
// Set errno=0 so we can detect whether it changed later
errno = 0;
auto result = make_unique<vector<string>>();
struct dirent *entry = ::readdir(dir);
while(entry != nullptr) {
result->push_back(entry->d_name);
entry = ::readdir(dir);
}
//On error, ::readdir returns nullptr and sets errno.
if (errno != 0) {
int readdir_errno = errno;
::closedir(dir);
throw FuseErrnoException(readdir_errno);
}
int retval = ::closedir(dir);
CHECK_RETVAL(retval);
return result;
}
} /* namespace fusepp */

31
src/fusepp/FuseDir.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#ifndef FUSEPP_FUSEDIR_H_
#define FUSEPP_FUSEDIR_H_
#include <fusepp/FuseNode.h>
#include <memory>
#include <string>
#include "utils/macros.h"
namespace fusepp {
class FuseDevice;
class FuseDir: public FuseNode {
public:
FuseDir(FuseDevice *device, const bf::path &path);
virtual ~FuseDir();
std::unique_ptr<FuseFile> createFile(const std::string &name, mode_t mode);
std::unique_ptr<FuseDir> createDir(const std::string &name, mode_t mode);
void rmdir();
std::unique_ptr<std::vector<std::string>> children() const;
private:
DISALLOW_COPY_AND_ASSIGN(FuseDir);
};
} /* namespace fusepp */
#endif /* FUSEPP_FUSEDIR_H_ */

View File

@ -0,0 +1,23 @@
#include <fusepp/FuseErrnoException.h>
#include <cstring>
#include <cassert>
#include <string>
using std::string;
using std::runtime_error;
namespace fusepp {
FuseErrnoException::FuseErrnoException(int errno_)
:runtime_error(strerror(errno_)), _errno(errno_) {
assert(_errno != 0);
}
FuseErrnoException::~FuseErrnoException() {
}
int FuseErrnoException::getErrno() const {
return _errno;
}
} /* namespace fusepp */

View File

@ -0,0 +1,28 @@
#pragma once
#ifndef FUSEPP_FUSEERRNOEXCEPTION_H_
#define FUSEPP_FUSEERRNOEXCEPTION_H_
#include <stdexcept>
#include <errno.h>
namespace fusepp {
class FuseErrnoException: public std::runtime_error {
public:
FuseErrnoException(int errno_);
virtual ~FuseErrnoException();
int getErrno() const;
private:
int _errno;
};
inline void CHECK_RETVAL(int retval) {
if (retval < 0) {
throw FuseErrnoException(errno);
}
}
} /* namespace fusepp */
#endif /* FUSEPP_FUSEERRNOEXCEPTION_H_ */

32
src/fusepp/FuseFile.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <fusepp/FuseErrnoException.h>
#include <fusepp/FuseFile.h>
#include <fusepp/FuseOpenFile.h>
using std::unique_ptr;
using std::make_unique;
namespace fusepp {
FuseFile::FuseFile(FuseDevice *device, const bf::path &path)
:FuseNode(device, path) {
assert(bf::is_regular_file(base_path()));
}
FuseFile::~FuseFile() {
}
std::unique_ptr<FuseOpenFile> FuseFile::open(int flags) const {
return make_unique<FuseOpenFile>(device(), path(), flags);
}
void FuseFile::truncate(off_t size) const {
int retval = ::truncate(base_path().c_str(), size);
CHECK_RETVAL(retval);
}
void FuseFile::unlink() {
int retval = ::unlink(base_path().c_str());
CHECK_RETVAL(retval);
}
} /* namespace fusepp */

28
src/fusepp/FuseFile.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#ifndef FUSEPP_FUSEFILE_H_
#define FUSEPP_FUSEFILE_H_
#include <fusepp/FuseNode.h>
#include <memory>
#include "utils/macros.h"
namespace fusepp {
class FuseDevice;
class FuseOpenFile;
class FuseFile: public FuseNode {
public:
FuseFile(FuseDevice *device, const bf::path &path);
virtual ~FuseFile();
std::unique_ptr<FuseOpenFile> open(int flags) const;
void truncate(off_t size) const;
void unlink();
private:
DISALLOW_COPY_AND_ASSIGN(FuseFile);
};
} /* namespace fusepp */
#endif /* FUSEPP_FUSEFILE_H_ */

40
src/fusepp/FuseNode.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <fusepp/FuseDevice.h>
#include <fusepp/FuseErrnoException.h>
#include <fusepp/FuseNode.h>
#include <sys/time.h>
namespace fusepp {
FuseNode::FuseNode(FuseDevice *device, const bf::path &path)
:_device(device), _path(path) {
}
FuseNode::~FuseNode() {
}
void FuseNode::stat(struct ::stat *result) const {
int retval = ::lstat(base_path().c_str(), result);
CHECK_RETVAL(retval);
}
void FuseNode::access(int mask) const {
int retval = ::access(base_path().c_str(), mask);
CHECK_RETVAL(retval);
}
void FuseNode::rename(const bf::path &to) {
auto new_base_path = device()->RootDir() / to;
int retval = ::rename(base_path().c_str(), new_base_path.c_str());
CHECK_RETVAL(retval);
_path = to;
}
void FuseNode::utimens(const timespec times[2]) {
struct timeval timevals[2];
TIMESPEC_TO_TIMEVAL(&timevals[0], &times[0]);
TIMESPEC_TO_TIMEVAL(&timevals[1], &times[1]);
::lutimes(base_path().c_str(), timevals);
}
} /* namespace fusepp */

56
src/fusepp/FuseNode.h Normal file
View File

@ -0,0 +1,56 @@
#pragma once
#ifndef FUSEPP_FUSENODE_H_
#define FUSEPP_FUSENODE_H_
#include <boost/filesystem.hpp>
#include <fusepp/FuseDevice.h>
#include "utils/macros.h"
#include <sys/stat.h>
namespace fusepp {
namespace bf = boost::filesystem;
class FuseNode {
public:
FuseNode(FuseDevice *device, const bf::path &path);
virtual ~FuseNode();
void stat(struct ::stat *result) const;
void access(int mask) const;
void rename(const bf::path &to);
void utimens(const timespec times[2]);
protected:
bf::path base_path() const;
const bf::path &path() const;
FuseDevice *device();
const FuseDevice *device() const;
private:
FuseDevice *const _device;
bf::path _path;
DISALLOW_COPY_AND_ASSIGN(FuseNode);
};
inline bf::path FuseNode::base_path() const {
return _device->RootDir() / _path;
}
inline const bf::path &FuseNode::path() const {
return _path;
}
inline FuseDevice *FuseNode::device() {
return const_cast<FuseDevice*>(const_cast<const FuseNode*>(this)->device());
}
inline const FuseDevice *FuseNode::device() const {
return _device;
}
} /* namespace fusepp */
#endif /* FUSEPP_FUSENODE_H_ */

View File

@ -0,0 +1,55 @@
#include <sys/types.h>
#include <fcntl.h>
#include <fusepp/FuseDevice.h>
#include <fusepp/FuseErrnoException.h>
#include <fusepp/FuseOpenFile.h>
using namespace fusepp;
FuseOpenFile::FuseOpenFile(const FuseDevice *device, const bf::path &path, int flags)
:_descriptor(::open((device->RootDir() / path).c_str(), flags)) {
CHECK_RETVAL(_descriptor);
}
FuseOpenFile::~FuseOpenFile() {
int retval = close(_descriptor);
CHECK_RETVAL(retval);
}
void FuseOpenFile::stat(struct ::stat *result) const {
int retval = ::fstat(_descriptor, result);
CHECK_RETVAL(retval);
}
void FuseOpenFile::truncate(off_t size) const {
int retval = ::ftruncate(_descriptor, size);
CHECK_RETVAL(retval);
}
int FuseOpenFile::read(void *buf, size_t count, off_t offset) {
//printf("Reading from real descriptor %d (%d, %d)\n", _descriptor, offset, count);
//fflush(stdout);
int retval = ::pread(_descriptor, buf, count, offset);
CHECK_RETVAL(retval);
//printf("retval: %d, count: %d\n", retval, count);
//fflush(stdout);
assert(static_cast<unsigned int>(retval) <= count);
return retval;
}
void FuseOpenFile::write(const void *buf, size_t count, off_t offset) {
int retval = ::pwrite(_descriptor, buf, count, offset);
CHECK_RETVAL(retval);
assert(static_cast<unsigned int>(retval) == count);
}
void FuseOpenFile::fsync() {
int retval = ::fsync(_descriptor);
CHECK_RETVAL(retval);
}
void FuseOpenFile::fdatasync() {
int retval = ::fdatasync(_descriptor);
CHECK_RETVAL(retval);
}

34
src/fusepp/FuseOpenFile.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#ifndef FUSEPP_FUSEOPENFILE_H_
#define FUSEPP_FUSEOPENFILE_H_
#include <boost/filesystem.hpp>
#include <sys/stat.h>
#include "utils/macros.h"
namespace fusepp {
class FuseDevice;
namespace bf = boost::filesystem;
class FuseOpenFile {
public:
FuseOpenFile(const FuseDevice *device, const bf::path &path, int flags);
virtual ~FuseOpenFile();
void stat(struct ::stat *result) const;
void truncate(off_t size) const;
int read(void *buf, size_t count, off_t offset);
void write(const void *buf, size_t count, off_t offset);
void fsync();
void fdatasync();
private:
int _descriptor;
DISALLOW_COPY_AND_ASSIGN(FuseOpenFile);
};
}
#endif /* FUSEPP_FUSEOPENFILE_H_ */

View File

@ -0,0 +1,25 @@
#include <fusepp/FuseFile.h>
#include <fusepp/FuseOpenFile.h>
#include <fusepp/FuseOpenFileList.h>
using namespace fusepp;
FuseOpenFileList::~FuseOpenFileList() {
}
FuseOpenFileList::FuseOpenFileList()
:_open_files() {
}
int FuseOpenFileList::open(const FuseFile &file, int flags) {
return _open_files.add(file.open(flags));
}
FuseOpenFile *FuseOpenFileList::get(int descriptor) {
return _open_files.get(descriptor);
}
void FuseOpenFileList::close(int descriptor) {
//The destructor of the stored FuseOpenFile closes the file
_open_files.remove(descriptor);
}

View File

@ -0,0 +1,29 @@
#pragma once
#ifndef FUSEPP_FUSEOPENFILELIST_H_
#define FUSEPP_FUSEOPENFILELIST_H_
#include "utils/macros.h"
#include "IdList.h"
namespace fusepp {
class FuseOpenFile;
class FuseFile;
class FuseOpenFileList {
public:
FuseOpenFileList();
virtual ~FuseOpenFileList();
int open(const FuseFile &rhs, int flags);
FuseOpenFile *get(int descriptor);
void close(int descriptor);
private:
IdList<FuseOpenFile> _open_files;
DISALLOW_COPY_AND_ASSIGN(FuseOpenFileList);
};
}
#endif /* FUSEPP_FUSEOPENFILELIST_H_ */

5
src/fusepp/IdList.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "IdList.h"
namespace fusepp {
} /* namespace fusepp */

68
src/fusepp/IdList.h Normal file
View File

@ -0,0 +1,68 @@
#pragma once
#ifndef FUSEPP_IDLIST_H_
#define FUSEPP_IDLIST_H_
#include <map>
#include <memory>
#include <mutex>
#include "utils/macros.h"
namespace fusepp {
template<class Entry>
class IdList {
public:
IdList();
virtual ~IdList();
int add(std::unique_ptr<Entry> entry);
Entry *get(int id);
const Entry *get(int id) const;
void remove(int id);
private:
std::map<int, std::unique_ptr<Entry>> _entries;
int _id_counter;
mutable std::mutex _mutex;
DISALLOW_COPY_AND_ASSIGN(IdList<Entry>)
};
template<class Entry>
IdList<Entry>::IdList()
: _entries(), _id_counter(0), _mutex() {
}
template<class Entry>
IdList<Entry>::~IdList() {
}
template<class Entry>
int IdList<Entry>::add(std::unique_ptr<Entry> entry) {
std::lock_guard<std::mutex> lock(_mutex);
//TODO Reuse IDs (ids = descriptors)
int new_id = ++_id_counter;
_entries[new_id] = std::move(entry);
return new_id;
}
template<class Entry>
Entry *IdList<Entry>::get(int id) {
return const_cast<Entry*>(const_cast<const IdList<Entry>*>(this)->get(id));
}
template<class Entry>
const Entry *IdList<Entry>::get(int id) const {
std::lock_guard<std::mutex> lock(_mutex);
const Entry *result = _entries.at(id).get();
return result;
}
template<class Entry>
void IdList<Entry>::remove(int id) {
std::lock_guard<std::mutex> lock(_mutex);
_entries.erase(id);
}
} /* namespace fusepp */
#endif /* FUSEPP_IDLIST_H_ */

View File

@ -1,7 +1,7 @@
#pragma once
#ifndef CRYFS_LIB_FUSEPP_PARAMS_H_
#define CRYFS_LIB_FUSEPP_PARAMS_H_
#ifndef FUSEPP_PARAMS_H_
#define FUSEPP_PARAMS_H_
#define FUSE_USE_VERSION 26
#endif /* CRYFS_LIB_FUSEPP_PARAMS_H_ */
#endif /* FUSEPP_PARAMS_H_ */

View File

@ -0,0 +1,9 @@
#pragma once
#ifndef FUSEPP_UTILS_MACROS_H_
#define FUSEPP_UTILS_MACROS_H_
#define DISALLOW_COPY_AND_ASSIGN(Class) \
Class(const Class &rhs) = delete; \
Class &operator=(const Class &rhs) = delete;
#endif /* FUSEPP_UTILS_MACROS_H_ */

View File

@ -0,0 +1,19 @@
#pragma once
#ifndef FUSEPP_UTILS_POINTER_H_
#define FUSEPP_UTILS_POINTER_H_
#include <memory>
namespace fusepp {
template<typename DST, typename SRC>
inline std::unique_ptr<DST> dynamic_pointer_move(std::unique_ptr<SRC> &source) {
DST *casted = dynamic_cast<DST*>(source.get());
if (casted != nullptr) {
source.release();
}
return std::unique_ptr<DST>(casted);
}
}
#endif