From 898faffa0dfe1bd0b48866bf28f54bbfc37a0a4a Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Wed, 2 Aug 2023 12:05:13 -0700 Subject: [PATCH] use ReplaceFileA directly This was introduced in Windows 95. exiv2 does not work on older. Signed-off-by: Rosen Penev --- src/basicio.cpp | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/basicio.cpp b/src/basicio.cpp index 1ff7949e..663c6b80 100644 --- a/src/basicio.cpp +++ b/src/basicio.cpp @@ -372,28 +372,17 @@ void FileIo::transfer(BasicIo& src) { // that file has been opened with FILE_SHARE_DELETE by another process, // like a virus scanner or disk indexer // (see also http://stackoverflow.com/a/11023068) - using ReplaceFileA_t = BOOL(WINAPI*)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID); - HMODULE hKernel = ::GetModuleHandleA("kernel32.dll"); - if (hKernel) { - auto pfcn_ReplaceFileA = reinterpret_cast(GetProcAddress(hKernel, "ReplaceFileA")); - if (pfcn_ReplaceFileA) { - BOOL ret = - pfcn_ReplaceFileA(pf, fileIo->path().c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr); - if (ret == 0) { - if (GetLastError() == ERROR_FILE_NOT_FOUND) { - fs::rename(fileIo->path(), pf); - fs::remove(fileIo->path()); - } else { - throw Error(ErrorCode::kerFileRenameFailed, fileIo->path(), pf, strError()); - } - } - } else { - if (fileExists(pf) && ::remove(pf) != 0) { - throw Error(ErrorCode::kerCallFailed, pf, strError(), "fs::remove"); - } - fs::rename(fileIo->path(), pf); - fs::remove(fileIo->path()); - } + auto ret = ReplaceFileA(pf, fileIo->path().c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr); + if (ret == 0) { + if (GetLastError() != ERROR_FILE_NOT_FOUND) + throw Error(ErrorCode::kerFileRenameFailed, fileIo->path(), pf, strError()); + fs::rename(fileIo->path(), pf); + fs::remove(fileIo->path()); + } else { + if (fileExists(pf) && ::remove(pf) != 0) + throw Error(ErrorCode::kerCallFailed, pf, strError(), "fs::remove"); + fs::rename(fileIo->path(), pf); + fs::remove(fileIo->path()); } #else if (fileExists(pf) && fs::remove(pf) != 0) {