Skip to content

Commit 898faff

Browse files
committed
use ReplaceFileA directly
This was introduced in Windows 95. exiv2 does not work on older. Signed-off-by: Rosen Penev <[email protected]>
1 parent 43ffc61 commit 898faff

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/basicio.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -372,28 +372,17 @@ void FileIo::transfer(BasicIo& src) {
372372
// that file has been opened with FILE_SHARE_DELETE by another process,
373373
// like a virus scanner or disk indexer
374374
// (see also http://stackoverflow.com/a/11023068)
375-
using ReplaceFileA_t = BOOL(WINAPI*)(LPCSTR, LPCSTR, LPCSTR, DWORD, LPVOID, LPVOID);
376-
HMODULE hKernel = ::GetModuleHandleA("kernel32.dll");
377-
if (hKernel) {
378-
auto pfcn_ReplaceFileA = reinterpret_cast<ReplaceFileA_t>(GetProcAddress(hKernel, "ReplaceFileA"));
379-
if (pfcn_ReplaceFileA) {
380-
BOOL ret =
381-
pfcn_ReplaceFileA(pf, fileIo->path().c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr);
382-
if (ret == 0) {
383-
if (GetLastError() == ERROR_FILE_NOT_FOUND) {
384-
fs::rename(fileIo->path(), pf);
385-
fs::remove(fileIo->path());
386-
} else {
387-
throw Error(ErrorCode::kerFileRenameFailed, fileIo->path(), pf, strError());
388-
}
389-
}
390-
} else {
391-
if (fileExists(pf) && ::remove(pf) != 0) {
392-
throw Error(ErrorCode::kerCallFailed, pf, strError(), "fs::remove");
393-
}
394-
fs::rename(fileIo->path(), pf);
395-
fs::remove(fileIo->path());
396-
}
375+
auto ret = ReplaceFileA(pf, fileIo->path().c_str(), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS, nullptr, nullptr);
376+
if (ret == 0) {
377+
if (GetLastError() != ERROR_FILE_NOT_FOUND)
378+
throw Error(ErrorCode::kerFileRenameFailed, fileIo->path(), pf, strError());
379+
fs::rename(fileIo->path(), pf);
380+
fs::remove(fileIo->path());
381+
} else {
382+
if (fileExists(pf) && ::remove(pf) != 0)
383+
throw Error(ErrorCode::kerCallFailed, pf, strError(), "fs::remove");
384+
fs::rename(fileIo->path(), pf);
385+
fs::remove(fileIo->path());
397386
}
398387
#else
399388
if (fileExists(pf) && fs::remove(pf) != 0) {

0 commit comments

Comments
 (0)