From 26f718b04bc0fba3831124d9dec705c2febd426e Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Wed, 12 Mar 2025 11:19:06 +0100 Subject: fix mac/linux builds command (#303) * fix linux/mac version of GetModificationTickFromPath and CopyFile --- src/zencore/filesystem.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'src/zencore/filesystem.cpp') diff --git a/src/zencore/filesystem.cpp b/src/zencore/filesystem.cpp index 85feab2f7..9f3f4f7fc 100644 --- a/src/zencore/filesystem.cpp +++ b/src/zencore/filesystem.cpp @@ -590,7 +590,7 @@ CopyFile(const std::filesystem::path& FromPath, const std::filesystem::path& ToP ScopedFd $From = {FromFd}; // To file - int ToFd = open(ToPath.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0666); + int ToFd = open(ToPath.c_str(), O_WRONLY | O_CREAT | O_CLOEXEC, 0666); if (ToFd < 0) { ThrowLastError(fmt::format("failed to create file {}", ToPath)); @@ -598,9 +598,14 @@ CopyFile(const std::filesystem::path& FromPath, const std::filesystem::path& ToP fchmod(ToFd, 0666); ScopedFd $To = {ToFd}; + struct stat Stat; + fstat(FromFd, &Stat); + + size_t FileSizeBytes = Stat.st_size; + // Copy impl - static const size_t BufferSize = 64 << 10; - void* Buffer = malloc(BufferSize); + const size_t BufferSize = Min(FileSizeBytes, 64u << 10); + void* Buffer = malloc(BufferSize); while (true) { int BytesRead = read(FromFd, Buffer, BufferSize); @@ -610,7 +615,7 @@ CopyFile(const std::filesystem::path& FromPath, const std::filesystem::path& ToP break; } - if (write(ToFd, Buffer, BytesRead) != BufferSize) + if (write(ToFd, Buffer, BytesRead) != BytesRead) { Success = false; break; @@ -621,7 +626,7 @@ CopyFile(const std::filesystem::path& FromPath, const std::filesystem::path& ToP if (!Success) { - ThrowLastError("file copy failed"sv); + ThrowLastError(fmt::format("file copy from {} to {} failed", FromPath, ToPath)); } return true; @@ -1483,16 +1488,7 @@ GetModificationTickFromPath(const std::filesystem::path& Filename) { ThrowLastError(fmt::format("Failed to open file {} to check modification tick.", Filename)); } - auto _ = MakeGuard([Handle]() { CloseHandle(Handle); }); -#else - int Fd = open(Filename.c_str(), O_RDONLY | O_CLOEXEC); - if (Fd <= 9) - { - ThrowLastError(fmt::format("Failed to open file {} to check modification tick.", Filename)); - } - Handle = (void*)uintptr_t(Fd); - auto _ = MakeGuard([Handle]() { close(int(uintptr_t(Handle))); }); -#endif + auto _ = MakeGuard([Handle]() { CloseHandle(Handle); }); std::error_code Ec; uint64_t ModificatonTick = GetModificationTickFromHandle(Handle, Ec); if (Ec) @@ -1500,6 +1496,15 @@ GetModificationTickFromPath(const std::filesystem::path& Filename) ThrowSystemError(Ec.value(), Ec.message()); } return ModificatonTick; +#else + struct stat Stat; + int err = stat(Filename.native().c_str(), &Stat); + if (err) + { + ThrowLastError(fmt::format("Failed to get mode of file {}", Filename)); + } + return gsl::narrow(Stat.st_mtime); +#endif } std::filesystem::path -- cgit v1.2.3