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: [[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
GetCustomDir(const StoreMetaData & metaData) const179 std::string DirectoryManager::GetCustomDir(const StoreMetaData &metaData) const
180 {
181 return metaData.customDir;
182 }
183
Split(const std::string & source,const std::string & pattern) const184 std::vector<std::string> DirectoryManager::Split(const std::string &source, const std::string &pattern) const
185 {
186 std::vector<std::string> values;
187 std::string::size_type pos = 0;
188 std::string::size_type nextPos = 0;
189 while (nextPos != std::string::npos) {
190 nextPos = source.find(pattern, pos);
191 if (nextPos == pos) {
192 pos = pos + pattern.size();
193 continue;
194 }
195 values.push_back(source.substr(pos, nextPos - pos));
196 pos = nextPos + pattern.size();
197 }
198 return values;
199 }
200
GetVersionIndex(uint32_t version) const201 int32_t DirectoryManager::GetVersionIndex(uint32_t version) const
202 {
203 for (size_t i = 0; i < strategies_.size(); ++i) {
204 if (version >= strategies_[i].version) {
205 return i;
206 }
207 }
208 return int32_t(strategies_.size()) - 1;
209 }
210
GetVersions()211 std::vector<uint32_t> DirectoryManager::GetVersions()
212 {
213 std::vector<uint32_t> versions;
214 for (size_t i = 0; i < strategies_.size(); ++i) {
215 versions.push_back(strategies_[i].version);
216 }
217 return versions;
218 }
219
GenPath(const StoreMetaData & metaData,uint32_t version,const std::string & exPath) const220 std::string DirectoryManager::GenPath(const StoreMetaData &metaData, uint32_t version, const std::string &exPath) const
221 {
222 int32_t index = GetVersionIndex(version);
223 if (index < 0) {
224 return "";
225 }
226 std::string path;
227 auto &strategy = strategies_[index];
228 for (size_t i = 0; i < strategy.pipes.size(); ++i) {
229 std::string section;
230 if (strategy.pipes[i] == nullptr) {
231 section = strategy.path[i];
232 } else {
233 section = (this->*(strategy.pipes[i]))(metaData);
234 }
235 if (section.empty()) {
236 continue;
237 }
238 path += "/" + section;
239 }
240 if (!exPath.empty()) {
241 path += "/" + exPath;
242 }
243 if (strategy.autoCreate) {
244 CreateDirectory(path);
245 }
246 return path;
247 }
248
CreateDirectory(const std::string & path) const249 bool DirectoryManager::CreateDirectory(const std::string &path) const
250 {
251 if (access(path.c_str(), F_OK) == 0) {
252 return true;
253 }
254
255 std::string::size_type index = 0;
256 do {
257 std::string subPath;
258 index = path.find('/', index + 1);
259 if (index == std::string::npos) {
260 subPath = path;
261 } else {
262 subPath = path.substr(0, index);
263 }
264
265 if (access(subPath.c_str(), F_OK) != 0) {
266 if (mkdir(subPath.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) != 0) {
267 return false;
268 }
269 }
270 } while (index != std::string::npos);
271
272 return access(path.c_str(), F_OK) == 0;
273 }
274 } // namespace OHOS::DistributedData
275