MCPcopy Create free account
hub / github.com/Tencent/MMKV / copyFile

Function copyFile

Core/MemoryFile_Win32.cpp:544–571  ·  view source on GitHub ↗

copy to a temp file then rename it this is the best we can do on Win32

Source from the content-addressed store, hash-verified

542// copy to a temp file then rename it
543// this is the best we can do on Win32
544bool 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
573bool copyFileContent(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
574 File dstFile(dstPath, OpenFlag::WriteOnly | OpenFlag::Create | OpenFlag::Truncate);

Callers

nothing calls this directly

Calls 4

MMKVPath_t2StringFunction · 0.85
createUniqueTempFileFunction · 0.70
copyFileContentFunction · 0.70
tryAtomicRenameFunction · 0.70

Tested by

no test coverage detected