MemoryFile_OSX.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2019 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. #include "MMKVLog.h"
  22. #ifdef MMKV_IOS
  23. using namespace std;
  24. namespace mmkv {
  25. void tryResetFileProtection(const string &path) {
  26. @autoreleasepool {
  27. NSString *nsPath = [NSString stringWithUTF8String:path.c_str()];
  28. NSDictionary *attr = [[NSFileManager defaultManager] attributesOfItemAtPath:nsPath error:nullptr];
  29. NSString *protection = [attr valueForKey:NSFileProtectionKey];
  30. MMKVInfo("protection on [%@] is %@", nsPath, protection);
  31. if ([protection isEqualToString:NSFileProtectionCompleteUntilFirstUserAuthentication] == NO) {
  32. NSMutableDictionary *newAttr = [NSMutableDictionary dictionaryWithDictionary:attr];
  33. [newAttr setObject:NSFileProtectionCompleteUntilFirstUserAuthentication forKey:NSFileProtectionKey];
  34. NSError *err = nil;
  35. [[NSFileManager defaultManager] setAttributes:newAttr ofItemAtPath:nsPath error:&err];
  36. if (err != nil) {
  37. MMKVError("fail to set attribute %@ on [%@]: %@", NSFileProtectionCompleteUntilFirstUserAuthentication,
  38. nsPath, err);
  39. }
  40. }
  41. }
  42. }
  43. } // namespace mmkv
  44. #endif // MMKV_IOS
  45. #ifdef MMKV_APPLE
  46. #include <copyfile.h>
  47. #include <mach/mach_time.h>
  48. namespace mmkv {
  49. bool tryAtomicRename(const char *src, const char *dst) {
  50. bool renamed = false;
  51. // try atomic swap first
  52. if (@available(iOS 10.0, watchOS 3.0, macOS 10.12, *)) {
  53. // renameat2() equivalent
  54. if (renamex_np(src, dst, RENAME_SWAP) == 0) {
  55. renamed = true;
  56. } else if (errno != ENOENT) {
  57. MMKVError("fail to renamex_np %s to %s, %s", src, dst, strerror(errno));
  58. }
  59. }
  60. if (!renamed) {
  61. // try old style rename
  62. if (rename(src, dst) != 0) {
  63. MMKVError("fail to rename %s to %s, %s", src, dst, strerror(errno));
  64. return false;
  65. }
  66. }
  67. unlink(src);
  68. return true;
  69. }
  70. bool copyFile(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
  71. // prepare a temp file for atomic rename, avoid data corruption of suddent crash
  72. NSString *uniqueFileName = [NSString stringWithFormat:@"mmkv_%llu", mach_absolute_time()];
  73. NSString *tmpFile = [NSTemporaryDirectory() stringByAppendingPathComponent:uniqueFileName];
  74. if (copyfile(srcPath.c_str(), tmpFile.UTF8String, nullptr, COPYFILE_UNLINK | COPYFILE_CLONE) != 0) {
  75. MMKVError("fail to copyfile [%s] to [%s], %s", srcPath.c_str(), tmpFile.UTF8String, strerror(errno));
  76. return false;
  77. }
  78. MMKVInfo("copyfile [%s] to [%s]", srcPath.c_str(), tmpFile.UTF8String);
  79. if (tryAtomicRename(tmpFile.UTF8String, dstPath.c_str())) {
  80. MMKVInfo("copyfile [%s] to [%s] finish.", srcPath.c_str(), dstPath.c_str());
  81. return true;
  82. }
  83. unlink(tmpFile.UTF8String);
  84. return false;
  85. }
  86. bool copyFileContent(const MMKVPath_t &srcPath, const MMKVPath_t &dstPath) {
  87. File dstFile(dstPath, OpenFlag::WriteOnly | OpenFlag::Create | OpenFlag::Truncate);
  88. if (!dstFile.isFileValid()) {
  89. return false;
  90. }
  91. if (copyFileContent(srcPath, dstFile.getFd())) {
  92. MMKVInfo("copy content from %s to fd[%s] finish", srcPath.c_str(), dstPath.c_str());
  93. return true;
  94. }
  95. MMKVError("fail to copyfile(): target file %s", dstPath.c_str());
  96. return false;
  97. }
  98. bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD) {
  99. if (dstFD < 0) {
  100. return false;
  101. }
  102. File srcFile(srcPath, OpenFlag::ReadOnly);
  103. if (!srcFile.isFileValid()) {
  104. return false;
  105. }
  106. // sendfile() equivalent
  107. if (::fcopyfile(srcFile.getFd(), dstFD, nullptr, COPYFILE_ACL | COPYFILE_STAT | COPYFILE_XATTR | COPYFILE_DATA) == 0) {
  108. MMKVInfo("copy content from %s to fd[%d] finish", srcPath.c_str(), dstFD);
  109. return true;
  110. }
  111. MMKVError("fail to copyfile(): %d(%s), source file %s", errno, strerror(errno), srcPath.c_str());
  112. return false;
  113. }
  114. bool copyFileContent(const MMKVPath_t &srcPath, MMKVFileHandle_t dstFD, bool needTruncate) {
  115. return copyFileContent(srcPath, dstFD);
  116. }
  117. } // namespace mmkv
  118. #endif // MMKV_APPLE