Major refactor, better error handling and directory/image merge

This commit is contained in:
Jack Andersen
2017-02-04 20:19:34 -10:00
parent b3a76428da
commit dc474ad156
13 changed files with 1502 additions and 276 deletions

View File

@@ -41,14 +41,17 @@ public:
{
FILE* fp;
int64_t m_maxWriteSize;
WriteStream(const SystemString& path, int64_t maxWriteSize)
WriteStream(const SystemString& path, int64_t maxWriteSize, bool& err)
: m_maxWriteSize(maxWriteSize)
{
fp = fopen(path.c_str(), "wb");
if (!fp)
LogModule.report(logvisor::Fatal, _S("unable to open '%s' for writing"), path.c_str());
{
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.c_str());
err = true;
}
}
WriteStream(const SystemString& path, uint64_t offset, int64_t maxWriteSize)
WriteStream(const SystemString& path, uint64_t offset, int64_t maxWriteSize, bool& err)
: m_maxWriteSize(maxWriteSize)
{
fp = fopen(path.c_str(), "ab");
@@ -61,7 +64,8 @@ public:
FSeek(fp, offset, SEEK_SET);
return;
FailLoc:
LogModule.report(logvisor::Fatal, _S("unable to open '%s' for writing"), path.c_str());
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.c_str());
err = true;
}
~WriteStream()
{
@@ -72,7 +76,10 @@ public:
if (m_maxWriteSize >= 0)
{
if (FTell(fp) + length > m_maxWriteSize)
LogModule.report(logvisor::Fatal, _S("write operation exceeds file's %" PRIi64 "-byte limit"), m_maxWriteSize);
{
LogModule.report(logvisor::Error, _S("write operation exceeds file's %" PRIi64 "-byte limit"), m_maxWriteSize);
return 0;
}
}
return fwrite(buf, 1, length, fp);
}
@@ -86,12 +93,12 @@ public:
uint64_t readSz = discio.read(buf, thisSz);
if (thisSz != readSz)
{
LogModule.report(logvisor::Fatal, "unable to read enough from disc");
LogModule.report(logvisor::Error, "unable to read enough from disc");
return read;
}
if (write(buf, readSz) != readSz)
{
LogModule.report(logvisor::Fatal, "unable to write in file");
LogModule.report(logvisor::Error, "unable to write in file");
return read;
}
length -= thisSz;
@@ -102,25 +109,38 @@ public:
};
std::unique_ptr<IWriteStream> beginWriteStream() const
{
return std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize));
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err));
if (Err)
return {};
return ret;
}
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
{
return std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize));
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err));
if (Err)
return {};
return ret;
}
struct ReadStream : public IFileIO::IReadStream
{
FILE* fp;
ReadStream(const SystemString& path)
ReadStream(const SystemString& path, bool& err)
{
fp = fopen(path.c_str(), "rb");
if (!fp)
LogModule.report(logvisor::Fatal, _S("unable to open '%s' for reading"), path.c_str());
{
err = true;
LogModule.report(logvisor::Error, _S("unable to open '%s' for reading"), path.c_str());
}
}
ReadStream(const SystemString& path, uint64_t offset)
: ReadStream(path)
ReadStream(const SystemString& path, uint64_t offset, bool& err)
: ReadStream(path, err)
{
if (err)
return;
FSeek(fp, offset, SEEK_SET);
}
~ReadStream()
@@ -148,12 +168,12 @@ public:
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
if (read(buf, thisSz) != thisSz)
{
LogModule.report(logvisor::Fatal, "unable to read enough from file");
LogModule.report(logvisor::Error, "unable to read enough from file");
return written;
}
if (discio.write(buf, thisSz) != thisSz)
{
LogModule.report(logvisor::Fatal, "unable to write enough to disc");
LogModule.report(logvisor::Error, "unable to write enough to disc");
return written;
}
length -= thisSz;
@@ -164,11 +184,19 @@ public:
};
std::unique_ptr<IReadStream> beginReadStream() const
{
return std::unique_ptr<IReadStream>(new ReadStream(m_path));
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err));
if (Err)
return {};
return ret;
}
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
{
return std::unique_ptr<IReadStream>(new ReadStream(m_path, offset));
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err));
if (Err)
return {};
return ret;
}
};