nod/lib/FileIOFILE.cpp

160 lines
4.4 KiB
C++
Raw Permalink Normal View History

#include <cinttypes>
2017-12-28 23:57:54 -08:00
#include <cstdio>
#include <cstdint>
2017-12-28 23:57:54 -08:00
#include <cstdlib>
2016-03-04 15:04:30 -08:00
#include "nod/IFileIO.hpp"
#include "nod/Util.hpp"
#include <logvisor/logvisor.hpp>
2015-06-27 22:43:53 -07:00
2018-12-07 21:21:47 -08:00
namespace nod {
class FileIOFILE : public IFileIO {
std::string m_path;
2018-12-07 21:21:47 -08:00
int64_t m_maxWriteSize;
2015-06-27 22:43:53 -07:00
public:
FileIOFILE(std::string_view path, int64_t maxWriteSize) : m_path(path), m_maxWriteSize(maxWriteSize) {}
2015-06-29 23:46:19 -07:00
bool exists() override {
FILE* fp = Fopen(m_path.c_str(), "rb");
2018-12-07 21:21:47 -08:00
if (!fp)
return false;
fclose(fp);
return true;
}
2016-01-24 21:55:31 -08:00
uint64_t size() override {
FILE* fp = Fopen(m_path.c_str(), "rb");
2018-12-07 21:21:47 -08:00
if (!fp)
return 0;
FSeek(fp, 0, SEEK_END);
uint64_t result = FTell(fp);
fclose(fp);
return result;
}
2015-06-29 23:46:19 -07:00
2018-12-07 21:21:47 -08:00
struct WriteStream : public IFileIO::IWriteStream {
FILE* fp;
int64_t m_maxWriteSize;
WriteStream(std::string_view path, int64_t maxWriteSize, bool& err) : m_maxWriteSize(maxWriteSize) {
fp = Fopen(path.data(), "wb");
2018-12-07 21:21:47 -08:00
if (!fp) {
LogModule.report(logvisor::Error, FMT_STRING("unable to open '{}' for writing"), path);
2018-12-07 21:21:47 -08:00
err = true;
}
2016-01-20 22:30:37 -08:00
}
WriteStream(std::string_view path, uint64_t offset, int64_t maxWriteSize, bool& err)
2018-12-07 21:21:47 -08:00
: m_maxWriteSize(maxWriteSize) {
fp = Fopen(path.data(), "ab");
2018-12-07 21:21:47 -08:00
if (!fp)
goto FailLoc;
fclose(fp);
fp = Fopen(path.data(), "r+b");
2018-12-07 21:21:47 -08:00
if (!fp)
goto FailLoc;
FSeek(fp, offset, SEEK_SET);
return;
FailLoc:
LogModule.report(logvisor::Error, FMT_STRING("unable to open '{}' for writing"), path);
2018-12-07 21:21:47 -08:00
err = true;
2016-01-20 22:30:37 -08:00
}
~WriteStream() override { fclose(fp); }
uint64_t write(const void* buf, uint64_t length) override {
2018-12-07 21:21:47 -08:00
if (m_maxWriteSize >= 0) {
if (FTell(fp) + length > m_maxWriteSize) {
LogModule.report(logvisor::Error, FMT_STRING("write operation exceeds file's {}-byte limit"),
2018-12-07 21:21:47 -08:00
m_maxWriteSize);
return 0;
2015-06-29 23:46:19 -07:00
}
2018-12-07 21:21:47 -08:00
}
return fwrite(buf, 1, length, fp);
}
};
std::unique_ptr<IWriteStream> beginWriteStream() const override {
bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, err));
2019-11-23 19:24:33 -08:00
if (err)
return {};
2018-12-07 21:21:47 -08:00
return ret;
}
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, err));
2019-11-23 19:24:33 -08:00
if (err)
return {};
2018-12-07 21:21:47 -08:00
return ret;
}
struct ReadStream : public IFileIO::IReadStream {
FILE* fp;
ReadStream(std::string_view path, bool& err) {
fp = Fopen(path.data(), "rb");
2018-12-07 21:21:47 -08:00
if (!fp) {
err = true;
LogModule.report(logvisor::Error, FMT_STRING("unable to open '{}' for reading"), path);
2018-12-07 21:21:47 -08:00
}
}
ReadStream(std::string_view path, uint64_t offset, bool& err) : ReadStream(path, err) {
2018-12-07 21:21:47 -08:00
if (err)
return;
FSeek(fp, offset, SEEK_SET);
}
~ReadStream() override { fclose(fp); }
void seek(int64_t offset, int whence) override { FSeek(fp, offset, whence); }
uint64_t position() const override { return FTell(fp); }
uint64_t read(void* buf, uint64_t length) override { return fread(buf, 1, length, fp); }
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length) override {
2018-12-07 21:21:47 -08:00
uint64_t written = 0;
uint8_t buf[0x7c00];
while (length) {
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
if (read(buf, thisSz) != thisSz) {
2020-04-11 15:45:06 -07:00
LogModule.report(logvisor::Error, FMT_STRING("unable to read enough from file"));
2018-12-07 21:21:47 -08:00
return written;
2016-01-24 21:55:31 -08:00
}
2018-12-07 21:21:47 -08:00
if (discio.write(buf, thisSz) != thisSz) {
2020-04-11 15:45:06 -07:00
LogModule.report(logvisor::Error, FMT_STRING("unable to write enough to disc"));
2018-12-07 21:21:47 -08:00
return written;
2015-06-29 23:46:19 -07:00
}
2018-12-07 21:21:47 -08:00
length -= thisSz;
written += thisSz;
}
return written;
2016-01-20 22:30:37 -08:00
}
2018-12-07 21:21:47 -08:00
};
std::unique_ptr<IReadStream> beginReadStream() const override {
bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, err));
2019-11-23 19:24:33 -08:00
if (err)
return {};
2018-12-07 21:21:47 -08:00
return ret;
}
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, err));
2019-11-23 19:24:33 -08:00
if (err)
return {};
2018-12-07 21:21:47 -08:00
return ret;
}
2015-06-27 22:43:53 -07:00
};
std::unique_ptr<IFileIO> NewFileIO(std::string_view path, int64_t maxWriteSize) {
return std::make_unique<FileIOFILE>(path, maxWriteSize);
2016-01-20 22:30:37 -08:00
}
2018-12-07 21:21:47 -08:00
} // namespace nod