Add overwrite param (#20)

This commit is contained in:
Alexander Batalov 2022-05-28 11:29:45 +03:00
parent 23c0d3b873
commit e1cf71eac7
2 changed files with 11 additions and 6 deletions

View File

@ -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);
}

View File

@ -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 */