From 6845ff38de696b94208262c5fcd2db85c02a9c54 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 8 May 2021 22:40:59 -0500 Subject: [PATCH] [ox/fs] Cleanup error handling --- deps/ox/src/ox/fs/filesystem/filesystem.hpp | 54 ++++++++------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/deps/ox/src/ox/fs/filesystem/filesystem.hpp b/deps/ox/src/ox/fs/filesystem/filesystem.hpp index e12086bd..2e6daf08 100644 --- a/deps/ox/src/ox/fs/filesystem/filesystem.hpp +++ b/deps/ox/src/ox/fs/filesystem/filesystem.hpp @@ -221,18 +221,15 @@ Error FileSystemTemplate::format(void *buff, uint64_t buff template Error FileSystemTemplate::mkdir(const char *path, bool recursive) noexcept { oxTrace("ox::fs::FileSystemTemplate::mkdir") << "path:" << path << "recursive:" << recursive; - auto rootDir = this->rootDir(); - oxReturnError(rootDir.error); - return rootDir.value.mkdir(path, recursive); + oxRequireM(rootDir, this->rootDir()); + return rootDir.mkdir(path, recursive); } template Error FileSystemTemplate::move(const char *src, const char *dest) noexcept { - auto fd = fileSystemData(); - oxReturnError(fd.error); - Directory rootDir(m_fs, fd.value.rootDirInode); - auto [inode, err] = rootDir.find(src); - oxReturnError(err); + oxRequire(fd, fileSystemData()); + Directory rootDir(m_fs, fd.rootDirInode); + oxRequireM(inode, rootDir.find(src)); oxReturnError(rootDir.write(dest, inode)); oxReturnError(rootDir.remove(src)); return OxError(0); @@ -240,21 +237,17 @@ Error FileSystemTemplate::move(const char *src, const char template Error FileSystemTemplate::read(const char *path, void *buffer, std::size_t buffSize) noexcept { - auto fd = fileSystemData(); - oxReturnError(fd.error); - Directory rootDir(m_fs, fd.value.rootDirInode); - auto [inode, err] = rootDir.find(path); - oxReturnError(err); + oxRequire(fd, fileSystemData()); + Directory rootDir(m_fs, fd.rootDirInode); + oxRequire(inode, rootDir.find(path)); return read(inode, buffer, buffSize); } template Result FileSystemTemplate::directAccess(const char *path) noexcept { - auto fd = fileSystemData(); - oxReturnError(fd.error); - Directory rootDir(m_fs, fd.value.rootDirInode); - auto [inode, err] = rootDir.find(path); - oxReturnError(err); + oxRequire(fd, fileSystemData()); + Directory rootDir(m_fs, fd.rootDirInode); + oxRequire(inode, rootDir.find(path)); return directAccess(inode); } @@ -290,26 +283,22 @@ Result> FileSystemTemplate::ls(const char * template template Error FileSystemTemplate::ls(const char *path, F cb) { - oxTrace("ox::FileSystemTemplate::ls") << "path:" << path; - auto [s, err] = stat(path); - oxReturnError(err); + oxTrace("ox::fs::FileSystemTemplate::ls") << "path:" << path; + oxRequire(s, stat(path)); Directory dir(m_fs, s.inode); return dir.ls(cb); } template Error FileSystemTemplate::remove(const char *path, bool recursive) noexcept { - auto fd = fileSystemData(); - oxReturnError(fd.error); - Directory rootDir(m_fs, fd.value.rootDirInode); - auto inode = rootDir.find(path); - oxReturnError(inode.error); - auto st = stat(inode.value); - oxReturnError(st.error); - if (st.value.fileType == FileType_NormalFile || recursive) { + oxRequire(fd, fileSystemData()); + Directory rootDir(m_fs, fd.rootDirInode); + oxRequire(inode, rootDir.find(path)); + oxRequire(st, stat(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.value)); + oxLogError(rootDir.write(path, inode)); return err; } } else { @@ -337,9 +326,8 @@ Error FileSystemTemplate::write(const char *path, void *bu oxRequire(generatedId, m_fs.generateInodeId()); inode = generatedId; } - auto rootDir = this->rootDir(); - oxReturnError(rootDir.error); - oxReturnError(rootDir.value.write(path, inode)); + oxRequireM(rootDir, this->rootDir()); + oxReturnError(rootDir.write(path, inode)); oxReturnError(write(inode, buffer, size, fileType)); return OxError(0); }