This commit is contained in:
Sebastian Messmer 2014-11-12 12:27:23 +01:00
parent 8182a45d65
commit c112def985
7 changed files with 46 additions and 9 deletions

View File

@ -80,12 +80,14 @@ int CryFuse::mknod(const path &path, mode_t mode, dev_t rdev) {
return 0; return 0;
} }
//TODO
int CryFuse::mkdir(const path &path, mode_t mode) { int CryFuse::mkdir(const path &path, mode_t mode) {
//printf("mkdir(%s, %d)\n", path.c_str(), mode); //printf("mkdir(%s, %d)\n", path.c_str(), mode);
auto real_path = _device->RootDir() / path; try {
int retstat = ::mkdir(real_path.c_str(), mode); _device->mkdir(path, mode);
return errcode_map(retstat); return 0;
} catch(cryfs::CryErrnoException &e) {
return -e.getErrno();
}
} }
//TODO //TODO

View File

@ -52,7 +52,11 @@ unique_ptr<CryDir> CryDevice::LoadDir(const bf::path &path) {
int CryDevice::openFile(const bf::path &path, int flags) { int CryDevice::openFile(const bf::path &path, int flags) {
auto file = LoadFile(path); auto file = LoadFile(path);
return _open_files.open(*file, flags); return openFile(*file, flags);
}
int CryDevice::openFile(const CryFile &file, int flags) {
return _open_files.open(file, flags);
} }
void CryDevice::closeFile(int descriptor) { void CryDevice::closeFile(int descriptor) {
@ -99,6 +103,11 @@ int CryDevice::createAndOpenFile(const bf::path &path, mode_t mode) {
//TODO Creating the file opens and closes it. We then reopen it afterwards. //TODO Creating the file opens and closes it. We then reopen it afterwards.
// This is slow. Improve! // This is slow. Improve!
auto dir = LoadDir(path.parent_path()); auto dir = LoadDir(path.parent_path());
dir->createFile(path.filename().native(), mode); auto file = dir->createFile(path.filename().native(), mode);
return openFile(path, O_CREAT | O_WRONLY | O_TRUNC); return openFile(*file, O_CREAT | 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);
} }

View File

@ -34,12 +34,14 @@ public:
void fdatasync(int descriptor); void fdatasync(int descriptor);
void access(const bf::path &path, int mask); void access(const bf::path &path, int mask);
int createAndOpenFile(const bf::path &path, mode_t mode); int createAndOpenFile(const bf::path &path, mode_t mode);
void mkdir(const bf::path &path, mode_t mode);
const bf::path &RootDir() const; const bf::path &RootDir() const;
private: private:
std::unique_ptr<CryNode> Load(const bf::path &path); std::unique_ptr<CryNode> Load(const bf::path &path);
std::unique_ptr<CryFile> LoadFile(const bf::path &path); std::unique_ptr<CryFile> LoadFile(const bf::path &path);
std::unique_ptr<CryDir> LoadDir(const bf::path &path); std::unique_ptr<CryDir> LoadDir(const bf::path &path);
int openFile(const CryFile &file, int flags);
const bf::path _rootdir; const bf::path _rootdir;
CryOpenFileList _open_files; CryOpenFileList _open_files;

View File

@ -5,26 +5,38 @@
#include <fcntl.h> #include <fcntl.h>
#include "CryDevice.h" #include "CryDevice.h"
#include "CryFile.h"
#include "CryErrnoException.h" #include "CryErrnoException.h"
using std::string; using std::string;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique;
namespace cryfs { namespace cryfs {
CryDir::CryDir(CryDevice *device, const bf::path &path) CryDir::CryDir(CryDevice *device, const bf::path &path)
:CryNode(device, path) { :CryNode(device, path) {
assert(bf::is_directory(base_path()));
} }
CryDir::~CryDir() { CryDir::~CryDir() {
} }
void CryDir::createFile(const string &name, mode_t mode) { unique_ptr<CryFile> CryDir::createFile(const string &name, mode_t mode) {
auto file_path = base_path() / name; auto file_path = base_path() / name;
//Create file //Create file
int fd = ::creat(file_path.c_str(), mode); int fd = ::creat(file_path.c_str(), mode);
CHECK_RETVAL(fd); CHECK_RETVAL(fd);
::close(fd); ::close(fd);
return make_unique<CryFile>(device(), path() / name);
}
unique_ptr<CryDir> CryDir::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);
} }
} /* namespace cryfs */ } /* namespace cryfs */

View File

@ -16,7 +16,8 @@ public:
CryDir(CryDevice *device, const bf::path &path); CryDir(CryDevice *device, const bf::path &path);
virtual ~CryDir(); virtual ~CryDir();
void createFile(const std::string &name, mode_t mode); std::unique_ptr<CryFile> createFile(const std::string &name, mode_t mode);
std::unique_ptr<CryDir> createDir(const std::string &name, mode_t mode);
private: private:
DISALLOW_COPY_AND_ASSIGN(CryDir); DISALLOW_COPY_AND_ASSIGN(CryDir);
}; };

View File

@ -10,6 +10,7 @@ namespace cryfs {
CryFile::CryFile(CryDevice *device, const bf::path &path) CryFile::CryFile(CryDevice *device, const bf::path &path)
:CryNode(device, path) { :CryNode(device, path) {
assert(bf::is_regular_file(base_path()));
} }
CryFile::~CryFile() { CryFile::~CryFile() {

View File

@ -22,6 +22,8 @@ public:
protected: protected:
bf::path base_path() const; bf::path base_path() const;
const bf::path &path() const;
CryDevice *device();
private: private:
CryDevice *const _device; CryDevice *const _device;
@ -34,6 +36,14 @@ inline bf::path CryNode::base_path() const {
return _device->RootDir() / _path; return _device->RootDir() / _path;
} }
inline const bf::path &CryNode::path() const {
return _path;
}
inline CryDevice *CryNode::device() {
return _device;
}
} /* namespace cryfs */ } /* namespace cryfs */
#endif /* CRYFS_LIB_CRYNODE_H_ */ #endif /* CRYFS_LIB_CRYNODE_H_ */