MemoryFile_Linux.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. extern bool getFileSize(int fd, size_t &size);
  46. bool tryAtomicRename(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
  47. bool renamed = false;
  48. // try renameat2() first
  49. #ifdef SYS_renameat2
  50. #ifdef MMKV_ANDROID
  51. static auto g_renameat2 = (renameat2_t) dlsym(RTLD_DEFAULT, "renameat2");
  52. if (g_renameat2) {
  53. renamed = (g_renameat2(AT_FDCWD, srcPath.c_str(), AT_FDCWD, dstPath.c_str(), RENAME_EXCHANGE) == 0);
  54. }
  55. #endif
  56. if (!renamed) {
  57. renamed = (syscall(SYS_renameat2, AT_FDCWD, srcPath.c_str(), AT_FDCWD, dstPath.c_str(), RENAME_EXCHANGE) == 0);
  58. }
  59. if (!renamed && errno != ENOENT) {
  60. MMKVError("fail on renameat2() [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno, strerror(errno));
  61. }
  62. #endif // SYS_renameat2
  63. if (!renamed) {
  64. if (::rename(srcPath.c_str(), dstPath.c_str()) != 0) {
  65. MMKVError("fail to rename [%s] to [%s], %d(%s)", srcPath.c_str(), dstPath.c_str(), errno, strerror(errno));
  66. return false;
  67. }
  68. }
  69. ::unlink(srcPath.c_str());
  70. return true;
  71. }
  72. // do it by sendfile()
  73. bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD, bool needTruncate) {
  74. if (dstFD < 0) {
  75. return false;
  76. }
  77. File srcFile(srcPath, OpenFlag::ReadOnly);
  78. if (!srcFile.isFileValid()) {
  79. return false;
  80. }
  81. auto srcFileSize = srcFile.getActualFileSize();
  82. lseek(dstFD, 0, SEEK_SET);
  83. auto writtenSize = ::sendfile(dstFD, srcFile.getFd(), nullptr, srcFileSize);
  84. auto ret = (writtenSize == srcFileSize);
  85. if (!ret) {
  86. if (writtenSize < 0) {
  87. MMKVError("fail to sendfile() %s to fd[%d], %d(%s)", srcPath.c_str(), dstFD, errno, strerror(errno));
  88. } else {
  89. MMKVError("sendfile() %s to fd[%d], written %lld < %zu", srcPath.c_str(), dstFD, writtenSize, srcFileSize);
  90. }
  91. } else if (needTruncate) {
  92. size_t dstFileSize = 0;
  93. getFileSize(dstFD, dstFileSize);
  94. if ((dstFileSize != srcFileSize) && (::ftruncate(dstFD, static_cast<off_t>(srcFileSize)) != 0)) {
  95. MMKVError("fail to truncate [%d] to size [%zu], %d(%s)", dstFD, srcFileSize, errno, strerror(errno));
  96. ret = false;
  97. }
  98. }
  99. if (ret) {
  100. MMKVInfo("copy content from %s to fd[%d] finish", srcPath.c_str(), dstFD);
  101. }
  102. return ret;
  103. }
  104. } // namespace mmkv
  105. #endif // defined(MMKV_ANDROID) || defined(MMKV_LINUX)