From 23c0d3b873ddaf35a2cd2cf99eaa3597d339b138 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C5=A0imek?= Date: Sat, 28 May 2022 09:38:16 +0200 Subject: [PATCH] Replace CopyFileA with a cross-platform function (#20) Co-authored-by: Alexander Batalov --- src/file_utils.cc | 15 ++++++++++----- src/file_utils.h | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/file_utils.cc b/src/file_utils.cc index 725eb62..dd48204 100644 --- a/src/file_utils.cc +++ b/src/file_utils.cc @@ -6,8 +6,7 @@ #include #include -#define WIN32_LEAN_AND_MEAN -#include +#include // 0x452740 int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath) @@ -50,7 +49,7 @@ int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath) return -1; } } else { - CopyFileA(existingFilePath, newFilePath, FALSE); + fileCopy(existingFilePath, newFilePath); } return 0; @@ -73,7 +72,7 @@ int fileCopyCompressed(const char* existingFilePath, const char* newFilePath) // Source file is already gzipped, there is no need to do anything // besides copying. fclose(inStream); - CopyFileA(existingFilePath, newFilePath, FALSE); + fileCopy(existingFilePath, newFilePath); } else { gzFile outStream = gzopen(newFilePath, "wb"); if (outStream == NULL) { @@ -136,8 +135,14 @@ int _gzdecompress_file(const char* existingFilePath, const char* newFilePath) gzclose(gzstream); fclose(stream); } else { - CopyFileA(existingFilePath, newFilePath, FALSE); + fileCopy(existingFilePath, newFilePath); } return 0; } + +void fileCopy(const char* existingFilePath, const char* newFilePath) +{ + std::error_code ec; + std::filesystem::copy_file(std::filesystem::path(existingFilePath), std::filesystem::path(newFilePath), ec); +} diff --git a/src/file_utils.h b/src/file_utils.h index 10f626a..fd886f7 100644 --- a/src/file_utils.h +++ b/src/file_utils.h @@ -4,5 +4,6 @@ int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath); int fileCopyCompressed(const char* existingFilePath, const char* newFilePath); int _gzdecompress_file(const char* existingFilePath, const char* newFilePath); +void fileCopy(const char* existingFilePath, const char* newFilePath); #endif /* FILE_UTILS_H */