| 49 | namespace mmkv { |
| 50 | |
| 51 | bool tryAtomicRename(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) { |
| 52 | bool renamed = false; |
| 53 | |
| 54 | // try renameat2() first |
| 55 | #if defined(SYS_renameat2) |
| 56 | #ifdef MMKV_ANDROID |
| 57 | static auto g_renameat2 = (renameat2_t) dlsym(RTLD_DEFAULT, "renameat2"); |
| 58 | if (g_renameat2) { |
| 59 | renamed = (g_renameat2(AT_FDCWD, srcPath.c_str(), AT_FDCWD, dstPath.c_str(), RENAME_EXCHANGE) == 0); |
| 60 | } |
| 61 | if (!renamed && errno != ENOENT) { |
| 62 | MMKVWarning("fail on renameat2() [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno, |
| 63 | strerror(errno)); |
| 64 | } |
| 65 | #endif |
| 66 | if (!renamed) { |
| 67 | renamed = (syscall(SYS_renameat2, AT_FDCWD, srcPath.c_str(), AT_FDCWD, dstPath.c_str(), RENAME_EXCHANGE) == 0); |
| 68 | } |
| 69 | if (!renamed && errno != ENOENT) { |
| 70 | MMKVWarning("fail on syscall(SYS_renameat2) [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno, |
| 71 | strerror(errno)); |
| 72 | } |
| 73 | |
| 74 | if (renamed && (srcPath != dstPath)) { |
| 75 | ::unlink(srcPath.c_str()); |
| 76 | } |
| 77 | #endif // SYS_renameat2 |
| 78 | |
| 79 | if (!renamed) { |
| 80 | if (::rename(srcPath.c_str(), dstPath.c_str()) != 0) { |
| 81 | MMKVError("fail to rename [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno, strerror(errno)); |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | // do it by sendfile() |
| 90 | bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD, bool needTruncate) { |
nothing calls this directly
no outgoing calls
no test coverage detected