| 1413 | // restore |
| 1414 | |
| 1415 | static 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. |
no test coverage detected