#include "CryDir.h" #include #include #include #include #include "fusepp/impl/FuseErrnoException.h" #include "CryDevice.h" #include "CryFile.h" //TODO Get rid of this in favor of exception hierarchy using fusepp::CHECK_RETVAL; namespace bf = boost::filesystem; using std::unique_ptr; using std::make_unique; using std::string; using std::vector; namespace cryfs { CryDir::CryDir(CryDevice *device, const bf::path &path) :CryNode(device, path) { assert(bf::is_directory(base_path())); } CryDir::~CryDir() { } unique_ptr CryDir::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(device(), path() / name); } unique_ptr 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(device(), path() / name); } void CryDir::rmdir() { int retval = ::rmdir(base_path().c_str()); CHECK_RETVAL(retval); } unique_ptr> CryDir::children() const { DIR *dir = ::opendir(base_path().c_str()); if (dir == nullptr) { throw fusepp::FuseErrnoException(errno); } // Set errno=0 so we can detect whether it changed later errno = 0; auto result = make_unique>(); 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 fusepp::FuseErrnoException(readdir_errno); } int retval = ::closedir(dir); CHECK_RETVAL(retval); return result; } } /* namespace cryfs */