[ox/fs] Cleanup error handling

This commit is contained in:
Gary Talent 2021-04-16 19:55:25 -05:00
parent 6a566ed35e
commit e2d74de9c3
3 changed files with 43 additions and 22 deletions

View File

@ -66,9 +66,9 @@ class FileSystem {
[[nodiscard]] virtual uint64_t spaceNeeded(uint64_t size) = 0; [[nodiscard]] virtual uint64_t spaceNeeded(uint64_t size) = 0;
[[nodiscard]] virtual uint64_t available() = 0; [[nodiscard]] virtual Result<uint64_t> available() = 0;
[[nodiscard]] virtual uint64_t size() const = 0; [[nodiscard]] virtual Result<uint64_t> size() const = 0;
[[nodiscard]] virtual char *buff() = 0; [[nodiscard]] virtual char *buff() = 0;
@ -143,9 +143,9 @@ class FileSystemTemplate: public FileSystem {
uint64_t spaceNeeded(uint64_t size) override; uint64_t spaceNeeded(uint64_t size) override;
uint64_t available() override; Result<uint64_t> available() override;
uint64_t size() const override; Result<uint64_t> size() const override;
char *buff() override; char *buff() override;
@ -353,12 +353,12 @@ uint64_t FileSystemTemplate<FileStore, Directory>::spaceNeeded(uint64_t size) {
} }
template<typename FileStore, typename Directory> template<typename FileStore, typename Directory>
uint64_t FileSystemTemplate<FileStore, Directory>::available() { Result<uint64_t> FileSystemTemplate<FileStore, Directory>::available() {
return m_fs.available(); return m_fs.available();
} }
template<typename FileStore, typename Directory> template<typename FileStore, typename Directory>
uint64_t FileSystemTemplate<FileStore, Directory>::size() const { Result<uint64_t> FileSystemTemplate<FileStore, Directory>::size() const {
return m_fs.size(); return m_fs.size();
} }

View File

@ -21,8 +21,8 @@ PassThroughFS::PassThroughFS(const char *dirPath) {
PassThroughFS::~PassThroughFS() { PassThroughFS::~PassThroughFS() {
} }
std::string PassThroughFS::basePath() { ox::String PassThroughFS::basePath() {
return m_path.string(); return m_path.string().c_str();
} }
Error PassThroughFS::mkdir(const char *path, bool recursive) { 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(); const auto u8p = p.u8string();
oxTrace("ox::fs::PassThroughFS::mkdir") << u8p.c_str(); oxTrace("ox::fs::PassThroughFS::mkdir") << u8p.c_str();
if (recursive) { 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; success = true;
} else { } else {
success = std::filesystem::create_directories(p); success = std::filesystem::create_directories(p, ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: mkdir failed"));
} }
} else { } 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); return OxError(success ? 0 : 1);
} }
Error PassThroughFS::move(const char *src, const char *dest) { 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); return OxError(0);
} }
@ -59,7 +69,7 @@ Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize)
file.read(static_cast<char*>(buffer), buffSize); file.read(static_cast<char*>(buffer), buffSize);
} catch (const std::fstream::failure&) { } catch (const std::fstream::failure&) {
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed:" << path; oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed:" << path;
throw OxError(2); return OxError(2);
} }
return OxError(0); return OxError(0);
} }
@ -102,7 +112,7 @@ Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_
f.write(static_cast<char*>(buffer), size); f.write(static_cast<char*>(buffer), size);
} catch (const std::fstream::failure&) { } catch (const std::fstream::failure&) {
oxTrace("ox::fs::PassThroughFS::write::error") << "Write failed:" << path; oxTrace("ox::fs::PassThroughFS::write::error") << "Write failed:" << path;
throw OxError(1); return OxError(1);
} }
return OxError(0); return OxError(0);
} }
@ -114,7 +124,7 @@ Error PassThroughFS::write(uint64_t, void*, uint64_t, uint8_t) {
Result<FileStat> PassThroughFS::stat(uint64_t) { Result<FileStat> PassThroughFS::stat(uint64_t) {
// unsupported // unsupported
return {{}, OxError(1)}; return OxError(1);
} }
Result<FileStat> PassThroughFS::stat(const char *path) { Result<FileStat> PassThroughFS::stat(const char *path) {
@ -133,15 +143,17 @@ uint64_t PassThroughFS::spaceNeeded(uint64_t size) {
return size; return size;
} }
uint64_t PassThroughFS::available() { Result<uint64_t> PassThroughFS::available() {
std::error_code ec; std::error_code ec;
auto s = std::filesystem::space(m_path, ec); auto s = std::filesystem::space(m_path, ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: could not get FS size"));
return s.available; return s.available;
} }
uint64_t PassThroughFS::size() const { Result<uint64_t> PassThroughFS::size() const {
std::error_code ec; std::error_code ec;
auto s = std::filesystem::space(m_path, ec); auto s = std::filesystem::space(m_path, ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: could not get FS size"));
return s.capacity; return s.capacity;
} }
@ -154,7 +166,12 @@ Error PassThroughFS::walk(Error(*)(uint8_t, uint64_t, uint64_t)) {
} }
bool PassThroughFS::valid() const { 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) { const char *PassThroughFS::stripSlash(const char *path) {

View File

@ -36,7 +36,8 @@ class PassThroughFS: public FileSystem {
~PassThroughFS(); ~PassThroughFS();
[[nodiscard]] std::string basePath(); [[nodiscard]]
ox::String basePath();
Error mkdir(const char *path, bool recursive = false) override; 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 spaceNeeded(uint64_t size) override;
uint64_t available() override; Result<uint64_t> available() override;
uint64_t size() const override; Result<uint64_t> size() const override;
char *buff() override; char *buff() override;
@ -89,7 +90,10 @@ class PassThroughFS: public FileSystem {
template<typename F> template<typename F>
Error PassThroughFS::ls(const char *dir, F cb) { 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(); auto u8p = p.path().filename().u8string();
oxReturnError(cb(ox::bit_cast<const char*>(u8p.c_str()), 0)); oxReturnError(cb(ox::bit_cast<const char*>(u8p.c_str()), 0));
} }