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

Function restoreOneFromDirectoryByFilePath

Core/MMKV.cpp:1415–1439  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1413// restore
1414
1415static bool restoreOneFromDirectoryByFilePath(const string &mmapKey, const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
1416 auto dstCRCPath = dstPath + CRC_SUFFIX;
1417 File dstCRCFile(std::move(dstCRCPath), OpenFlag::ReadWrite | OpenFlag::Create);
1418 if (!dstCRCFile.isFileValid()) {
1419 return false;
1420 }
1421
1422 bool ret;
1423 {
1424 const auto &srcUTF8Path = MMKVPath_t2String(srcPath);
1425 const auto &dstUTF8Path = MMKVPath_t2String(dstPath);
1426 MMKVInfo("restore one mmkv[%s] from [%s] to [%s]", mmapKey.c_str(), srcUTF8Path.c_str(), dstUTF8Path.c_str());
1427 FileLock fileLock(dstCRCFile.getFd());
1428 InterProcessLock lock(&fileLock, ExclusiveLockType);
1429 SCOPED_LOCK(&lock);
1430
1431 ret = copyFileContent(srcPath, dstPath);
1432 if (ret) {
1433 auto srcCRCPath = srcPath + CRC_SUFFIX;
1434 ret = copyFileContent(srcCRCPath, dstCRCFile.getFd());
1435 }
1436 MMKVInfo("finish restore one mmkv[%s]", mmapKey.c_str());
1437 }
1438 return ret;
1439}
1440
1441// We can't simply replace the existing file, because other processes might have already open it.
1442// They won't know a difference when the file has been replaced.

Callers 1

Calls 4

MMKVPath_t2StringFunction · 0.85
copyFileContentFunction · 0.70
isFileValidMethod · 0.45
getFdMethod · 0.45

Tested by

no test coverage detected