[ox/fs] Add exception handling to PassthroughFS for fstream usage
This commit is contained in:
parent
5f56a5cc9b
commit
c9f91c16c2
19
deps/ox/src/ox/fs/filesystem/passthroughfs.cpp
vendored
19
deps/ox/src/ox/fs/filesystem/passthroughfs.cpp
vendored
@ -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) {
|
||||||
|
try {
|
||||||
std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate);
|
std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate);
|
||||||
const std::size_t size = file.tellg();
|
const std::size_t size = file.tellg();
|
||||||
file.seekg(0, std::ios::beg);
|
file.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
if (!file.good()) {
|
|
||||||
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed:" << path;
|
|
||||||
return OxError(1);
|
|
||||||
}
|
|
||||||
if (size > buffSize) {
|
if (size > buffSize) {
|
||||||
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed: Buffer too small:" << path;
|
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed: Buffer too small:" << path;
|
||||||
return OxError(1);
|
return OxError(1);
|
||||||
}
|
}
|
||||||
file.read(static_cast<char*>(buffer), buffSize);
|
file.read(static_cast<char*>(buffer), buffSize);
|
||||||
|
} catch (const std::fstream::failure&) {
|
||||||
|
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed:" << path;
|
||||||
|
throw OxError(2);
|
||||||
|
}
|
||||||
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));
|
||||||
|
try {
|
||||||
std::ofstream f(p, std::ios::binary);
|
std::ofstream f(p, std::ios::binary);
|
||||||
if (!f.good()) {
|
|
||||||
return OxError(1);
|
|
||||||
}
|
|
||||||
f.write(static_cast<char*>(buffer), size);
|
f.write(static_cast<char*>(buffer), size);
|
||||||
|
} catch (const std::fstream::failure&) {
|
||||||
|
oxTrace("ox::fs::PassThroughFS::write::error") << "Write failed:" << path;
|
||||||
|
throw OxError(1);
|
||||||
|
}
|
||||||
return OxError(0);
|
return OxError(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user