• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "GdbStoreManager"
16 #include "db_store_manager.h"
17 
18 #include <unistd.h>
19 
20 #include <regex>
21 
22 #include "gdb_errors.h"
23 #include "db_store_impl.h"
24 #include "gdb_utils.h"
25 #include "logger.h"
26 #include "security_label.h"
27 
28 namespace OHOS::DistributedDataAip {
GetInstance()29 StoreManager &StoreManager::GetInstance()
30 {
31     static StoreManager manager;
32     return manager;
33 }
34 
35 StoreManager::StoreManager() = default;
36 
~StoreManager()37 StoreManager::~StoreManager()
38 {
39     Clear();
40 }
41 
IsValidName(const std::string & name)42 bool StoreManager::IsValidName(const std::string &name)
43 {
44     const std::regex pattern("^[a-zA-Z0-9_]+$");
45     return std::regex_match(name, pattern);
46 }
47 
IsValidSecurityLevel(const int32_t securityLevel)48 bool StoreManager::IsValidSecurityLevel(const int32_t securityLevel)
49 {
50     return securityLevel >= SecurityLevel::S1 && securityLevel <= SecurityLevel::S4;
51 }
52 
GetDBStore(const StoreConfig & config,int & errCode)53 std::shared_ptr<DBStore> StoreManager::GetDBStore(const StoreConfig &config, int &errCode)
54 {
55     if (!IsValidName(config.GetName())) {
56         LOG_ERROR("GetDBStore failed. Invalid name");
57         errCode = E_GRD_INVAILD_NAME_ERR;
58         return nullptr;
59     }
60     if (!IsValidSecurityLevel(config.GetSecurityLevel())) {
61         LOG_ERROR("GetDBStore failed. Invalid securityLevel: %{public}d", config.GetSecurityLevel());
62         errCode = E_INVALID_ARGS;
63         return nullptr;
64     }
65     std::lock_guard<std::mutex> lock(mutex_);
66     auto path = config.GetFullPath();
67     if (storeCache_.find(path) != storeCache_.end()) {
68         std::shared_ptr<DBStoreImpl> dbStore = storeCache_[path].lock();
69         if (dbStore != nullptr) {
70             LOG_ERROR("GetDBStore reuse success.");
71             return dbStore;
72         }
73         storeCache_.erase(path);
74     }
75     // open and store DBStore
76     std::shared_ptr<DBStoreImpl> dbStore = std::make_shared<DBStoreImpl>(config);
77     errCode = dbStore->InitConn();
78     if (errCode != E_OK) {
79         LOG_ERROR("GetDBStore InitConn failed, name=%{public}s, errCode=%{public}d",
80             GdbUtils::Anonymous(config.GetName()).c_str(), errCode);
81         return nullptr;
82     }
83     errCode = SetSecurityLabel(config);
84     if (errCode != E_OK) {
85         LOG_ERROR("GetDBStore SetSecurityLabel failed, errCode=%{public}d", errCode);
86         return nullptr;
87     }
88     storeCache_[path] = dbStore;
89     return dbStore;
90 }
91 
Clear()92 void StoreManager::Clear()
93 {
94     std::lock_guard<std::mutex> lock(mutex_);
95     auto iter = storeCache_.begin();
96     while (iter != storeCache_.end()) {
97         if (auto store = iter->second.lock()) {
98             store->Close();
99         }
100         iter = storeCache_.erase(iter);
101     }
102     storeCache_.clear();
103 }
104 
Delete(const std::string & path)105 bool StoreManager::Delete(const std::string &path)
106 {
107     LOG_DEBUG("Delete file, path=%{public}s", GdbUtils::Anonymous(path).c_str());
108     {
109         std::lock_guard<std::mutex> lock(mutex_);
110         auto item = storeCache_.find(path);
111         if (item != storeCache_.end()) {
112             if (auto store = item->second.lock()) {
113                 store->Close();
114             }
115             storeCache_.erase(path); // clean invalid store ptr
116         }
117     }
118 
119     bool deleteResult = true;
120     for (auto &suffix : GRD_POST_FIXES) {
121         deleteResult = DeleteFile(path + suffix) && deleteResult;
122     }
123     return deleteResult;
124 }
125 
DeleteFile(const std::string & path)126 bool StoreManager::DeleteFile(const std::string &path)
127 {
128     if (access(path.c_str(), 0) != 0) {
129         LOG_WARN("access return, path=%{public}s", GdbUtils::Anonymous(path).c_str());
130         return true;
131     }
132     auto ret = remove(path.c_str());
133     if (ret != 0) {
134         LOG_ERROR("remove file failed errno %{public}d ret %{public}d %{public}s", errno, ret,
135             GdbUtils::Anonymous(path).c_str());
136         return false;
137     }
138     return true;
139 }
140 
SetSecurityLabel(const StoreConfig & config)141 int StoreManager::SetSecurityLabel(const StoreConfig &config)
142 {
143     if (config.GetSecurityLevel() >= SecurityLevel::S1 && config.GetSecurityLevel() <= SecurityLevel::S4) {
144         auto toSetLevel = GetSecurityLevelValue(static_cast<SecurityLevel>(config.GetSecurityLevel()));
145         auto errCode =
146             FileManagement::ModuleSecurityLabel::SecurityLabel::SetSecurityLabel(config.GetFullPath(), toSetLevel)
147                 ? E_OK
148                 : E_CONFIG_INVALID_CHANGE;
149         if (errCode != E_OK) {
150             auto currentLevel = GetFileSecurityLevel(config.GetFullPath());
151             LOG_ERROR(
152                 "Set security level:%{public}s -> %{public}s, result=%{public}d, errno=%{public}d, name=%{public}s.",
153                 currentLevel.c_str(), toSetLevel.c_str(), errCode, errno,
154                 GdbUtils::Anonymous(config.GetName()).c_str());
155         }
156         return errCode;
157     }
158     return E_OK;
159 }
160 
GetSecurityLevelValue(SecurityLevel securityLevel)161 std::string StoreManager::GetSecurityLevelValue(SecurityLevel securityLevel)
162 {
163     switch (securityLevel) {
164         case SecurityLevel::S1:
165             return "s1";
166         case SecurityLevel::S2:
167             return "s2";
168         case SecurityLevel::S3:
169             return "s3";
170         case SecurityLevel::S4:
171             return "s4";
172         default:
173             return "";
174     }
175 }
176 
GetFileSecurityLevel(const std::string & filePath)177 std::string StoreManager::GetFileSecurityLevel(const std::string &filePath)
178 {
179     return FileManagement::ModuleSecurityLabel::SecurityLabel::GetSecurityLabel(filePath);
180 }
181 
182 } // namespace OHOS::DistributedDataAip