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 }, { "{customDir}", &DirectoryManager::GetCustomDir } })
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:
146 [[fallthrough]];
147 case SecurityLevel::S1:
148 return "misc_de";
149 }
150 return "misc_ce";
151 }
152
GetArea(const StoreMetaData & metaData) const153 std::string DirectoryManager::GetArea(const StoreMetaData &metaData) const
154 {
155 return std::string("el") + std::to_string(metaData.area);
156 }
157
GetUserId(const StoreMetaData & metaData) const158 std::string DirectoryManager::GetUserId(const StoreMetaData &metaData) const
159 {
160 auto type = AccessTokenKit::GetTokenTypeFlag(metaData.tokenId);
161 if ((type == TOKEN_NATIVE || type == TOKEN_SHELL) && (metaData.user == StoreMetaData::ROOT_USER)) {
162 return "public";
163 }
164 return metaData.user;
165 }
166
GetBundleName(const StoreMetaData & metaData) const167 std::string DirectoryManager::GetBundleName(const StoreMetaData &metaData) const
168 {
169 if (metaData.instanceId == 0) {
170 return metaData.bundleName;
171 }
172 return "+clone-" + std::to_string(metaData.instanceId) + "+" + metaData.bundleName;
173 }
174
GetHapName(const StoreMetaData & metaData) const175 std::string DirectoryManager::GetHapName(const StoreMetaData &metaData) const
176 {
177 return metaData.hapName;
178 }
179
GetCustomDir(const StoreMetaData & metaData) const180 std::string DirectoryManager::GetCustomDir(const StoreMetaData &metaData) const
181 {
182 return metaData.customDir;
183 }
184
Split(const std::string & source,const std::string & pattern) const185 std::vector<std::string> DirectoryManager::Split(const std::string &source, const std::string &pattern) const
186 {
187 std::vector<std::string> values;
188 std::string::size_type pos = 0;
189 std::string::size_type nextPos = 0;
190 while (nextPos != std::string::npos) {
191 nextPos = source.find(pattern, pos);
192 if (nextPos == pos) {
193 pos = pos + pattern.size();
194 continue;
195 }
196 values.push_back(source.substr(pos, nextPos - pos));
197 pos = nextPos + pattern.size();
198 }
199 return values;
200 }
201
GetVersionIndex(uint32_t version) const202 int32_t DirectoryManager::GetVersionIndex(uint32_t version) const
203 {
204 for (size_t i = 0; i < strategies_.size(); ++i) {
205 if (version >= strategies_[i].version) {
206 return i;
207 }
208 }
209 return int32_t(strategies_.size()) - 1;
210 }
211
GetVersions()212 std::vector<uint32_t> DirectoryManager::GetVersions()
213 {
214 std::vector<uint32_t> versions;
215 for (size_t i = 0; i < strategies_.size(); ++i) {
216 versions.push_back(strategies_[i].version);
217 }
218 return versions;
219 }
220
GenPath(const StoreMetaData & metaData,uint32_t version,const std::string & exPath) const221 std::string DirectoryManager::GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath) const
222 {
223 int32_t index = GetVersionIndex(version);
224 if (index < 0) {
225 return "";
226 }
227 std::string path;
228 auto &strategy = strategies_[index];
229 for (size_t i = 0; i < strategy.pipes.size(); ++i) {
230 std::string section;
231 if (strategy.pipes[i] == nullptr) {
232 section = strategy.path[i];
233 } else {
234 section = (this->*(strategy.pipes[i]))(metaData);
235 }
236 if (section.empty()) {
237 continue;
238 }
239 path += "/" + section;
240 }
241 if (!exPath.empty()) {
242 path += "/" + exPath;
243 }
244 if (strategy.autoCreate) {
245 CreateDirectory(path);
246 }
247 return path;
248 }
249
CreateDirectory(const std::string & path) const250 bool DirectoryManager::CreateDirectory(const std::string &path) const
251 {
252 if (access(path.c_str(), F_OK) == 0) {
253 return true;
254 }
255
256 std::string::size_type index = 0;
257 do {
258 std::string subPath;
259 index = path.find('/', index + 1);
260 if (index == std::string::npos) {
261 subPath = path;
262 } else {
263 subPath = path.substr(0, index);
264 }
265
266 if (access(subPath.c_str(), F_OK) != 0) {
267 if (mkdir(subPath.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0) {
268 return false;
269 }
270 }
271 } while (index != std::string::npos);
272
273 return access(path.c_str(), F_OK) == 0;
274 }
275 } // namespace OHOS::DistributedData
276