2015-06-30 06:46:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-02-02 21:18:16 +00:00
|
|
|
#include <inttypes.h>
|
2016-03-04 23:04:30 +00:00
|
|
|
#include "nod/Util.hpp"
|
|
|
|
#include "nod/IFileIO.hpp"
|
2015-06-28 05:43:53 +00:00
|
|
|
|
2016-03-04 23:04:30 +00:00
|
|
|
namespace nod
|
2015-06-28 05:43:53 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
class FileIOFILE : public IFileIO
|
|
|
|
{
|
2015-07-02 18:33:55 +00:00
|
|
|
SystemString m_path;
|
2016-02-02 21:18:16 +00:00
|
|
|
int64_t m_maxWriteSize;
|
2015-06-28 05:43:53 +00:00
|
|
|
public:
|
2017-11-13 06:18:53 +00:00
|
|
|
FileIOFILE(SystemStringView path, int64_t maxWriteSize)
|
2016-02-02 21:18:16 +00:00
|
|
|
: m_path(path), m_maxWriteSize(maxWriteSize) {}
|
2015-06-30 06:46:19 +00:00
|
|
|
|
2016-01-25 05:55:31 +00:00
|
|
|
bool exists()
|
2015-06-28 05:43:53 +00:00
|
|
|
{
|
2015-06-30 06:46:19 +00:00
|
|
|
FILE* fp = fopen(m_path.c_str(), "rb");
|
2016-01-25 05:55:31 +00:00
|
|
|
if (!fp)
|
|
|
|
return false;
|
|
|
|
fclose(fp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t size()
|
|
|
|
{
|
|
|
|
FILE* fp = fopen(m_path.c_str(), "rb");
|
2015-06-30 06:46:19 +00:00
|
|
|
if (!fp)
|
|
|
|
return 0;
|
2016-01-25 05:55:31 +00:00
|
|
|
FSeek(fp, 0, SEEK_END);
|
2016-01-26 06:39:49 +00:00
|
|
|
uint64_t result = FTell(fp);
|
2015-06-30 06:46:19 +00:00
|
|
|
fclose(fp);
|
|
|
|
return result;
|
2015-06-28 05:43:53 +00:00
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
|
|
|
|
struct WriteStream : public IFileIO::IWriteStream
|
|
|
|
{
|
2016-01-21 06:30:37 +00:00
|
|
|
FILE* fp;
|
2016-02-02 21:18:16 +00:00
|
|
|
int64_t m_maxWriteSize;
|
2017-11-13 06:18:53 +00:00
|
|
|
WriteStream(SystemStringView path, int64_t maxWriteSize, bool& err)
|
2016-02-02 21:18:16 +00:00
|
|
|
: m_maxWriteSize(maxWriteSize)
|
2015-06-30 06:46:19 +00:00
|
|
|
{
|
2017-11-13 06:18:53 +00:00
|
|
|
fp = fopen(path.data(), "wb");
|
2015-06-30 06:46:19 +00:00
|
|
|
if (!fp)
|
2017-02-05 06:19:34 +00:00
|
|
|
{
|
2017-11-13 06:18:53 +00:00
|
|
|
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.data());
|
2017-02-05 06:19:34 +00:00
|
|
|
err = true;
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
}
|
2017-11-13 06:18:53 +00:00
|
|
|
WriteStream(SystemStringView path, uint64_t offset, int64_t maxWriteSize, bool& err)
|
2016-02-02 21:18:16 +00:00
|
|
|
: m_maxWriteSize(maxWriteSize)
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
2017-11-13 06:18:53 +00:00
|
|
|
fp = fopen(path.data(), "ab");
|
2016-01-25 05:55:31 +00:00
|
|
|
if (!fp)
|
|
|
|
goto FailLoc;
|
2016-01-22 23:45:58 +00:00
|
|
|
fclose(fp);
|
2017-11-13 06:18:53 +00:00
|
|
|
fp = fopen(path.data(), "r+b");
|
2016-01-21 06:30:37 +00:00
|
|
|
if (!fp)
|
2016-01-25 05:55:31 +00:00
|
|
|
goto FailLoc;
|
2016-01-23 23:36:58 +00:00
|
|
|
FSeek(fp, offset, SEEK_SET);
|
2016-01-25 05:55:31 +00:00
|
|
|
return;
|
|
|
|
FailLoc:
|
2017-11-13 06:18:53 +00:00
|
|
|
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.data());
|
2017-02-05 06:19:34 +00:00
|
|
|
err = true;
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
|
|
|
~WriteStream()
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
uint64_t write(const void* buf, uint64_t length)
|
|
|
|
{
|
2016-02-02 21:34:47 +00:00
|
|
|
if (m_maxWriteSize >= 0)
|
|
|
|
{
|
|
|
|
if (FTell(fp) + length > m_maxWriteSize)
|
2017-02-05 06:19:34 +00:00
|
|
|
{
|
|
|
|
LogModule.report(logvisor::Error, _S("write operation exceeds file's %" PRIi64 "-byte limit"), m_maxWriteSize);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-02 21:34:47 +00:00
|
|
|
}
|
2016-01-21 06:30:37 +00:00
|
|
|
return fwrite(buf, 1, length, fp);
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
};
|
|
|
|
std::unique_ptr<IWriteStream> beginWriteStream() const
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
2017-02-05 06:19:34 +00:00
|
|
|
bool Err = false;
|
|
|
|
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err));
|
|
|
|
if (Err)
|
|
|
|
return {};
|
|
|
|
return ret;
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
2016-01-22 02:30:17 +00:00
|
|
|
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
2017-02-05 06:19:34 +00:00
|
|
|
bool Err = false;
|
|
|
|
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err));
|
|
|
|
if (Err)
|
|
|
|
return {};
|
|
|
|
return ret;
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
|
|
|
|
struct ReadStream : public IFileIO::IReadStream
|
|
|
|
{
|
|
|
|
FILE* fp;
|
2017-11-13 06:18:53 +00:00
|
|
|
ReadStream(SystemStringView path, bool& err)
|
2015-06-30 06:46:19 +00:00
|
|
|
{
|
2017-11-13 06:18:53 +00:00
|
|
|
fp = fopen(path.data(), "rb");
|
2015-06-30 06:46:19 +00:00
|
|
|
if (!fp)
|
2017-02-05 06:19:34 +00:00
|
|
|
{
|
|
|
|
err = true;
|
2017-11-13 06:18:53 +00:00
|
|
|
LogModule.report(logvisor::Error, _S("unable to open '%s' for reading"), path.data());
|
2017-02-05 06:19:34 +00:00
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
}
|
2017-11-13 06:18:53 +00:00
|
|
|
ReadStream(SystemStringView path, uint64_t offset, bool& err)
|
2017-02-05 06:19:34 +00:00
|
|
|
: ReadStream(path, err)
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
2017-02-05 06:19:34 +00:00
|
|
|
if (err)
|
|
|
|
return;
|
2016-01-23 23:36:58 +00:00
|
|
|
FSeek(fp, offset, SEEK_SET);
|
2016-01-25 05:55:31 +00:00
|
|
|
}
|
|
|
|
~ReadStream()
|
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
void seek(int64_t offset, int whence)
|
|
|
|
{
|
|
|
|
FSeek(fp, offset, whence);
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
2017-07-01 23:36:16 +00:00
|
|
|
uint64_t position() const
|
2016-01-26 02:01:55 +00:00
|
|
|
{
|
|
|
|
return FTell(fp);
|
|
|
|
}
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t read(void* buf, uint64_t length)
|
2016-01-25 05:55:31 +00:00
|
|
|
{
|
|
|
|
return fread(buf, 1, length, fp);
|
|
|
|
}
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length)
|
2015-06-30 06:46:19 +00:00
|
|
|
{
|
2015-06-30 19:38:51 +00:00
|
|
|
uint64_t written = 0;
|
2016-01-26 02:01:55 +00:00
|
|
|
uint8_t buf[0x7c00];
|
2015-06-30 06:46:19 +00:00
|
|
|
while (length)
|
|
|
|
{
|
2016-03-04 23:04:30 +00:00
|
|
|
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
|
2016-01-25 05:55:31 +00:00
|
|
|
if (read(buf, thisSz) != thisSz)
|
2015-07-26 02:44:44 +00:00
|
|
|
{
|
2017-02-05 06:19:34 +00:00
|
|
|
LogModule.report(logvisor::Error, "unable to read enough from file");
|
2015-07-26 02:44:44 +00:00
|
|
|
return written;
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
if (discio.write(buf, thisSz) != thisSz)
|
2015-07-26 02:44:44 +00:00
|
|
|
{
|
2017-02-05 06:19:34 +00:00
|
|
|
LogModule.report(logvisor::Error, "unable to write enough to disc");
|
2015-07-26 02:44:44 +00:00
|
|
|
return written;
|
|
|
|
}
|
2015-06-30 06:46:19 +00:00
|
|
|
length -= thisSz;
|
|
|
|
written += thisSz;
|
|
|
|
}
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::unique_ptr<IReadStream> beginReadStream() const
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
2017-02-05 06:19:34 +00:00
|
|
|
bool Err = false;
|
|
|
|
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err));
|
|
|
|
if (Err)
|
|
|
|
return {};
|
|
|
|
return ret;
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
2016-01-22 02:30:17 +00:00
|
|
|
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
2017-02-05 06:19:34 +00:00
|
|
|
bool Err = false;
|
|
|
|
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err));
|
|
|
|
if (Err)
|
|
|
|
return {};
|
|
|
|
return ret;
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
2015-06-28 05:43:53 +00:00
|
|
|
};
|
|
|
|
|
2017-11-13 06:18:53 +00:00
|
|
|
std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize)
|
2016-01-21 06:30:37 +00:00
|
|
|
{
|
2016-02-02 21:18:16 +00:00
|
|
|
return std::unique_ptr<IFileIO>(new FileIOFILE(path, maxWriteSize));
|
2016-01-21 06:30:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-28 05:43:53 +00:00
|
|
|
}
|