[ox/fs] Change PassThroughFS to use fstream instead of C file IO for compatibility reasons

This commit is contained in:
Gary Talent 2019-12-22 00:30:08 -06:00
parent 942d548fdd
commit 4b832dd3d7

View File

@ -11,6 +11,7 @@
#if defined(OX_PASSTHROUGHFS_HAS_DEPENDENCIES) #if defined(OX_PASSTHROUGHFS_HAS_DEPENDENCIES)
#include <stdio.h> #include <stdio.h>
#include <fstream>
namespace ox { namespace ox {
@ -43,19 +44,21 @@ 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) {
auto file = fopen((m_path / stripSlash(path)).c_str(), "r"); std::ifstream file((m_path / stripSlash(path)), std::ios::binary | std::ios::ate);
if (file) { const std::size_t size = file.tellg();
fseek(file, 0, SEEK_END); file.seekg(0, std::ios::beg);
const std::size_t size = ftell(file);
if (size <= buffSize) { if (!file.good()) {
rewind(file); oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed:" << path;
auto itemsRead = fread(buffer, buffSize, 1, file); return OxError(1);
fclose(file);
return OxError(itemsRead == 1 ? 0 : 1);
}
} }
oxTrace("ox::fs::PassThroughFS::read::error") << "Read failed: " << path; if (size > buffSize) {
return OxError(1); 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);
} }
ValErr<uint8_t*> PassThroughFS::read(const char*) { ValErr<uint8_t*> PassThroughFS::read(const char*) {
@ -91,13 +94,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));
auto f = fopen(p.c_str(), "w"); std::ofstream f(p, std::ios::binary);
if (!f) { if (!f.good()) {
return OxError(1); return OxError(1);
} }
auto err = OxError(fwrite(buffer, size, 1, f) == 1 ? 0 : 1); f.write(static_cast<char*>(buffer), size);
fclose(f); f.close();
return err; return OxError(0);
} }
Error PassThroughFS::write(uint64_t, void*, uint64_t, uint8_t) { Error PassThroughFS::write(uint64_t, void*, uint64_t, uint8_t) {