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 RdbStoreConfig config(dbFileName);
79 config.SetDBType(DB_SQLITE);
80 int errCodeSqlite = DeleteRdbStore(config);
81
82 config.SetDBType(DB_VECTOR);
83 int errCodeVector = DeleteRdbStore(config);
84 return (errCodeSqlite == E_OK && errCodeVector == E_OK) ? E_OK : E_REMOVE_FILE;
85 }
86
DeleteRdbStore(const RdbStoreConfig & config)87 int RdbHelper::DeleteRdbStore(const RdbStoreConfig &config)
88 {
89 DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
90 auto dbFile = config.GetPath();
91 if (dbFile.empty()) {
92 return E_INVALID_FILE_PATH;
93 }
94 if (access(dbFile.c_str(), F_OK) == 0) {
95 RdbStoreManager::GetInstance().Delete(dbFile);
96 }
97 Reportor::ReportRestore(Reportor::Create(config, E_OK, "RestoreType:Delete"));
98 Connection::Delete(config);
99 RdbSecurityManager::GetInstance().DelAllKeyFiles(dbFile);
100 LOG_INFO("Delete rdb store, dbType:%{public}d, path %{public}s", config.GetDBType(),
101 SqliteUtils::Anonymous(dbFile).c_str());
102 return E_OK;
103 }
104 } // namespace NativeRdb
105 } // namespace OHOS
106