nod/lib/FileIOWin32.cpp

208 lines
6.3 KiB
C++
Raw Normal View History

#include <cinttypes>
#include <cstdint>
2017-12-29 07:57:54 +00:00
#include <cstdio>
#include <cstdlib>
2016-03-04 23:04:30 +00:00
#include "nod/IFileIO.hpp"
#include "nod/Util.hpp"
#include <logvisor/logvisor.hpp>
2016-01-25 20:21:10 +00:00
2018-12-08 05:21:47 +00:00
namespace nod {
class FileIOWin32 : public IFileIO {
SystemString m_path;
int64_t m_maxWriteSize;
2016-01-25 20:21:10 +00:00
public:
2018-12-08 05:21:47 +00:00
FileIOWin32(SystemStringView path, int64_t maxWriteSize) : m_path(path), m_maxWriteSize(maxWriteSize) {}
2016-01-25 20:21:10 +00:00
bool exists() override {
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2018-12-08 05:21:47 +00:00
HANDLE fp = CreateFileW(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, nullptr);
2017-12-06 03:23:58 +00:00
#else
2018-12-08 05:21:47 +00:00
HANDLE fp = CreateFile2(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
2017-12-06 03:23:58 +00:00
#endif
2018-12-08 05:21:47 +00:00
if (fp == INVALID_HANDLE_VALUE)
return false;
CloseHandle(fp);
return true;
}
2016-01-25 20:21:10 +00:00
uint64_t size() override {
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2018-12-08 05:21:47 +00:00
HANDLE fp = CreateFileW(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, nullptr);
2017-12-06 03:23:58 +00:00
#else
2018-12-08 05:21:47 +00:00
HANDLE fp = CreateFile2(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
2017-12-06 03:23:58 +00:00
#endif
2018-12-08 05:21:47 +00:00
if (fp == INVALID_HANDLE_VALUE)
return 0;
LARGE_INTEGER sz;
if (!GetFileSizeEx(fp, &sz)) {
CloseHandle(fp);
return 0;
2016-01-25 20:21:10 +00:00
}
2018-12-08 05:21:47 +00:00
CloseHandle(fp);
return sz.QuadPart;
}
2016-01-25 20:21:10 +00:00
2018-12-08 05:21:47 +00:00
struct WriteStream : public IFileIO::IWriteStream {
HANDLE fp;
int64_t m_maxWriteSize;
WriteStream(SystemStringView path, int64_t maxWriteSize, bool& err) : m_maxWriteSize(maxWriteSize) {
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2018-12-08 05:21:47 +00:00
fp = CreateFileW(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
nullptr);
2017-12-06 03:23:58 +00:00
#else
2018-12-08 05:21:47 +00:00
fp = CreateFile2(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, CREATE_ALWAYS, nullptr);
2017-12-06 03:23:58 +00:00
#endif
2018-12-08 05:21:47 +00:00
if (fp == INVALID_HANDLE_VALUE) {
2020-04-11 22:45:06 +00:00
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for writing")), path);
2018-12-08 05:21:47 +00:00
err = true;
}
}
WriteStream(SystemStringView path, uint64_t offset, int64_t maxWriteSize, bool& err)
: m_maxWriteSize(maxWriteSize) {
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2018-12-08 05:21:47 +00:00
fp = CreateFileW(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
nullptr);
2017-12-06 03:23:58 +00:00
#else
2018-12-08 05:21:47 +00:00
fp = CreateFile2(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, OPEN_ALWAYS, nullptr);
2017-12-06 03:23:58 +00:00
#endif
2018-12-08 05:21:47 +00:00
if (fp == INVALID_HANDLE_VALUE) {
2020-04-11 22:45:06 +00:00
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for writing")), path);
2018-12-08 05:21:47 +00:00
err = true;
return;
}
LARGE_INTEGER lioffset;
lioffset.QuadPart = offset;
SetFilePointerEx(fp, lioffset, nullptr, FILE_BEGIN);
}
~WriteStream() override { CloseHandle(fp); }
uint64_t write(const void* buf, uint64_t length) override {
2018-12-08 05:21:47 +00:00
if (m_maxWriteSize >= 0) {
LARGE_INTEGER li = {};
LARGE_INTEGER res;
SetFilePointerEx(fp, li, &res, FILE_CURRENT);
if (res.QuadPart + int64_t(length) > m_maxWriteSize) {
2020-04-11 22:45:06 +00:00
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("write operation exceeds file's {}-byte limit")),
2018-12-08 05:21:47 +00:00
m_maxWriteSize);
return 0;
2016-01-25 20:21:10 +00:00
}
2018-12-08 05:21:47 +00:00
}
2016-02-02 21:25:19 +00:00
2018-12-08 05:21:47 +00:00
DWORD ret = 0;
WriteFile(fp, buf, length, &ret, nullptr);
return ret;
2016-01-25 20:21:10 +00:00
}
2018-12-08 05:21:47 +00:00
};
std::unique_ptr<IWriteStream> beginWriteStream() const override {
bool err = false;
auto ret = std::make_unique<WriteStream>(m_path, m_maxWriteSize, err);
if (err) {
return nullptr;
}
2018-12-08 05:21:47 +00:00
return ret;
}
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
bool err = false;
auto ret = std::make_unique<WriteStream>(m_path, offset, m_maxWriteSize, err);
if (err) {
return nullptr;
}
2018-12-08 05:21:47 +00:00
return ret;
}
2016-01-25 20:21:10 +00:00
2018-12-08 05:21:47 +00:00
struct ReadStream : public IFileIO::IReadStream {
HANDLE fp;
ReadStream(SystemStringView path, bool& err) {
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2018-12-08 05:21:47 +00:00
fp = CreateFileW(path.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
nullptr);
2017-12-06 03:23:58 +00:00
#else
2018-12-08 05:21:47 +00:00
fp = CreateFile2(path.data(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
2017-12-06 03:23:58 +00:00
#endif
2018-12-08 05:21:47 +00:00
if (fp == INVALID_HANDLE_VALUE) {
err = true;
2020-04-11 22:45:06 +00:00
LogModule.report(logvisor::Error, FMT_STRING(_SYS_STR("unable to open '{}' for reading")), path);
2018-12-08 05:21:47 +00:00
}
}
ReadStream(SystemStringView path, uint64_t offset, bool& err) : ReadStream(path, err) {
if (err)
return;
LARGE_INTEGER lioffset;
lioffset.QuadPart = offset;
SetFilePointerEx(fp, lioffset, nullptr, FILE_BEGIN);
}
~ReadStream() override { CloseHandle(fp); }
void seek(int64_t offset, int whence) override {
2018-12-08 05:21:47 +00:00
LARGE_INTEGER li;
li.QuadPart = offset;
SetFilePointerEx(fp, li, nullptr, whence);
}
uint64_t position() const override {
2018-12-08 05:21:47 +00:00
LARGE_INTEGER li = {};
LARGE_INTEGER res;
SetFilePointerEx(fp, li, &res, FILE_CURRENT);
return res.QuadPart;
}
uint64_t read(void* buf, uint64_t length) override {
2018-12-08 05:21:47 +00:00
DWORD ret = 0;
ReadFile(fp, buf, length, &ret, nullptr);
return ret;
}
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length) override {
2018-12-08 05:21:47 +00: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 22:45:06 +00:00
LogModule.report(logvisor::Error, FMT_STRING("unable to read enough from file"));
2018-12-08 05:21:47 +00:00
return written;
2016-01-25 20:21:10 +00:00
}
2018-12-08 05:21:47 +00:00
if (discio.write(buf, thisSz) != thisSz) {
2020-04-11 22:45:06 +00:00
LogModule.report(logvisor::Error, FMT_STRING("unable to write enough to disc"));
2018-12-08 05:21:47 +00:00
return written;
2016-01-25 20:21:10 +00:00
}
2018-12-08 05:21:47 +00:00
length -= thisSz;
written += thisSz;
}
return written;
2016-01-25 20:21:10 +00:00
}
2018-12-08 05:21:47 +00:00
};
std::unique_ptr<IReadStream> beginReadStream() const override {
bool err = false;
auto ret = std::make_unique<ReadStream>(m_path, err);
if (err) {
return nullptr;
}
2018-12-08 05:21:47 +00:00
return ret;
}
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool err = false;
auto ret = std::make_unique<ReadStream>(m_path, offset, err);
if (err) {
return nullptr;
}
2018-12-08 05:21:47 +00:00
return ret;
}
2016-01-25 20:21:10 +00:00
};
2018-12-08 05:21:47 +00:00
std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize) {
return std::make_unique<FileIOWin32>(path, maxWriteSize);
2016-01-25 20:21:10 +00:00
}
2018-12-08 05:21:47 +00:00
} // namespace nod