diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp index 450e18bb..5c472346 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -66,9 +66,9 @@ class FileSystem { [[nodiscard]] virtual uint64_t spaceNeeded(uint64_t size) = 0; - [[nodiscard]] virtual uint64_t available() = 0; + [[nodiscard]] virtual Result available() = 0; - [[nodiscard]] virtual uint64_t size() const = 0; + [[nodiscard]] virtual Result size() const = 0; [[nodiscard]] virtual char *buff() = 0; @@ -143,9 +143,9 @@ class FileSystemTemplate: public FileSystem { uint64_t spaceNeeded(uint64_t size) override; - uint64_t available() override; + Result available() override; - uint64_t size() const override; + Result size() const override; char *buff() override; @@ -353,12 +353,12 @@ uint64_t FileSystemTemplate::spaceNeeded(uint64_t size) { } template -uint64_t FileSystemTemplate::available() { +Result FileSystemTemplate::available() { return m_fs.available(); } template -uint64_t FileSystemTemplate::size() const { +Result FileSystemTemplate::size() const { return m_fs.size(); } diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp index 191ea422..d60e9404 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.cpp @@ -21,8 +21,8 @@ PassThroughFS::PassThroughFS(const char *dirPath) { PassThroughFS::~PassThroughFS() { } -std::string PassThroughFS::basePath() { - return m_path.string(); +ox::String PassThroughFS::basePath() { + return m_path.string().c_str(); } Error PassThroughFS::mkdir(const char *path, bool recursive) { @@ -31,19 +31,29 @@ Error PassThroughFS::mkdir(const char *path, bool recursive) { const auto u8p = p.u8string(); oxTrace("ox::fs::PassThroughFS::mkdir") << u8p.c_str(); if (recursive) { - if (std::filesystem::is_directory(p)) { + std::error_code ec; + auto isDir = std::filesystem::is_directory(p, ec); + oxReturnError(OxError(ec.value(), "PassThroughFS: mkdir failed")); + if (isDir) { success = true; } else { - success = std::filesystem::create_directories(p); + success = std::filesystem::create_directories(p, ec); + oxReturnError(OxError(ec.value(), "PassThroughFS: mkdir failed")); } } else { - success = std::filesystem::create_directory(p); + std::error_code ec; + success = std::filesystem::create_directory(p, ec); + oxReturnError(OxError(ec.value(), "PassThroughFS: mkdir failed")); } return OxError(success ? 0 : 1); } Error PassThroughFS::move(const char *src, const char *dest) { - std::filesystem::rename(m_path / stripSlash(src), m_path / stripSlash(dest)); + std::error_code ec; + std::filesystem::rename(m_path / stripSlash(src), m_path / stripSlash(dest), ec); + if (ec.value()) { + return OxError(1); + } return OxError(0); } @@ -59,7 +69,7 @@ Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize) file.read(static_cast(buffer), buffSize); } catch (const std::fstream::failure&) { oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed:" << path; - throw OxError(2); + return OxError(2); } return OxError(0); } @@ -102,7 +112,7 @@ Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_ f.write(static_cast(buffer), size); } catch (const std::fstream::failure&) { oxTrace("ox::fs::PassThroughFS::write::error") << "Write failed:" << path; - throw OxError(1); + return OxError(1); } return OxError(0); } @@ -114,7 +124,7 @@ Error PassThroughFS::write(uint64_t, void*, uint64_t, uint8_t) { Result PassThroughFS::stat(uint64_t) { // unsupported - return {{}, OxError(1)}; + return OxError(1); } Result PassThroughFS::stat(const char *path) { @@ -133,15 +143,17 @@ uint64_t PassThroughFS::spaceNeeded(uint64_t size) { return size; } -uint64_t PassThroughFS::available() { +Result PassThroughFS::available() { std::error_code ec; auto s = std::filesystem::space(m_path, ec); + oxReturnError(OxError(ec.value(), "PassThroughFS: could not get FS size")); return s.available; } -uint64_t PassThroughFS::size() const { +Result PassThroughFS::size() const { std::error_code ec; auto s = std::filesystem::space(m_path, ec); + oxReturnError(OxError(ec.value(), "PassThroughFS: could not get FS size")); return s.capacity; } @@ -154,7 +166,12 @@ Error PassThroughFS::walk(Error(*)(uint8_t, uint64_t, uint64_t)) { } bool PassThroughFS::valid() const { - return std::filesystem::is_directory(m_path); + std::error_code ec; + auto out = std::filesystem::is_directory(m_path, ec); + if (!ec.value()) { + return out; + } + return false; } const char *PassThroughFS::stripSlash(const char *path) { diff --git a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp index a8362034..7e7fc517 100644 --- a/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp +++ b/deps/ox/src/ox/fs/filesystem/passthroughfs.hpp @@ -36,7 +36,8 @@ class PassThroughFS: public FileSystem { ~PassThroughFS(); - [[nodiscard]] std::string basePath(); + [[nodiscard]] + ox::String basePath(); Error mkdir(const char *path, bool recursive = false) override; @@ -69,9 +70,9 @@ class PassThroughFS: public FileSystem { uint64_t spaceNeeded(uint64_t size) override; - uint64_t available() override; + Result available() override; - uint64_t size() const override; + Result size() const override; char *buff() override; @@ -89,7 +90,10 @@ class PassThroughFS: public FileSystem { template Error PassThroughFS::ls(const char *dir, F cb) { - for (auto &p : std::filesystem::directory_iterator(m_path / stripSlash(dir))) { + std::error_code ec; + const auto di = std::filesystem::directory_iterator(m_path / stripSlash(dir), ec); + oxReturnError(OxError(ec.value(), "PassThroughFS: ls failed")); + for (auto &p : di) { auto u8p = p.path().filename().u8string(); oxReturnError(cb(ox::bit_cast(u8p.c_str()), 0)); }