aboutsummaryrefslogtreecommitdiff
path: root/zenstore/basicfile.cpp
blob: 35ccdd042238e9541c5b6ee7fcd8e9404d9ccb52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zenstore/basicfile.h"

#include <zencore/except.h>
#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>

#include <fmt/format.h>
#include <gsl/gsl-lite.hpp>

namespace zen {

using namespace fmt::literals;

void
BasicFile::Open(std::filesystem::path FileName, bool isCreate)
{
	const DWORD dwCreationDisposition = isCreate ? CREATE_ALWAYS : OPEN_EXISTING;

	HRESULT hRes = m_File.Create(FileName.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, dwCreationDisposition);

	if (FAILED(hRes))
	{
		ThrowSystemException(hRes, "Failed to open bucket sobs file '{}'"_format(FileName));
	}
}

void
BasicFile::Read(void* Data, uint64_t Size, uint64_t Offset)
{
	OVERLAPPED Ovl{};

	Ovl.Offset	   = DWORD(Offset & 0xffff'ffffu);
	Ovl.OffsetHigh = DWORD(Offset >> 32);

	HRESULT hRes = m_File.Read(Data, gsl::narrow<DWORD>(Size), &Ovl);

	if (FAILED(hRes))
	{
		ThrowSystemException(hRes, "Failed to read from file '{}'"_format(zen::PathFromHandle(m_File)));
	}
}

IoBuffer
BasicFile::ReadAll()
{
	IoBuffer Buffer(FileSize());

	Read((void*)Buffer.Data(), Buffer.Size(), 0);

	return Buffer;
}

void
BasicFile::Write(const void* Data, uint64_t Size, uint64_t Offset)
{
	OVERLAPPED Ovl{};

	Ovl.Offset	   = DWORD(Offset & 0xffff'ffffu);
	Ovl.OffsetHigh = DWORD(Offset >> 32);

	HRESULT hRes = m_File.Write(Data, gsl::narrow<DWORD>(Size), &Ovl);

	if (FAILED(hRes))
	{
		ThrowSystemException(hRes, "Failed to write to file '{}'"_format(zen::PathFromHandle(m_File)));
	}
}

void
BasicFile::Flush()
{
	m_File.Flush();
}

uint64_t
BasicFile::FileSize()
{
	ULONGLONG Sz;
	m_File.GetSize(Sz);

	return uint64_t(Sz);
}

void
BasicFile::Close()
{
	m_File.Close();
}

}  // namespace zen