forked from ammarahm-ed/react-native-mmkv-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemoryFile.cpp
304 lines (265 loc) · 8.11 KB
/
MemoryFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Tencent is pleased to support the open source community by making
* MMKV available.
*
* Copyright (C) 2018 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MemoryFile.h"
#ifndef MMKV_WIN32
# include "InterProcessLock.h"
# include "MMBuffer.h"
# include "MMKVLog.h"
# include "ScopedLock.hpp"
# include <cerrno>
# include <fcntl.h>
# include <sys/mman.h>
# include <sys/stat.h>
# include <unistd.h>
using namespace std;
namespace mmkv {
static bool getFileSize(int fd, size_t &size);
# ifdef MMKV_ANDROID
extern size_t ASharedMemory_getSize(int fd);
# else
MemoryFile::MemoryFile(const MMKVPath_t &path) : m_name(path), m_fd(-1), m_ptr(nullptr), m_size(0) {
reloadFromFile();
}
# endif // MMKV_ANDROID
# ifdef MMKV_IOS
void tryResetFileProtection(const string &path);
# endif
bool MemoryFile::truncate(size_t size) {
if (m_fd < 0) {
return false;
}
if (size == m_size) {
return true;
}
# ifdef MMKV_ANDROID
if (m_fileType == MMFILE_TYPE_ASHMEM) {
if (size > m_size) {
MMKVError("ashmem %s reach size limit:%zu, consider configure with larger size", m_name.c_str(), m_size);
} else {
MMKVInfo("no way to trim ashmem %s from %zu to smaller size %zu", m_name.c_str(), m_size, size);
}
return false;
}
# endif // MMKV_ANDROID
auto oldSize = m_size;
m_size = size;
// round up to (n * pagesize)
if (m_size < DEFAULT_MMAP_SIZE || (m_size % DEFAULT_MMAP_SIZE != 0)) {
m_size = ((m_size / DEFAULT_MMAP_SIZE) + 1) * DEFAULT_MMAP_SIZE;
}
if (::ftruncate(m_fd, static_cast<off_t>(m_size)) != 0) {
MMKVError("fail to truncate [%s] to size %zu, %s", m_name.c_str(), m_size, strerror(errno));
m_size = oldSize;
return false;
}
if (m_size > oldSize) {
if (!zeroFillFile(m_fd, oldSize, m_size - oldSize)) {
MMKVError("fail to zeroFile [%s] to size %zu, %s", m_name.c_str(), m_size, strerror(errno));
m_size = oldSize;
return false;
}
}
if (m_ptr) {
if (munmap(m_ptr, oldSize) != 0) {
MMKVError("fail to munmap [%s], %s", m_name.c_str(), strerror(errno));
}
}
auto ret = mmap();
if (!ret) {
doCleanMemoryCache(true);
}
return ret;
}
bool MemoryFile::msync(SyncFlag syncFlag) {
if (m_ptr) {
auto ret = ::msync(m_ptr, m_size, syncFlag ? MS_SYNC : MS_ASYNC);
if (ret == 0) {
return true;
}
MMKVError("fail to msync [%s], %s", m_name.c_str(), strerror(errno));
}
return false;
}
bool MemoryFile::mmap() {
m_ptr = (char *) ::mmap(m_ptr, m_size, PROT_READ | PROT_WRITE, MAP_SHARED, m_fd, 0);
if (m_ptr == MAP_FAILED) {
MMKVError("fail to mmap [%s], %s", m_name.c_str(), strerror(errno));
m_ptr = nullptr;
return false;
}
return true;
}
void MemoryFile::reloadFromFile() {
# ifdef MMKV_ANDROID
if (m_fileType == MMFILE_TYPE_ASHMEM) {
return;
}
# endif
if (isFileValid()) {
MMKVWarning("calling reloadFromFile while the cache [%s] is still valid", m_name.c_str());
MMKV_ASSERT(0);
clearMemoryCache();
}
m_fd = open(m_name.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, S_IRWXU);
if (m_fd < 0) {
MMKVError("fail to open:%s, %s", m_name.c_str(), strerror(errno));
} else {
FileLock fileLock(m_fd);
InterProcessLock lock(&fileLock, ExclusiveLockType);
SCOPED_LOCK(&lock);
mmkv::getFileSize(m_fd, m_size);
// round up to (n * pagesize)
if (m_size < DEFAULT_MMAP_SIZE || (m_size % DEFAULT_MMAP_SIZE != 0)) {
size_t roundSize = ((m_size / DEFAULT_MMAP_SIZE) + 1) * DEFAULT_MMAP_SIZE;
truncate(roundSize);
} else {
auto ret = mmap();
if (!ret) {
doCleanMemoryCache(true);
}
}
# ifdef MMKV_IOS
tryResetFileProtection(m_name);
# endif
}
}
void MemoryFile::doCleanMemoryCache(bool forceClean) {
# ifdef MMKV_ANDROID
if (m_fileType == MMFILE_TYPE_ASHMEM && !forceClean) {
return;
}
# endif
if (m_ptr && m_ptr != MAP_FAILED) {
if (munmap(m_ptr, m_size) != 0) {
MMKVError("fail to munmap [%s], %s", m_name.c_str(), strerror(errno));
}
}
m_ptr = nullptr;
if (m_fd >= 0) {
if (::close(m_fd) != 0) {
MMKVError("fail to close [%s], %s", m_name.c_str(), strerror(errno));
}
}
m_fd = -1;
m_size = 0;
}
size_t MemoryFile::getActualFileSize() {
# ifdef MMKV_ANDROID
if (m_fileType == MMFILE_TYPE_ASHMEM) {
return ASharedMemory_getSize(m_fd);
}
# endif
size_t size = 0;
mmkv::getFileSize(m_fd, size);
return size;
}
bool isFileExist(const string &nsFilePath) {
if (nsFilePath.empty()) {
return false;
}
struct stat temp = {};
return lstat(nsFilePath.c_str(), &temp) == 0;
}
extern bool mkPath(const MMKVPath_t &str) {
char *path = strdup(str.c_str());
struct stat sb = {};
bool done = false;
char *slash = path;
while (!done) {
slash += strspn(slash, "/");
slash += strcspn(slash, "/");
done = (*slash == '\0');
*slash = '\0';
if (stat(path, &sb) != 0) {
if (errno != ENOENT || mkdir(path, 0777) != 0) {
MMKVWarning("%s : %s", path, strerror(errno));
free(path);
return false;
}
} else if (!S_ISDIR(sb.st_mode)) {
MMKVWarning("%s: %s", path, strerror(ENOTDIR));
free(path);
return false;
}
*slash = '/';
}
free(path);
return true;
}
MMBuffer *readWholeFile(const MMKVPath_t &path) {
MMBuffer *buffer = nullptr;
int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (fd >= 0) {
auto fileLength = lseek(fd, 0, SEEK_END);
if (fileLength > 0) {
buffer = new MMBuffer(static_cast<size_t>(fileLength));
lseek(fd, 0, SEEK_SET);
auto readSize = read(fd, buffer->getPtr(), static_cast<size_t>(fileLength));
if (readSize != -1) {
//fileSize = readSize;
} else {
MMKVWarning("fail to read %s: %s", path.c_str(), strerror(errno));
delete buffer;
buffer = nullptr;
}
}
close(fd);
} else {
MMKVWarning("fail to open %s: %s", path.c_str(), strerror(errno));
}
return buffer;
}
bool zeroFillFile(int fd, size_t startPos, size_t size) {
if (fd < 0) {
return false;
}
if (lseek(fd, static_cast<off_t>(startPos), SEEK_SET) < 0) {
MMKVError("fail to lseek fd[%d], error:%s", fd, strerror(errno));
return false;
}
static const char zeros[4096] = {};
while (size >= sizeof(zeros)) {
if (write(fd, zeros, sizeof(zeros)) < 0) {
MMKVError("fail to write fd[%d], error:%s", fd, strerror(errno));
return false;
}
size -= sizeof(zeros);
}
if (size > 0) {
if (write(fd, zeros, size) < 0) {
MMKVError("fail to write fd[%d], error:%s", fd, strerror(errno));
return false;
}
}
return true;
}
static bool getFileSize(int fd, size_t &size) {
struct stat st = {};
if (fstat(fd, &st) != -1) {
size = (size_t) st.st_size;
return true;
}
return false;
}
size_t getPageSize() {
return static_cast<size_t>(getpagesize());
}
} // namespace mmkv
#endif // MMKV_WIN32