• 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 "global_resource.h"
19 #include "logger.h"
20 #include "rdb_errno.h"
21 #include "rdb_fault_hiview_reporter.h"
22 #include "rdb_security_manager.h"
23 #include "rdb_store_manager.h"
24 #include "rdb_trace.h"
25 #include "sqlite_global_config.h"
26 #include "sqlite_utils.h"
27 #include "unistd.h"
28 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
29 #include "security_policy.h"
30 #endif
31 #include "rdb_fault_hiview_reporter.h"
32 
33 namespace OHOS {
34 namespace NativeRdb {
35 using namespace OHOS::Rdb;
36 using Reportor = RdbFaultHiViewReporter;
37 
GetRdbStore(const RdbStoreConfig & config,int version,RdbOpenCallback & openCallback,int & errCode)38 std::shared_ptr<RdbStore> RdbHelper::GetRdbStore(
39     const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode)
40 {
41     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
42     SqliteGlobalConfig::InitSqliteGlobalConfig();
43     auto rdb = RdbStoreManager::GetInstance().GetRdbStore(config, errCode, version, openCallback);
44     if (errCode != E_OK) {
45         Reportor::ReportFault(RdbFaultDbFileEvent(FT_OPEN, errCode, config,
46             SqliteUtils::FormatDebugInfoBrief(Connection::Collect(config), SqliteUtils::Anonymous(config.GetName())),
47             true));
48     }
49 
50     return rdb;
51 }
52 
ClearCache()53 void RdbHelper::ClearCache()
54 {
55     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
56     RdbStoreManager::GetInstance().Clear();
57 }
58 
Init()59 bool RdbHelper::Init()
60 {
61     LOG_INFO("Init start");
62     RdbStoreManager::GetInstance().Init();
63     return true;
64 }
65 
Destroy(const DestroyOption & option)66 bool RdbHelper::Destroy(const DestroyOption &option)
67 {
68     LOG_INFO("Destroy resource start");
69     bool ret = RdbStoreManager::GetInstance().Destroy();
70     if (option.cleanICU && GlobalResource::CleanUp(GlobalResource::ICU) != E_OK) {
71         LOG_WARN("CleanUp ICU failed");
72         ret = false;
73     }
74     if (option.cleanOpenSSL && GlobalResource::CleanUp(GlobalResource::OPEN_SSL) != E_OK) {
75         LOG_WARN("CleanUp OPEN_SSL failed");
76         ret = false;
77     }
78     if (GlobalResource::CleanUp(GlobalResource::DB_CLIENT) != E_OK) {
79         LOG_WARN("CleanUp DB_CLIENT failed");
80         ret = false;
81     }
82     if (GlobalResource::CleanUp(GlobalResource::OBS) != E_OK) {
83         LOG_WARN("CleanUp OBS failed");
84         ret = false;
85     }
86     if (GlobalResource::CleanUp(GlobalResource::IPC) != E_OK) {
87         LOG_WARN("CleanUp IPC failed");
88         ret = false;
89     }
90     return ret;
91 }
92 
DeleteRdbStore(const std::string & dbFileName,bool shouldClose)93 int RdbHelper::DeleteRdbStore(const std::string &dbFileName, bool shouldClose)
94 {
95     RdbStoreConfig config(dbFileName);
96     config.SetDBType(DB_SQLITE);
97     int errCode = DeleteRdbStore(config, shouldClose);
98 
99     config.SetStorageMode(StorageMode::MODE_MEMORY);
100     errCode = DeleteRdbStore(config, shouldClose) == E_OK ? errCode : E_REMOVE_FILE;
101 
102     config.SetStorageMode(StorageMode::MODE_DISK);
103     config.SetDBType(DB_VECTOR);
104     errCode = DeleteRdbStore(config, shouldClose) == E_OK ? errCode : E_REMOVE_FILE;
105     return errCode;
106 }
107 
DeleteRdbStore(const RdbStoreConfig & config,bool shouldClose)108 int RdbHelper::DeleteRdbStore(const RdbStoreConfig &config, bool shouldClose)
109 {
110     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
111     std::string dbFile;
112     auto errCode = SqliteGlobalConfig::GetDbPath(config, dbFile);
113     if (errCode != E_OK || dbFile.empty()) {
114         return E_INVALID_FILE_PATH;
115     }
116     if (config.IsMemoryRdb()) {
117         RdbStoreManager::GetInstance().Remove(dbFile, shouldClose);
118         LOG_INFO("Remove memory store, dbType:%{public}d, path %{public}s", config.GetDBType(),
119             SqliteUtils::Anonymous(dbFile).c_str());
120         return E_OK;
121     }
122     if (access(dbFile.c_str(), F_OK) == 0) {
123         RdbStoreManager::GetInstance().Delete(config, shouldClose);
124     }
125     Reportor::ReportRestore(Reportor::Create(config, E_OK, "RestoreType:Delete", false));
126     Connection::Delete(config);
127     RdbSecurityManager::GetInstance().DelAllKeyFiles(dbFile);
128     LOG_INFO("Delete rdb store, dbType:%{public}d, path %{public}s", config.GetDBType(),
129         SqliteUtils::Anonymous(dbFile).c_str());
130     return E_OK;
131 }
132 
IsSupportArkDataDb()133 bool RdbHelper::IsSupportArkDataDb()
134 {
135 #ifdef ARKDATA_DB_CORE_IS_EXISTS
136     return true;
137 #else
138     return false;
139 #endif
140 }
141 
IsSupportedTokenizer(Tokenizer tokenizer)142 bool RdbHelper::IsSupportedTokenizer(Tokenizer tokenizer)
143 {
144     if (tokenizer >= TOKENIZER_END) {
145         return false;
146     }
147     if (tokenizer == CUSTOM_TOKENIZER) {
148 #if !defined(CROSS_PLATFORM) && defined(ARKDATA_DATABASE_CORE_ENABLE)
149         return true;
150 #else
151         LOG_WARN("CUSTOM_TOKENIZER not support this platform.");
152         return false;
153 #endif
154     }
155     return true;
156 }
157 } // namespace NativeRdb
158 } // namespace OHOS
159