From 9da47f75c000c5eb0ace02719465dab14d09f472 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 1 Nov 2019 01:25:24 -0500 Subject: [PATCH] [ox/fs] Add support for direct access to FS memory --- deps/ox/src/ox/fs/filesystem/filesystem.cpp | 12 ++++ deps/ox/src/ox/fs/filesystem/filesystem.hpp | 63 ++++++++++++++----- .../ox/src/ox/fs/filesystem/passthroughfs.cpp | 8 +++ .../ox/src/ox/fs/filesystem/passthroughfs.hpp | 4 ++ 4 files changed, 70 insertions(+), 17 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.cpp b/deps/ox/src/ox/fs/filesystem/filesystem.cpp index 27b67ac6..a776617b 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.cpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.cpp @@ -10,6 +10,18 @@ namespace ox { +[[nodiscard]] ox::ValErr FileSystem::read(FileAddress addr) { + switch (addr.type()) { + case FileAddressType::Inode: + return read(addr.getInode().value); + case FileAddressType::ConstPath: + case FileAddressType::Path: + return read(addr.getPath().value); + default: + return OxError(1); + } +} + [[nodiscard]] ox::Error FileSystem::read(FileAddress addr, void *buffer, std::size_t size) { 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 a511f4c6..10282f3b 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -21,54 +21,60 @@ class FileSystem { public: virtual ~FileSystem() = default; - virtual ox::Error mkdir(const char *path, bool recursive = false) = 0; + [[nodiscard]] virtual ox::Error mkdir(const char *path, bool recursive = false) = 0; /** * Moves an entry from one directory to another. * @param src the path to the file * @param dest the path of the destination directory */ - virtual ox::Error move(const char *src, const char *dest) = 0; + [[nodiscard]] virtual ox::Error move(const char *src, const char *dest) = 0; - virtual ox::Error read(const char *path, void *buffer, std::size_t buffSize) = 0; + [[nodiscard]] virtual ox::Error read(const char *path, void *buffer, std::size_t buffSize) = 0; - virtual ox::Error read(uint64_t inode, void *buffer, std::size_t size) = 0; + [[nodiscard]] virtual ox::ValErr read(const char *path) = 0; - virtual ox::Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) = 0; + [[nodiscard]] virtual ox::Error read(uint64_t inode, void *buffer, std::size_t size) = 0; + + [[nodiscard]] virtual ox::Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) = 0; + + [[nodiscard]] virtual ox::ValErr read(uint64_t inode) = 0; [[nodiscard]] ox::Error read(FileAddress addr, void *buffer, std::size_t size); [[nodiscard]] ox::Error read(FileAddress addr, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size); - virtual ox::Error remove(const char *path, bool recursive = false) = 0; + [[nodiscard]] ox::ValErr read(FileAddress addr); + + [[nodiscard]] virtual ox::Error remove(const char *path, bool recursive = false) = 0; [[nodiscard]] ox::Error remove(FileAddress addr, bool recursive = false); - virtual ox::Error resize(uint64_t size, void *buffer = nullptr) = 0; + [[nodiscard]] virtual ox::Error resize(uint64_t size, void *buffer = nullptr) = 0; - virtual ox::Error write(const char *path, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; + [[nodiscard]] virtual ox::Error write(const char *path, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; - virtual ox::Error write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; + [[nodiscard]] virtual ox::Error write(uint64_t inode, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile) = 0; [[nodiscard]] ox::Error write(FileAddress addr, void *buffer, uint64_t size, uint8_t fileType = FileType_NormalFile); - virtual ox::ValErr stat(uint64_t inode) = 0; + [[nodiscard]] virtual ox::ValErr stat(uint64_t inode) = 0; - virtual ox::ValErr stat(const char *path) = 0; + [[nodiscard]] virtual ox::ValErr stat(const char *path) = 0; [[nodiscard]] ox::ValErr stat(FileAddress addr); - virtual uint64_t spaceNeeded(uint64_t size) = 0; + [[nodiscard]] virtual uint64_t spaceNeeded(uint64_t size) = 0; - virtual uint64_t available() = 0; + [[nodiscard]] virtual uint64_t available() = 0; - virtual uint64_t size() const = 0; + [[nodiscard]] virtual uint64_t size() const = 0; - virtual uint8_t *buff() = 0; + [[nodiscard]] virtual uint8_t *buff() = 0; - virtual ox::Error walk(ox::Error(*cb)(uint8_t, uint64_t, uint64_t)) = 0; + [[nodiscard]] virtual ox::Error walk(ox::Error(*cb)(uint8_t, uint64_t, uint64_t)) = 0; - virtual bool valid() const = 0; + [[nodiscard]] virtual bool valid() const = 0; }; @@ -104,10 +110,14 @@ class FileSystemTemplate: public FileSystem { [[nodiscard]] ox::Error read(const char *path, void *buffer, std::size_t buffSize) override; + [[nodiscard]] ox::ValErr read(const char*) override; + [[nodiscard]] ox::Error read(uint64_t inode, void *buffer, std::size_t size) override; [[nodiscard]] ox::Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) override; + [[nodiscard]] ox::ValErr read(uint64_t) override; + template [[nodiscard]] ox::Error ls(const char *dir, F cb); @@ -212,6 +222,16 @@ ox::Error FileSystemTemplate::read(const char *path, void return read(inode, buffer, buffSize); } +template +[[nodiscard]] ox::ValErr FileSystemTemplate::read(const char *path) { + auto fd = fileSystemData(); + oxReturnError(fd.error); + Directory rootDir(m_fs, fd.value.rootDirInode); + auto [inode, err] = rootDir.find(path); + oxReturnError(err); + return read(inode); +} + template ox::Error FileSystemTemplate::read(uint64_t inode, void *buffer, std::size_t buffSize) { return m_fs.read(inode, buffer, buffSize); @@ -222,6 +242,15 @@ ox::Error FileSystemTemplate::read(uint64_t inode, std::si return m_fs.read(inode, readStart, readSize, reinterpret_cast(buffer), size); } +template +[[nodiscard]] ox::ValErr FileSystemTemplate::read(uint64_t inode) { + auto data = m_fs.read(inode); + if (!data.valid()) { + return OxError(1); + } + return data.get(); +} + template template ox::Error FileSystemTemplate::ls(const char *path, F cb) { diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index 56612d1d..cf8d5884 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -58,6 +58,10 @@ Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize) return OxError(1); } +ValErr PassThroughFS::read(const char*) { + return OxError(1); +} + Error PassThroughFS::read(uint64_t, void*, std::size_t) { // unsupported return OxError(1); @@ -68,6 +72,10 @@ Error PassThroughFS::read(uint64_t, std::size_t, std::size_t, void*, std::size_t return OxError(1); } +ValErr PassThroughFS::read(uint64_t) { + return OxError(1); +} + Error PassThroughFS::remove(const char *path, bool recursive) { if (recursive) { return OxError(std::filesystem::remove_all(m_path / stripSlash(path)) != 0); diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index 1eaa62ce..64bbd3c7 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -43,10 +43,14 @@ class PassThroughFS: public FileSystem { ox::Error read(const char *path, void *buffer, std::size_t buffSize) override; + ox::ValErr read(const char*) override; + ox::Error read(uint64_t inode, void *buffer, std::size_t size) override; ox::Error read(uint64_t inode, std::size_t readStart, std::size_t readSize, void *buffer, std::size_t *size) override; + ox::ValErr read(uint64_t) override; + template ox::Error ls(const char *dir, F cb);