[ox/fs] Add exception handling to PassthroughFS for fstream usage

This commit is contained in:
Gary Talent 2020-01-25 15:18:08 -06:00
parent 5f56a5cc9b
commit c9f91c16c2

View File

@ -43,20 +43,19 @@ Error PassThroughFS::move(const char *src, const char *dest) {
} }
Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize) { Error PassThroughFS::read(const char *path, void *buffer, std::size_t buffSize) {
std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate); try {
const std::size_t size = file.tellg(); std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate);
file.seekg(0, std::ios::beg); const std::size_t size = file.tellg();
file.seekg(0, std::ios::beg);
if (!file.good()) { if (size > buffSize) {
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed: Buffer too small:" << path;
return OxError(1);
}
file.read(static_cast<char*>(buffer), buffSize);
} catch (const std::fstream::failure&) {
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed:" << path; oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed:" << path;
return OxError(1); throw OxError(2);
} }
if (size > buffSize) {
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed: Buffer too small:" << path;
return OxError(1);
}
file.read(static_cast<char*>(buffer), buffSize);
return OxError(0); return OxError(0);
} }
@ -93,11 +92,13 @@ ox::Error PassThroughFS::resize(uint64_t, void*) {
Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_t) { Error PassThroughFS::write(const char *path, void *buffer, uint64_t size, uint8_t) {
auto p = (m_path / stripSlash(path)); auto p = (m_path / stripSlash(path));
std::ofstream f(p, std::ios::binary); try {
if (!f.good()) { std::ofstream f(p, std::ios::binary);
return OxError(1); f.write(static_cast<char*>(buffer), size);
} catch (const std::fstream::failure&) {
oxTrace("ox::fs::PassThroughFS::write::error") << "Write failed:" << path;
throw OxError(1);
} }
f.write(static_cast<char*>(buffer), size);
return OxError(0); return OxError(0);
} }