diff --git a/fs_interface/Device.h b/fs_interface/Device.h index e2cb8acf..2899909d 100644 --- a/fs_interface/Device.h +++ b/fs_interface/Device.h @@ -3,7 +3,7 @@ #define FSPP_DEVICE_H_ #include -#include +#include #include namespace fspp { @@ -14,7 +14,7 @@ public: virtual ~Device() {} virtual void statfs(const boost::filesystem::path &path, struct ::statvfs *fsstat) = 0; - virtual std::unique_ptr Load(const boost::filesystem::path &path) = 0; + virtual boost::optional> Load(const boost::filesystem::path &path) = 0; }; } diff --git a/fs_interface/Dir.h b/fs_interface/Dir.h index e1f5b02a..fe07455d 100644 --- a/fs_interface/Dir.h +++ b/fs_interface/Dir.h @@ -3,7 +3,7 @@ #define FSPP_DIR_H_ #include "Node.h" -#include +#include #include namespace fspp { @@ -26,13 +26,13 @@ public: std::string name; }; - virtual std::unique_ptr createAndOpenFile(const std::string &name, mode_t mode, uid_t uid, gid_t gid) = 0; + virtual cpputils::unique_ref createAndOpenFile(const std::string &name, mode_t mode, uid_t uid, gid_t gid) = 0; virtual void createDir(const std::string &name, mode_t mode, uid_t uid, gid_t gid) = 0; virtual void createSymlink(const std::string &name, const boost::filesystem::path &target, uid_t uid, gid_t gid) = 0; //TODO Allow alternative implementation returning only children names without more information //virtual std::unique_ptr> children() const = 0; - virtual std::unique_ptr> children() const = 0; + virtual cpputils::unique_ref> children() const = 0; }; } diff --git a/fs_interface/File.h b/fs_interface/File.h index 368429e6..6d085d29 100644 --- a/fs_interface/File.h +++ b/fs_interface/File.h @@ -3,7 +3,7 @@ #define FSPP_FILE_H_ #include "Node.h" -#include +#include namespace fspp { class Device; @@ -13,7 +13,7 @@ class File: public virtual Node { public: virtual ~File() {} - virtual std::unique_ptr open(int flags) const = 0; + virtual cpputils::unique_ref open(int flags) const = 0; virtual void truncate(off_t size) const = 0; }; diff --git a/fs_interface/Symlink.h b/fs_interface/Symlink.h index c7f912b0..5c3a11da 100644 --- a/fs_interface/Symlink.h +++ b/fs_interface/Symlink.h @@ -3,7 +3,7 @@ #define FSPP_SYMLINK_H_ #include "Node.h" -#include +#include #include namespace fspp { diff --git a/fstest/testutils/FileSystemTest.h b/fstest/testutils/FileSystemTest.h index d5a9dfbb..d60a3d58 100644 --- a/fstest/testutils/FileSystemTest.h +++ b/fstest/testutils/FileSystemTest.h @@ -2,10 +2,10 @@ #define MESSMER_FSPP_FSTEST_TESTUTILS_FILESYSTEMTEST_H_ #include -#include #include #include -#include +#include +#include #include "../../fs_interface/Device.h" #include "../../fs_interface/Dir.h" @@ -14,7 +14,7 @@ class FileSystemTestFixture { public: - virtual std::unique_ptr createDevice() = 0; + virtual cpputils::unique_ref createDevice() = 0; }; template @@ -28,22 +28,24 @@ public: FileSystemTest(): fixture(), device(fixture.createDevice()) {} ConcreteFileSystemTestFixture fixture; - std::unique_ptr device; + cpputils::unique_ref device; static constexpr mode_t MODE_PUBLIC = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH; - std::unique_ptr LoadDir(const boost::filesystem::path &path) { + cpputils::unique_ref LoadDir(const boost::filesystem::path &path) { auto loaded = device->Load(path); - auto dir = cpputils::dynamic_pointer_move(loaded); - EXPECT_NE(nullptr, dir.get()); - return dir; + EXPECT_NE(boost::none, loaded); + auto dir = cpputils::dynamic_pointer_move(*loaded); + EXPECT_NE(boost::none, dir); + return std::move(*dir); } - std::unique_ptr LoadFile(const boost::filesystem::path &path) { + cpputils::unique_ref LoadFile(const boost::filesystem::path &path) { auto loaded = device->Load(path); - auto file = cpputils::dynamic_pointer_move(loaded); - EXPECT_NE(nullptr, file.get()); - return file; + EXPECT_NE(boost::none, loaded); + auto file = cpputils::dynamic_pointer_move(*loaded); + EXPECT_NE(boost::none, file); + return std::move(*file); } }; diff --git a/fstest/testutils/FileTest.h b/fstest/testutils/FileTest.h index 54bb428c..614b5830 100644 --- a/fstest/testutils/FileTest.h +++ b/fstest/testutils/FileTest.h @@ -3,19 +3,18 @@ #include "FileSystemTest.h" #include - -#include +#include template class FileTest: public FileSystemTest { public: FileTest() { this->LoadDir("/")->createAndOpenFile("myfile", this->MODE_PUBLIC, 0, 0); - file_root = this->LoadFile("/myfile"); + file_root = cpputils::to_unique_ptr(this->LoadFile("/myfile")); this->LoadDir("/")->createDir("mydir", this->MODE_PUBLIC, 0, 0); this->LoadDir("/mydir")->createAndOpenFile("mynestedfile", this->MODE_PUBLIC, 0, 0); - file_nested = this->LoadFile("/mydir/mynestedfile"); + file_nested = cpputils::to_unique_ptr(this->LoadFile("/mydir/mynestedfile")); } std::unique_ptr file_root; std::unique_ptr file_nested; diff --git a/fuse/Filesystem.h b/fuse/Filesystem.h index b9e6eb67..d89bd2ac 100644 --- a/fuse/Filesystem.h +++ b/fuse/Filesystem.h @@ -3,7 +3,7 @@ #define FSPP_IMPL_FILESYSTEM_H_ #include -#include +#include #include #include #include "../fs_interface/Dir.h" @@ -40,7 +40,7 @@ public: virtual void utimens(const boost::filesystem::path &path, const timespec times[2]) = 0; virtual void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) = 0; //TODO We shouldn't use Dir::Entry here, that's in another layer - virtual std::unique_ptr> readDir(const boost::filesystem::path &path) = 0; + virtual cpputils::unique_ref> readDir(const boost::filesystem::path &path) = 0; //TODO Test createSymlink virtual void createSymlink(const boost::filesystem::path &to, const boost::filesystem::path &from, uid_t uid, gid_t gid) = 0; //TODO Test readSymlink diff --git a/impl/FilesystemImpl.cpp b/impl/FilesystemImpl.cpp index c8d35286..ebcffa5e 100644 --- a/impl/FilesystemImpl.cpp +++ b/impl/FilesystemImpl.cpp @@ -1,6 +1,5 @@ #include "FilesystemImpl.h" -#include #include #include "../fs_interface/Device.h" #include "../fs_interface/Dir.h" @@ -10,15 +9,15 @@ #include "../fs_interface/File.h" -#include "messmer/cpp-utils/pointer.h" +#include using namespace fspp; using cpputils::dynamic_pointer_move; - -using std::unique_ptr; -using std::make_unique; +using cpputils::unique_ref; +using cpputils::make_unique_ref; using std::vector; using std::string; +using boost::none; namespace bf = boost::filesystem; @@ -29,31 +28,40 @@ FilesystemImpl::FilesystemImpl(Device *device) FilesystemImpl::~FilesystemImpl() { } -unique_ptr FilesystemImpl::LoadFile(const bf::path &path) { +unique_ref FilesystemImpl::LoadFile(const bf::path &path) { auto node = _device->Load(path); - auto file = dynamic_pointer_move(node); - if (!file) { + if (node == none) { + throw fuse::FuseErrnoException(EIO); + } + auto file = dynamic_pointer_move(*node); + if (file == none) { throw fuse::FuseErrnoException(EISDIR); } - return file; + return std::move(*file); } -unique_ptr FilesystemImpl::LoadDir(const bf::path &path) { +unique_ref FilesystemImpl::LoadDir(const bf::path &path) { auto node = _device->Load(path); - auto dir = dynamic_pointer_move(node); - if (!dir) { + if (node == none) { + throw fuse::FuseErrnoException(EIO); + } + auto dir = dynamic_pointer_move(*node); + if (dir == none) { throw fuse::FuseErrnoException(ENOTDIR); } - return dir; + return std::move(*dir); } -unique_ptr FilesystemImpl::LoadSymlink(const bf::path &path) { +unique_ref FilesystemImpl::LoadSymlink(const bf::path &path) { auto node = _device->Load(path); - auto dir = dynamic_pointer_move(node); - if (!dir) { + if (node == none) { + throw fuse::FuseErrnoException(EIO); + } + auto lnk = dynamic_pointer_move(*node); + if (lnk == none) { throw fuse::FuseErrnoException(ENOTDIR); } - return dir; + return std::move(*lnk); } int FilesystemImpl::openFile(const bf::path &path, int flags) { @@ -74,7 +82,12 @@ void FilesystemImpl::closeFile(int descriptor) { } void FilesystemImpl::lstat(const bf::path &path, struct ::stat *stbuf) { - _device->Load(path)->stat(stbuf); + auto node = _device->Load(path); + if(node == none) { + throw fuse::FuseErrnoException(ENOENT); + } else { + (*node)->stat(stbuf); + } } void FilesystemImpl::fstat(int descriptor, struct ::stat *stbuf) { @@ -82,11 +95,21 @@ void FilesystemImpl::fstat(int descriptor, struct ::stat *stbuf) { } void FilesystemImpl::chmod(const boost::filesystem::path &path, mode_t mode) { - _device->Load(path)->chmod(mode); + auto node = _device->Load(path); + if(node == none) { + throw fuse::FuseErrnoException(ENOENT); + } else { + (*node)->chmod(mode); + } } void FilesystemImpl::chown(const boost::filesystem::path &path, uid_t uid, gid_t gid) { - _device->Load(path)->chown(uid, gid); + auto node = _device->Load(path); + if(node == none) { + throw fuse::FuseErrnoException(ENOENT); + } else { + (*node)->chown(uid, gid); + } } void FilesystemImpl::truncate(const bf::path &path, off_t size) { @@ -114,7 +137,12 @@ void FilesystemImpl::fdatasync(int descriptor) { } void FilesystemImpl::access(const bf::path &path, int mask) { - _device->Load(path)->access(mask); + auto node = _device->Load(path); + if(node == none) { + throw fuse::FuseErrnoException(ENOENT); + } else { + (*node)->access(mask); + } } int FilesystemImpl::createAndOpenFile(const bf::path &path, mode_t mode, uid_t uid, gid_t gid) { @@ -142,17 +170,25 @@ void FilesystemImpl::unlink(const bf::path &path) { void FilesystemImpl::rename(const bf::path &from, const bf::path &to) { auto node = _device->Load(from); - node->rename(to); + if(node == none) { + throw fuse::FuseErrnoException(ENOENT); + } else { + (*node)->rename(to); + } } -unique_ptr> FilesystemImpl::readDir(const bf::path &path) { +unique_ref> FilesystemImpl::readDir(const bf::path &path) { auto dir = LoadDir(path); return dir->children(); } void FilesystemImpl::utimens(const bf::path &path, const timespec times[2]) { auto node = _device->Load(path); - node->utimens(times); + if(node == none) { + throw fuse::FuseErrnoException(ENOENT); + } else { + (*node)->utimens(times); + } } void FilesystemImpl::statfs(const bf::path &path, struct statvfs *fsstat) { diff --git a/impl/FilesystemImpl.h b/impl/FilesystemImpl.h index 1e209051..f4ba41da 100644 --- a/impl/FilesystemImpl.h +++ b/impl/FilesystemImpl.h @@ -5,7 +5,7 @@ #include "FuseOpenFileList.h" #include "../fuse/Filesystem.h" -#include "messmer/cpp-utils/macros.h" +#include namespace fspp { class Node; @@ -37,16 +37,16 @@ public: void rmdir(const boost::filesystem::path &path) override; void unlink(const boost::filesystem::path &path) override; void rename(const boost::filesystem::path &from, const boost::filesystem::path &to) override; - std::unique_ptr> readDir(const boost::filesystem::path &path) override; + cpputils::unique_ref> readDir(const boost::filesystem::path &path) override; void utimens(const boost::filesystem::path &path, const timespec times[2]) override; void statfs(const boost::filesystem::path &path, struct statvfs *fsstat) override; void createSymlink(const boost::filesystem::path &to, const boost::filesystem::path &from, uid_t uid, gid_t gid) override; void readSymlink(const boost::filesystem::path &path, char *buf, size_t size) override; private: - std::unique_ptr LoadFile(const boost::filesystem::path &path); - std::unique_ptr LoadDir(const boost::filesystem::path &path); - std::unique_ptr LoadSymlink(const boost::filesystem::path &path); + cpputils::unique_ref LoadFile(const boost::filesystem::path &path); + cpputils::unique_ref LoadDir(const boost::filesystem::path &path); + cpputils::unique_ref LoadSymlink(const boost::filesystem::path &path); int openFile(const File &file, int flags); Device *_device; diff --git a/impl/FuseOpenFileList.h b/impl/FuseOpenFileList.h index f0f3e41c..85bbf6db 100644 --- a/impl/FuseOpenFileList.h +++ b/impl/FuseOpenFileList.h @@ -14,7 +14,7 @@ public: FuseOpenFileList(); virtual ~FuseOpenFileList(); - int open(std::unique_ptr file); + int open(cpputils::unique_ref file); OpenFile *get(int descriptor); void close(int descriptor); @@ -31,7 +31,7 @@ inline FuseOpenFileList::FuseOpenFileList() inline FuseOpenFileList::~FuseOpenFileList() { } -inline int FuseOpenFileList::open(std::unique_ptr file) { +inline int FuseOpenFileList::open(cpputils::unique_ref file) { return _open_files.add(std::move(file)); } diff --git a/impl/IdList.h b/impl/IdList.h index ceaef16a..4b4ddace 100644 --- a/impl/IdList.h +++ b/impl/IdList.h @@ -3,10 +3,9 @@ #define FSPP_FUSE_IDLIST_H_ #include -#include #include #include -#include "messmer/cpp-utils/macros.h" +#include namespace fspp { @@ -16,12 +15,12 @@ public: IdList(); virtual ~IdList(); - int add(std::unique_ptr entry); + int add(cpputils::unique_ref entry); Entry *get(int id); const Entry *get(int id) const; void remove(int id); private: - std::map> _entries; + std::map> _entries; int _id_counter; mutable std::mutex _mutex; @@ -38,11 +37,11 @@ IdList::~IdList() { } template -int IdList::add(std::unique_ptr entry) { +int IdList::add(cpputils::unique_ref entry) { std::lock_guard lock(_mutex); //TODO Reuse IDs (ids = descriptors) int new_id = ++_id_counter; - _entries[new_id] = std::move(entry); + _entries.insert(std::make_pair(new_id, std::move(entry))); return new_id; } diff --git a/test/fuse/read/FuseReadReturnedDataTest.cpp b/test/fuse/read/FuseReadReturnedDataTest.cpp index 02c91827..57db6aa6 100644 --- a/test/fuse/read/FuseReadReturnedDataTest.cpp +++ b/test/fuse/read/FuseReadReturnedDataTest.cpp @@ -2,6 +2,7 @@ #include #include "../../testutils/InMemoryFile.h" #include "testutils/FuseReadTest.h" +#include #include "../../../fuse/FuseErrnoException.h" @@ -51,7 +52,9 @@ public: std::unique_ptr testFile; TestData testData; - FuseReadReturnedDataTest(): testFile(nullptr), testData(GetParam()) { + FuseReadReturnedDataTest() + : testFile(nullptr), + testData(GetParam()) { testFile = make_unique(DataFixture::generate(testData.fileSize())); ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize()); OnOpenReturnFileDescriptor(FILENAME, 0); diff --git a/test/fuse/readDir/FuseReadDirDirnameTest.cpp b/test/fuse/readDir/FuseReadDirDirnameTest.cpp index de48c9c8..c0556c7d 100644 --- a/test/fuse/readDir/FuseReadDirDirnameTest.cpp +++ b/test/fuse/readDir/FuseReadDirDirnameTest.cpp @@ -4,8 +4,6 @@ using ::testing::_; using ::testing::StrEq; using ::testing::Return; -using std::make_unique; -using std::unique_ptr; using std::vector; using std::string; diff --git a/test/fuse/readDir/FuseReadDirErrorTest.cpp b/test/fuse/readDir/FuseReadDirErrorTest.cpp index d21804df..70082f52 100644 --- a/test/fuse/readDir/FuseReadDirErrorTest.cpp +++ b/test/fuse/readDir/FuseReadDirErrorTest.cpp @@ -8,8 +8,6 @@ using ::testing::Throw; using ::testing::WithParamInterface; using ::testing::Values; -using std::make_unique; -using std::unique_ptr; using std::vector; using std::string; diff --git a/test/fuse/readDir/FuseReadDirReturnTest.cpp b/test/fuse/readDir/FuseReadDirReturnTest.cpp index 0a9dddcb..358eb1dc 100644 --- a/test/fuse/readDir/FuseReadDirReturnTest.cpp +++ b/test/fuse/readDir/FuseReadDirReturnTest.cpp @@ -1,5 +1,5 @@ #include "testutils/FuseReadDirTest.h" - +#include #include "../../../fuse/FuseErrnoException.h" using ::testing::_; @@ -8,16 +8,16 @@ using ::testing::Throw; using ::testing::WithParamInterface; using ::testing::Values; -using std::make_unique; -using std::unique_ptr; +using cpputils::unique_ref; +using cpputils::make_unique_ref; using std::vector; using std::string; using namespace fspp::fuse; using fspp::Dir; -unique_ptr> LARGE_DIR(int num_entries) { - auto result = make_unique>(); +unique_ref> LARGE_DIR(int num_entries) { + auto result = make_unique_ref>(); result->reserve(num_entries); for(int i=0; ipush_back("File "+std::to_string(i)+" file"); diff --git a/test/fuse/readDir/testutils/FuseReadDirTest.cpp b/test/fuse/readDir/testutils/FuseReadDirTest.cpp index 92aaff1a..0fe0723a 100644 --- a/test/fuse/readDir/testutils/FuseReadDirTest.cpp +++ b/test/fuse/readDir/testutils/FuseReadDirTest.cpp @@ -1,7 +1,7 @@ #include "FuseReadDirTest.h" -using std::unique_ptr; -using std::make_unique; +using cpputils::unique_ref; +using cpputils::make_unique_ref; using std::vector; using std::string; using std::initializer_list; @@ -9,12 +9,12 @@ using std::initializer_list; using ::testing::Action; using ::testing::Return; -unique_ptr> FuseReadDirTest::ReadDir(const char *dirname) { +unique_ref> FuseReadDirTest::ReadDir(const char *dirname) { auto fs = TestFS(); DIR *dir = openDir(fs.get(), dirname); - auto result = make_unique>(); + auto result = make_unique_ref>(); readDirEntries(dir, result.get()); closeDir(dir); return result; @@ -30,7 +30,7 @@ int FuseReadDirTest::ReadDirReturnError(const char *dirname) { return errno; } - auto result = make_unique>(); + auto result = make_unique_ref>(); int error = readDirEntriesAllowError(dir, result.get()); closeDir(dir); return error; diff --git a/test/fuse/readDir/testutils/FuseReadDirTest.h b/test/fuse/readDir/testutils/FuseReadDirTest.h index de6c0b95..01e108e5 100644 --- a/test/fuse/readDir/testutils/FuseReadDirTest.h +++ b/test/fuse/readDir/testutils/FuseReadDirTest.h @@ -10,7 +10,7 @@ class FuseReadDirTest: public FuseTest { public: const char *DIRNAME = "/mydir"; - std::unique_ptr> ReadDir(const char *dirname); + cpputils::unique_ref> ReadDir(const char *dirname); int ReadDirReturnError(const char *dirname); static ::testing::Action*(const char*)> ReturnDirEntries(std::vector entries); diff --git a/test/fuse/write/FuseWriteDataTest.cpp b/test/fuse/write/FuseWriteDataTest.cpp index 2139e382..7d382fd1 100644 --- a/test/fuse/write/FuseWriteDataTest.cpp +++ b/test/fuse/write/FuseWriteDataTest.cpp @@ -49,7 +49,9 @@ public: std::unique_ptr testFile; TestData testData; - FuseWriteDataTest(): testFile(nullptr), testData(GetParam()) { + FuseWriteDataTest() + : testFile(nullptr), + testData(GetParam()) { testFile = make_unique(DataFixture::generate(testData.fileSize(), 1)); ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize()); OnOpenReturnFileDescriptor(FILENAME, 0); diff --git a/test/impl/FuseOpenFileListTest.cpp b/test/impl/FuseOpenFileListTest.cpp index 8b783820..20be1ebd 100644 --- a/test/impl/FuseOpenFileListTest.cpp +++ b/test/impl/FuseOpenFileListTest.cpp @@ -5,8 +5,7 @@ #include -using std::unique_ptr; -using std::make_unique; +using cpputils::make_unique_ref; using namespace fspp; @@ -37,7 +36,7 @@ struct FuseOpenFileListTest: public ::testing::Test { FuseOpenFileList list; int open(int fileid, int flags) { - return list.open(make_unique(fileid, flags)); + return list.open(make_unique_ref(fileid, flags)); } int open() { return open(FILEID1, FILEID2); diff --git a/test/impl/IdListTest.cpp b/test/impl/IdListTest.cpp index e782d78a..5e68e89a 100644 --- a/test/impl/IdListTest.cpp +++ b/test/impl/IdListTest.cpp @@ -3,7 +3,7 @@ #include "../../impl/IdList.h" #include -using std::make_unique; +using cpputils::make_unique_ref; using namespace fspp; @@ -20,7 +20,7 @@ struct IdListTest: public ::testing::Test { IdList list; int add(int num) { - return list.add(make_unique(num)); + return list.add(make_unique_ref(num)); } int add() { return add(OBJ1); diff --git a/test/testutils/FuseTest.cpp b/test/testutils/FuseTest.cpp index 85b09329..1676eabd 100644 --- a/test/testutils/FuseTest.cpp +++ b/test/testutils/FuseTest.cpp @@ -7,8 +7,8 @@ using ::testing::Throw; using ::testing::Action; using ::testing::Invoke; -using std::unique_ptr; -using std::make_unique; +using cpputils::unique_ref; +using cpputils::make_unique_ref; namespace bf = boost::filesystem; @@ -44,8 +44,8 @@ FuseTest::FuseTest(): fsimpl() { ON_CALL(fsimpl, readSymlink(_,_,_)).WillByDefault(defaultAction); } -unique_ptr FuseTest::TestFS() { - return make_unique(&fsimpl); +unique_ref FuseTest::TestFS() { + return make_unique_ref(&fsimpl); } FuseTest::TempTestFS::TempTestFS(MockFilesystem *fsimpl): _mountDir(), _fuse(fsimpl), _fuse_thread(&_fuse) { diff --git a/test/testutils/FuseTest.h b/test/testutils/FuseTest.h index 2b558319..ec1a4381 100644 --- a/test/testutils/FuseTest.h +++ b/test/testutils/FuseTest.h @@ -64,8 +64,8 @@ public: return rename(from.c_str(), to.c_str()); } MOCK_METHOD2(rename, void(const char*, const char*)); - std::unique_ptr> readDir(const boost::filesystem::path &path) { - return std::unique_ptr>(readDir(path.c_str())); + cpputils::unique_ref> readDir(const boost::filesystem::path &path) { + return std::move(cpputils::nullcheck(std::unique_ptr>(readDir(path.c_str()))).get()); } MOCK_METHOD1(readDir, std::vector*(const char*)); void utimens(const boost::filesystem::path &path, const timespec ts[2]) override { @@ -100,7 +100,7 @@ public: FuseThread _fuse_thread; }; - std::unique_ptr TestFS(); + cpputils::unique_ref TestFS(); MockFilesystem fsimpl;