From 83e3c364e6403883cd60e7dda10165b6e84269f3 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 21 May 2021 20:40:09 +0200 Subject: Moved CasBlobFile into basicfile.h --- zenstore/basicfile.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 zenstore/basicfile.cpp (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp new file mode 100644 index 000000000..961e43735 --- /dev/null +++ b/zenstore/basicfile.cpp @@ -0,0 +1,85 @@ +#include "zenstore/basicfile.h" + +#include +#include + +#include +#include + +namespace zen { + +using namespace fmt::literals; + +void +CasBlobFile::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)) + { + throw std::system_error(GetLastError(), std::system_category(), "Failed to open bucket sobs file '{}'"_format(FileName)); + } +} + +void +CasBlobFile::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(Size), &Ovl); + + if (FAILED(hRes)) + { + throw std::system_error(GetLastError(), + std::system_category(), + "Failed to read from file '{}'"_format(zen::PathFromHandle(m_File))); + } +} + +IoBuffer +CasBlobFile::ReadAll() +{ + IoBuffer Buffer(FileSize()); + + Read((void*)Buffer.Data(), Buffer.Size(), 0); + + return Buffer; +} + +void +CasBlobFile::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(Size), &Ovl); + + if (FAILED(hRes)) + { + throw std::system_error(GetLastError(), std::system_category(), "Failed to write to file '{}'"_format(zen::PathFromHandle(m_File))); + } +} + +void +CasBlobFile::Flush() +{ + m_File.Flush(); +} + +uint64_t +CasBlobFile::FileSize() +{ + ULONGLONG Sz; + m_File.GetSize(Sz); + + return uint64_t(Sz); +} + +} -- cgit v1.2.3 From edb2ea0cae30aeac74d1d48ab9754754f010c818 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 21 May 2021 20:43:36 +0200 Subject: Renamed CasBlobFile -> BasicFile --- zenstore/basicfile.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 961e43735..05af4f792 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -11,7 +11,7 @@ namespace zen { using namespace fmt::literals; void -CasBlobFile::Open(std::filesystem::path FileName, bool isCreate) +BasicFile::Open(std::filesystem::path FileName, bool isCreate) { const DWORD dwCreationDisposition = isCreate ? CREATE_ALWAYS : OPEN_EXISTING; @@ -24,7 +24,7 @@ CasBlobFile::Open(std::filesystem::path FileName, bool isCreate) } void -CasBlobFile::Read(void* Data, uint64_t Size, uint64_t Offset) +BasicFile::Read(void* Data, uint64_t Size, uint64_t Offset) { OVERLAPPED Ovl{}; @@ -42,7 +42,7 @@ CasBlobFile::Read(void* Data, uint64_t Size, uint64_t Offset) } IoBuffer -CasBlobFile::ReadAll() +BasicFile::ReadAll() { IoBuffer Buffer(FileSize()); @@ -52,7 +52,7 @@ CasBlobFile::ReadAll() } void -CasBlobFile::Write(const void* Data, uint64_t Size, uint64_t Offset) +BasicFile::Write(const void* Data, uint64_t Size, uint64_t Offset) { OVERLAPPED Ovl{}; @@ -68,13 +68,13 @@ CasBlobFile::Write(const void* Data, uint64_t Size, uint64_t Offset) } void -CasBlobFile::Flush() +BasicFile::Flush() { m_File.Flush(); } uint64_t -CasBlobFile::FileSize() +BasicFile::FileSize() { ULONGLONG Sz; m_File.GetSize(Sz); -- cgit v1.2.3 From bce1dfc6b75857e46cd36a44467454a7b02cb7ed Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 21 May 2021 21:22:41 +0200 Subject: Fixed up error reporting in BasicFile, now uses ThrowSystemException() to correctly report the HRESULT error code which would previously always be zero --- zenstore/basicfile.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 05af4f792..0164a45ee 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -19,7 +20,7 @@ BasicFile::Open(std::filesystem::path FileName, bool isCreate) if (FAILED(hRes)) { - throw std::system_error(GetLastError(), std::system_category(), "Failed to open bucket sobs file '{}'"_format(FileName)); + ThrowSystemException(hRes, "Failed to open bucket sobs file '{}'"_format(FileName)); } } @@ -35,9 +36,7 @@ BasicFile::Read(void* Data, uint64_t Size, uint64_t Offset) if (FAILED(hRes)) { - throw std::system_error(GetLastError(), - std::system_category(), - "Failed to read from file '{}'"_format(zen::PathFromHandle(m_File))); + ThrowSystemException(hRes, "Failed to read from file '{}'"_format(zen::PathFromHandle(m_File))); } } @@ -63,7 +62,7 @@ BasicFile::Write(const void* Data, uint64_t Size, uint64_t Offset) if (FAILED(hRes)) { - throw std::system_error(GetLastError(), std::system_category(), "Failed to write to file '{}'"_format(zen::PathFromHandle(m_File))); + ThrowSystemException(hRes, "Failed to write to file '{}'"_format(zen::PathFromHandle(m_File))); } } @@ -82,4 +81,4 @@ BasicFile::FileSize() return uint64_t(Sz); } -} +} // namespace zen -- cgit v1.2.3 From c0760dafc01a21abffb207a2598b978930d6fcb9 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Sat, 22 May 2021 11:40:03 +0200 Subject: clang-format --- zenstore/basicfile.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 0164a45ee..7f10fc5e6 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -1,8 +1,10 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + #include "zenstore/basicfile.h" +#include #include #include -#include #include #include -- cgit v1.2.3