1 /*
2 * Copyright (c) 2021 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 "DirectoryManager"
16 #include "directory/directory_manager.h"
17
18 #include <dirent.h>
19 #include <string>
20 #include <sys/stat.h>
21
22 #include "accesstoken_kit.h"
23 #include "log_print.h"
24 #include "types.h"
25 #include "unistd.h"
26 namespace OHOS::DistributedData {
27 using OHOS::DistributedKv::SecurityLevel;
28 using namespace OHOS::Security::AccessToken;
DirectoryManager()29 DirectoryManager::DirectoryManager()
30 : actions_({ { "{security}", &DirectoryManager::GetSecurity }, { "{store}", &DirectoryManager::GetStore },
31 { "{type}", &DirectoryManager::GetType }, { "{area}", &DirectoryManager::GetArea },
32 { "{userId}", &DirectoryManager::GetUserId }, { "{bundleName}", &DirectoryManager::GetBundleName },
33 { "{hapName}", &DirectoryManager::GetHapName } })
34 {
35 }
36
GetInstance()37 DirectoryManager &DirectoryManager::GetInstance()
38 {
39 static DirectoryManager instance;
40 return instance;
41 }
42
GetStorePath(const StoreMetaData & metaData,uint32_t version)43 std::string DirectoryManager::GetStorePath(const StoreMetaData &metaData, uint32_t version)
44 {
45 return GenPath(metaData, version, "");
46 }
47
GetStoreBackupPath(const StoreMetaData & metaData,uint32_t version)48 std::string DirectoryManager::GetStoreBackupPath(const StoreMetaData &metaData, uint32_t version)
49 {
50 auto rootBackupPath = GenPath(metaData, version, "backup");
51 return rootBackupPath + "/" + metaData.storeId;
52 }
53
GetSecretKeyPath(const StoreMetaData & metaData,uint32_t version)54 std::string DirectoryManager::GetSecretKeyPath(const StoreMetaData &metaData, uint32_t version)
55 {
56 return GenPath(metaData, version, "secret");
57 }
58
GetMetaStorePath(uint32_t version)59 std::string DirectoryManager::GetMetaStorePath(uint32_t version)
60 {
61 int32_t index = GetVersionIndex(version);
62 if (index < 0) {
63 return "";
64 }
65
66 auto &strategy = strategies_[index];
67 if (strategy.autoCreate) {
68 CreateDirectory(strategy.metaPath);
69 }
70 return strategy.metaPath;
71 }
72
GetMetaBackupPath(uint32_t version)73 std::string DirectoryManager::GetMetaBackupPath(uint32_t version)
74 {
75 int32_t index = GetVersionIndex(version);
76 if (index < 0) {
77 return "";
78 }
79
80 auto &strategy = strategies_[index];
81 std::string path = strategy.metaPath + "/backup";
82 if (strategy.autoCreate) {
83 CreateDirectory(path);
84 }
85 return path;
86 }
87
Initialize(const std::vector<Strategy> & strategies)88 void DirectoryManager::Initialize(const std::vector<Strategy> &strategies)
89 {
90 strategies_.resize(strategies.size());
91 for (size_t i = 0; i < strategies.size(); ++i) {
92 const Strategy &strategy = strategies[i];
93 StrategyImpl &impl = strategies_[i];
94 impl.autoCreate = strategy.autoCreate;
95 impl.version = strategy.version;
96 impl.metaPath = strategy.metaPath;
97 impl.path = Split(strategy.pattern, "/");
98 impl.pipes.clear();
99 for (auto &value : impl.path) {
100 auto it = actions_.find(value);
101 impl.pipes.push_back(it == actions_.end() ? nullptr : it->second);
102 }
103 }
104
105 std::sort(strategies_.begin(), strategies_.end(),
106 [](const StrategyImpl &curr, const StrategyImpl &prev) { return curr.version > prev.version; });
107 }
108
GetType(const StoreMetaData & metaData) const109 std::string DirectoryManager::GetType(const StoreMetaData &metaData) const
110 {
111 auto type = AccessTokenKit::GetTokenTypeFlag(metaData.tokenId);
112 if (type == TOKEN_NATIVE || type == TOKEN_SHELL) {
113 return "service";
114 }
115 return "app";
116 }
117
GetStore(const StoreMetaData & metaData) const118 std::string DirectoryManager::GetStore(const StoreMetaData &metaData) const
119 {
120 if (metaData.storeType >= StoreMetaData::StoreType::STORE_KV_BEGIN
121 && metaData.storeType <= StoreMetaData::StoreType::STORE_KV_END) {
122 return "kvdb";
123 }
124 // rdb use empty session
125 if (metaData.storeType >= StoreMetaData::StoreType::STORE_RELATIONAL_BEGIN
126 && metaData.storeType <= StoreMetaData::StoreType::STORE_RELATIONAL_END) {
127 return "rdb";
128 }
129 // object use meta
130 if (metaData.storeType >= StoreMetaData::StoreType::STORE_OBJECT_BEGIN
131 && metaData.storeType <= StoreMetaData::StoreType::STORE_OBJECT_END) {
132 return "kvdb";
133 }
134 return "other";
135 }
136
GetSecurity(const StoreMetaData & metaData) const137 std::string DirectoryManager::GetSecurity(const StoreMetaData &metaData) const
138 {
139 switch (metaData.securityLevel) {
140 case SecurityLevel::NO_LABEL:
141 if ((metaData.bundleName != metaData.appId) || (metaData.appType != "harmony")) {
142 break;
143 }
144 [[fallthrough]];
145 case SecurityLevel::S0: [[fallthrough]];
146 case SecurityLevel::S1:
147 return "misc_de";
148 }
149 return "misc_ce";
150 }
151
GetArea(const StoreMetaData & metaData) const152 std::string DirectoryManager::GetArea(const StoreMetaData &metaData) const
153 {
154 return std::string("el") + std::to_string(metaData.area);
155 }
156
GetUserId(const StoreMetaData & metaData) const157 std::string DirectoryManager::GetUserId(const StoreMetaData &metaData) const
158 {
159 auto type = AccessTokenKit::GetTokenTypeFlag(metaData.tokenId);
160 if (type == TOKEN_NATIVE || type == TOKEN_SHELL) {
161 return "public";
162 }
163 return metaData.user;
164 }
165
GetBundleName(const StoreMetaData & metaData) const166 std::string DirectoryManager::GetBundleName(const StoreMetaData &metaData) const
167 {
168 if (metaData.instanceId == 0) {
169 return metaData.bundleName;
170 }
171 return metaData.bundleName + "_" + std::to_string(metaData.instanceId);
172 }
173
GetHapName(const StoreMetaData & metaData) const174 std::string DirectoryManager::GetHapName(const StoreMetaData &metaData) const
175 {
176 return metaData.hapName;
177 }
178
Split(const std::string & source,const std::string & pattern) const179 std::vector<std::string> DirectoryManager::Split(const std::string &source, const std::string &pattern) const
180 {
181 std::vector<std::string> values;
182 std::string::size_type pos = 0;
183 std::string::size_type nextPos = 0;
184 while (nextPos != std::string::npos) {
185 nextPos = source.find(pattern, pos);
186 if (nextPos == pos) {
187 pos = pos + pattern.size();
188 continue;
189 }
190 values.push_back(source.substr(pos, nextPos - pos));
191 pos = nextPos + pattern.size();
192 }
193 return values;
194 }
195
GetVersionIndex(uint32_t version) const196 int32_t DirectoryManager::GetVersionIndex(uint32_t version) const
197 {
198 for (size_t i = 0; i < strategies_.size(); ++i) {
199 if (version >= strategies_[i].version) {
200 return i;
201 }
202 }
203 return int32_t(strategies_.size()) - 1;
204 }
205
GetVersions()206 std::vector<uint32_t> DirectoryManager::GetVersions()
207 {
208 std::vector<uint32_t> versions;
209 for (size_t i = 0; i < strategies_.size(); ++i) {
210 versions.push_back(strategies_[i].version);
211 }
212 return versions;
213 }
214
GenPath(const StoreMetaData & metaData,uint32_t version,const std::string & exPath) const215 std::string DirectoryManager::GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath) const
216 {
217 int32_t index = GetVersionIndex(version);
218 if (index < 0) {
219 return "";
220 }
221 std::string path;
222 auto &strategy = strategies_[index];
223 for (size_t i = 0; i < strategy.pipes.size(); ++i) {
224 std::string section;
225 if (strategy.pipes[i] == nullptr) {
226 section = strategy.path[i];
227 } else {
228 section = (this->*(strategy.pipes[i]))(metaData);
229 }
230 if (section.empty()) {
231 continue;
232 }
233 path += "/" + section;
234 }
235 if (!exPath.empty()) {
236 path += "/" + exPath;
237 }
238 if (strategy.autoCreate) {
239 CreateDirectory(path);
240 }
241 return path;
242 }
243
CreateDirectory(const std::string & path) const244 bool DirectoryManager::CreateDirectory(const std::string &path) const
245 {
246 if (access(path.c_str(), F_OK) == 0) {
247 return true;
248 }
249
250 std::string::size_type index = 0;
251 do {
252 std::string subPath;
253 index = path.find('/', index + 1);
254 if (index == std::string::npos) {
255 subPath = path;
256 } else {
257 subPath = path.substr(0, index);
258 }
259
260 if (access(subPath.c_str(), F_OK) != 0) {
261 if (mkdir(subPath.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0) {
262 return false;
263 }
264 }
265 } while (index != std::string::npos);
266
267 return access(path.c_str(), F_OK) == 0;
268 }
269 } // namespace OHOS::DistributedData
270