Skip to content

Commit

Permalink
make copyFile() robust with rename() failture #1432
Browse files Browse the repository at this point in the history
  • Loading branch information
lingol committed Nov 15, 2024
1 parent 90516e3 commit b3e4995
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Core/MemoryFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,13 @@ bool copyFile(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
if (copyFileContent(srcPath, tmpFD, false)) {
MMKVInfo("copyfile [%s] to [%s]", srcPath.c_str(), tmpPath.c_str());
renamed = tryAtomicRename(tmpPath, dstPath);
if (!renamed) {
MMKVInfo("rename fail, try copy file content instead.");
if (copyFileContent(tmpPath, dstPath)) {
renamed = true;
::unlink(tmpPath.c_str());
}
}
if (renamed) {
MMKVInfo("copyfile [%s] to [%s] finish.", srcPath.c_str(), dstPath.c_str());
}
Expand Down
6 changes: 5 additions & 1 deletion Core/MemoryFile_OSX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ bool copyFile(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
MMKVInfo("copyfile [%s] to [%s] finish.", srcPath.c_str(), dstPath.c_str());
return true;
}

MMKVInfo("rename fail, try copy file content instead.");
auto ret = copyFileContent(tmpFile.UTF8String, dstPath);

unlink(tmpFile.UTF8String);
return false;
return ret;
}

bool copyFileContent(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
Expand Down

0 comments on commit b3e4995

Please sign in to comment.