1 /*
2 * Copyright (c) 2025 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 "context_impl.h"
17
18 #include <cstring>
19 #include <regex>
20
21 #include "bundle_container.h"
22 #include "bundle_info.h"
23 #include "hilog_tag_wrapper.h"
24 #include "js_data_converter.h"
25 #include "res_common.h"
26 #include "res_config.h"
27 #include "resource_manager_helper.h"
28
29 namespace OHOS {
30 namespace AbilityRuntime {
31 const size_t Context::CONTEXT_TYPE_ID(std::hash<const char *>{}("Context"));
32 const int64_t ContextImpl::CONTEXT_CREATE_BY_SYSTEM_APP(0x00000001);
33 constexpr const char *CONTEXT_DISTRIBUTEDFILES("distributedfiles");
34 constexpr const char *CONTEXT_CLOUD("cloud");
35 constexpr const char *CONTEXT_FILE_SEPARATOR("/");
36 constexpr const char *CONTEXT_FILE_OPPOSITE_SEPARATOR("\\");
37 constexpr const char *CONTEXT_BASE("base");
38 constexpr const char *CONTEXT_CACHE("cache");
39 constexpr const char *CONTEXT_PREFERENCES("preferences");
40 constexpr const char *CONTEXT_DATABASE("database");
41 constexpr const char *CONTEXT_TEMP("temp");
42 constexpr const char *CONTEXT_FILES("files");
43 constexpr const char *CONTEXT_HAPS("haps");
44 constexpr const char *CONTEXT_ASSET("asset");
45 constexpr const char *CONTEXT_ELS[] = {"el1", "el2", "el3", "el4", "el5"};
46 constexpr const char *CONTEXT_RESOURCE_BASE("/data/storage/el1/bundle");
47 constexpr const char *CONTEXT_RESOURCE_END("/resources/resfile");
48 const int32_t TYPE_RESERVE = 1;
49 const int32_t TYPE_OTHERS = 2;
50 const int32_t API11 = 11;
51 const int32_t API_VERSION_MOD = 100;
52 const int AREA1 = 1;
53 const int AREA2 = 2;
54 const int AREA3 = 3;
55 const int AREA4 = 4;
56 constexpr int DIR_DEFAULT_PERM = 0770;
GetConfiguration()57 std::shared_ptr<AppExecFwk::Configuration> ContextImpl::GetConfiguration()
58 {
59 return configuration_;
60 }
61
SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & configuration)62 void ContextImpl::SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &configuration)
63 {
64 if (configuration == nullptr) {
65 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null configuration");
66 return;
67 }
68 configuration_ = configuration;
69 }
70
GetApplicationInfo() const71 std::shared_ptr<AppExecFwk::ApplicationInfo> ContextImpl::GetApplicationInfo() const
72 {
73 return applicationInfo_;
74 }
75
SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> & info)76 void ContextImpl::SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info)
77 {
78 if (info == nullptr) {
79 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null applicationInfo");
80 return;
81 }
82 applicationInfo_ = info;
83 }
84
GetHapModuleInfo() const85 std::shared_ptr<AppExecFwk::HapModuleInfo> ContextImpl::GetHapModuleInfo() const
86 {
87 if (hapModuleInfo_ == nullptr) {
88 TAG_LOGD(AAFwkTag::ABILITY_SIM, "hapModuleInfo is empty");
89 }
90 return hapModuleInfo_;
91 }
92
InitHapModuleInfo(const AppExecFwk::HapModuleInfo & hapModuleInfo)93 void ContextImpl::InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo)
94 {
95 hapModuleInfo_ = std::make_shared<AppExecFwk::HapModuleInfo>(hapModuleInfo);
96 }
97
GetResourceManager() const98 std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::GetResourceManager() const
99 {
100 return resourceManager_;
101 }
102
SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)103 void ContextImpl::SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
104 {
105 resourceManager_ = resourceManager;
106 }
107
GetOptions()108 Options ContextImpl::GetOptions()
109 {
110 return options_;
111 }
112
SetOptions(const Options & options)113 void ContextImpl::SetOptions(const Options &options)
114 {
115 options_ = options;
116 }
117
GetBundleName() const118 std::string ContextImpl::GetBundleName() const
119 {
120 return (applicationInfo_ != nullptr) ? applicationInfo_->bundleName : "";
121 }
122
GetBundleCodePath()123 std::string ContextImpl::GetBundleCodePath()
124 {
125 return (applicationInfo_ != nullptr) ? applicationInfo_->codePath : "";
126 }
127
GetPreviewPath()128 std::string ContextImpl::GetPreviewPath()
129 {
130 auto path = AppExecFwk::BundleContainer::GetInstance().GetBundleCodeDir();
131 auto pos = path.find(CONTEXT_FILE_SEPARATOR);
132 if (pos == std::string::npos) {
133 fileSeparator_ = CONTEXT_FILE_OPPOSITE_SEPARATOR;
134 }
135 return path;
136 }
137
GetBundleCodeDir()138 std::string ContextImpl::GetBundleCodeDir()
139 {
140 return GetPreviewPath();
141 }
142
GetCacheDir()143 std::string ContextImpl::GetCacheDir()
144 {
145 if (GetPreviewPath().empty()) {
146 return "";
147 }
148
149 auto dir = GetBaseDir() + fileSeparator_ + CONTEXT_CACHE;
150 CreateMultiDir(dir);
151 return dir;
152 }
153
GetTempDir()154 std::string ContextImpl::GetTempDir()
155 {
156 if (GetPreviewPath().empty()) {
157 return "";
158 }
159
160 auto dir = GetBaseDir() + fileSeparator_ + CONTEXT_TEMP;
161 CreateMultiDir(dir);
162 return dir;
163 }
164
GetResourceDir()165 std::string ContextImpl::GetResourceDir()
166 {
167 std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfoPtr = GetHapModuleInfo();
168 if (hapModuleInfoPtr == nullptr || hapModuleInfoPtr->moduleName.empty()) {
169 return "";
170 }
171 auto dir = std::string(CONTEXT_RESOURCE_BASE) +
172 CONTEXT_FILE_SEPARATOR + hapModuleInfoPtr->moduleName + CONTEXT_RESOURCE_END;
173 if (Access(dir)) {
174 return dir;
175 }
176 return "";
177 }
178
GetFilesDir()179 std::string ContextImpl::GetFilesDir()
180 {
181 if (GetPreviewPath().empty()) {
182 return "";
183 }
184
185 auto dir = GetBaseDir() + fileSeparator_ + CONTEXT_FILES;
186 CreateMultiDir(dir);
187 return dir;
188 }
189
GetDatabaseDir()190 std::string ContextImpl::GetDatabaseDir()
191 {
192 auto preivewDir = GetPreviewPath();
193 if (preivewDir.empty()) {
194 return "";
195 }
196
197 std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfoPtr = GetHapModuleInfo();
198 if (hapModuleInfoPtr == nullptr || hapModuleInfoPtr->moduleName.empty()) {
199 return "";
200 }
201 auto dir = preivewDir + fileSeparator_ + currArea_ + fileSeparator_ + CONTEXT_DATABASE +
202 fileSeparator_ + hapModuleInfoPtr->moduleName;
203 CreateMultiDir(dir);
204 return dir;
205 }
206
GetPreferencesDir()207 std::string ContextImpl::GetPreferencesDir()
208 {
209 if (GetPreviewPath().empty()) {
210 return "";
211 }
212
213 auto dir = GetBaseDir() + fileSeparator_ + CONTEXT_PREFERENCES;
214 CreateMultiDir(dir);
215 return dir;
216 }
217
GetDistributedFilesDir()218 std::string ContextImpl::GetDistributedFilesDir()
219 {
220 auto preivewDir = GetPreviewPath();
221 if (preivewDir.empty()) {
222 return "";
223 }
224
225 auto dir = preivewDir + fileSeparator_ + currArea_ + fileSeparator_ + CONTEXT_DISTRIBUTEDFILES;
226 CreateMultiDir(dir);
227 return dir;
228 }
229
GetCloudFileDir()230 std::string ContextImpl::GetCloudFileDir()
231 {
232 auto preivewDir = GetPreviewPath();
233 if (preivewDir.empty()) {
234 return "";
235 }
236
237 auto dir = GetBaseDir() + fileSeparator_ + CONTEXT_CLOUD;
238 CreateMultiDir(dir);
239 return dir;
240 }
241
SwitchArea(int mode)242 void ContextImpl::SwitchArea(int mode)
243 {
244 TAG_LOGD(AAFwkTag::ABILITY_SIM, "mode:%{public}d", mode);
245 if (mode < 0 || mode >= (int)(sizeof(CONTEXT_ELS) / sizeof(CONTEXT_ELS[0]))) {
246 TAG_LOGE(AAFwkTag::ABILITY_SIM, "mode invalid");
247 return;
248 }
249 currArea_ = CONTEXT_ELS[mode];
250 TAG_LOGD(AAFwkTag::ABILITY_SIM, "currArea:%{public}s", currArea_.c_str());
251 }
GetArea()252 int ContextImpl::GetArea()
253 {
254 TAG_LOGD(AAFwkTag::ABILITY_SIM, "begin");
255 int mode = -1;
256 for (int i = 0; i < (int)(sizeof(CONTEXT_ELS) / sizeof(CONTEXT_ELS[0])); i++) {
257 if (currArea_ == CONTEXT_ELS[i]) {
258 mode = i;
259 break;
260 }
261 }
262 if (mode == -1) {
263 TAG_LOGE(AAFwkTag::ABILITY_SIM, "not find mode");
264 return EL_DEFAULT;
265 }
266 TAG_LOGD(AAFwkTag::ABILITY_SIM, "end");
267 return mode;
268 }
269
GetBaseDir()270 std::string ContextImpl::GetBaseDir()
271 {
272 auto previewPath = GetPreviewPath();
273 if (previewPath.empty()) {
274 return "";
275 }
276 std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfoPtr = GetHapModuleInfo();
277 if (hapModuleInfoPtr == nullptr || hapModuleInfoPtr->moduleName.empty()) {
278 return "";
279 }
280 return previewPath + fileSeparator_ + currArea_ + fileSeparator_ + CONTEXT_BASE + fileSeparator_ +
281 CONTEXT_HAPS + fileSeparator_ + hapModuleInfoPtr->moduleName;
282 }
283
GetBundleInfo(const std::string & bundleName,const std::string & moduleName,AppExecFwk::BundleInfo & bundleInfo)284 void ContextImpl::GetBundleInfo(
285 const std::string &bundleName, const std::string &moduleName, AppExecFwk::BundleInfo &bundleInfo)
286 {
287 TAG_LOGD(AAFwkTag::ABILITY_SIM, "begin");
288 if (bundleName.empty() || moduleName.empty()) {
289 return;
290 }
291 AppExecFwk::BundleContainer::GetInstance().GetBundleInfo(bundleName, moduleName, bundleInfo);
292 }
293
UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)294 void ContextImpl::UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
295 {
296 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
297 if (resConfig == nullptr) {
298 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null resConfig");
299 return;
300 }
301
302 if (GetHapModuleInfo() != nullptr && GetApplicationInfo() != nullptr) {
303 std::vector<AppExecFwk::Metadata> metadata = GetHapModuleInfo()->metadata;
304 bool load = std::any_of(metadata.begin(), metadata.end(), [](const auto &metadataItem) {
305 return metadataItem.name == "ContextResourceConfigLoadFromParentTemp" && metadataItem.value == "true";
306 });
307 if (load && GetApplicationInfo()->apiTargetVersion % API_VERSION_MOD >= API11) {
308 std::shared_ptr<Global::Resource::ResourceManager> currentResMgr = GetResourceManager();
309 if (currentResMgr != nullptr) {
310 TAG_LOGD(AAFwkTag::ABILITY_SIM, "apiVersion: %{public}d, load parent config",
311 GetApplicationInfo()->apiTargetVersion);
312 currentResMgr->GetResConfig(*resConfig);
313 }
314 }
315 }
316 ResourceManagerHelper::GetInstance().GetResConfig(*resConfig, true);
317 resourceManager->UpdateResConfig(*resConfig);
318 }
319
UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> src,std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)320 void ContextImpl::UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> src,
321 std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
322 {
323 if (src == nullptr) {
324 UpdateResConfig(resourceManager);
325 return;
326 }
327 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
328 if (resConfig == nullptr) {
329 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null resConfig");
330 return;
331 }
332 src->GetResConfig(*resConfig);
333 resourceManager->UpdateResConfig(*resConfig);
334 }
335
InitOthersResourceManagerInner(const AppExecFwk::BundleInfo & bundleInfo,bool currentBundle,const std::string & moduleName)336 std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::InitOthersResourceManagerInner(
337 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string &moduleName)
338 {
339 std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
340 std::string hapPath;
341 std::vector<std::string> overlayPaths;
342 int32_t appType;
343 if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_RESERVE)) {
344 appType = TYPE_RESERVE;
345 } else if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_OTHERS)) {
346 appType = TYPE_OTHERS;
347 } else {
348 appType = 0;
349 }
350 std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager(
351 bundleInfo.name, moduleName, hapPath, overlayPaths, *resConfig, appType));
352 return resourceManager;
353 }
354
InitResourceManagerInner(const AppExecFwk::BundleInfo & bundleInfo,bool currentBundle,const std::string & moduleName,std::shared_ptr<Context> inputContext)355 std::shared_ptr<Global::Resource::ResourceManager> ContextImpl::InitResourceManagerInner(
356 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string &moduleName,
357 std::shared_ptr<Context> inputContext)
358 {
359 std::shared_ptr<Global::Resource::ResourceManager> resourceManager =
360 InitOthersResourceManagerInner(bundleInfo, currentBundle, moduleName);
361 if (resourceManager == nullptr) {
362 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null resourceManager");
363 return resourceManager;
364 }
365 if (!moduleName.empty() || !bundleInfo.applicationInfo.multiProjects) {
366 TAG_LOGD(AAFwkTag::ABILITY_SIM, "hapModuleInfos count: %{public}zu", bundleInfo.hapModuleInfos.size());
367 for (auto hapModuleInfo : bundleInfo.hapModuleInfos) {
368 if (!moduleName.empty() && hapModuleInfo.moduleName != moduleName) {
369 continue;
370 }
371 std::string loadPath = hapModuleInfo.hapPath.empty() ? hapModuleInfo.resourcePath : hapModuleInfo.hapPath;
372 if (loadPath.empty()) {
373 TAG_LOGD(AAFwkTag::ABILITY_SIM, "loadPath is empty");
374 continue;
375 }
376 TAG_LOGD(AAFwkTag::ABILITY_SIM, "loadPath: %{public}s", loadPath.c_str());
377 if (!resourceManager->AddResource(loadPath.c_str())) {
378 TAG_LOGE(AAFwkTag::ABILITY_SIM, "Add resource failed");
379 }
380 ResourceManagerHelper::GetInstance().AddSystemResource(resourceManager);
381 }
382 }
383 return resourceManager;
384 }
385
InitResourceManager(const AppExecFwk::BundleInfo & bundleInfo,const std::shared_ptr<ContextImpl> & appContext,bool currentBundle,const std::string & moduleName,std::shared_ptr<Context> inputContext)386 void ContextImpl::InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo,
387 const std::shared_ptr<ContextImpl> &appContext, bool currentBundle, const std::string &moduleName,
388 std::shared_ptr<Context> inputContext)
389 {
390 TAG_LOGD(AAFwkTag::ABILITY_SIM, "begin, bundleName:%{public}s, moduleName:%{public}s", bundleInfo.name.c_str(),
391 moduleName.c_str());
392
393 if (appContext == nullptr) {
394 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null appContext");
395 return;
396 }
397 if (bundleInfo.applicationInfo.codePath == std::to_string(TYPE_RESERVE) ||
398 bundleInfo.applicationInfo.codePath == std::to_string(TYPE_OTHERS)) {
399 std::shared_ptr<Global::Resource::ResourceManager> resourceManager =
400 InitOthersResourceManagerInner(bundleInfo, currentBundle, moduleName);
401 if (resourceManager == nullptr) {
402 TAG_LOGE(AAFwkTag::ABILITY_SIM, "null resourceManager");
403 return;
404 }
405 appContext->SetResourceManager(resourceManager);
406 return;
407 }
408
409 std::shared_ptr<Global::Resource::ResourceManager> resourceManager =
410 InitResourceManagerInner(bundleInfo, currentBundle, moduleName, inputContext);
411 if (resourceManager == nullptr) {
412 return;
413 }
414 std::shared_ptr<Global::Resource::ResourceManager> src = nullptr;
415 if (inputContext) {
416 src = inputContext->GetResourceManager();
417 }
418 UpdateResConfig(src, resourceManager);
419 appContext->SetResourceManager(resourceManager);
420 }
421
CreateModuleContext(const std::string & moduleName)422 std::shared_ptr<Context> ContextImpl::CreateModuleContext(const std::string &moduleName)
423 {
424 return CreateModuleContext(GetBundleName(), moduleName, nullptr);
425 }
426
CreateModuleContext(const std::string & bundleName,const std::string & moduleName)427 std::shared_ptr<Context> ContextImpl::CreateModuleContext(const std::string &bundleName, const std::string &moduleName)
428 {
429 return CreateModuleContext(bundleName, moduleName, nullptr);
430 }
431
GetBundleNameWithContext(std::shared_ptr<Context> inputContext) const432 std::string ContextImpl::GetBundleNameWithContext(std::shared_ptr<Context> inputContext) const
433 {
434 if (inputContext != nullptr) {
435 return inputContext->GetBundleName();
436 }
437 return GetBundleName();
438 }
439
CreateModuleContext(const std::string & moduleName,std::shared_ptr<Context> inputContext)440 std::shared_ptr<Context> ContextImpl::CreateModuleContext(
441 const std::string &moduleName, std::shared_ptr<Context> inputContext)
442 {
443 return CreateModuleContext(GetBundleNameWithContext(inputContext), moduleName, inputContext);
444 }
445
CreateModuleContext(const std::string & bundleName,const std::string & moduleName,std::shared_ptr<Context> inputContext)446 std::shared_ptr<Context> ContextImpl::CreateModuleContext(
447 const std::string &bundleName, const std::string &moduleName, std::shared_ptr<Context> inputContext)
448 {
449 if (bundleName.empty() || moduleName.empty()) {
450 return nullptr;
451 }
452
453 TAG_LOGD(AAFwkTag::ABILITY_SIM, "bundleName: %{public}s", bundleName.c_str());
454 AppExecFwk::BundleInfo bundleInfo;
455 GetBundleInfo(bundleName, moduleName, bundleInfo);
456 if (bundleInfo.name.empty() || bundleInfo.applicationInfo.name.empty()) {
457 TAG_LOGE(AAFwkTag::ABILITY_SIM, "GetBundleInfo error");
458 ErrCode ret = AppExecFwk::BundleContainer::GetInstance().GetDependentBundleInfo(
459 bundleName, moduleName, bundleInfo, AppExecFwk::GetDependentBundleInfoFlag::GET_ALL_DEPENDENT_BUNDLE_INFO);
460 if (ret != ERR_OK) {
461 TAG_LOGE(AAFwkTag::ABILITY_SIM, "GetDependentBundleInfo failed:%{public}d", ret);
462 return nullptr;
463 }
464 }
465
466 auto appContext = std::make_shared<ContextImpl>();
467 if (bundleInfo.applicationInfo.codePath != std::to_string(TYPE_RESERVE) &&
468 bundleInfo.applicationInfo.codePath != std::to_string(TYPE_OTHERS)) {
469 TAG_LOGD(AAFwkTag::ABILITY_SIM, "modulename: %{public}s, bundleName: %{public}s", moduleName.c_str(),
470 bundleName.c_str());
471 auto info = std::find_if(bundleInfo.hapModuleInfos.begin(), bundleInfo.hapModuleInfos.end(),
472 [&moduleName](
473 const AppExecFwk::HapModuleInfo &hapModuleInfo) { return hapModuleInfo.moduleName == moduleName; });
474 if (info == bundleInfo.hapModuleInfos.end()) {
475 TAG_LOGE(AAFwkTag::ABILITY_SIM, "moduleName %{public}s error", moduleName.c_str());
476 return nullptr;
477 }
478 appContext->InitHapModuleInfo(*info);
479 }
480
481 appContext->SetConfiguration(configuration_);
482 bool self = false;
483 if (inputContext != nullptr) {
484 self = (bundleName == inputContext->GetBundleName());
485 } else {
486 self = bundleName == GetBundleName();
487 }
488 InitResourceManager(bundleInfo, appContext, self, moduleName, inputContext);
489 appContext->SetApplicationInfo(std::make_shared<AppExecFwk::ApplicationInfo>(bundleInfo.applicationInfo));
490 return appContext;
491 }
492
FsReqCleanup(uv_fs_t * req)493 void ContextImpl::FsReqCleanup(uv_fs_t *req)
494 {
495 uv_fs_req_cleanup(req);
496 if (req) {
497 delete req;
498 req = nullptr;
499 }
500 }
501
Access(const std::string & path)502 bool ContextImpl::Access(const std::string &path)
503 {
504 TAG_LOGD(AAFwkTag::ABILITY_SIM, "Access: dir: %{public}s", path.c_str());
505 std::unique_ptr<uv_fs_t, decltype(ContextImpl::FsReqCleanup) *> access_req = { new uv_fs_t,
506 ContextImpl::FsReqCleanup };
507 if (!access_req) {
508 TAG_LOGE(AAFwkTag::ABILITY_SIM, "request heap memory failed");
509 return false;
510 }
511
512 return (uv_fs_access(nullptr, access_req.get(), path.c_str(), 0, nullptr) == 0);
513 }
514
Mkdir(const std::string & path)515 void ContextImpl::Mkdir(const std::string &path)
516 {
517 TAG_LOGD(AAFwkTag::ABILITY_SIM, "Mkdir: dir: %{public}s", path.c_str());
518 std::unique_ptr<uv_fs_t, decltype(ContextImpl::FsReqCleanup) *> mkdir_req = { new uv_fs_t,
519 ContextImpl::FsReqCleanup };
520 if (!mkdir_req) {
521 TAG_LOGE(AAFwkTag::ABILITY_SIM, "request heap memory failed");
522 return;
523 }
524
525 int ret = uv_fs_mkdir(nullptr, mkdir_req.get(), path.c_str(), DIR_DEFAULT_PERM, nullptr);
526 if (ret < 0) {
527 TAG_LOGE(AAFwkTag::ABILITY_SIM, "create directory failed");
528 }
529 }
530
CreateMultiDir(const std::string & path)531 bool ContextImpl::CreateMultiDir(const std::string &path)
532 {
533 if (path.empty()) {
534 TAG_LOGD(AAFwkTag::ABILITY_SIM, "empty path");
535 return false;
536 }
537
538 if (Access(path)) {
539 TAG_LOGD(AAFwkTag::ABILITY_SIM, "path existed");
540 return true;
541 }
542
543 std::string tempStr = path;
544 tempStr += CONTEXT_FILE_SEPARATOR;
545
546 std::string::size_type pos = 0;
547 std::string::size_type prePos = 0;
548 std::string strFolderPath;
549
550 while ((pos = tempStr.find(CONTEXT_FILE_SEPARATOR, pos)) != std::string::npos) {
551 strFolderPath = tempStr.substr(0, pos);
552 if (Access(strFolderPath)) {
553 pos = pos + 1;
554 prePos = pos;
555 continue;
556 }
557
558 Mkdir(strFolderPath);
559 pos = pos + 1;
560 prePos = pos;
561 }
562
563 return Access(tempStr);
564 }
565 } // namespace AbilityRuntime
566 } // namespace OHOS
567