diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.cpp b/deps/ox/src/ox/fs/filesystem/filesystem.cpp index 750e4172..105e2b93 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.cpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.cpp @@ -63,18 +63,6 @@ Error FileSystem::read(const FileAddress &addr, std::size_t readStart, std::size } } -Error FileSystem::remove(const FileAddress &addr, bool recursive) noexcept { - switch (addr.type()) { - case FileAddressType::Inode: - return remove(addr.getInode().value, recursive); - case FileAddressType::ConstPath: - case FileAddressType::Path: - return remove(StringView(addr.getPath().value), recursive); - default: - return ox::Error(1); - } -} - Error FileSystem::write(const FileAddress &addr, const void *buffer, uint64_t size, FileType fileType) noexcept { switch (addr.type()) { case FileAddressType::Inode: diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp index e785e4b1..ebc83217 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -57,9 +57,9 @@ class FileSystem { virtual Result> ls(StringViewCR dir) const noexcept = 0; - virtual Error remove(StringViewCR path, bool recursive) noexcept = 0; - - Error remove(const FileAddress &addr, bool recursive = false) noexcept; + Error remove(StringViewCR path, bool recursive = false) noexcept { + return removePath(path, recursive); + } virtual Error resize(uint64_t size, void *buffer) noexcept = 0; @@ -142,6 +142,8 @@ class FileSystem { virtual Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept = 0; + virtual Error removePath(StringViewCR path, bool recursive) noexcept = 0; + virtual Error writeFilePath(StringViewCR path, const void *buffer, uint64_t size, FileType fileType) noexcept = 0; virtual Error writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept = 0; @@ -209,6 +211,8 @@ class FileSystemTemplate: public MemFS { Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override; + Error removePath(StringViewCR path, bool recursive) noexcept override; + Result directAccessInode(uint64_t) const noexcept override; Result> ls(StringViewCR dir) const noexcept override; @@ -216,8 +220,6 @@ class FileSystemTemplate: public MemFS { template Error ls(StringViewCR path, F cb) const; - Error remove(StringViewCR path, bool recursive) noexcept override; - /** * Resizes FileSystem to minimum possible size. */ @@ -356,6 +358,25 @@ Error FileSystemTemplate::readFileInodeRange(uint64_t inod return m_fs.read(inode, readStart, readSize, reinterpret_cast(buffer), size); } +template +Error FileSystemTemplate::removePath(StringViewCR path, bool recursive) noexcept { + OX_REQUIRE(fd, fileSystemData()); + Directory rootDir(m_fs, fd.rootDirInode); + OX_REQUIRE(inode, rootDir.find(path)); + OX_REQUIRE(st, statInode(inode)); + if (st.fileType == FileType::NormalFile || recursive) { + if (auto err = rootDir.remove(path)) { + // removal failed, try putting the index back + oxLogError(rootDir.write(path, inode)); + return err; + } + } else { + oxTrace("FileSystemTemplate.remove.fail", "Tried to remove directory without recursive setting."); + return ox::Error(1); + } + return ox::Error(0); +} + template Result FileSystemTemplate::directAccessInode(uint64_t inode) const noexcept { auto data = m_fs.read(inode); @@ -384,25 +405,6 @@ Error FileSystemTemplate::ls(StringViewCR path, F cb) cons return dir.ls(cb); } -template -Error FileSystemTemplate::remove(StringViewCR path, bool recursive) noexcept { - OX_REQUIRE(fd, fileSystemData()); - Directory rootDir(m_fs, fd.rootDirInode); - OX_REQUIRE(inode, rootDir.find(path)); - OX_REQUIRE(st, statInode(inode)); - if (st.fileType == FileType::NormalFile || recursive) { - if (auto err = rootDir.remove(path)) { - // removal failed, try putting the index back - oxLogError(rootDir.write(path, inode)); - return err; - } - } else { - oxTrace("FileSystemTemplate.remove.fail", "Tried to remove directory without recursive setting."); - return ox::Error(1); - } - return ox::Error(0); -} - template Error FileSystemTemplate::resize() noexcept { return m_fs.resize(); diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index dc56b9c4..4cc7aa90 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -75,14 +75,6 @@ Result> PassThroughFS::ls(StringViewCR dir) const noexcept { return out; } -Error PassThroughFS::remove(StringViewCR path, bool recursive) noexcept { - if (recursive) { - return ox::Error(std::filesystem::remove_all(m_path / stripSlash(path)) != 0); - } else { - return ox::Error(std::filesystem::remove(m_path / stripSlash(path)) != 0); - } -} - Error PassThroughFS::resize(uint64_t, void*) noexcept { // unsupported return ox::Error(1, "resize is not supported by PassThroughFS"); @@ -167,6 +159,14 @@ Error PassThroughFS::readFileInodeRange(uint64_t, std::size_t, std::size_t, void return ox::Error(1, "read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) is not supported by PassThroughFS"); } +Error PassThroughFS::removePath(StringViewCR path, bool const recursive) noexcept { + if (recursive) { + return ox::Error{std::filesystem::remove_all(m_path / stripSlash(path)) == 0}; + } else { + return ox::Error{!std::filesystem::remove(m_path / stripSlash(path))}; + } +} + Error PassThroughFS::writeFilePath(StringViewCR path, const void *buffer, uint64_t size, FileType) noexcept { const auto p = (m_path / stripSlash(path)); try { diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index 0a42d829..a424cefe 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -45,8 +45,6 @@ class PassThroughFS: public FileSystem { template Error ls(StringViewCR dir, F cb) const noexcept; - Error remove(StringViewCR path, bool recursive) noexcept override; - Error resize(uint64_t size, void *buffer) noexcept override; Result statInode(uint64_t inode) const noexcept override; @@ -75,6 +73,8 @@ class PassThroughFS: public FileSystem { Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override; + Error removePath(StringViewCR path, bool recursive) noexcept override; + Error writeFilePath(StringViewCR path, const void *buffer, uint64_t size, FileType fileType) noexcept override; Error writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept override;