Simplify readdir

This commit is contained in:
Sebastian Messmer 2014-11-13 00:03:03 +01:00
parent 27057f8c82
commit 1cd2364b7f
10 changed files with 44 additions and 166 deletions

View File

@ -260,21 +260,19 @@ int CryFuse::fsync(const path &path, int datasync, fuse_file_info *fileinfo) {
}
int CryFuse::opendir(const path &path, fuse_file_info *fileinfo) {
UNUSED(path);
UNUSED(fileinfo);
//printf("opendir(%s, _)\n", path.c_str());
try {
fileinfo->fh = _device->openDir(path);
//We don't need opendir, because readdir works directly on the path
return 0;
} catch(CryErrnoException &e) {
return -e.getErrno();
}
}
int CryFuse::readdir(const path &path, void *buf, fuse_fill_dir_t filler, off_t offset, fuse_file_info *fileinfo) {
UNUSED(path);
UNUSED(fileinfo);
//printf("readdir(%s, _, _, %zu, _)\n", path.c_str(), offset);
UNUSED(offset);
try {
auto entries = _device->readDir(fileinfo->fh);
auto entries = _device->readDir(path);
for (const auto &entry : *entries) {
//We could pass file metadata to filler() in its third parameter,
//but it doesn't help performance since fuse seems to ignore it.
@ -290,14 +288,11 @@ int CryFuse::readdir(const path &path, void *buf, fuse_fill_dir_t filler, off_t
}
int CryFuse::releasedir(const path &path, fuse_file_info *fileinfo) {
//printf("releasedir(%s, _)\n", path.c_str());
UNUSED(path);
try {
_device->closeDir(fileinfo->fh);
UNUSED(fileinfo);
//printf("releasedir(%s, _)\n", path.c_str());
//We don't need releasedir, because readdir works directly on the path
return 0;
} catch (CryErrnoException &e) {
return -e.getErrno();
}
}
//TODO

View File

@ -1,3 +1,3 @@
add_library(cryfs_lib CryDevice.cpp CryDir.cpp CryErrnoException.cpp CryFile.cpp CryNode.cpp CryOpenFile.cpp CryOpenDir.cpp CryOpenFileList.cpp CryOpenDirList.cpp IdList.cpp)
add_library(cryfs_lib CryDevice.cpp CryDir.cpp CryErrnoException.cpp CryFile.cpp CryNode.cpp CryOpenFile.cpp CryOpenFileList.cpp IdList.cpp)
target_link_libraries(cryfs_lib boost_filesystem)

View File

@ -9,8 +9,6 @@
#include "CryErrnoException.h"
#include "utils/pointer.h"
#include "CryOpenDir.h"
using namespace cryfs;
using std::unique_ptr;
@ -19,7 +17,7 @@ using std::vector;
using std::string;
CryDevice::CryDevice(const bf::path &rootdir)
:_rootdir(rootdir), _open_files(), _open_dirs() {
:_rootdir(rootdir), _open_files() {
}
CryDevice::~CryDevice() {
@ -131,17 +129,9 @@ void CryDevice::rename(const bf::path &from, const bf::path &to) {
node->rename(to);
}
int CryDevice::openDir(const bf::path &path) {
unique_ptr<vector<string>> CryDevice::readDir(const bf::path &path) {
auto dir = LoadDir(path);
return _open_dirs.open(*dir);
}
unique_ptr<vector<string>> CryDevice::readDir(int descriptor) {
return _open_dirs.get(descriptor)->readdir();
}
void CryDevice::closeDir(int descriptor) {
_open_dirs.close(descriptor);
return dir->children();
}
void CryDevice::utimens(const bf::path &path, const timespec times[2]) {

View File

@ -9,7 +9,6 @@
#include "utils/macros.h"
#include "CryOpenFileList.h"
#include "CryOpenDirList.h"
namespace cryfs {
class CryNode;
@ -40,9 +39,7 @@ public:
void rmdir(const bf::path &path);
void unlink(const bf::path &path);
void rename(const bf::path &from, const bf::path &to);
int openDir(const bf::path &path);
std::unique_ptr<std::vector<std::string>> readDir(int descriptor);
void closeDir(int descriptor);
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);
@ -54,7 +51,6 @@ private:
int openFile(const CryFile &file, int flags);
const bf::path _rootdir;
CryOpenFileList _open_files;
CryOpenDirList _open_dirs;
DISALLOW_COPY_AND_ASSIGN(CryDevice);
};

View File

@ -3,15 +3,16 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include "CryDevice.h"
#include "CryOpenDir.h"
#include "CryFile.h"
#include "CryErrnoException.h"
using std::string;
using std::unique_ptr;
using std::make_unique;
using std::vector;
namespace cryfs {
@ -45,8 +46,31 @@ void CryDir::rmdir() {
CHECK_RETVAL(retval);
}
unique_ptr<CryOpenDir> CryDir::opendir() const {
return make_unique<CryOpenDir>(device(), path());
unique_ptr<vector<string>> CryDir::children() const {
DIR *dir = ::opendir(base_path().c_str());
if (dir == nullptr) {
throw CryErrnoException(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 CryErrnoException(readdir_errno);
}
int retval = ::closedir(dir);
CHECK_RETVAL(retval);
return result;
}
} /* namespace cryfs */

View File

@ -21,7 +21,8 @@ public:
std::unique_ptr<CryDir> createDir(const std::string &name, mode_t mode);
void rmdir();
std::unique_ptr<CryOpenDir> opendir() const;
std::unique_ptr<std::vector<std::string>> children() const;
private:
DISALLOW_COPY_AND_ASSIGN(CryDir);
};

View File

@ -1,44 +0,0 @@
#include <cryfs_lib/CryOpenDir.h>
#include "CryDevice.h"
#include "CryErrnoException.h"
using std::unique_ptr;
using std::make_unique;
using std::vector;
using std::string;
namespace cryfs {
CryOpenDir::CryOpenDir(const CryDevice *device, const bf::path &path)
:_dir(::opendir((device->RootDir() / path).c_str())) {
if (_dir == nullptr) {
throw CryErrnoException(errno);
}
}
CryOpenDir::~CryOpenDir() {
int retval = ::closedir(_dir);
CHECK_RETVAL(retval);
}
unique_ptr<vector<string>> CryOpenDir::readdir() const {
::rewinddir(_dir);
// 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) {
throw CryErrnoException(errno);
}
return result;
}
} /* namespace cryfs */

View File

@ -1,31 +0,0 @@
#ifndef CRYFS_LIB_CRYOPENDIR_H_
#define CRYFS_LIB_CRYOPENDIR_H_
#include <boost/filesystem.hpp>
#include <memory>
#include <vector>
#include <string>
#include <dirent.h>
#include "utils/macros.h"
namespace cryfs {
class CryDevice;
namespace bf = boost::filesystem;
class CryOpenDir {
public:
CryOpenDir(const CryDevice *device, const bf::path &path);
virtual ~CryOpenDir();
std::unique_ptr<std::vector<std::string>> readdir() const;
private:
DIR *_dir;
DISALLOW_COPY_AND_ASSIGN(CryOpenDir);
};
} /* namespace cryfs */
#endif /* CRYFS_LIB_CRYOPENDIR_H_ */

View File

@ -1,25 +0,0 @@
#include <cryfs_lib/CryOpenDirList.h>
#include "CryDir.h"
#include "CryOpenDir.h"
using namespace cryfs;
CryOpenDirList::~CryOpenDirList() {
}
CryOpenDirList::CryOpenDirList()
:_open_dirs() {
}
int CryOpenDirList::open(const CryDir &dir) {
return _open_dirs.add(dir.opendir());
}
CryOpenDir *CryOpenDirList::get(int descriptor) {
return _open_dirs.get(descriptor);
}
void CryOpenDirList::close(int descriptor) {
//The destructor of the stored CryOpenDir closes the dir
_open_dirs.remove(descriptor);
}

View File

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