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

This commit is contained in:
Sebastian Messmer 2014-11-15 15:16:18 +01:00
parent 1cd2364b7f
commit d088995178
35 changed files with 522 additions and 524 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(cryfs_lib CryDevice.cpp CryDir.cpp CryErrnoException.cpp CryFile.cpp CryNode.cpp CryOpenFile.cpp CryOpenFileList.cpp IdList.cpp)
add_library(cryfs_lib )
target_link_libraries(cryfs_lib boost_filesystem)
target_link_libraries(cryfs_lib fusepp)

View File

@ -1,145 +0,0 @@
#include "../cryfs_lib/CryDevice.h"
#include <memory>
#include <fcntl.h>
#include "CryDir.h"
#include "CryFile.h"
#include "CryOpenFile.h"
#include "CryErrnoException.h"
#include "utils/pointer.h"
using namespace cryfs;
using std::unique_ptr;
using std::make_unique;
using std::vector;
using std::string;
CryDevice::CryDevice(const bf::path &rootdir)
:_rootdir(rootdir), _open_files() {
}
CryDevice::~CryDevice() {
}
unique_ptr<CryNode> CryDevice::Load(const bf::path &path) {
auto real_path = RootDir() / path;
if(bf::is_directory(real_path)) {
return make_unique<CryDir>(this, path);
} else if(bf::is_regular_file(real_path)) {
return make_unique<CryFile>(this, path);
}
throw CryErrnoException(ENOENT);
}
unique_ptr<CryFile> CryDevice::LoadFile(const bf::path &path) {
auto node = Load(path);
auto file = dynamic_pointer_move<CryFile>(node);
if (!file) {
throw CryErrnoException(EISDIR);
}
return file;
}
unique_ptr<CryDir> CryDevice::LoadDir(const bf::path &path) {
auto node = Load(path);
auto dir = dynamic_pointer_move<CryDir>(node);
if (!dir) {
throw CryErrnoException(ENOTDIR);
}
return dir;
}
int CryDevice::openFile(const bf::path &path, int flags) {
auto file = LoadFile(path);
return openFile(*file, flags);
}
int CryDevice::openFile(const CryFile &file, int flags) {
return _open_files.open(file, flags);
}
void CryDevice::closeFile(int descriptor) {
_open_files.close(descriptor);
}
void CryDevice::lstat(const bf::path &path, struct ::stat *stbuf) {
Load(path)->stat(stbuf);
}
void CryDevice::fstat(int descriptor, struct ::stat *stbuf) {
_open_files.get(descriptor)->stat(stbuf);
}
void CryDevice::truncate(const bf::path &path, off_t size) {
LoadFile(path)->truncate(size);
}
void CryDevice::ftruncate(int descriptor, off_t size) {
_open_files.get(descriptor)->truncate(size);
}
int CryDevice::read(int descriptor, void *buf, size_t count, off_t offset) {
return _open_files.get(descriptor)->read(buf, count, offset);
}
void CryDevice::write(int descriptor, const void *buf, size_t count, off_t offset) {
_open_files.get(descriptor)->write(buf, count, offset);
}
void CryDevice::fsync(int descriptor) {
_open_files.get(descriptor)->fsync();
}
void CryDevice::fdatasync(int descriptor) {
_open_files.get(descriptor)->fdatasync();
}
void CryDevice::access(const bf::path &path, int mask) {
Load(path)->access(mask);
}
int CryDevice::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 CryDevice::mkdir(const bf::path &path, mode_t mode) {
auto dir = LoadDir(path.parent_path());
dir->createDir(path.filename().native(), mode);
}
void CryDevice::rmdir(const bf::path &path) {
auto dir = LoadDir(path);
dir->rmdir();
}
void CryDevice::unlink(const bf::path &path) {
auto file = LoadFile(path);
file->unlink();
}
void CryDevice::rename(const bf::path &from, const bf::path &to) {
auto node = Load(from);
node->rename(to);
}
unique_ptr<vector<string>> CryDevice::readDir(const bf::path &path) {
auto dir = LoadDir(path);
return dir->children();
}
void CryDevice::utimens(const bf::path &path, const timespec times[2]) {
auto node = Load(path);
node->utimens(times);
}
void CryDevice::statfs(const bf::path &path, struct statvfs *fsstat) {
int retval = ::statvfs(path.c_str(), fsstat);
CHECK_RETVAL(retval);
}

View File

@ -1,32 +0,0 @@
#pragma once
#ifndef CRYFS_LIB_CRYDIR_H_
#define CRYFS_LIB_CRYDIR_H_
#include <memory>
#include <string>
#include "CryNode.h"
#include "utils/macros.h"
namespace cryfs {
class CryDevice;
class CryOpenDir;
class CryDir: public CryNode {
public:
CryDir(CryDevice *device, const bf::path &path);
virtual ~CryDir();
std::unique_ptr<CryFile> createFile(const std::string &name, mode_t mode);
std::unique_ptr<CryDir> createDir(const std::string &name, mode_t mode);
void rmdir();
std::unique_ptr<std::vector<std::string>> children() const;
private:
DISALLOW_COPY_AND_ASSIGN(CryDir);
};
} /* namespace cryfs */
#endif /* CRYFS_LIB_CRYDIR_H_ */

View File

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

View File

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

View File

@ -1,33 +0,0 @@
#include "CryFile.h"
#include "CryOpenFile.h"
#include "CryErrnoException.h"
using std::unique_ptr;
using std::make_unique;
namespace cryfs {
CryFile::CryFile(CryDevice *device, const bf::path &path)
:CryNode(device, path) {
assert(bf::is_regular_file(base_path()));
}
CryFile::~CryFile() {
}
std::unique_ptr<CryOpenFile> CryFile::open(int flags) const {
return make_unique<CryOpenFile>(device(), path(), flags);
}
void CryFile::truncate(off_t size) const {
int retval = ::truncate(base_path().c_str(), size);
CHECK_RETVAL(retval);
}
void CryFile::unlink() {
int retval = ::unlink(base_path().c_str());
CHECK_RETVAL(retval);
}
} /* namespace cryfs */

View File

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

View File

@ -1,56 +0,0 @@
#pragma once
#ifndef CRYFS_LIB_CRYNODE_H_
#define CRYFS_LIB_CRYNODE_H_
#include <boost/filesystem.hpp>
#include "utils/macros.h"
#include "CryDevice.h"
#include <sys/stat.h>
namespace cryfs {
namespace bf = boost::filesystem;
class CryNode {
public:
CryNode(CryDevice *device, const bf::path &path);
virtual ~CryNode();
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;
CryDevice *device();
const CryDevice *device() const;
private:
CryDevice *const _device;
bf::path _path;
DISALLOW_COPY_AND_ASSIGN(CryNode);
};
inline bf::path CryNode::base_path() const {
return _device->RootDir() / _path;
}
inline const bf::path &CryNode::path() const {
return _path;
}
inline CryDevice *CryNode::device() {
return const_cast<CryDevice*>(const_cast<const CryNode*>(this)->device());
}
inline const CryDevice *CryNode::device() const {
return _device;
}
} /* namespace cryfs */
#endif /* CRYFS_LIB_CRYNODE_H_ */

View File

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

View File

@ -1,28 +0,0 @@
#ifndef CRYFS_LIB_CRYOPENFILELIST_H_
#define CRYFS_LIB_CRYOPENFILELIST_H_
#include "utils/macros.h"
#include "IdList.h"
namespace cryfs {
class CryOpenFile;
class CryFile;
class CryOpenFileList {
public:
CryOpenFileList();
virtual ~CryOpenFileList();
int open(const CryFile &rhs, int flags);
CryOpenFile *get(int descriptor);
void close(int descriptor);
private:
IdList<CryOpenFile> _open_files;
DISALLOW_COPY_AND_ASSIGN(CryOpenFileList);
};
}
#endif /* CRYFS_LIB_CRYOPENFILELIST_H_ */

View File

@ -1,5 +0,0 @@
#include <cryfs_lib/IdList.h>
namespace cryfs {
} /* namespace cryfs */

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);
}

View File

@ -1,27 +1,27 @@
#pragma once
#ifndef CRYFS_LIB_CRYDEVICE_H_
#define CRYFS_LIB_CRYDEVICE_H_
#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"
#include "CryOpenFileList.h"
namespace cryfs {
class CryNode;
class CryFile;
class CryOpenFile;
class CryDir;
namespace fusepp {
class FuseNode;
class FuseFile;
class FuseOpenFile;
class FuseDir;
namespace bf = boost::filesystem;
class CryDevice {
class FuseDevice {
public:
CryDevice(const bf::path &rootdir);
virtual ~CryDevice();
FuseDevice(const bf::path &rootdir);
virtual ~FuseDevice();
int openFile(const bf::path &path, int flags);
void closeFile(int descriptor);
@ -45,20 +45,20 @@ public:
const bf::path &RootDir() const;
private:
std::unique_ptr<CryNode> Load(const bf::path &path);
std::unique_ptr<CryFile> LoadFile(const bf::path &path);
std::unique_ptr<CryDir> LoadDir(const bf::path &path);
int openFile(const CryFile &file, int flags);
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;
CryOpenFileList _open_files;
FuseOpenFileList _open_files;
DISALLOW_COPY_AND_ASSIGN(CryDevice);
DISALLOW_COPY_AND_ASSIGN(FuseDevice);
};
inline const bf::path &CryDevice::RootDir() const {
inline const bf::path &FuseDevice::RootDir() const {
return _rootdir;
}
}
#endif /* CRYFS_LIB_CRYDEVICE_H_ */
#endif /* FUSEPP_FUSEDEVICE_H_ */

View File

@ -1,55 +1,54 @@
#include "CryDir.h"
#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>
#include "CryDevice.h"
#include "CryFile.h"
#include "CryErrnoException.h"
using std::string;
using std::unique_ptr;
using std::make_unique;
using std::vector;
namespace cryfs {
namespace fusepp {
CryDir::CryDir(CryDevice *device, const bf::path &path)
:CryNode(device, path) {
FuseDir::FuseDir(FuseDevice *device, const bf::path &path)
:FuseNode(device, path) {
assert(bf::is_directory(base_path()));
}
CryDir::~CryDir() {
FuseDir::~FuseDir() {
}
unique_ptr<CryFile> CryDir::createFile(const string &name, mode_t mode) {
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<CryFile>(device(), path() / name);
return make_unique<FuseFile>(device(), path() / name);
}
unique_ptr<CryDir> CryDir::createDir(const string &name, mode_t mode) {
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<CryDir>(device(), path() / name);
return make_unique<FuseDir>(device(), path() / name);
}
void CryDir::rmdir() {
void FuseDir::rmdir() {
int retval = ::rmdir(base_path().c_str());
CHECK_RETVAL(retval);
}
unique_ptr<vector<string>> CryDir::children() const {
unique_ptr<vector<string>> FuseDir::children() const {
DIR *dir = ::opendir(base_path().c_str());
if (dir == nullptr) {
throw CryErrnoException(errno);
throw FuseErrnoException(errno);
}
// Set errno=0 so we can detect whether it changed later
@ -66,11 +65,11 @@ unique_ptr<vector<string>> CryDir::children() const {
if (errno != 0) {
int readdir_errno = errno;
::closedir(dir);
throw CryErrnoException(readdir_errno);
throw FuseErrnoException(readdir_errno);
}
int retval = ::closedir(dir);
CHECK_RETVAL(retval);
return result;
}
} /* namespace cryfs */
} /* 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_ */

View File

@ -1,41 +1,40 @@
#include "CryNode.h"
#include <fusepp/FuseDevice.h>
#include <fusepp/FuseErrnoException.h>
#include <fusepp/FuseNode.h>
#include <sys/time.h>
#include "CryDevice.h"
#include "CryErrnoException.h"
namespace cryfs {
namespace fusepp {
CryNode::CryNode(CryDevice *device, const bf::path &path)
FuseNode::FuseNode(FuseDevice *device, const bf::path &path)
:_device(device), _path(path) {
}
CryNode::~CryNode() {
FuseNode::~FuseNode() {
}
void CryNode::stat(struct ::stat *result) const {
void FuseNode::stat(struct ::stat *result) const {
int retval = ::lstat(base_path().c_str(), result);
CHECK_RETVAL(retval);
}
void CryNode::access(int mask) const {
void FuseNode::access(int mask) const {
int retval = ::access(base_path().c_str(), mask);
CHECK_RETVAL(retval);
}
void CryNode::rename(const bf::path &to) {
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 CryNode::utimens(const timespec times[2]) {
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 cryfs */
} /* 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

@ -1,34 +1,33 @@
#include <cryfs_lib/CryOpenFile.h>
#include <sys/types.h>
#include <fcntl.h>
#include <fusepp/FuseDevice.h>
#include <fusepp/FuseErrnoException.h>
#include <fusepp/FuseOpenFile.h>
#include "CryErrnoException.h"
#include "CryDevice.h"
using namespace cryfs;
using namespace fusepp;
CryOpenFile::CryOpenFile(const CryDevice *device, const bf::path &path, int flags)
FuseOpenFile::FuseOpenFile(const FuseDevice *device, const bf::path &path, int flags)
:_descriptor(::open((device->RootDir() / path).c_str(), flags)) {
CHECK_RETVAL(_descriptor);
}
CryOpenFile::~CryOpenFile() {
FuseOpenFile::~FuseOpenFile() {
int retval = close(_descriptor);
CHECK_RETVAL(retval);
}
void CryOpenFile::stat(struct ::stat *result) const {
void FuseOpenFile::stat(struct ::stat *result) const {
int retval = ::fstat(_descriptor, result);
CHECK_RETVAL(retval);
}
void CryOpenFile::truncate(off_t size) const {
void FuseOpenFile::truncate(off_t size) const {
int retval = ::ftruncate(_descriptor, size);
CHECK_RETVAL(retval);
}
int CryOpenFile::read(void *buf, size_t count, off_t offset) {
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);
@ -39,18 +38,18 @@ int CryOpenFile::read(void *buf, size_t count, off_t offset) {
return retval;
}
void CryOpenFile::write(const void *buf, size_t count, off_t offset) {
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 CryOpenFile::fsync() {
void FuseOpenFile::fsync() {
int retval = ::fsync(_descriptor);
CHECK_RETVAL(retval);
}
void CryOpenFile::fdatasync() {
void FuseOpenFile::fdatasync() {
int retval = ::fdatasync(_descriptor);
CHECK_RETVAL(retval);
}

View File

@ -1,20 +1,21 @@
#ifndef CRYFS_LIB_CRYOPENFILE_H_
#define CRYFS_LIB_CRYOPENFILE_H_
#pragma once
#ifndef FUSEPP_FUSEOPENFILE_H_
#define FUSEPP_FUSEOPENFILE_H_
#include <boost/filesystem.hpp>
#include <sys/stat.h>
#include "utils/macros.h"
namespace cryfs {
class CryDevice;
namespace fusepp {
class FuseDevice;
namespace bf = boost::filesystem;
class CryOpenFile {
class FuseOpenFile {
public:
CryOpenFile(const CryDevice *device, const bf::path &path, int flags);
virtual ~CryOpenFile();
FuseOpenFile(const FuseDevice *device, const bf::path &path, int flags);
virtual ~FuseOpenFile();
void stat(struct ::stat *result) const;
void truncate(off_t size) const;
@ -25,9 +26,9 @@ public:
private:
int _descriptor;
DISALLOW_COPY_AND_ASSIGN(CryOpenFile);
DISALLOW_COPY_AND_ASSIGN(FuseOpenFile);
};
}
#endif /* CRYFS_LIB_CRYOPENFILE_H_ */
#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 */

View File

@ -1,12 +1,13 @@
#ifndef CRYFS_LIB_IDLIST_H_
#define CRYFS_LIB_IDLIST_H_
#pragma once
#ifndef FUSEPP_IDLIST_H_
#define FUSEPP_IDLIST_H_
#include <map>
#include <memory>
#include <mutex>
#include "utils/macros.h"
namespace cryfs {
namespace fusepp {
template<class Entry>
class IdList {
@ -62,6 +63,6 @@ void IdList<Entry>::remove(int id) {
_entries.erase(id);
}
} /* namespace cryfs */
} /* namespace fusepp */
#endif /* CRYFS_LIB_IDLIST_H_ */
#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

@ -1,9 +1,9 @@
#pragma once
#ifndef CRYFS_LIB_UTILS_MACROS_H_
#define CRYFS_LIB_UTILS_MACROS_H_
#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 /* CRYFS_LIB_UTILS_MACROS_H_ */
#endif /* FUSEPP_UTILS_MACROS_H_ */

View File

@ -1,10 +1,11 @@
#pragma once
#ifndef CRYFS_LIB_UTILS_POINTER_H_
#define CRYFS_LIB_UTILS_POINTER_H_
#ifndef FUSEPP_UTILS_POINTER_H_
#define FUSEPP_UTILS_POINTER_H_
#include <memory>
namespace cryfs {
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());

View File

@ -8,8 +8,8 @@
int main (int argc, char *argv[])
{
printf("Version: %d\n", buildconfig::VERSION::MAJOR);
cryfs::CryDevice device(fusepp::path("/home/heinzi/cryfstest/root"));
cryfs::CryFuse fuse(&device);
fusepp::FuseDevice device(fusepp::path("/home/heinzi/cryfstest/root"));
fusepp::CryFuse fuse(&device);
fuse.run(argc, argv);
return 0;
}