| 452 | } |
| 453 | |
| 454 | static pair<MMKVPath_t, MMKVFileHandle_t> createUniqueTempFile(const wchar_t *prefix) { |
| 455 | wchar_t lpTempPathBuffer[MAX_PATH]; |
| 456 | // Gets the temp path env string (no guarantee it's a valid path). |
| 457 | auto dwRetVal = GetTempPath(MAX_PATH, lpTempPathBuffer); |
| 458 | if (dwRetVal > MAX_PATH || (dwRetVal == 0)) { |
| 459 | MMKVError("GetTempPath failed %d", GetLastError()); |
| 460 | return {L"", INVALID_HANDLE_VALUE}; |
| 461 | } |
| 462 | // Generates a temporary file name. |
| 463 | wchar_t szTempFileName[MAX_PATH]; |
| 464 | if (!GetTempFileName(lpTempPathBuffer, prefix, 0, szTempFileName)) { |
| 465 | MMKVError("GetTempFileName failed %d", GetLastError()); |
| 466 | return {L"", INVALID_HANDLE_VALUE}; |
| 467 | } |
| 468 | auto hTempFile = |
| 469 | CreateFile(szTempFileName, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 470 | const auto &utf8Path = MMKVPath_t2String(szTempFileName); |
| 471 | if (hTempFile == INVALID_HANDLE_VALUE) { |
| 472 | MMKVError("fail to create unique temp file [%s], %d", utf8Path.c_str(), GetLastError()); |
| 473 | return {L"", INVALID_HANDLE_VALUE}; |
| 474 | } |
| 475 | MMKVDebug("create unique temp file [%s] with fd[%p]", utf8Path.c_str(), hTempFile); |
| 476 | return {MMKVPath_t(szTempFileName), hTempFile}; |
| 477 | } |
| 478 | |
| 479 | bool tryAtomicRename(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) { |
| 480 | if (MoveFileEx(srcPath.c_str(), dstPath.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) == 0) { |
no test coverage detected