From e1cf71eac7cf552bd81ab894afdfe207f27117fd Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 11:29:45 +0300 Subject: [PATCH] Add overwrite param (#20) --- src/file_utils.cc | 15 ++++++++++----- src/file_utils.h | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/file_utils.cc b/src/file_utils.cc index dd48204..7edba4b 100644 --- a/src/file_utils.cc +++ b/src/file_utils.cc @@ -49,7 +49,7 @@ int fileCopyDecompressed(const char* existingFilePath, const char* newFilePath) return -1; } } else { - fileCopy(existingFilePath, newFilePath); + fileCopy(existingFilePath, newFilePath, true); } return 0; @@ -72,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); - fileCopy(existingFilePath, newFilePath); + fileCopy(existingFilePath, newFilePath, true); } else { gzFile outStream = gzopen(newFilePath, "wb"); if (outStream == NULL) { @@ -135,14 +135,19 @@ int _gzdecompress_file(const char* existingFilePath, const char* newFilePath) gzclose(gzstream); fclose(stream); } else { - fileCopy(existingFilePath, newFilePath); + fileCopy(existingFilePath, newFilePath, true); } return 0; } -void fileCopy(const char* existingFilePath, const char* newFilePath) +// Modelled as replacement for `CopyFileA`, but `overwrite` is the opposite to +// `bFailIfExists` param. Update callers accordingly. +void fileCopy(const char* existingFilePath, const char* newFilePath, bool overwrite) { std::error_code ec; - std::filesystem::copy_file(std::filesystem::path(existingFilePath), std::filesystem::path(newFilePath), ec); + std::filesystem::copy_options options = overwrite + ? std::filesystem::copy_options::overwrite_existing + : std::filesystem::copy_options::none; + std::filesystem::copy_file(std::filesystem::path(existingFilePath), std::filesystem::path(newFilePath), options, ec); } diff --git a/src/file_utils.h b/src/file_utils.h index fd886f7..cdac4d8 100644 --- a/src/file_utils.h +++ b/src/file_utils.h @@ -4,6 +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); +void fileCopy(const char* existingFilePath, const char* newFilePath, bool overwrite); #endif /* FILE_UTILS_H */