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
16 #include "rdb_helper.h"
17
18 #include "logger.h"
19 #include "rdb_errno.h"
20 #include "rdb_store_manager.h"
21 #include "rdb_trace.h"
22 #include "sqlite_global_config.h"
23 #include "unistd.h"
24
25 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
26 #include "rdb_security_manager.h"
27 #include "security_policy.h"
28 #endif
29
30 namespace OHOS {
31 namespace NativeRdb {
32 using namespace OHOS::Rdb;
33
GetRdbStore(const RdbStoreConfig & config,int version,RdbOpenCallback & openCallback,int & errCode)34 std::shared_ptr<RdbStore> RdbHelper::GetRdbStore(
35 const RdbStoreConfig &config, int version, RdbOpenCallback &openCallback, int &errCode)
36 {
37 DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
38 SqliteGlobalConfig::InitSqliteGlobalConfig();
39 return RdbStoreManager::GetInstance().GetRdbStore(config, errCode, version, openCallback);
40 }
41
ClearCache()42 void RdbHelper::ClearCache()
43 {
44 RdbStoreManager::GetInstance().Clear();
45 }
46
DeleteRdbKeyFiles(const std::string & dbFileName)47 static void DeleteRdbKeyFiles(const std::string &dbFileName)
48 {
49 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
50 RdbSecurityManager::GetInstance().DelRdbSecretDataFile(dbFileName);
51 #endif
52 }
53
DeleteRdbStore(const std::string & dbFileName)54 int RdbHelper::DeleteRdbStore(const std::string &dbFileName)
55 {
56 DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
57 if (dbFileName.empty()) {
58 return E_EMPTY_FILE_NAME;
59 }
60 RdbStoreManager::GetInstance().Delete(dbFileName); // maybe need to return here
61 if (access(dbFileName.c_str(), F_OK) != 0) {
62 return E_OK; // not not exist
63 }
64 int result = remove(dbFileName.c_str());
65 if (result != 0) {
66 LOG_ERROR("RdbHelper DeleteRdbStore failed to delete the db file err = %{public}d", errno);
67 return E_REMOVE_FILE;
68 }
69
70 int errCode = E_OK;
71 std::string shmFileName = dbFileName + "-shm";
72 if (access(shmFileName.c_str(), F_OK) == 0) {
73 result = remove(shmFileName.c_str());
74 if (result < 0) {
75 LOG_ERROR("RdbHelper DeleteRdbStore failed to delete the shm file err = %{public}d", errno);
76 errCode = E_REMOVE_FILE;
77 }
78 }
79
80 std::string walFileName = dbFileName + "-wal";
81 if (access(walFileName.c_str(), F_OK) == 0) {
82 result = remove(walFileName.c_str());
83 if (result < 0) {
84 LOG_ERROR("RdbHelper DeleteRdbStore failed to delete the wal file err = %{public}d", errno);
85 errCode = E_REMOVE_FILE;
86 }
87 }
88
89 std::string journalFileName = dbFileName + "-journal";
90 if (access(journalFileName.c_str(), F_OK) == 0) {
91 result = remove(journalFileName.c_str());
92 if (result < 0) {
93 LOG_ERROR("RdbHelper DeleteRdbStore failed to delete the journal file err = %{public}d", errno);
94 errCode = E_REMOVE_FILE;
95 }
96 }
97 DeleteRdbKeyFiles(dbFileName);
98
99 return errCode;
100 }
101 } // namespace NativeRdb
102 } // namespace OHOS
103