From 1cfa594c925bdea7c8d0d4ff82c9df001facab71 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 27 Jan 2023 00:00:35 -0600 Subject: [PATCH] [ox/fs] Wrap overloaded virtual functions in FileSystem --- .../src/ox/fs/filestore/filestoretemplate.hpp | 83 ++++++---- deps/ox/src/ox/fs/filesystem/filesystem.cpp | 18 +-- deps/ox/src/ox/fs/filesystem/filesystem.hpp | 144 ++++++++++++------ .../ox/src/ox/fs/filesystem/passthroughfs.cpp | 110 +++++++------ .../ox/src/ox/fs/filesystem/passthroughfs.hpp | 36 ++--- deps/ox/src/ox/fs/ptrarith/ptr.hpp | 1 - 6 files changed, 225 insertions(+), 167 deletions(-) diff --git a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp index 21346bf2..bfaee268 100644 --- a/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp +++ b/deps/ox/src/ox/fs/filestore/filestoretemplate.hpp @@ -324,7 +324,7 @@ Error FileStoreTemplate::read(InodeId_t id, void *out, FsSize_t outSize, auto srcData = m_buffer->template dataOf(src); oxTracef("ox::fs::FileStoreTemplate::read::found", "{} found at {} with data section at {}", - id, src.offset(), srcData.offset()); + id, src.offset(), srcData.offset()); oxTracef("ox::fs::FileStoreTemplate::read::outSize", "{} {} {}", srcData.offset(), srcData.size(), outSize); // error check @@ -344,40 +344,67 @@ Error FileStoreTemplate::read(InodeId_t id, void *out, FsSize_t outSize, } template -Error FileStoreTemplate::read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *data, FsSize_t *size) const { - oxRequireM(src, find(id).validate()); - oxRequireM(srcData, src->data().validate()); - oxRequire(sub, srcData.template subPtr(readStart, readSize).validate()); - memcpy(data, sub, sub.size()); - if (size) { - *size = sub.size(); +Error FileStoreTemplate::read(InodeId_t id, FsSize_t readStart, FsSize_t readSize, void *out, FsSize_t *size) const { + oxTracef("ox::fs::FileStoreTemplate::read", "Attempting to read from inode {}", id); + auto src = find(id); + // error check + if (!src.valid()) { + oxTracef("ox::fs::FileStoreTemplate::read::fail", "Could not find requested item: {}", id); + return OxError(1); } - return OxError(0); + + auto srcData = m_buffer->template dataOf(src); + oxTracef("ox::fs::FileStoreTemplate::read::found", "{} found at {} with data section at {}", + id, src.offset(), srcData.offset()); + oxTracef("ox::fs::FileStoreTemplate::read::readSize", "{} {} {}", srcData.offset(), srcData.size(), readSize); + + // error check + if (!(srcData.valid() && srcData.size() - readStart <= readSize)) { + oxTracef("ox::fs::FileStoreTemplate::read::fail", "Could not read data section of item: {}", id); + oxTracef("ox::fs::FileStoreTemplate::read::fail", + "Item data section size: {}, Expected size: {}", srcData.size(), readSize); + return OxError(1); + } + + ox_memcpy(out, srcData.get() + readStart, readSize); + if (size) { + *size = src.size(); + } + + return {}; } template template Error FileStoreTemplate::read(InodeId_t id, FsSize_t readStart, - FsSize_t readSize, T *data, FsSize_t *size) const { - oxRequireM(src, find(id).validate()); - oxRequireM(srcData, src->data().validate()); - auto sub = srcData.template subPtr(readStart, readSize); - if (sub.valid() && sub.size() % sizeof(T)) { - for (FsSize_t i = 0; i < sub.size() / sizeof(T); i++) { - // do byte-by-byte copy to ensure alignment is right when - // copying to final destination - T tmp; - for (size_t ii = 0; ii < sizeof(T); ii++) { - reinterpret_cast(&tmp)[ii] = *(sub.get() + ii); - } - *(data + i) = tmp; - } - if (size) { - *size = sub.size(); - } - return OxError(0); + FsSize_t readSize, T *out, FsSize_t *size) const { + oxTracef("ox::fs::FileStoreTemplate::read", "Attempting to read from inode {}", id); + auto src = find(id); + // error check + if (!src.valid()) { + oxTracef("ox::fs::FileStoreTemplate::read::fail", "Could not find requested item: {}", id); + return OxError(1); } - return OxError(1); + + auto srcData = m_buffer->template dataOf(src); + oxTracef("ox::fs::FileStoreTemplate::read::found", "{} found at {} with data section at {}", + id, src.offset(), srcData.offset()); + oxTracef("ox::fs::FileStoreTemplate::read::readSize", "{} {} {}", srcData.offset(), srcData.size(), readSize); + + // error check + if (!(srcData.valid() && srcData.size() - readStart <= readSize)) { + oxTracef("ox::fs::FileStoreTemplate::read::fail", "Could not read data section of item: {}", id); + oxTracef("ox::fs::FileStoreTemplate::read::fail", + "Item data section size: {}, Expected size: {}", srcData.size(), readSize); + return OxError(1); + } + + ox_memcpy(out, srcData.get() + readStart, readSize); + if (size) { + *size = src.size(); + } + + return {}; } template diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.cpp b/deps/ox/src/ox/fs/filesystem/filesystem.cpp index 5c52f107..35f5c527 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.cpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.cpp @@ -13,7 +13,7 @@ namespace ox { -Result FileSystem::directAccess(const FileAddress &addr) noexcept { +Result MemFS::directAccess(const FileAddress &addr) noexcept { switch (addr.type()) { case FileAddressType::Inode: return directAccess(addr.getInode().value); @@ -28,10 +28,10 @@ Result FileSystem::directAccess(const FileAddress &addr) noexcept { Error FileSystem::read(const FileAddress &addr, void *buffer, std::size_t size) noexcept { switch (addr.type()) { case FileAddressType::Inode: - return read(addr.getInode().value, buffer, size); + return readFileInode(addr.getInode().value, buffer, size); case FileAddressType::ConstPath: case FileAddressType::Path: - return read(StringView(addr.getPath().value), buffer, size); + return readFilePath(StringView(addr.getPath().value), buffer, size); default: return OxError(1); } @@ -45,9 +45,9 @@ Result FileSystem::read(const FileAddress &addr) noexcept { } Result FileSystem::read(CRStringView path) noexcept { - oxRequire(s, stat(path)); + oxRequire(s, statPath(path)); Buffer buff(s.size); - oxReturnError(read(path, buff.data(), buff.size())); + oxReturnError(readFilePath(path, buff.data(), buff.size())); return buff; } @@ -78,10 +78,10 @@ Error FileSystem::remove(const FileAddress &addr, bool recursive) noexcept { Error FileSystem::write(const FileAddress &addr, const void *buffer, uint64_t size, FileType fileType) noexcept { switch (addr.type()) { case FileAddressType::Inode: - return write(addr.getInode().value, buffer, size, fileType); + return writeFileInode(addr.getInode().value, buffer, size, fileType); case FileAddressType::ConstPath: case FileAddressType::Path: - return write(StringView(addr.getPath().value), buffer, size, fileType); + return writeFilePath(StringView(addr.getPath().value), buffer, size, fileType); default: return OxError(1); } @@ -90,10 +90,10 @@ Error FileSystem::write(const FileAddress &addr, const void *buffer, uint64_t si Result FileSystem::stat(const FileAddress &addr) const noexcept { switch (addr.type()) { case FileAddressType::Inode: - return stat(addr.getInode().value); + return statInode(addr.getInode().value); case FileAddressType::ConstPath: case FileAddressType::Path: - return stat(StringView(addr.getPath().value)); + return statPath(StringView(addr.getPath().value)); default: return OxError(1); } diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp index c22b61c9..18f291db 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -38,26 +38,21 @@ class FileSystem { */ virtual Error move(CRStringView src, CRStringView dest) noexcept = 0; - virtual Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept = 0; - - virtual Result directAccess(CRStringView path) noexcept = 0; - - virtual Error read(uint64_t inode, void *buffer, std::size_t size) noexcept = 0; - - virtual Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept = 0; - - virtual Result directAccess(uint64_t inode) noexcept = 0; - Error read(const FileAddress &addr, void *buffer, std::size_t size) noexcept; Result read(const FileAddress &addr) noexcept; Result read(CRStringView path) noexcept; - Error read(const FileAddress &addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept; + inline Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept { + return readFilePath(path, buffer, buffSize); + } - [[maybe_unused]] - Result directAccess(const FileAddress &addr) noexcept; + inline Error read(uint64_t inode, void *buffer, std::size_t buffSize) noexcept { + return readFileInode(inode, buffer, buffSize); + } + + Error read(const FileAddress &addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept; virtual Result> ls(CRStringView dir) const noexcept = 0; @@ -67,12 +62,8 @@ class FileSystem { virtual Error resize(uint64_t size, void *buffer) noexcept = 0; - virtual Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept = 0; - - virtual Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept = 0; - Error write(CRStringView path, const void *buffer, uint64_t size) noexcept { - return write(path, buffer, size, FileType::NormalFile); + return writeFilePath(path, buffer, size, FileType::NormalFile); } Error write(uint64_t inode, const void *buffer, uint64_t size) noexcept { @@ -81,9 +72,21 @@ class FileSystem { Error write(const FileAddress &addr, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept; - virtual Result stat(uint64_t inode) const noexcept = 0; + inline Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept { + return writeFilePath(path, buffer, size, fileType); + } - virtual Result stat(CRStringView path) const noexcept = 0; + inline Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept { + return writeFileInode(inode, buffer, size, fileType); + } + + inline Result stat(uint64_t inode) const noexcept { + return statInode(inode); + } + + inline Result stat(CRStringView path) const noexcept { + return statPath(path); + } Result stat(const FileAddress &addr) const noexcept; @@ -104,6 +107,39 @@ class FileSystem { [[nodiscard]] virtual bool valid() const noexcept = 0; + protected: + virtual Result statInode(uint64_t inode) const noexcept = 0; + + virtual Result statPath(CRStringView path) const noexcept = 0; + + virtual Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept = 0; + + virtual Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept = 0; + + virtual Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept = 0; + + virtual Error writeFilePath(CRStringView 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; + +}; + +class MemFS: public FileSystem { + public: + Result directAccess(const FileAddress &addr) noexcept; + + inline Result directAccess(CRStringView path) noexcept { + return directAccessPath(path); + } + + inline Result directAccess(uint64_t inode) noexcept { + return directAccessInode(inode); + } + + protected: + virtual Result directAccessPath(CRStringView path) noexcept = 0; + + virtual Result directAccessInode(uint64_t inode) noexcept = 0; }; /** @@ -113,7 +149,7 @@ class FileSystem { * Note: Directory parameter must have a default constructor. */ template -class FileSystemTemplate: public FileSystem { +class FileSystemTemplate: public MemFS { private: static constexpr auto InodeFsData = 2; @@ -139,15 +175,15 @@ class FileSystemTemplate: public FileSystem { Error move(CRStringView src, CRStringView dest) noexcept override; - Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept override; + Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept override; - Result directAccess(CRStringView) noexcept override; + Result directAccessPath(CRStringView) noexcept override; - Error read(uint64_t inode, void *buffer, std::size_t size) noexcept override; + Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept override; - Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override; + Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override; - Result directAccess(uint64_t) noexcept override; + Result directAccessInode(uint64_t) noexcept override; Result> ls(CRStringView dir) const noexcept override; @@ -163,14 +199,15 @@ class FileSystemTemplate: public FileSystem { Error resize(uint64_t size, void *buffer) noexcept override; - Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept override; + Error writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept override; - Error write(uint64_t inode, 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; - Result stat(uint64_t inode) const noexcept override; + Result statInode(uint64_t inode) const noexcept override; - Result stat(CRStringView path) const noexcept override; + Result statPath(CRStringView path) const noexcept override; + [[nodiscard]] uint64_t spaceNeeded(uint64_t size) const noexcept override; Result available() const noexcept override; @@ -254,33 +291,40 @@ Error FileSystemTemplate::move(CRStringView src, CRStringV } template -Error FileSystemTemplate::read(CRStringView path, void *buffer, std::size_t buffSize) noexcept { +Error FileSystemTemplate::readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept { + oxRequire(fd, fileSystemData()); + Directory rootDir(m_fs, fd.rootDirInode); + oxRequire(s, stat(path)); + if (s.size > buffSize) { + return OxError(1, "Buffer to small to load file"); + } + return readFileInodeRange(s.inode, 0, s.size, buffer, &buffSize); +} + +template +Result FileSystemTemplate::directAccessPath(CRStringView path) noexcept { oxRequire(fd, fileSystemData()); Directory rootDir(m_fs, fd.rootDirInode); oxRequire(inode, rootDir.find(path)); - return read(inode, buffer, buffSize); + return directAccessInode(inode); } template -Result FileSystemTemplate::directAccess(CRStringView path) noexcept { - oxRequire(fd, fileSystemData()); - Directory rootDir(m_fs, fd.rootDirInode); - oxRequire(inode, rootDir.find(path)); - return directAccess(inode); +Error FileSystemTemplate::readFileInode(uint64_t inode, void *buffer, std::size_t buffSize) noexcept { + oxRequire(s, stat(inode)); + if (s.size > buffSize) { + return OxError(1, "Buffer to small to load file"); + } + return readFileInodeRange(inode, 0, s.size, buffer, &buffSize); } template -Error FileSystemTemplate::read(uint64_t inode, void *buffer, std::size_t buffSize) noexcept { - return m_fs.read(inode, buffer, buffSize); -} - -template -Error FileSystemTemplate::read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept { +Error FileSystemTemplate::readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept { return m_fs.read(inode, readStart, readSize, reinterpret_cast(buffer), size); } template -Result FileSystemTemplate::directAccess(uint64_t inode) noexcept { +Result FileSystemTemplate::directAccessInode(uint64_t inode) noexcept { auto data = m_fs.read(inode); if (!data.valid()) { return OxError(1, "Data not valid"); @@ -312,7 +356,7 @@ Error FileSystemTemplate::remove(CRStringView path, bool r oxRequire(fd, fileSystemData()); Directory rootDir(m_fs, fd.rootDirInode); oxRequire(inode, rootDir.find(path)); - oxRequire(st, stat(inode)); + oxRequire(st, statInode(inode)); if (st.fileType == FileType::NormalFile || recursive) { if (auto err = rootDir.remove(path)) { // removal failed, try putting the index back @@ -338,7 +382,7 @@ Error FileSystemTemplate::resize(uint64_t size, void *buff } template -Error FileSystemTemplate::write(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept { +Error FileSystemTemplate::writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType fileType) noexcept { auto [inode, err] = find(path); if (err) { oxRequire(generatedId, m_fs.generateInodeId()); @@ -346,17 +390,17 @@ Error FileSystemTemplate::write(CRStringView path, const v oxRequireM(rootDir, this->rootDir()); oxReturnError(rootDir.write(path, inode)); } - oxReturnError(write(inode, buffer, size, fileType)); + oxReturnError(writeFileInode(inode, buffer, size, fileType)); return OxError(0); } template -Error FileSystemTemplate::write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept { +Error FileSystemTemplate::writeFileInode(uint64_t inode, const void *buffer, uint64_t size, FileType fileType) noexcept { return m_fs.write(inode, buffer, size, static_cast(fileType)); } template -Result FileSystemTemplate::stat(uint64_t inode) const noexcept { +Result FileSystemTemplate::statInode(uint64_t inode) const noexcept { oxRequire(s, m_fs.stat(inode)); FileStat out; out.inode = s.inode; @@ -367,7 +411,7 @@ Result FileSystemTemplate::stat(uint64_t inode) } template -Result FileSystemTemplate::stat(CRStringView path) const noexcept { +Result FileSystemTemplate::statPath(CRStringView path) const noexcept { oxRequire(inode, find(path)); return stat(inode); } diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index fa5b7e39..f2bcdd2f 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -63,41 +63,6 @@ Error PassThroughFS::move(CRStringView src, CRStringView dest) noexcept { return OxError(0); } -Error PassThroughFS::read(CRStringView path, void *buffer, std::size_t buffSize) noexcept { - try { - std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate); - const std::size_t size = file.tellg(); - file.seekg(0, std::ios::beg); - if (size > buffSize) { - oxTracef("ox::fs::PassThroughFS::read::error", "Read failed: Buffer too small: {}", path); - return OxError(1); - } - file.read(static_cast(buffer), static_cast(buffSize)); - } catch (const std::fstream::failure &f) { - oxTracef("ox::fs::PassThroughFS::read::error", "Read of {} failed: {}", path, f.what()); - return OxError(2); - } - return OxError(0); -} - -Result PassThroughFS::directAccess(CRStringView) noexcept { - return OxError(1, "PassThroughFS::directAccess not supported"); -} - -Error PassThroughFS::read(uint64_t, void*, std::size_t) noexcept { - // unsupported - return OxError(1, "read(uint64_t, void*, std::size_t) is not supported by PassThroughFS"); -} - -Error PassThroughFS::read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) noexcept { - // unsupported - return OxError(1, "read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) is not supported by PassThroughFS"); -} - -Result PassThroughFS::directAccess(uint64_t) noexcept { - return OxError(1, "PassThroughFS::directAccess not supported"); -} - Result> PassThroughFS::ls(CRStringView dir) const noexcept { Vector out; std::error_code ec; @@ -123,37 +88,20 @@ Error PassThroughFS::resize(uint64_t, void*) noexcept { return OxError(1, "resize is not supported by PassThroughFS"); } -Error PassThroughFS::write(CRStringView path, const void *buffer, uint64_t size, FileType) noexcept { - const auto p = (m_path / stripSlash(path)); - try { - std::ofstream f(p, std::ios::binary); - f.write(static_cast(buffer), size); - } catch (const std::fstream::failure &f) { - oxTracef("ox::fs::PassThroughFS::read::error", "Write of {} failed: {}", path, f.what()); - return OxError(1); - } - return OxError(0); -} - -Error PassThroughFS::write(uint64_t, const void*, uint64_t, FileType) noexcept { +Result PassThroughFS::statInode(uint64_t) const noexcept { // unsupported - return OxError(1, "write(uint64_t, void*, uint64_t, uint8_t) is not supported by PassThroughFS"); + return OxError(1, "statInode(uint64_t) is not supported by PassThroughFS"); } -Result PassThroughFS::stat(uint64_t) const noexcept { - // unsupported - return OxError(1, "stat(uint64_t) is not supported by PassThroughFS"); -} - -Result PassThroughFS::stat(CRStringView path) const noexcept { +Result PassThroughFS::statPath(CRStringView path) const noexcept { std::error_code ec; const auto p = m_path / stripSlash(path); const FileType type = std::filesystem::is_directory(p, ec) ? FileType::Directory : FileType::NormalFile; - oxTracef("ox::fs::PassThroughFS::stat", "{} {}", ec.message(), path); + oxTracef("ox::fs::PassThroughFS::statInode", "{} {}", ec.message(), path); const uint64_t size = type == FileType::Directory ? 0 : std::filesystem::file_size(p, ec); - oxTracef("ox::fs::PassThroughFS::stat", "{} {}", ec.message(), path); - oxTracef("ox::fs::PassThroughFS::stat::size", "{} {}", path, size); + oxTracef("ox::fs::PassThroughFS::statInode", "{} {}", ec.message(), path); + oxTracef("ox::fs::PassThroughFS::statInode::size", "{} {}", path, size); oxReturnError(OxError(ec.value())); return FileStat{0, 0, size, type}; } @@ -193,12 +141,56 @@ bool PassThroughFS::valid() const noexcept { return false; } +Error PassThroughFS::readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept { + try { + std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate); + const std::size_t size = file.tellg(); + file.seekg(0, std::ios::beg); + if (size > buffSize) { + oxTracef("ox::fs::PassThroughFS::read::error", "Read failed: Buffer too small: {}", path); + return OxError(1); + } + file.read(static_cast(buffer), static_cast(buffSize)); + } catch (const std::fstream::failure &f) { + oxTracef("ox::fs::PassThroughFS::read::error", "Read of {} failed: {}", path, f.what()); + return OxError(2); + } + return OxError(0); +} + +Error PassThroughFS::readFileInode(uint64_t, void*, std::size_t) noexcept { + // unsupported + return OxError(1, "readFileInode(uint64_t, void*, std::size_t) is not supported by PassThroughFS"); +} + +Error PassThroughFS::readFileInodeRange(uint64_t, std::size_t, std::size_t, void*, std::size_t*) noexcept { + // unsupported + return OxError(1, "read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) is not supported by PassThroughFS"); +} + +Error PassThroughFS::writeFilePath(CRStringView path, const void *buffer, uint64_t size, FileType) noexcept { + const auto p = (m_path / stripSlash(path)); + try { + std::ofstream f(p, std::ios::binary); + f.write(static_cast(buffer), static_cast(size)); + } catch (const std::fstream::failure &f) { + oxTracef("ox::fs::PassThroughFS::read::error", "Write of {} failed: {}", path, f.what()); + return OxError(1); + } + return OxError(0); +} + +Error PassThroughFS::writeFileInode(uint64_t, const void*, uint64_t, FileType) noexcept { + // unsupported + return OxError(1, "writeFileInode(uint64_t, void*, uint64_t, uint8_t) is not supported by PassThroughFS"); +} + std::string_view PassThroughFS::stripSlash(StringView path) noexcept { const auto pathLen = ox_strlen(path); for (auto i = 0u; i < pathLen && path[0] == '/'; i++) { path = path.substr(1); } - return std::string_view(path.data(), path.bytes()); + return {path.data(), path.bytes()}; } } diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index 16146b8f..f46a2178 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -11,7 +11,6 @@ #if __has_include() && defined(OX_USE_STDLIB) #include -#include #include #include "filesystem.hpp" @@ -37,36 +36,22 @@ class PassThroughFS: public FileSystem { [[nodiscard]] String basePath() const noexcept; - Error mkdir(CRStringView path, bool recursive = false) noexcept override; + Error mkdir(CRStringView path, bool recursive) noexcept override; Error move(CRStringView src, CRStringView dest) noexcept override; - Error read(CRStringView path, void *buffer, std::size_t buffSize) noexcept override; - - Result directAccess(CRStringView) noexcept override; - - Error read(uint64_t inode, void *buffer, std::size_t size) noexcept override; - - Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override; - - Result directAccess(uint64_t) noexcept override; - Result> ls(CRStringView dir) const noexcept override; template Error ls(CRStringView dir, F cb) const noexcept; - Error remove(CRStringView path, bool recursive = false) noexcept override; + Error remove(CRStringView path, bool recursive) noexcept override; - Error resize(uint64_t size, void *buffer = nullptr) noexcept override; + Error resize(uint64_t size, void *buffer) noexcept override; - Error write(CRStringView path, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept override; + Result statInode(uint64_t inode) const noexcept override; - Error write(uint64_t inode, const void *buffer, uint64_t size, FileType fileType = FileType::NormalFile) noexcept override; - - Result stat(uint64_t inode) const noexcept override; - - Result stat(CRStringView path) const noexcept override; + Result statPath(CRStringView path) const noexcept override; [[nodiscard]] uint64_t spaceNeeded(uint64_t size) const noexcept override; @@ -83,6 +68,17 @@ class PassThroughFS: public FileSystem { [[nodiscard]] bool valid() const noexcept override; + protected: + Error readFilePath(CRStringView path, void *buffer, std::size_t buffSize) noexcept override; + + Error readFileInode(uint64_t inode, void *buffer, std::size_t size) noexcept override; + + Error readFileInodeRange(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) noexcept override; + + Error writeFilePath(CRStringView 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; + private: /** * Strips the leading slashes from a string. diff --git a/deps/ox/src/ox/fs/ptrarith/ptr.hpp b/deps/ox/src/ox/fs/ptrarith/ptr.hpp index 5ae29293..694461b5 100644 --- a/deps/ox/src/ox/fs/ptrarith/ptr.hpp +++ b/deps/ox/src/ox/fs/ptrarith/ptr.hpp @@ -223,7 +223,6 @@ template constexpr Ptr Ptr::subPtr(size_t offset) noexcept { oxTracef("ox::fs::Ptr::subPtr", "{} {} {} {} {}", m_itemOffset, this->size(), offset, m_itemSize, (m_itemSize - offset)); return subPtr(offset, m_itemSize - offset); - return subPtr(offset, m_itemSize - offset); } template