react-native-mmkv-dz 2.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +219 -0
- package/MMKV/CHANGELOG.md +553 -0
- package/MMKV/Core/CMakeLists.txt +153 -0
- package/MMKV/Core/CodedInputData.cpp +228 -0
- package/MMKV/Core/CodedInputData.h +83 -0
- package/MMKV/Core/CodedInputDataCrypt.cpp +280 -0
- package/MMKV/Core/CodedInputDataCrypt.h +87 -0
- package/MMKV/Core/CodedInputDataCrypt_OSX.cpp +62 -0
- package/MMKV/Core/CodedInputData_OSX.cpp +92 -0
- package/MMKV/Core/CodedOutputData.cpp +174 -0
- package/MMKV/Core/CodedOutputData.h +82 -0
- package/MMKV/Core/Core.xcodeproj/project.pbxproj +702 -0
- package/MMKV/Core/Core.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- package/MMKV/Core/Core.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/Core.xcscheme +67 -0
- package/MMKV/Core/Core.xcodeproj/xcshareddata/xcschemes/MMKVWatchCore.xcscheme +67 -0
- package/MMKV/Core/InterProcessLock.cpp +186 -0
- package/MMKV/Core/InterProcessLock.h +119 -0
- package/MMKV/Core/InterProcessLock_Android.cpp +103 -0
- package/MMKV/Core/InterProcessLock_Win32.cpp +108 -0
- package/MMKV/Core/KeyValueHolder.cpp +236 -0
- package/MMKV/Core/KeyValueHolder.h +118 -0
- package/MMKV/Core/MMBuffer.cpp +185 -0
- package/MMKV/Core/MMBuffer.h +107 -0
- package/MMKV/Core/MMKV.cpp +1418 -0
- package/MMKV/Core/MMKV.h +386 -0
- package/MMKV/Core/MMKVLog.cpp +127 -0
- package/MMKV/Core/MMKVLog.h +86 -0
- package/MMKV/Core/MMKVLog_Android.cpp +79 -0
- package/MMKV/Core/MMKVMetaInfo.hpp +81 -0
- package/MMKV/Core/MMKVPredef.h +245 -0
- package/MMKV/Core/MMKV_Android.cpp +259 -0
- package/MMKV/Core/MMKV_IO.cpp +1119 -0
- package/MMKV/Core/MMKV_IO.h +57 -0
- package/MMKV/Core/MMKV_OSX.cpp +347 -0
- package/MMKV/Core/MMKV_OSX.h +51 -0
- package/MMKV/Core/MemoryFile.cpp +537 -0
- package/MMKV/Core/MemoryFile.h +182 -0
- package/MMKV/Core/MemoryFile_Android.cpp +211 -0
- package/MMKV/Core/MemoryFile_Linux.cpp +120 -0
- package/MMKV/Core/MemoryFile_OSX.cpp +142 -0
- package/MMKV/Core/MemoryFile_Win32.cpp +536 -0
- package/MMKV/Core/MiniPBCoder.cpp +366 -0
- package/MMKV/Core/MiniPBCoder.h +129 -0
- package/MMKV/Core/MiniPBCoder_OSX.cpp +228 -0
- package/MMKV/Core/PBEncodeItem.hpp +86 -0
- package/MMKV/Core/PBUtility.cpp +61 -0
- package/MMKV/Core/PBUtility.h +153 -0
- package/MMKV/Core/ScopedLock.hpp +69 -0
- package/MMKV/Core/ThreadLock.cpp +68 -0
- package/MMKV/Core/ThreadLock.h +78 -0
- package/MMKV/Core/ThreadLock_Win32.cpp +89 -0
- package/MMKV/Core/aes/AESCrypt.cpp +256 -0
- package/MMKV/Core/aes/AESCrypt.h +107 -0
- package/MMKV/Core/aes/openssl/openssl_aes-armv4.S +1231 -0
- package/MMKV/Core/aes/openssl/openssl_aes.h +118 -0
- package/MMKV/Core/aes/openssl/openssl_aes_core.cpp +1044 -0
- package/MMKV/Core/aes/openssl/openssl_aes_locl.h +38 -0
- package/MMKV/Core/aes/openssl/openssl_aesv8-armx.S +308 -0
- package/MMKV/Core/aes/openssl/openssl_arm_arch.h +84 -0
- package/MMKV/Core/aes/openssl/openssl_cfb128.cpp +97 -0
- package/MMKV/Core/aes/openssl/openssl_md32_common.h +254 -0
- package/MMKV/Core/aes/openssl/openssl_md5.h +49 -0
- package/MMKV/Core/aes/openssl/openssl_md5_dgst.cpp +166 -0
- package/MMKV/Core/aes/openssl/openssl_md5_locl.h +75 -0
- package/MMKV/Core/aes/openssl/openssl_md5_one.cpp +30 -0
- package/MMKV/Core/aes/openssl/openssl_opensslconf.h +271 -0
- package/MMKV/Core/core.vcxproj +186 -0
- package/MMKV/Core/core.vcxproj.filters +150 -0
- package/MMKV/Core/crc32/Checksum.h +67 -0
- package/MMKV/Core/crc32/crc32_armv8.cpp +132 -0
- package/MMKV/Core/crc32/zlib/crc32.cpp +55 -0
- package/MMKV/Core/crc32/zlib/crc32.h +48 -0
- package/MMKV/Core/crc32/zlib/zconf.h +380 -0
- package/MMKV/Core/crc32/zlib/zutil.h +25 -0
- package/MMKV/LICENSE.TXT +193 -0
- package/MMKV/README.md +288 -0
- package/README.md +221 -0
- package/android/CMakeLists.txt +71 -0
- package/android/build.gradle +371 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/cpp/MmkvHostObject.cpp +302 -0
- package/android/src/main/cpp/MmkvHostObject.h +26 -0
- package/android/src/main/cpp/cpp-adapter.cpp +65 -0
- package/android/src/main/java/com/reactnativemmkv/MmkvModule.java +49 -0
- package/android/src/main/java/com/reactnativemmkv/MmkvPackage.java +26 -0
- package/cpp/TypedArray.cpp +341 -0
- package/cpp/TypedArray.h +175 -0
- package/ios/JSIUtils.h +50 -0
- package/ios/JSIUtils.mm +194 -0
- package/ios/Mmkv.xcodeproj/project.pbxproj +291 -0
- package/ios/MmkvHostObject.h +27 -0
- package/ios/MmkvHostObject.mm +299 -0
- package/ios/MmkvModule.h +5 -0
- package/ios/MmkvModule.mm +73 -0
- package/lib/commonjs/MMKV.js +146 -0
- package/lib/commonjs/MMKV.js.map +1 -0
- package/lib/commonjs/PlatformChecker.js +16 -0
- package/lib/commonjs/PlatformChecker.js.map +1 -0
- package/lib/commonjs/createMMKV.js +66 -0
- package/lib/commonjs/createMMKV.js.map +1 -0
- package/lib/commonjs/createMMKV.mock.js +40 -0
- package/lib/commonjs/createMMKV.mock.js.map +1 -0
- package/lib/commonjs/createMMKV.web.js +77 -0
- package/lib/commonjs/createMMKV.web.js.map +1 -0
- package/lib/commonjs/createTextEncoder.js +24 -0
- package/lib/commonjs/createTextEncoder.js.map +1 -0
- package/lib/commonjs/hooks.js +200 -0
- package/lib/commonjs/hooks.js.map +1 -0
- package/lib/commonjs/index.js +32 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/module/MMKV.js +134 -0
- package/lib/module/MMKV.js.map +1 -0
- package/lib/module/PlatformChecker.js +9 -0
- package/lib/module/PlatformChecker.js.map +1 -0
- package/lib/module/createMMKV.js +55 -0
- package/lib/module/createMMKV.js.map +1 -0
- package/lib/module/createMMKV.mock.js +31 -0
- package/lib/module/createMMKV.mock.js.map +1 -0
- package/lib/module/createMMKV.web.js +67 -0
- package/lib/module/createMMKV.web.js.map +1 -0
- package/lib/module/createTextEncoder.js +17 -0
- package/lib/module/createTextEncoder.js.map +1 -0
- package/lib/module/hooks.js +181 -0
- package/lib/module/hooks.js.map +1 -0
- package/lib/module/index.js +3 -0
- package/lib/module/index.js.map +1 -0
- package/lib/typescript/MMKV.d.ts +137 -0
- package/lib/typescript/PlatformChecker.d.ts +1 -0
- package/lib/typescript/createMMKV.d.ts +6 -0
- package/lib/typescript/createMMKV.mock.d.ts +2 -0
- package/lib/typescript/createMMKV.web.d.ts +2 -0
- package/lib/typescript/createTextEncoder.d.ts +1 -0
- package/lib/typescript/hooks.d.ts +81 -0
- package/lib/typescript/index.d.ts +2 -0
- package/package.json +168 -0
- package/react-native-mmkv.podspec +32 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Tencent is pleased to support the open source community by making
|
|
3
|
+
* MMKV available.
|
|
4
|
+
*
|
|
5
|
+
* Copyright (C) 2018 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
|
+
|
|
21
|
+
#ifndef MMKV_MMKVMETAINFO_H
|
|
22
|
+
#define MMKV_MMKVMETAINFO_H
|
|
23
|
+
#ifdef __cplusplus
|
|
24
|
+
|
|
25
|
+
#include "aes/AESCrypt.h"
|
|
26
|
+
#include <cstdint>
|
|
27
|
+
#include <cstring>
|
|
28
|
+
|
|
29
|
+
namespace mmkv {
|
|
30
|
+
|
|
31
|
+
enum MMKVVersion : uint32_t {
|
|
32
|
+
MMKVVersionDefault = 0,
|
|
33
|
+
|
|
34
|
+
// record full write back count
|
|
35
|
+
MMKVVersionSequence = 1,
|
|
36
|
+
|
|
37
|
+
// store random iv for encryption
|
|
38
|
+
MMKVVersionRandomIV = 2,
|
|
39
|
+
|
|
40
|
+
// store actual size together with crc checksum, try to reduce file corruption
|
|
41
|
+
MMKVVersionActualSize = 3,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
struct MMKVMetaInfo {
|
|
45
|
+
uint32_t m_crcDigest = 0;
|
|
46
|
+
uint32_t m_version = MMKVVersionSequence;
|
|
47
|
+
uint32_t m_sequence = 0; // full write-back count
|
|
48
|
+
uint8_t m_vector[AES_KEY_LEN] = {};
|
|
49
|
+
uint32_t m_actualSize = 0;
|
|
50
|
+
|
|
51
|
+
// confirmed info: it's been synced to file
|
|
52
|
+
struct {
|
|
53
|
+
uint32_t lastActualSize = 0;
|
|
54
|
+
uint32_t lastCRCDigest = 0;
|
|
55
|
+
uint32_t _reserved[16] = {};
|
|
56
|
+
} m_lastConfirmedMetaInfo;
|
|
57
|
+
|
|
58
|
+
void write(void *ptr) const {
|
|
59
|
+
MMKV_ASSERT(ptr);
|
|
60
|
+
memcpy(ptr, this, sizeof(MMKVMetaInfo));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
void writeCRCAndActualSizeOnly(void *ptr) const {
|
|
64
|
+
MMKV_ASSERT(ptr);
|
|
65
|
+
auto other = (MMKVMetaInfo *) ptr;
|
|
66
|
+
other->m_crcDigest = m_crcDigest;
|
|
67
|
+
other->m_actualSize = m_actualSize;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
void read(const void *ptr) {
|
|
71
|
+
MMKV_ASSERT(ptr);
|
|
72
|
+
memcpy(this, ptr, sizeof(MMKVMetaInfo));
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
static_assert(sizeof(MMKVMetaInfo) <= (4 * 1024), "MMKVMetaInfo lager than one pagesize");
|
|
77
|
+
|
|
78
|
+
} // namespace mmkv
|
|
79
|
+
|
|
80
|
+
#endif
|
|
81
|
+
#endif //MMKV_MMKVMETAINFO_H
|
|
@@ -0,0 +1,245 @@
|
|
|
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
|
+
|
|
21
|
+
#ifndef MMKV_SRC_MMKVPREDEF_H
|
|
22
|
+
#define MMKV_SRC_MMKVPREDEF_H
|
|
23
|
+
|
|
24
|
+
// disable encryption & decryption to reduce some code
|
|
25
|
+
//#define MMKV_DISABLE_CRYPT
|
|
26
|
+
//#define MMKV_DISABLE_FLUTTER
|
|
27
|
+
|
|
28
|
+
// using POSIX implementation
|
|
29
|
+
//#define FORCE_POSIX
|
|
30
|
+
|
|
31
|
+
#ifdef __cplusplus
|
|
32
|
+
|
|
33
|
+
#include <string>
|
|
34
|
+
#include <vector>
|
|
35
|
+
#include <unordered_map>
|
|
36
|
+
|
|
37
|
+
constexpr auto MMKV_VERSION = "v1.2.13";
|
|
38
|
+
|
|
39
|
+
#ifdef DEBUG
|
|
40
|
+
# define MMKV_DEBUG
|
|
41
|
+
#endif
|
|
42
|
+
|
|
43
|
+
#ifdef NDEBUG
|
|
44
|
+
# undef MMKV_DEBUG
|
|
45
|
+
#endif
|
|
46
|
+
|
|
47
|
+
#ifdef __ANDROID__
|
|
48
|
+
# ifdef FORCE_POSIX
|
|
49
|
+
# define MMKV_POSIX
|
|
50
|
+
# else
|
|
51
|
+
# define MMKV_ANDROID
|
|
52
|
+
# endif
|
|
53
|
+
#elif __APPLE__
|
|
54
|
+
# ifdef FORCE_POSIX
|
|
55
|
+
# define MMKV_POSIX
|
|
56
|
+
# else
|
|
57
|
+
# define MMKV_APPLE
|
|
58
|
+
# ifdef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__
|
|
59
|
+
# define MMKV_IOS
|
|
60
|
+
# elif __ENVIRONMENT_WATCH_OS_VERSION_MIN_REQUIRED__
|
|
61
|
+
# define MMKV_WATCH
|
|
62
|
+
# else
|
|
63
|
+
# define MMKV_MAC
|
|
64
|
+
# endif
|
|
65
|
+
# endif // FORCE_POSIX
|
|
66
|
+
#elif __linux__ || __unix__
|
|
67
|
+
# define MMKV_POSIX
|
|
68
|
+
# if __linux__
|
|
69
|
+
# define MMKV_LINUX
|
|
70
|
+
# endif
|
|
71
|
+
#elif _WIN32
|
|
72
|
+
# define MMKV_WIN32
|
|
73
|
+
#endif
|
|
74
|
+
|
|
75
|
+
#ifdef MMKV_WIN32
|
|
76
|
+
# if !defined(_WIN32_WINNT)
|
|
77
|
+
# define _WIN32_WINNT _WIN32_WINNT_WINXP
|
|
78
|
+
# endif
|
|
79
|
+
|
|
80
|
+
# include <SDKDDKVer.h>
|
|
81
|
+
// Exclude rarely-used stuff from Windows headers
|
|
82
|
+
# define WIN32_LEAN_AND_MEAN
|
|
83
|
+
// Windows Header Files
|
|
84
|
+
# include <windows.h>
|
|
85
|
+
|
|
86
|
+
constexpr auto MMKV_PATH_SLASH = L"\\";
|
|
87
|
+
# define MMKV_PATH_FORMAT "%ws"
|
|
88
|
+
using MMKVFileHandle_t = HANDLE;
|
|
89
|
+
using MMKVPath_t = std::wstring;
|
|
90
|
+
extern MMKVPath_t string2MMKVPath_t(const std::string &str);
|
|
91
|
+
extern std::string MMKVPath_t2String(const MMKVPath_t &str);
|
|
92
|
+
|
|
93
|
+
# ifndef MMKV_EMBED_ZLIB
|
|
94
|
+
# define MMKV_EMBED_ZLIB 1
|
|
95
|
+
# endif
|
|
96
|
+
|
|
97
|
+
#else // MMKV_WIN32
|
|
98
|
+
|
|
99
|
+
constexpr auto MMKV_PATH_SLASH = "/";
|
|
100
|
+
# define MMKV_PATH_FORMAT "%s"
|
|
101
|
+
using MMKVFileHandle_t = int;
|
|
102
|
+
using MMKVPath_t = std::string;
|
|
103
|
+
# define string2MMKVPath_t(str) (str)
|
|
104
|
+
# define MMKVPath_t2String(str) (str)
|
|
105
|
+
|
|
106
|
+
# ifndef MMKV_EMBED_ZLIB
|
|
107
|
+
# define MMKV_EMBED_ZLIB 0
|
|
108
|
+
# endif
|
|
109
|
+
|
|
110
|
+
#endif // MMKV_WIN32
|
|
111
|
+
|
|
112
|
+
#ifdef MMKV_APPLE
|
|
113
|
+
# import <Foundation/Foundation.h>
|
|
114
|
+
# define MMKV_NAMESPACE_BEGIN namespace mmkv {
|
|
115
|
+
# define MMKV_NAMESPACE_END }
|
|
116
|
+
# define MMKV_NAMESPACE_PREFIX mmkv
|
|
117
|
+
using MMKVLog_t = NSString *;
|
|
118
|
+
#else
|
|
119
|
+
# define MMKV_NAMESPACE_BEGIN
|
|
120
|
+
# define MMKV_NAMESPACE_END
|
|
121
|
+
# define MMKV_NAMESPACE_PREFIX
|
|
122
|
+
using MMKVLog_t = const std::string &;
|
|
123
|
+
#endif // MMKV_APPLE
|
|
124
|
+
|
|
125
|
+
MMKV_NAMESPACE_BEGIN
|
|
126
|
+
|
|
127
|
+
enum MMKVLogLevel : int {
|
|
128
|
+
MMKVLogDebug = 0, // not available for release/product build
|
|
129
|
+
MMKVLogInfo = 1, // default level
|
|
130
|
+
MMKVLogWarning,
|
|
131
|
+
MMKVLogError,
|
|
132
|
+
MMKVLogNone, // special level used to disable all log messages
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
enum MMKVRecoverStrategic : int {
|
|
136
|
+
OnErrorDiscard = 0,
|
|
137
|
+
OnErrorRecover,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
enum MMKVErrorType : int {
|
|
141
|
+
MMKVCRCCheckFail = 0,
|
|
142
|
+
MMKVFileLength,
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
enum SyncFlag : bool { MMKV_SYNC = true, MMKV_ASYNC = false };
|
|
146
|
+
|
|
147
|
+
MMKV_NAMESPACE_END
|
|
148
|
+
|
|
149
|
+
namespace mmkv {
|
|
150
|
+
|
|
151
|
+
typedef void (*LogHandler)(MMKVLogLevel level, const char *file, int line, const char *function, MMKVLog_t message);
|
|
152
|
+
|
|
153
|
+
// by default MMKV will discard all datas on failure
|
|
154
|
+
// return `OnErrorRecover` to recover any data from file
|
|
155
|
+
typedef MMKVRecoverStrategic (*ErrorHandler)(const std::string &mmapID, MMKVErrorType errorType);
|
|
156
|
+
|
|
157
|
+
// called when content is changed by other process
|
|
158
|
+
// doesn't guarantee real-time notification
|
|
159
|
+
typedef void (*ContentChangeHandler)(const std::string &mmapID);
|
|
160
|
+
|
|
161
|
+
extern size_t DEFAULT_MMAP_SIZE;
|
|
162
|
+
#define DEFAULT_MMAP_ID "mmkv.default"
|
|
163
|
+
|
|
164
|
+
class MMBuffer;
|
|
165
|
+
struct KeyValueHolder;
|
|
166
|
+
|
|
167
|
+
#ifdef MMKV_DISABLE_CRYPT
|
|
168
|
+
using KeyValueHolderCrypt = KeyValueHolder;
|
|
169
|
+
#else
|
|
170
|
+
struct KeyValueHolderCrypt;
|
|
171
|
+
#endif
|
|
172
|
+
|
|
173
|
+
#ifdef MMKV_APPLE
|
|
174
|
+
struct KeyHasher {
|
|
175
|
+
size_t operator()(NSString *key) const { return key.hash; }
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
struct KeyEqualer {
|
|
179
|
+
bool operator()(NSString *left, NSString *right) const {
|
|
180
|
+
if (left == right) {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
return ([left isEqualToString:right] == YES);
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
using MMKVVector = std::vector<std::pair<NSString *, mmkv::MMBuffer>>;
|
|
188
|
+
using MMKVMap = std::unordered_map<NSString *, mmkv::KeyValueHolder, KeyHasher, KeyEqualer>;
|
|
189
|
+
using MMKVMapCrypt = std::unordered_map<NSString *, mmkv::KeyValueHolderCrypt, KeyHasher, KeyEqualer>;
|
|
190
|
+
#else
|
|
191
|
+
using MMKVVector = std::vector<std::pair<std::string, mmkv::MMBuffer>>;
|
|
192
|
+
using MMKVMap = std::unordered_map<std::string, mmkv::KeyValueHolder>;
|
|
193
|
+
using MMKVMapCrypt = std::unordered_map<std::string, mmkv::KeyValueHolderCrypt>;
|
|
194
|
+
#endif // MMKV_APPLE
|
|
195
|
+
|
|
196
|
+
template <typename T>
|
|
197
|
+
void unused(const T &) {}
|
|
198
|
+
|
|
199
|
+
constexpr size_t AES_KEY_LEN = 16;
|
|
200
|
+
constexpr size_t AES_KEY_BITSET_LEN = 128;
|
|
201
|
+
|
|
202
|
+
} // namespace mmkv
|
|
203
|
+
|
|
204
|
+
#ifdef MMKV_DEBUG
|
|
205
|
+
# include <cassert>
|
|
206
|
+
# define MMKV_ASSERT(var) assert(var)
|
|
207
|
+
#else
|
|
208
|
+
# define MMKV_ASSERT(var) mmkv::unused(var)
|
|
209
|
+
#endif
|
|
210
|
+
|
|
211
|
+
#endif //cplus-plus
|
|
212
|
+
|
|
213
|
+
#if defined(__arm__)
|
|
214
|
+
#if defined(__ARM_ARCH_7A__)
|
|
215
|
+
#if defined(__ARM_NEON__)
|
|
216
|
+
#if defined(__ARM_PCS_VFP)
|
|
217
|
+
#define MMKV_ABI "armeabi-v7a/NEON (hard-float)"
|
|
218
|
+
#else
|
|
219
|
+
#define MMKV_ABI "armeabi-v7a/NEON"
|
|
220
|
+
#endif
|
|
221
|
+
#else
|
|
222
|
+
#if defined(__ARM_PCS_VFP)
|
|
223
|
+
#define MMKV_ABI "armeabi-v7a (hard-float)"
|
|
224
|
+
#else
|
|
225
|
+
#define MMKV_ABI "armeabi-v7a"
|
|
226
|
+
#endif
|
|
227
|
+
#endif
|
|
228
|
+
#else
|
|
229
|
+
#define MMKV_ABI "armeabi"
|
|
230
|
+
#endif
|
|
231
|
+
#elif defined(__i386__) || defined(_M_IX86)
|
|
232
|
+
#define MMKV_ABI "x86"
|
|
233
|
+
#elif defined(__x86_64__) || defined(_M_X64)
|
|
234
|
+
#define MMKV_ABI "x86_64"
|
|
235
|
+
#elif defined(__mips64)
|
|
236
|
+
#define MMKV_ABI "mips64"
|
|
237
|
+
#elif defined(__mips__)
|
|
238
|
+
#define MMKV_ABI "mips"
|
|
239
|
+
#elif defined(__aarch64__) || defined(_M_ARM64)
|
|
240
|
+
#define MMKV_ABI "arm64-v8a"
|
|
241
|
+
#else
|
|
242
|
+
#define MMKV_ABI "unknown"
|
|
243
|
+
#endif
|
|
244
|
+
|
|
245
|
+
#endif //MMKV_SRC_MMKVPREDEF_H
|
|
@@ -0,0 +1,259 @@
|
|
|
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
|
+
|
|
21
|
+
#include "MMKV.h"
|
|
22
|
+
|
|
23
|
+
#ifdef MMKV_ANDROID
|
|
24
|
+
|
|
25
|
+
# include "InterProcessLock.h"
|
|
26
|
+
# include "KeyValueHolder.h"
|
|
27
|
+
# include "MMKVLog.h"
|
|
28
|
+
# include "MMKVMetaInfo.hpp"
|
|
29
|
+
# include "MemoryFile.h"
|
|
30
|
+
# include "ScopedLock.hpp"
|
|
31
|
+
# include "ThreadLock.h"
|
|
32
|
+
# include <unistd.h>
|
|
33
|
+
# include "MMKV_IO.h"
|
|
34
|
+
|
|
35
|
+
using namespace std;
|
|
36
|
+
using namespace mmkv;
|
|
37
|
+
|
|
38
|
+
extern unordered_map<string, MMKV *> *g_instanceDic;
|
|
39
|
+
extern ThreadLock *g_instanceLock;
|
|
40
|
+
|
|
41
|
+
MMKV::MMKV(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath)
|
|
42
|
+
: m_mmapID((mode & MMKV_BACKUP) ? mmapID : mmapedKVKey(mmapID, rootPath)) // historically Android mistakenly use mmapKey as mmapID
|
|
43
|
+
, m_path(mappedKVPathWithID(m_mmapID, mode, rootPath))
|
|
44
|
+
, m_crcPath(crcPathWithID(m_mmapID, mode, rootPath))
|
|
45
|
+
, m_dic(nullptr)
|
|
46
|
+
, m_dicCrypt(nullptr)
|
|
47
|
+
, m_file(new MemoryFile(m_path, size, (mode & MMKV_ASHMEM) ? MMFILE_TYPE_ASHMEM : MMFILE_TYPE_FILE))
|
|
48
|
+
, m_metaFile(new MemoryFile(m_crcPath, DEFAULT_MMAP_SIZE, m_file->m_fileType))
|
|
49
|
+
, m_metaInfo(new MMKVMetaInfo())
|
|
50
|
+
, m_crypter(nullptr)
|
|
51
|
+
, m_lock(new ThreadLock())
|
|
52
|
+
, m_fileLock(new FileLock(m_metaFile->getFd(), (mode & MMKV_ASHMEM)))
|
|
53
|
+
, m_sharedProcessLock(new InterProcessLock(m_fileLock, SharedLockType))
|
|
54
|
+
, m_exclusiveProcessLock(new InterProcessLock(m_fileLock, ExclusiveLockType))
|
|
55
|
+
, m_isInterProcess((mode & MMKV_MULTI_PROCESS) != 0 || (mode & CONTEXT_MODE_MULTI_PROCESS) != 0) {
|
|
56
|
+
m_actualSize = 0;
|
|
57
|
+
m_output = nullptr;
|
|
58
|
+
|
|
59
|
+
// force use fcntl(), otherwise will conflict with MemoryFile::reloadFromFile()
|
|
60
|
+
m_fileModeLock = new FileLock(m_file->getFd(), true);
|
|
61
|
+
m_sharedProcessModeLock = new InterProcessLock(m_fileModeLock, SharedLockType);
|
|
62
|
+
m_exclusiveProcessModeLock = nullptr;
|
|
63
|
+
|
|
64
|
+
# ifndef MMKV_DISABLE_CRYPT
|
|
65
|
+
if (cryptKey && cryptKey->length() > 0) {
|
|
66
|
+
m_dicCrypt = new MMKVMapCrypt();
|
|
67
|
+
m_crypter = new AESCrypt(cryptKey->data(), cryptKey->length());
|
|
68
|
+
} else
|
|
69
|
+
# endif
|
|
70
|
+
{
|
|
71
|
+
m_dic = new MMKVMap();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
m_needLoadFromFile = true;
|
|
75
|
+
m_hasFullWriteback = false;
|
|
76
|
+
|
|
77
|
+
m_crcDigest = 0;
|
|
78
|
+
|
|
79
|
+
m_sharedProcessLock->m_enable = m_isInterProcess;
|
|
80
|
+
m_exclusiveProcessLock->m_enable = m_isInterProcess;
|
|
81
|
+
|
|
82
|
+
// sensitive zone
|
|
83
|
+
{
|
|
84
|
+
SCOPED_LOCK(m_sharedProcessLock);
|
|
85
|
+
loadFromFile();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
MMKV::MMKV(const string &mmapID, int ashmemFD, int ashmemMetaFD, string *cryptKey)
|
|
90
|
+
: m_mmapID(mmapID)
|
|
91
|
+
, m_path(mappedKVPathWithID(m_mmapID, MMKV_ASHMEM, nullptr))
|
|
92
|
+
, m_crcPath(crcPathWithID(m_mmapID, MMKV_ASHMEM, nullptr))
|
|
93
|
+
, m_dic(nullptr)
|
|
94
|
+
, m_dicCrypt(nullptr)
|
|
95
|
+
, m_file(new MemoryFile(ashmemFD))
|
|
96
|
+
, m_metaFile(new MemoryFile(ashmemMetaFD))
|
|
97
|
+
, m_metaInfo(new MMKVMetaInfo())
|
|
98
|
+
, m_crypter(nullptr)
|
|
99
|
+
, m_lock(new ThreadLock())
|
|
100
|
+
, m_fileLock(new FileLock(m_metaFile->getFd(), true))
|
|
101
|
+
, m_sharedProcessLock(new InterProcessLock(m_fileLock, SharedLockType))
|
|
102
|
+
, m_exclusiveProcessLock(new InterProcessLock(m_fileLock, ExclusiveLockType))
|
|
103
|
+
, m_isInterProcess(true) {
|
|
104
|
+
|
|
105
|
+
m_actualSize = 0;
|
|
106
|
+
m_output = nullptr;
|
|
107
|
+
|
|
108
|
+
// force use fcntl(), otherwise will conflict with MemoryFile::reloadFromFile()
|
|
109
|
+
m_fileModeLock = new FileLock(m_file->getFd(), true);
|
|
110
|
+
m_sharedProcessModeLock = new InterProcessLock(m_fileModeLock, SharedLockType);
|
|
111
|
+
m_exclusiveProcessModeLock = nullptr;
|
|
112
|
+
|
|
113
|
+
# ifndef MMKV_DISABLE_CRYPT
|
|
114
|
+
if (cryptKey && cryptKey->length() > 0) {
|
|
115
|
+
m_dicCrypt = new MMKVMapCrypt();
|
|
116
|
+
m_crypter = new AESCrypt(cryptKey->data(), cryptKey->length());
|
|
117
|
+
} else
|
|
118
|
+
# endif
|
|
119
|
+
{
|
|
120
|
+
m_dic = new MMKVMap();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
m_needLoadFromFile = true;
|
|
124
|
+
m_hasFullWriteback = false;
|
|
125
|
+
|
|
126
|
+
m_crcDigest = 0;
|
|
127
|
+
|
|
128
|
+
m_sharedProcessLock->m_enable = m_isInterProcess;
|
|
129
|
+
m_exclusiveProcessLock->m_enable = m_isInterProcess;
|
|
130
|
+
|
|
131
|
+
// sensitive zone
|
|
132
|
+
{
|
|
133
|
+
SCOPED_LOCK(m_sharedProcessLock);
|
|
134
|
+
loadFromFile();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, string *cryptKey, string *rootPath) {
|
|
139
|
+
if (mmapID.empty()) {
|
|
140
|
+
return nullptr;
|
|
141
|
+
}
|
|
142
|
+
SCOPED_LOCK(g_instanceLock);
|
|
143
|
+
|
|
144
|
+
auto mmapKey = mmapedKVKey(mmapID, rootPath);
|
|
145
|
+
auto itr = g_instanceDic->find(mmapKey);
|
|
146
|
+
if (itr != g_instanceDic->end()) {
|
|
147
|
+
MMKV *kv = itr->second;
|
|
148
|
+
return kv;
|
|
149
|
+
}
|
|
150
|
+
if (rootPath) {
|
|
151
|
+
if (!isFileExist(*rootPath)) {
|
|
152
|
+
if (!mkPath(*rootPath)) {
|
|
153
|
+
return nullptr;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
MMKVInfo("prepare to load %s (id %s) from rootPath %s", mmapID.c_str(), mmapKey.c_str(), rootPath->c_str());
|
|
157
|
+
}
|
|
158
|
+
auto kv = new MMKV(mmapID, size, mode, cryptKey, rootPath);
|
|
159
|
+
(*g_instanceDic)[mmapKey] = kv;
|
|
160
|
+
return kv;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
MMKV *MMKV::mmkvWithAshmemFD(const string &mmapID, int fd, int metaFD, string *cryptKey) {
|
|
164
|
+
|
|
165
|
+
if (fd < 0) {
|
|
166
|
+
return nullptr;
|
|
167
|
+
}
|
|
168
|
+
SCOPED_LOCK(g_instanceLock);
|
|
169
|
+
|
|
170
|
+
auto itr = g_instanceDic->find(mmapID);
|
|
171
|
+
if (itr != g_instanceDic->end()) {
|
|
172
|
+
MMKV *kv = itr->second;
|
|
173
|
+
# ifndef MMKV_DISABLE_CRYPT
|
|
174
|
+
kv->checkReSetCryptKey(fd, metaFD, cryptKey);
|
|
175
|
+
# endif
|
|
176
|
+
return kv;
|
|
177
|
+
}
|
|
178
|
+
auto kv = new MMKV(mmapID, fd, metaFD, cryptKey);
|
|
179
|
+
(*g_instanceDic)[mmapID] = kv;
|
|
180
|
+
return kv;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
int MMKV::ashmemFD() {
|
|
184
|
+
return (m_file->m_fileType & mmkv::MMFILE_TYPE_ASHMEM) ? m_file->getFd() : -1;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
int MMKV::ashmemMetaFD() {
|
|
188
|
+
return (m_file->m_fileType & mmkv::MMFILE_TYPE_ASHMEM) ? m_metaFile->getFd() : -1;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
# ifndef MMKV_DISABLE_CRYPT
|
|
192
|
+
void MMKV::checkReSetCryptKey(int fd, int metaFD, string *cryptKey) {
|
|
193
|
+
SCOPED_LOCK(m_lock);
|
|
194
|
+
|
|
195
|
+
checkReSetCryptKey(cryptKey);
|
|
196
|
+
|
|
197
|
+
if (m_file->m_fileType & MMFILE_TYPE_ASHMEM) {
|
|
198
|
+
if (m_file->getFd() != fd) {
|
|
199
|
+
::close(fd);
|
|
200
|
+
}
|
|
201
|
+
if (m_metaFile->getFd() != metaFD) {
|
|
202
|
+
::close(metaFD);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
# endif // MMKV_DISABLE_CRYPT
|
|
207
|
+
|
|
208
|
+
bool MMKV::checkProcessMode() {
|
|
209
|
+
// avoid exception on open() error
|
|
210
|
+
if (!m_file->isFileValid()) {
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (m_isInterProcess) {
|
|
215
|
+
if (!m_exclusiveProcessModeLock) {
|
|
216
|
+
m_exclusiveProcessModeLock = new InterProcessLock(m_fileModeLock, ExclusiveLockType);
|
|
217
|
+
}
|
|
218
|
+
// avoid multiple processes get shared lock at the same time, https://github.com/Tencent/MMKV/issues/523
|
|
219
|
+
auto tryAgain = false;
|
|
220
|
+
auto exclusiveLocked = m_exclusiveProcessModeLock->try_lock(&tryAgain);
|
|
221
|
+
if (exclusiveLocked) {
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
auto shareLocked = m_sharedProcessModeLock->try_lock();
|
|
225
|
+
if (!shareLocked) {
|
|
226
|
+
// this call will fail on most case, just do it to make sure
|
|
227
|
+
m_exclusiveProcessModeLock->try_lock();
|
|
228
|
+
return true;
|
|
229
|
+
} else {
|
|
230
|
+
if (!tryAgain) {
|
|
231
|
+
// something wrong with the OS/filesystem, let's try again
|
|
232
|
+
exclusiveLocked = m_exclusiveProcessModeLock->try_lock(&tryAgain);
|
|
233
|
+
if (!exclusiveLocked && !tryAgain) {
|
|
234
|
+
// still something wrong, we have to give up and assume it passed the test
|
|
235
|
+
MMKVWarning("Got a shared lock, but fail to exclusive lock [%s], assume it's ok", m_mmapID.c_str());
|
|
236
|
+
exclusiveLocked = true;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (!exclusiveLocked) {
|
|
240
|
+
MMKVError("Got a shared lock, but fail to exclusive lock [%s]", m_mmapID.c_str());
|
|
241
|
+
}
|
|
242
|
+
return exclusiveLocked;
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
auto tryAgain = false;
|
|
246
|
+
auto shareLocked = m_sharedProcessModeLock->try_lock(&tryAgain);
|
|
247
|
+
if (!shareLocked && !tryAgain) {
|
|
248
|
+
// something wrong with the OS/filesystem, we have to give up and assume it passed the test
|
|
249
|
+
MMKVWarning("Fail to shared lock [%s], assume it's ok", m_mmapID.c_str());
|
|
250
|
+
shareLocked = true;
|
|
251
|
+
}
|
|
252
|
+
if (!shareLocked) {
|
|
253
|
+
MMKVError("Fail to share lock [%s]", m_mmapID.c_str());
|
|
254
|
+
}
|
|
255
|
+
return shareLocked;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
#endif // MMKV_ANDROID
|