[ox/fs] Cleanup, Add error messages for unsupported methods in PassThroughFS

This commit is contained in:
Gary Talent 2021-05-11 00:52:47 -05:00
parent 5c66f3e0dc
commit cdd1de89f8
2 changed files with 19 additions and 19 deletions

View File

@ -34,7 +34,7 @@ Error PassThroughFS::mkdir(const char *path, bool recursive) noexcept {
oxTrace("ox::fs::PassThroughFS::mkdir") << u8p.c_str();
if (recursive) {
std::error_code ec;
auto isDir = std::filesystem::is_directory(p, ec);
const auto isDir = std::filesystem::is_directory(p, ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: mkdir failed"));
if (isDir) {
success = true;
@ -82,12 +82,12 @@ Result<const char*> PassThroughFS::directAccess(const char*) noexcept {
Error PassThroughFS::read(uint64_t, void*, std::size_t) noexcept {
// unsupported
return OxError(1);
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);
return OxError(1, "read(uint64_t, std::size_t, std::size_t, void*, std::size_t*) is not supported by PassThroughFS");
}
Result<const char*> PassThroughFS::directAccess(uint64_t) noexcept {
@ -99,8 +99,8 @@ Result<Vector<String>> PassThroughFS::ls(const char *dir) noexcept {
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();
for (const auto &p : di) {
const auto u8p = p.path().filename().u8string();
out.emplace_back(bit_cast<const char*>(u8p.c_str()));
}
return ox::move(out);
@ -116,11 +116,11 @@ Error PassThroughFS::remove(const char *path, bool recursive) noexcept {
Error PassThroughFS::resize(uint64_t, void*) noexcept {
// unsupported
return OxError(1);
return OxError(1, "resize is not supported by PassThroughFS");
}
Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_t) noexcept {
auto p = (m_path / stripSlash(path));
const auto p = (m_path / stripSlash(path));
try {
std::ofstream f(p, std::ios::binary);
f.write(static_cast<char*>(buffer), size);
@ -133,21 +133,21 @@ Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_
Error PassThroughFS::write(uint64_t, void*, uint64_t, uint8_t) noexcept {
// unsupported
return OxError(1);
return OxError(1, "write(uint64_t, void*, uint64_t, uint8_t) is not supported by PassThroughFS");
}
Result<FileStat> PassThroughFS::stat(uint64_t) noexcept {
// unsupported
return OxError(1);
return OxError(1, "stat(uint64_t) is not supported by PassThroughFS");
}
Result<FileStat> PassThroughFS::stat(const char *path) noexcept {
std::error_code ec;
const auto p = m_path / stripSlash(path);
uint8_t type = std::filesystem::is_directory(p, ec) ?
const uint8_t type = std::filesystem::is_directory(p, ec) ?
FileType_Directory : FileType_NormalFile;
oxTrace("ox::fs::PassThroughFS::stat") << ec.message().c_str() << path;
uint64_t size = type == FileType_Directory ? 0 : std::filesystem::file_size(p, ec);
const uint64_t size = type == FileType_Directory ? 0 : std::filesystem::file_size(p, ec);
oxTrace("ox::fs::PassThroughFS::stat") << ec.message().c_str() << path;
oxTrace("ox::fs::PassThroughFS::stat::size") << path << size;
oxReturnError(OxError(ec.value()));
@ -160,14 +160,14 @@ uint64_t PassThroughFS::spaceNeeded(uint64_t size) noexcept {
Result<uint64_t> PassThroughFS::available() noexcept {
std::error_code ec;
auto s = std::filesystem::space(m_path, ec);
const auto s = std::filesystem::space(m_path, ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: could not get FS size"));
return s.available;
}
Result<uint64_t> PassThroughFS::size() const noexcept {
std::error_code ec;
auto s = std::filesystem::space(m_path, ec);
const auto s = std::filesystem::space(m_path, ec);
oxReturnError(OxError(ec.value(), "PassThroughFS: could not get FS size"));
return s.capacity;
}
@ -177,12 +177,12 @@ char *PassThroughFS::buff() noexcept {
}
Error PassThroughFS::walk(Error(*)(uint8_t, uint64_t, uint64_t)) noexcept {
return OxError(1);
return OxError(1, "walk(Error(*)(uint8_t, uint64_t, uint64_t)) is not supported by PassThroughFS");
}
bool PassThroughFS::valid() const noexcept {
std::error_code ec;
auto out = std::filesystem::is_directory(m_path, ec);
const auto out = std::filesystem::is_directory(m_path, ec);
if (!ec.value()) {
return out;
}
@ -190,8 +190,8 @@ bool PassThroughFS::valid() const noexcept {
}
const char *PassThroughFS::stripSlash(const char *path) noexcept {
auto pathLen = ox_strlen(path);
for (decltype(pathLen) i = 0; i < pathLen && path[0] == '/'; i++) {
const auto pathLen = ox_strlen(path);
for (auto i = 0u; i < pathLen && path[0] == '/'; i++) {
path++;
}
return path;

View File

@ -84,7 +84,7 @@ class PassThroughFS: public FileSystem {
/**
* Strips the leading slashes from a string.
*/
const char *stripSlash(const char *path) noexcept;
static const char *stripSlash(const char *path) noexcept;
};
@ -94,7 +94,7 @@ Error PassThroughFS::ls(const char *dir, F cb) noexcept {
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();
const auto u8p = p.path().filename().u8string();
oxReturnError(cb(bit_cast<const char*>(u8p.c_str()), 0));
}
return OxError(0);