aboutsummaryrefslogtreecommitdiff
path: root/zencore/filesystem.cpp
diff options
context:
space:
mode:
authorMartin Ridgers <[email protected]>2021-09-16 16:40:28 +0200
committerMartin Ridgers <[email protected]>2021-09-16 16:40:28 +0200
commit20d31dc186d227e99f5f9949ddf1e011f96de248 (patch)
tree73bdc3cd838ea917bc323038862607c58c2be53a /zencore/filesystem.cpp
parentStubbed out seldom-used *File() functions with a ZEN_ERROR() for Linux (diff)
downloadzen-20d31dc186d227e99f5f9949ddf1e011f96de248.tar.xz
zen-20d31dc186d227e99f5f9949ddf1e011f96de248.zip
POSIX implementation of WriteFile()
Diffstat (limited to 'zencore/filesystem.cpp')
-rw-r--r--zencore/filesystem.cpp31
1 files changed, 30 insertions, 1 deletions
diff --git a/zencore/filesystem.cpp b/zencore/filesystem.cpp
index 4b96f057e..6c5c32565 100644
--- a/zencore/filesystem.cpp
+++ b/zencore/filesystem.cpp
@@ -16,7 +16,11 @@
# include <atlfile.h>
# include <winioctl.h>
# include <winnt.h>
+#else
+# include <fcntl.h>
+# include <sys/stat.h>
#endif
+
#include <filesystem>
#include <doctest/doctest.h>
@@ -447,6 +451,7 @@ WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t Buffer
{
using namespace fmt::literals;
+#if ZEN_PLATFORM_WINDOWS
CAtlFile Outfile;
HRESULT hRes = Outfile.Create(Path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS);
if (hRes == HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND))
@@ -461,6 +466,20 @@ WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t Buffer
zen::ThrowSystemException(hRes, "File open failed for '{}'"_format(Path).c_str());
}
+#else
+ int Fd = open(Path.c_str(), O_WRONLY);
+ if (Fd < 0)
+ {
+ zen::CreateDirectories(Path.parent_path());
+ Fd = open(Path.c_str(), O_WRONLY);
+ }
+
+ if (Fd < 0)
+ {
+ ThrowLastError("File open failed for '{}'"_format(Path));
+ }
+#endif
+
// TODO: this should be block-enlightened
for (size_t i = 0; i < BufferCount; ++i)
@@ -472,17 +491,27 @@ WriteFile(std::filesystem::path Path, const IoBuffer* const* Data, size_t Buffer
{
const uint64_t ChunkSize = zen::Min<uint64_t>(WriteSize, uint64_t(2) * 1024 * 1024 * 1024);
+#if ZEN_PLATFORM_WINDOWS
hRes = Outfile.Write(DataPtr, gsl::narrow_cast<uint32_t>(WriteSize));
-
if (FAILED(hRes))
{
zen::ThrowSystemException(hRes, "File write failed for '{}'"_format(Path).c_str());
}
+#else
+ if (write(Fd, DataPtr, WriteSize) != WriteSize)
+ {
+ ThrowLastError("File write failed for '{}'"_format(Path));
+ }
+#endif // ZEN_PLATFORM_WINDOWS
WriteSize -= ChunkSize;
DataPtr = reinterpret_cast<const uint8_t*>(DataPtr) + ChunkSize;
}
}
+
+#if !ZEN_PLATFORM_WINDOWS
+ close(Fd);
+#endif
}
void