From 9741fb3f5cc1981ff472c0a0e53276c38b673be9 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 11 Mar 2015 00:22:36 +0100 Subject: [PATCH] - ReadDir also returns whether the entry is a file or a directory - OpenFileList has a simpler interface --- fs_interface/Dir.h | 6 +++--- fuse/Fuse.cpp | 8 +++++++- impl/FilesystemImpl.cpp | 6 +++--- impl/FuseOpenFileList.h | 6 +++--- test/impl/FuseOpenFileListTest.cpp | 18 +----------------- 5 files changed, 17 insertions(+), 27 deletions(-) diff --git a/fs_interface/Dir.h b/fs_interface/Dir.h index f495fc75..f7054570 100644 --- a/fs_interface/Dir.h +++ b/fs_interface/Dir.h @@ -8,7 +8,7 @@ namespace fspp { class Device; -class File; +class OpenFile; class Dir: public virtual Node { public: @@ -25,8 +25,8 @@ public: std::string name; }; - virtual std::unique_ptr createFile(const std::string &name, mode_t mode) = 0; - virtual std::unique_ptr createDir(const std::string &name, mode_t mode) = 0; + virtual std::unique_ptr createAndOpenFile(const std::string &name, mode_t mode) = 0; + virtual void createDir(const std::string &name, mode_t mode) = 0; virtual void rmdir() = 0; //TODO Allow alternative implementation returning only children names without more information diff --git a/fuse/Fuse.cpp b/fuse/Fuse.cpp index f15ac122..240fa372 100644 --- a/fuse/Fuse.cpp +++ b/fuse/Fuse.cpp @@ -468,11 +468,17 @@ int Fuse::readdir(const bf::path &path, void *buf, fuse_fill_dir_t filler, off_t UNUSED(offset); try { auto entries = _fs->readDir(path); + struct stat stbuf; 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. //It does getattr() calls on all entries nevertheless. - if (filler(buf, entry.name.c_str(), nullptr, 0) != 0) { + if (entry.type == Dir::EntryType::DIR) { + stbuf.st_mode = S_IFDIR | S_IRUSR | S_IXUSR | S_IWUSR; + } else { + stbuf.st_mode = S_IFREG | S_IRUSR | S_IXUSR | S_IWUSR; + } + if (filler(buf, entry.name.c_str(), &stbuf, 0) != 0) { return -ENOMEM; } } diff --git a/impl/FilesystemImpl.cpp b/impl/FilesystemImpl.cpp index e0ca905a..5905c5e7 100644 --- a/impl/FilesystemImpl.cpp +++ b/impl/FilesystemImpl.cpp @@ -52,7 +52,7 @@ int FilesystemImpl::openFile(const bf::path &path, int flags) { } int FilesystemImpl::openFile(const File &file, int flags) { - return _open_files.open(file, flags); + return _open_files.open(file.open(flags)); } void FilesystemImpl::flush(int descriptor) { @@ -103,8 +103,8 @@ int FilesystemImpl::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); + auto file = dir->createAndOpenFile(path.filename().native(), mode); + return _open_files.open(std::move(file)); } void FilesystemImpl::mkdir(const bf::path &path, mode_t mode) { diff --git a/impl/FuseOpenFileList.h b/impl/FuseOpenFileList.h index 1d2a23cf..f0f3e41c 100644 --- a/impl/FuseOpenFileList.h +++ b/impl/FuseOpenFileList.h @@ -14,7 +14,7 @@ public: FuseOpenFileList(); virtual ~FuseOpenFileList(); - int open(const File &rhs, int flags); + int open(std::unique_ptr file); OpenFile *get(int descriptor); void close(int descriptor); @@ -31,8 +31,8 @@ inline FuseOpenFileList::FuseOpenFileList() inline FuseOpenFileList::~FuseOpenFileList() { } -inline int FuseOpenFileList::open(const File &file, int flags) { - return _open_files.add(file.open(flags)); +inline int FuseOpenFileList::open(std::unique_ptr file) { + return _open_files.add(std::move(file)); } inline OpenFile *FuseOpenFileList::get(int descriptor) { diff --git a/test/impl/FuseOpenFileListTest.cpp b/test/impl/FuseOpenFileListTest.cpp index f4a5794d..19865f2e 100644 --- a/test/impl/FuseOpenFileListTest.cpp +++ b/test/impl/FuseOpenFileListTest.cpp @@ -27,22 +27,6 @@ public: MOCK_METHOD0(fdatasync, void()); }; -class MockFile: public File { -public: - MockFile(int id_): id(id_) {} - int id; - - unique_ptr open(int flags) const override { - return make_unique(id, flags); - } - MOCK_CONST_METHOD1(truncate, void(off_t)); - MOCK_METHOD0(unlink, void()); - MOCK_CONST_METHOD1(stat, void(struct ::stat*)); - MOCK_CONST_METHOD1(access, void(int)); - MOCK_METHOD1(rename, void(const boost::filesystem::path &)); - MOCK_METHOD1(utimens, void(const timespec[2])); -}; - struct FuseOpenFileListTest: public ::testing::Test { const int FILEID1 = 4; const int FLAGS1 = 5; @@ -53,7 +37,7 @@ struct FuseOpenFileListTest: public ::testing::Test { FuseOpenFileList list; int open(int fileid, int flags) { - return list.open(MockFile(fileid), flags); + return list.open(make_unique(fileid, flags)); } int open() { return open(FILEID1, FILEID2);