• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #define LOG_TAG "RdbHelper"
16 #include "rdb_helper.h"
17 
18 #include "logger.h"
19 #include "rdb_errno.h"
20 #include "rdb_fault_hiview_reporter.h"
21 #include "rdb_security_manager.h"
22 #include "rdb_store_manager.h"
23 #include "rdb_trace.h"
24 #include "sqlite_global_config.h"
25 #include "sqlite_utils.h"
26 #include "unistd.h"
27 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
28 #include "security_policy.h"
29 #endif
30 #include "rdb_fault_hiview_reporter.h"
31 
32 namespace OHOS {
33 namespace NativeRdb {
34 using namespace OHOS::Rdb;
35 using Reportor = RdbFaultHiViewReporter;
36 
GetRdbStore(const RdbStoreConfig & config,int version,RdbOpenCallback & openCallback,int & errCode)37 std::shared_ptr<RdbStore> RdbHelper::GetRdbStore(
38     const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode)
39 {
40     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
41     SqliteGlobalConfig::InitSqliteGlobalConfig();
42     auto rdb = RdbStoreManager::GetInstance().GetRdbStore(config, errCode, version, openCallback);
43     if (errCode != E_OK) {
44         Reportor::ReportFault(RdbFaultDbFileEvent(FT_OPEN, errCode, config, "LOG:RdbHelper::GetRdbStore", true));
45     }
46 
47     return rdb;
48 }
49 
ClearCache()50 void RdbHelper::ClearCache()
51 {
52     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
53     RdbStoreManager::GetInstance().Clear();
54 }
55 
DeleteRdbStore(const std::string & dbFileName,bool shouldClose)56 int RdbHelper::DeleteRdbStore(const std::string &dbFileName, bool shouldClose)
57 {
58     RdbStoreConfig config(dbFileName);
59     config.SetDBType(DB_SQLITE);
60     int errCode = DeleteRdbStore(config, shouldClose);
61 
62     config.SetStorageMode(StorageMode::MODE_MEMORY);
63     errCode = DeleteRdbStore(config, shouldClose) == E_OK ? errCode : E_REMOVE_FILE;
64 
65     config.SetStorageMode(StorageMode::MODE_DISK);
66     config.SetDBType(DB_VECTOR);
67     errCode = DeleteRdbStore(config, shouldClose) == E_OK ? errCode : E_REMOVE_FILE;
68     return errCode;
69 }
70 
DeleteRdbStore(const RdbStoreConfig & config,bool shouldClose)71 int RdbHelper::DeleteRdbStore(const RdbStoreConfig &config, bool shouldClose)
72 {
73     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
74     std::string dbFile;
75     auto errCode = SqliteGlobalConfig::GetDbPath(config, dbFile);
76     if (errCode != E_OK || dbFile.empty()) {
77         return E_INVALID_FILE_PATH;
78     }
79     if (config.IsMemoryRdb()) {
80         RdbStoreManager::GetInstance().Remove(dbFile, shouldClose);
81         LOG_INFO("Remove memory store, dbType:%{public}d, path %{public}s", config.GetDBType(),
82             SqliteUtils::Anonymous(dbFile).c_str());
83         return E_OK;
84     }
85     if (access(dbFile.c_str(), F_OK) == 0) {
86         RdbStoreManager::GetInstance().Delete(config, shouldClose);
87     }
88     Reportor::ReportRestore(Reportor::Create(config, E_OK, "RestoreType:Delete", false));
89     Connection::Delete(config);
90     RdbSecurityManager::GetInstance().DelAllKeyFiles(dbFile);
91     LOG_INFO("Delete rdb store, dbType:%{public}d, path %{public}s", config.GetDBType(),
92         SqliteUtils::Anonymous(dbFile).c_str());
93     return E_OK;
94 }
95 
IsSupportArkDataDb()96 bool RdbHelper::IsSupportArkDataDb()
97 {
98 #ifdef ARKDATA_DB_CORE_IS_EXISTS
99     return true;
100 #else
101     return false;
102 #endif
103 }
104 
IsSupportedTokenizer(Tokenizer tokenizer)105 bool RdbHelper::IsSupportedTokenizer(Tokenizer tokenizer)
106 {
107     if (tokenizer >= TOKENIZER_END) {
108         return false;
109     }
110     if (tokenizer == CUSTOM_TOKENIZER) {
111 #if !defined(CROSS_PLATFORM) && defined(ARKDATA_DATABASE_CORE_ENABLE)
112         return true;
113 #else
114         LOG_WARN("CUSTOM_TOKENIZER not support this platform.");
115         return false;
116 #endif
117     }
118     return true;
119 }
120 } // namespace NativeRdb
121 } // namespace OHOS
122