mirror of
https://github.com/Tencent/MMKV.git
synced 2026-10-17 04:30:56 +08:00
support correct filename on custom root path
This commit is contained in:
+16
-8
@@ -343,11 +343,7 @@ void MMKV::close() {
|
||||
SCOPED_LOCK(g_instanceLock);
|
||||
m_lock->lock();
|
||||
|
||||
#ifndef MMKV_ANDROID
|
||||
auto itr = g_instanceDic->find(m_mmapKey);
|
||||
#else
|
||||
auto itr = g_instanceDic->find(m_mmapID);
|
||||
#endif
|
||||
if (itr != g_instanceDic->end()) {
|
||||
g_instanceDic->erase(itr);
|
||||
}
|
||||
@@ -1308,8 +1304,14 @@ bool MMKV::backupOneToDirectory(const string &mmapID, const MMKVPath_t &dstDir,
|
||||
auto dstPath = dstDir + MMKV_PATH_SLASH + encodePath;
|
||||
auto mmapKey = mmapedKVKey(mmapID, rootPath);
|
||||
#ifdef MMKV_ANDROID
|
||||
// historically Android mistakenly use mmapKey as mmapID
|
||||
auto srcPath = *rootPath + MMKV_PATH_SLASH + encodeFilePath(mmapKey, *rootPath);
|
||||
string srcPath;
|
||||
auto correctPath = *rootPath + MMKV_PATH_SLASH + encodePath;
|
||||
if (srcDir && isFileExist(correctPath)) {
|
||||
srcPath = correctPath;
|
||||
} else {
|
||||
// historically Android mistakenly use mmapKey as mmapID
|
||||
srcPath = *rootPath + MMKV_PATH_SLASH + encodeFilePath(mmapKey, *rootPath);
|
||||
}
|
||||
#else
|
||||
auto srcPath = *rootPath + MMKV_PATH_SLASH + encodePath;
|
||||
#endif
|
||||
@@ -1491,8 +1493,14 @@ bool MMKV::restoreOneFromDirectory(const string &mmapID, const MMKVPath_t &srcDi
|
||||
auto srcPath = srcDir + MMKV_PATH_SLASH + encodePath;
|
||||
auto mmapKey = mmapedKVKey(mmapID, rootPath);
|
||||
#ifdef MMKV_ANDROID
|
||||
// historically Android mistakenly use mmapKey as mmapID
|
||||
auto dstPath = *rootPath + MMKV_PATH_SLASH + encodeFilePath(mmapKey, *rootPath);
|
||||
string dstPath;
|
||||
auto correctPath = *rootPath + MMKV_PATH_SLASH + encodePath;
|
||||
if (dstDir && isFileExist(correctPath)) {
|
||||
dstPath = correctPath;
|
||||
} else {
|
||||
// historically Android mistakenly use mmapKey as mmapID
|
||||
dstPath = *rootPath + MMKV_PATH_SLASH + encodeFilePath(mmapKey, *rootPath);
|
||||
}
|
||||
#else
|
||||
auto dstPath = *rootPath + MMKV_PATH_SLASH + encodePath;
|
||||
#endif
|
||||
|
||||
+1
-1
@@ -95,7 +95,6 @@ concept MMKV_SUPPORTED_VALUE_TYPE = MMKV_SUPPORTED_PRIMITIVE_VALUE_TYPE<T> || MM
|
||||
|
||||
class MMKV {
|
||||
#ifndef MMKV_ANDROID
|
||||
std::string m_mmapKey;
|
||||
MMKV(const std::string &mmapID, MMKVMode mode, std::string *cryptKey, MMKVPath_t *rootPath, size_t expectedCapacity = 0);
|
||||
#else // defined(MMKV_ANDROID)
|
||||
mmkv::FileLock *m_fileModeLock;
|
||||
@@ -109,6 +108,7 @@ class MMKV {
|
||||
|
||||
~MMKV();
|
||||
|
||||
std::string m_mmapKey;
|
||||
std::string m_mmapID;
|
||||
const MMKVMode m_mode;
|
||||
MMKVPath_t m_path;
|
||||
|
||||
+14
-2
@@ -40,7 +40,7 @@ extern unordered_map<string, MMKV *> *g_instanceDic;
|
||||
extern ThreadLock *g_instanceLock;
|
||||
|
||||
MMKV::MMKV(const string &mmapID, int size, MMKVMode mode, const string *cryptKey, const string *rootPath, size_t expectedCapacity)
|
||||
: m_mmapID((mode & MMKV_BACKUP) ? mmapID : mmapedKVKey(mmapID, rootPath)) // historically Android mistakenly use mmapKey as mmapID
|
||||
: m_mmapID(mmapID)
|
||||
, m_mode(mode)
|
||||
, m_path(mappedKVPathWithID(m_mmapID, mode, rootPath))
|
||||
, m_crcPath(crcPathWithID(m_mmapID, mode, rootPath))
|
||||
@@ -157,7 +157,18 @@ MMKV *MMKV::mmkvWithID(const string &mmapID, int size, MMKVMode mode, const stri
|
||||
}
|
||||
MMKVInfo("prepare to load %s (id %s) from rootPath %s", mmapID.c_str(), mmapKey.c_str(), rootPath->c_str());
|
||||
}
|
||||
auto kv = new MMKV(mmapID, size, mode, cryptKey, rootPath, expectedCapacity);
|
||||
|
||||
string realID;
|
||||
auto correctPath = mappedKVPathWithID(mmapID, mode, rootPath);
|
||||
if ((mode & MMKV_BACKUP) || (rootPath && isFileExist(correctPath))) {
|
||||
// it's successfully migrated to the correct path by newer version of MMKV
|
||||
realID = mmapID;
|
||||
} else {
|
||||
// historically Android mistakenly use mmapKey as mmapID
|
||||
realID = mmapKey;
|
||||
}
|
||||
auto kv = new MMKV(realID, size, mode, cryptKey, rootPath, expectedCapacity);
|
||||
kv->m_mmapKey = mmapKey;
|
||||
(*g_instanceDic)[mmapKey] = kv;
|
||||
return kv;
|
||||
}
|
||||
@@ -178,6 +189,7 @@ MMKV *MMKV::mmkvWithAshmemFD(const string &mmapID, int fd, int metaFD, const str
|
||||
return kv;
|
||||
}
|
||||
auto kv = new MMKV(mmapID, fd, metaFD, cryptKey);
|
||||
kv->m_mmapKey = mmapID;
|
||||
(*g_instanceDic)[mmapID] = kv;
|
||||
return kv;
|
||||
}
|
||||
|
||||
+9
-1
@@ -1549,7 +1549,15 @@ bool MMKV::removeStorage(const std::string &mmapID, MMKVPath_t *relatePath) {
|
||||
}
|
||||
auto mmapKey = mmapedKVKey(mmapID, relatePath);
|
||||
#ifdef MMKV_ANDROID
|
||||
auto &realID = mmapKey; // historically Android mistakenly use mmapKey as mmapID
|
||||
std::string realID;
|
||||
auto correctPath = mappedKVPathWithID(mmapID, MMKV_SINGLE_PROCESS, relatePath);
|
||||
if (relatePath && isFileExist(correctPath)) {
|
||||
// it's successfully migrated to the correct path by newer version of MMKV
|
||||
realID = mmapID;
|
||||
} else {
|
||||
// historically Android mistakenly use mmapKey as mmapID
|
||||
realID = mmapKey;
|
||||
}
|
||||
#else
|
||||
auto &realID = mmapID;
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user