nod/lib/FileIOWin32.cpp

226 lines
7.1 KiB
C++
Raw Normal View History

2016-01-25 20:21:10 +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"
2016-01-25 20:21:10 +00:00
2016-03-04 23:04:30 +00:00
namespace nod
2016-01-25 20:21:10 +00:00
{
class FileIOWin32 : public IFileIO
{
SystemString m_path;
2016-02-02 21:18:16 +00:00
int64_t m_maxWriteSize;
2016-01-25 20:21:10 +00:00
public:
2017-11-13 06:18:53 +00:00
FileIOWin32(SystemStringView path, int64_t maxWriteSize)
2016-02-02 21:18:16 +00:00
: m_path(path), m_maxWriteSize(maxWriteSize) {}
2016-01-25 20:21:10 +00:00
bool exists()
{
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2016-01-25 20:21:10 +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
HANDLE fp = CreateFile2(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
#endif
2016-01-25 20:21:10 +00:00
if (fp == INVALID_HANDLE_VALUE)
return false;
CloseHandle(fp);
return true;
}
uint64_t size()
{
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2016-01-25 20:21:10 +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
HANDLE fp = CreateFile2(m_path.c_str(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
#endif
2016-01-25 20:21:10 +00:00
if (fp == INVALID_HANDLE_VALUE)
return 0;
LARGE_INTEGER sz;
if (!GetFileSizeEx(fp, &sz))
{
CloseHandle(fp);
return 0;
}
CloseHandle(fp);
return sz.QuadPart;
}
struct WriteStream : public IFileIO::IWriteStream
{
HANDLE 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)
2016-01-25 20:21:10 +00:00
{
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2017-11-13 06:18:53 +00:00
fp = CreateFileW(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE,
2016-01-25 20:21:10 +00:00
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
2017-12-06 03:23:58 +00:00
#else
fp = CreateFile2(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, CREATE_ALWAYS, nullptr);
#endif
2016-01-25 20:21:10 +00:00
if (fp == INVALID_HANDLE_VALUE)
{
2017-11-13 06:18:53 +00:00
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.data());
err = true;
}
2016-01-25 20:21:10 +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-25 20:21:10 +00:00
{
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2017-11-13 06:18:53 +00:00
fp = CreateFileW(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE,
2016-01-25 20:21:10 +00:00
nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
2017-12-06 03:23:58 +00:00
#else
fp = CreateFile2(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, OPEN_ALWAYS, nullptr);
#endif
2016-01-25 20:21:10 +00:00
if (fp == INVALID_HANDLE_VALUE)
{
2017-11-13 06:18:53 +00:00
LogModule.report(logvisor::Error, _S("unable to open '%s' for writing"), path.data());
err = true;
return;
}
2016-01-25 20:21:10 +00:00
LARGE_INTEGER lioffset;
lioffset.QuadPart = offset;
SetFilePointerEx(fp, lioffset, nullptr, FILE_BEGIN);
}
~WriteStream()
{
CloseHandle(fp);
}
uint64_t write(const void* buf, uint64_t length)
{
2016-02-02 21:34: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)
{
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-02-02 21:25:19 +00:00
2016-01-25 20:21:10 +00:00
DWORD ret = 0;
WriteFile(fp, buf, length, &ret, nullptr);
return ret;
}
};
std::unique_ptr<IWriteStream> beginWriteStream() const
{
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err));
if (Err)
return {};
return ret;
2016-01-25 20:21:10 +00:00
}
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const
{
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err));
if (Err)
return {};
return ret;
2016-01-25 20:21:10 +00:00
}
struct ReadStream : public IFileIO::IReadStream
{
HANDLE fp;
2017-11-13 06:18:53 +00:00
ReadStream(SystemStringView path, bool& err)
2016-01-25 20:21:10 +00:00
{
2017-12-06 03:23:58 +00:00
#if !WINDOWS_STORE
2017-11-14 03:35:34 +00:00
fp = CreateFileW(path.data(), GENERIC_READ, FILE_SHARE_READ,
2016-01-25 20:21:10 +00:00
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
2017-12-06 03:23:58 +00:00
#else
fp = CreateFile2(path.data(), GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);
#endif
2016-01-25 20:21:10 +00:00
if (fp == INVALID_HANDLE_VALUE)
{
err = true;
2017-11-14 03:35:34 +00:00
LogModule.report(logvisor::Error, _S("unable to open '%s' for reading"), path.data());
}
2016-01-25 20:21:10 +00:00
}
2017-11-13 06:18:53 +00:00
ReadStream(SystemStringView path, uint64_t offset, bool& err)
2017-02-05 06:59:46 +00:00
: ReadStream(path, err)
2016-01-25 20:21:10 +00:00
{
2017-02-05 06:59:46 +00:00
if (err)
return;
2016-01-25 20:21:10 +00:00
LARGE_INTEGER lioffset;
lioffset.QuadPart = offset;
SetFilePointerEx(fp, lioffset, nullptr, FILE_BEGIN);
}
~ReadStream()
{
CloseHandle(fp);
}
void seek(int64_t offset, int whence)
{
LARGE_INTEGER li;
li.QuadPart = offset;
SetFilePointerEx(fp, li, nullptr, whence);
}
uint64_t position() const
2016-01-26 02:01:55 +00:00
{
LARGE_INTEGER li = {};
LARGE_INTEGER res;
SetFilePointerEx(fp, li, &res, FILE_CURRENT);
return res.QuadPart;
}
2016-01-25 20:21:10 +00:00
uint64_t read(void* buf, uint64_t length)
{
DWORD ret = 0;
ReadFile(fp, buf, length, &ret, nullptr);
return ret;
}
uint64_t copyToDisc(IPartWriteStream& discio, uint64_t length)
{
uint64_t written = 0;
2016-01-26 02:01:55 +00:00
uint8_t buf[0x7c00];
2016-01-25 20:21:10 +00:00
while (length)
{
2016-03-04 23:04:30 +00:00
uint64_t thisSz = nod::min(uint64_t(0x7c00), length);
2016-01-25 20:21:10 +00:00
if (read(buf, thisSz) != thisSz)
{
LogModule.report(logvisor::Error, "unable to read enough from file");
2016-01-25 20:21:10 +00:00
return written;
}
if (discio.write(buf, thisSz) != thisSz)
{
LogModule.report(logvisor::Error, "unable to write enough to disc");
2016-01-25 20:21:10 +00:00
return written;
}
length -= thisSz;
written += thisSz;
}
return written;
}
};
std::unique_ptr<IReadStream> beginReadStream() const
{
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err));
if (Err)
return {};
return ret;
2016-01-25 20:21:10 +00:00
}
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const
{
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err));
if (Err)
return {};
return ret;
2016-01-25 20:21:10 +00:00
}
};
2017-11-13 06:18:53 +00:00
std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize)
2016-01-25 20:21:10 +00:00
{
2016-02-02 21:25:19 +00:00
return std::unique_ptr<IFileIO>(new FileIOWin32(path, maxWriteSize));
2016-01-25 20:21:10 +00:00
}
}