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 
31 namespace OHOS {
32 namespace NativeRdb {
33 using namespace OHOS::Rdb;
34 using Reportor = RdbFaultHiViewReporter;
35 
GetRdbStore(const RdbStoreConfig & config,int version,RdbOpenCallback & openCallback,int & errCode)36 std::shared_ptr<RdbStore> RdbHelper::GetRdbStore(
37     const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode)
38 {
39     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
40     SqliteGlobalConfig::InitSqliteGlobalConfig();
41     return RdbStoreManager::GetInstance().GetRdbStore(config, errCode, version, openCallback);
42 }
43 
ClearCache()44 void RdbHelper::ClearCache()
45 {
46     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
47     RdbStoreManager::GetInstance().Clear();
48 }
49 
50 static std::vector<std::string> rdPostFixes = {
51     "",
52     ".redo",
53     ".undo",
54     ".ctrl",
55     ".ctrl.dwr",
56     ".safe",
57     ".map",
58 };
59 
DeleteRdFiles(const std::string & dbFileName)60 int DeleteRdFiles(const std::string &dbFileName)
61 {
62     int errCode = E_OK;
63     for (std::string &postFix : rdPostFixes) {
64         std::string shmFileName = dbFileName + postFix;
65         if (access(shmFileName.c_str(), F_OK) == 0) {
66             int result = remove(shmFileName.c_str());
67             if (result < 0) {
68                 LOG_ERROR("RdbHelper DeleteRdbStore failed to delete the shm file err = %{public}d", errno);
69                 errCode = E_REMOVE_FILE;
70             }
71         }
72     }
73     return errCode;
74 }
75 
DeleteRdbStore(const std::string & dbFileName)76 int RdbHelper::DeleteRdbStore(const std::string &dbFileName)
77 {
78     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
79     if (dbFileName.empty()) {
80         return E_INVALID_FILE_PATH;
81     }
82     if (access(dbFileName.c_str(), F_OK) != 0) {
83         LOG_ERROR("Store to delete doesn't exist, path %{public}s", SqliteUtils::Anonymous(dbFileName).c_str());
84         return E_OK; // not not exist
85     }
86     RdbStoreConfig config(dbFileName);
87     Reportor::ReportRestore(Reportor::Create(config, E_OK, "RestoreType:Restore"));
88 
89     RdbStoreManager::GetInstance().Delete(dbFileName);
90     RdbSecurityManager::GetInstance().DelAllKeyFiles(dbFileName);
91     DeleteRdbStore(SqliteUtils::GetSlavePath(dbFileName));
92 
93     config.SetDBType(DB_SQLITE);
94     int errCodeSqlite = Connection::Delete(config);
95 
96     config.SetDBType(DB_VECTOR);
97     int errCodeVector = Connection::Delete(config);
98 
99     int errCode = (errCodeSqlite == E_OK && errCodeVector == E_OK) ? E_OK : E_REMOVE_FILE;
100     LOG_INFO("Delete rdb store ret sqlite=%{public}d, vector=%{public}d, path %{public}s",
101         errCodeSqlite, errCodeVector, SqliteUtils::Anonymous(dbFileName).c_str());
102     return errCode;
103 }
104 
DeleteRdbStore(const RdbStoreConfig & config)105 int RdbHelper::DeleteRdbStore(const RdbStoreConfig &config)
106 {
107     DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
108     auto dbFile = config.GetPath();
109     if (dbFile.empty()) {
110         return E_INVALID_FILE_PATH;
111     }
112     if (access(dbFile.c_str(), F_OK) != 0) {
113         LOG_ERROR("not exist, path %{public}s", SqliteUtils::Anonymous(dbFile).c_str());
114         return E_OK; // not not exist
115     }
116     Reportor::ReportRestore(Reportor::Create(config, E_OK, "RestoreType:Restore"));
117     RdbStoreManager::GetInstance().Delete(dbFile);
118     Connection::Delete(config);
119     RdbSecurityManager::GetInstance().DelAllKeyFiles(dbFile);
120     LOG_INFO("Delete rdb store, path %{public}s", SqliteUtils::Anonymous(dbFile).c_str());
121     return E_OK;
122 }
123 } // namespace NativeRdb
124 } // namespace OHOS
125