MemoryFile_Linux.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2021 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include "MemoryFile.h"
  21. #if defined(MMKV_ANDROID) || defined(MMKV_LINUX)
  22. # include "InterProcessLock.h"
  23. # include "MMBuffer.h"
  24. # include "MMKVLog.h"
  25. # include "ScopedLock.hpp"
  26. # include <cerrno>
  27. # include <utility>
  28. # include <fcntl.h>
  29. # include <sys/mman.h>
  30. # include <sys/stat.h>
  31. # include <unistd.h>
  32. # include <sys/file.h>
  33. # include <dirent.h>
  34. # include <cstring>
  35. # include <sys/sendfile.h>
  36. # include <sys/syscall.h>
  37. #ifdef MMKV_ANDROID
  38. #include <dlfcn.h>
  39. typedef int (*renameat2_t)(int old_dir_fd, const char* old_path, int new_dir_fd, const char* new_path, unsigned flags);
  40. #endif
  41. #ifndef RENAME_EXCHANGE
  42. #define RENAME_EXCHANGE (1 << 1) /* Exchange source and dest */
  43. #endif
  44. namespace mmkv {
  45. bool tryAtomicRename(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
  46. bool renamed = false;
  47. // try renameat2() first
  48. #if defined(SYS_renameat2)
  49. #ifdef MMKV_ANDROID
  50. static auto g_renameat2 = (renameat2_t) dlsym(RTLD_DEFAULT, "renameat2");
  51. if (g_renameat2) {
  52. renamed = (g_renameat2(AT_FDCWD, srcPath.c_str(), AT_FDCWD, dstPath.c_str(), RENAME_EXCHANGE) == 0);
  53. }
  54. if (!renamed && errno != ENOENT) {
  55. MMKVWarning("fail on renameat2() [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno,
  56. strerror(errno));
  57. }
  58. #endif
  59. if (!renamed) {
  60. renamed = (syscall(SYS_renameat2, AT_FDCWD, srcPath.c_str(), AT_FDCWD, dstPath.c_str(), RENAME_EXCHANGE) == 0);
  61. }
  62. if (!renamed && errno != ENOENT) {
  63. MMKVWarning("fail on syscall(SYS_renameat2) [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno,
  64. strerror(errno));
  65. }
  66. if (renamed && (srcPath != dstPath)) {
  67. ::unlink(srcPath.c_str());
  68. }
  69. #endif // SYS_renameat2
  70. if (!renamed) {
  71. if (::rename(srcPath.c_str(), dstPath.c_str()) != 0) {
  72. MMKVError("fail to rename [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno, strerror(errno));
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. // do it by sendfile()
  79. bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD, bool needTruncate) {
  80. if (dstFD < 0) {
  81. return false;
  82. }
  83. File srcFile(srcPath, OpenFlag::ReadOnly);
  84. if (!srcFile.isFileValid()) {
  85. return false;
  86. }
  87. auto srcFileSize = srcFile.getActualFileSize();
  88. lseek(dstFD, 0, SEEK_SET);
  89. auto writtenSize = ::sendfile(dstFD, srcFile.getFd(), nullptr, srcFileSize);
  90. auto ret = (writtenSize == srcFileSize);
  91. if (!ret) {
  92. if (writtenSize < 0) {
  93. MMKVError("fail to sendfile() %s to fd[%d], %d(%s)", srcPath.c_str(), dstFD, errno, strerror(errno));
  94. } else {
  95. MMKVError("sendfile() %s to fd[%d], written %lld < %zu", srcPath.c_str(), dstFD, writtenSize, srcFileSize);
  96. }
  97. } else if (needTruncate) {
  98. size_t dstFileSize = 0;
  99. getFileSize(dstFD, dstFileSize);
  100. if ((dstFileSize != srcFileSize) && (::ftruncate(dstFD, static_cast<off_t>(srcFileSize)) != 0)) {
  101. MMKVError("fail to truncate [%d] to size [%zu], %d(%s)", dstFD, srcFileSize, errno, strerror(errno));
  102. ret = false;
  103. }
  104. }
  105. if (ret) {
  106. MMKVInfo("copy content from %s to fd[%d] finish", srcPath.c_str(), dstFD);
  107. }
  108. return ret;
  109. }
  110. } // namespace mmkv
  111. #endif // defined(MMKV_ANDROID) || defined(MMKV_LINUX)