copy to a temp file then rename it this is the best we can do on Win32
| 542 | // copy to a temp file then rename it |
| 543 | // this is the best we can do on Win32 |
| 544 | bool copyFile(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) { |
| 545 | auto pair = createUniqueTempFile(L"MMKV"); |
| 546 | auto tmpFD = pair.second; |
| 547 | auto &tmpPath = pair.first; |
| 548 | if (tmpFD == INVALID_HANDLE_VALUE) { |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | bool renamed = false; |
| 553 | if (copyFileContent(srcPath, tmpFD, false)) { |
| 554 | const auto &utf8SrcPath = MMKVPath_t2String(srcPath); |
| 555 | const auto &utf8TmpPath = MMKVPath_t2String(tmpPath); |
| 556 | MMKVInfo("copied file [%s] to [%s]", utf8SrcPath.c_str(), utf8TmpPath.c_str()); |
| 557 | CloseHandle(tmpFD); |
| 558 | renamed = tryAtomicRename(tmpPath.c_str(), dstPath.c_str()); |
| 559 | if (renamed) { |
| 560 | const auto &utf8DstPath = MMKVPath_t2String(dstPath); |
| 561 | MMKVInfo("copyfile [%s] to [%s] finish.", utf8SrcPath.c_str(), utf8DstPath.c_str()); |
| 562 | } |
| 563 | } else { |
| 564 | CloseHandle(tmpFD); |
| 565 | } |
| 566 | |
| 567 | if (!renamed) { |
| 568 | DeleteFile(tmpPath.c_str()); |
| 569 | } |
| 570 | return renamed; |
| 571 | } |
| 572 | |
| 573 | bool copyFileContent(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) { |
| 574 | File dstFile(dstPath, OpenFlag::WriteOnly | OpenFlag::Create | OpenFlag::Truncate); |
nothing calls this directly
no test coverage detected