• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "user_unlocked_event_subscriber.h"
17 
18 #include <filesystem>
19 #include <sys/stat.h>
20 
21 #include "account_helper.h"
22 #include "app_log_tag_wrapper.h"
23 #include "bundle_mgr_service.h"
24 #if defined (BUNDLE_FRAMEWORK_SANDBOX_APP) && defined (DLP_PERMISSION_ENABLE)
25 #include "dlp_permission_kit.h"
26 #endif
27 #include "installd_client.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32 static constexpr int16_t MODE_BASE = 07777;
33 constexpr const char* BUNDLE_BACKUP_HOME_PATH_EL1_NEW = "/data/app/el1/%/base/";
34 constexpr const char* BUNDLE_BACKUP_HOME_PATH_EL2_NEW = "/data/app/el2/%/base/";
35 constexpr const char* BUNDLE_BACKUP_INNER_DIR = "/.backup";
36 const std::vector<std::string> BUNDLE_DATA_DIR = {
37     "/cache",
38     "/files",
39     "/temp",
40     "/preferences",
41     "/haps"
42 };
43 static std::mutex TASK_MUTEX;
44 static std::atomic<uint32_t> CURRENT_TASK_NUM = 0;
45 
46 template<typename Func, typename...Args>
ReturnIfNewTask(Func func,uint32_t tempTask,Args &&...args)47 inline void ReturnIfNewTask(Func func, uint32_t tempTask, Args&&... args)
48 {
49     if (CURRENT_TASK_NUM != tempTask) {
50         APP_LOGI("need stop current task, new first");
51         return;
52     }
53     func(std::forward<Args>(args)...);
54 }
55 }
UserUnlockedEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)56 UserUnlockedEventSubscriber::UserUnlockedEventSubscriber(
57     const EventFwk::CommonEventSubscribeInfo &subscribeInfo) : EventFwk::CommonEventSubscriber(subscribeInfo)
58 {}
59 
~UserUnlockedEventSubscriber()60 UserUnlockedEventSubscriber::~UserUnlockedEventSubscriber()
61 {}
62 
OnReceiveEvent(const EventFwk::CommonEventData & data)63 void UserUnlockedEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
64 {
65     std::string action = data.GetWant().GetAction();
66     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED) {
67         int32_t userId = data.GetCode();
68         APP_LOGI_NOFUNC("UserUnlockedEventSubscriber -u %{public}d unlocked", userId);
69         std::lock_guard<std::mutex> lock(mutex_);
70         if ((userId_ != userId)) {
71             userId_ = userId;
72             std::thread updateDataDirThread(UpdateAppDataMgr::UpdateAppDataDirSelinuxLabel, userId);
73             updateDataDirThread.detach();
74             std::thread(UpdateAppDataMgr::DeleteUninstallTmpDirs, std::set<int32_t>{userId}).detach();
75 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
76             DelayedSingleton<AppControlManager>::GetInstance()->SetAppInstallControlStatus();
77 #endif
78         }
79     }
80     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
81         int32_t userId = data.GetCode();
82         APP_LOGI_NOFUNC("UserUnlockedEventSubscriber -u %{public}d switched", userId);
83         std::lock_guard<std::mutex> lock(mutex_);
84         if (AccountHelper::IsOsAccountVerified(userId) && (userId_ != userId)) {
85             APP_LOGI_NOFUNC("UserUnlockedEventSubscriber -u %{public}d unlocked", userId);
86             userId_ = userId;
87             std::thread updateDataDirThread(UpdateAppDataMgr::UpdateAppDataDirSelinuxLabel, userId);
88             updateDataDirThread.detach();
89             std::thread(UpdateAppDataMgr::DeleteUninstallTmpDirs, std::set<int32_t>{userId}).detach();
90         }
91 #if defined (BUNDLE_FRAMEWORK_SANDBOX_APP) && defined (DLP_PERMISSION_ENABLE)
92         APP_LOGI("RemoveUnreservedSandbox call ClearUnreservedSandbox");
93         Security::DlpPermission::DlpPermissionKit::ClearUnreservedSandbox();
94 #endif
95 #ifdef BUNDLE_FRAMEWORK_APP_CONTROL
96         DelayedSingleton<AppControlManager>::GetInstance()->SetAppInstallControlStatus();
97 #endif
98     }
99 }
100 
CheckPathAttribute(const std::string & path,const BundleInfo & bundleInfo,bool & isExist)101 void UpdateAppDataMgr::CheckPathAttribute(const std::string &path, const BundleInfo &bundleInfo, bool &isExist)
102 {
103     if (!isExist) {
104         return;
105     }
106     FileStat fileStat;
107     if (InstalldClient::GetInstance()->GetFileStat(path, fileStat) != ERR_OK) {
108         APP_LOGE("GetFileStat path(%{public}s) failed", path.c_str());
109         return;
110     }
111     if (fileStat.uid != bundleInfo.uid) {
112         APP_LOGW("path: %{public}s uid is not same, fileStat.uid:%{public}d, bundleInfo.uid:%{public}d",
113             path.c_str(), static_cast<int32_t>(fileStat.uid), bundleInfo.uid);
114         isExist = false;
115     }
116     if (fileStat.gid != ServiceConstants::DATABASE_DIR_GID) {
117         APP_LOGW("path: %{public}s gid is not same, fileStat.gid:%{public}d, gid:%{public}d",
118             path.c_str(), static_cast<int32_t>(fileStat.gid), ServiceConstants::DATABASE_DIR_GID);
119         isExist = false;
120     }
121     uint32_t fileMode = static_cast<uint32_t>(fileStat.mode);
122     if ((fileMode & MODE_BASE) != (S_IRWXU | S_IRWXG | S_ISGID)) {
123         APP_LOGW("path: %{public}s mode is not same, fileStat.mode:%{public}d, mode:%{public}d",
124             path.c_str(), static_cast<int32_t>(fileStat.mode), static_cast<int32_t>((S_IRWXU | S_IRWXG | S_ISGID)));
125     }
126 }
127 
CreateBundleDataDir(const BundleInfo & bundleInfo,int32_t userId,const std::string & elDir)128 bool UpdateAppDataMgr::CreateBundleDataDir(
129     const BundleInfo &bundleInfo, int32_t userId, const std::string &elDir)
130 {
131     std::string baseBundleDataDir = ServiceConstants::BUNDLE_APP_DATA_BASE_DIR + elDir +
132         ServiceConstants::PATH_SEPARATOR + std::to_string(userId) + ServiceConstants::DATABASE + bundleInfo.name;
133     bool isExist = false;
134     if (InstalldClient::GetInstance()->IsExistDir(baseBundleDataDir, isExist) != ERR_OK) {
135         APP_LOGE("path: %{public}s IsExistDir failed", baseBundleDataDir.c_str());
136         return false;
137     }
138     CheckPathAttribute(baseBundleDataDir, bundleInfo, isExist);
139     if (!isExist) {
140         APP_LOGI_NOFUNC("path: %{public}s need CreateBundleDataDir", baseBundleDataDir.c_str());
141         CreateDirParam createDirParam;
142         createDirParam.userId = userId;
143         createDirParam.bundleName = bundleInfo.name;
144         createDirParam.uid = bundleInfo.uid;
145         createDirParam.gid = bundleInfo.gid;
146         createDirParam.apl = bundleInfo.applicationInfo.appPrivilegeLevel;
147         createDirParam.isPreInstallApp = bundleInfo.isPreInstallApp;
148         createDirParam.debug = bundleInfo.applicationInfo.appProvisionType == Constants::APP_PROVISION_TYPE_DEBUG;
149         if (elDir != ServiceConstants::BUNDLE_EL[0]) {
150             createDirParam.createDirFlag = CreateDirFlag::CREATE_DIR_UNLOCKED;
151         }
152         if (elDir == ServiceConstants::DIR_EL5) {
153             return CreateEl5Dir(createDirParam);
154         }
155         ProcessExtensionDir(bundleInfo, createDirParam.extensionDirs);
156         if (InstalldClient::GetInstance()->CreateBundleDataDir(createDirParam) != ERR_OK) {
157             APP_LOGW("failed to CreateBundleDataDir");
158         }
159     }
160     if (elDir == ServiceConstants::BUNDLE_EL[1]) {
161         CreateDataGroupDir(bundleInfo, userId);
162     }
163     return true;
164 }
165 
CreateEl5Dir(const CreateDirParam & createDirParam)166 bool UpdateAppDataMgr::CreateEl5Dir(const CreateDirParam &createDirParam)
167 {
168     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
169     if (dataMgr == nullptr) {
170         APP_LOGE("CreateEl5Dir failed for DataMgr is nullptr");
171         return false;
172     }
173     std::vector<CreateDirParam> params;
174     params.emplace_back(createDirParam);
175     InnerBundleInfo info;
176     if (dataMgr->FetchInnerBundleInfo(createDirParam.bundleName, info)) {
177         InnerBundleUserInfo userInfo;
178         if (info.GetInnerBundleUserInfo(createDirParam.userId, userInfo)) {
179             for (const auto &cloneInfo : userInfo.cloneInfos) {
180                 CreateDirParam cloneParam = createDirParam;
181                 cloneParam.uid = cloneInfo.second.uid;
182                 cloneParam.gid = cloneInfo.second.uid;
183                 cloneParam.appIndex = cloneInfo.second.appIndex;
184                 params.emplace_back(cloneParam);
185             }
186         }
187     }
188     dataMgr->CreateEl5Dir(params, true);
189     dataMgr->CreateAppEl5GroupDir(createDirParam.bundleName, createDirParam.userId);
190     return true;
191 }
192 
DeleteUninstallTmpDirs(const std::set<int32_t> & userIds)193 void UpdateAppDataMgr::DeleteUninstallTmpDirs(const std::set<int32_t>& userIds)
194 {
195     std::vector<std::string> dirs;
196     for (const int32_t &userId : userIds) {
197         std::vector<std::string> tmpDirs = GetBundleDataDirs(userId);
198         dirs.insert(dirs.end(), tmpDirs.begin(), tmpDirs.end());
199     }
200     dirs.emplace_back(Constants::BUNDLE_CODE_DIR);
201     ErrCode ret = InstalldClient::GetInstance()->DeleteUninstallTmpDirs(dirs);
202     if (ret != ERR_OK) {
203         LOG_W(BMS_TAG_DEFAULT, "delete tmp dirs failed:%{public}d", ret);
204     }
205 }
206 
GetBundleDataDirs(const int32_t userId)207 std::vector<std::string> UpdateAppDataMgr::GetBundleDataDirs(const int32_t userId)
208 {
209     std::vector<std::string> dirs;
210     std::vector<std::string> dataVector = { "base", "database" };
211     for (const std::string &el : ServiceConstants::BUNDLE_EL) {
212         std::filesystem::path userPath =
213             std::filesystem::path(ServiceConstants::BUNDLE_APP_DATA_BASE_DIR) / el / std::to_string(userId);
214         for (const std::string &data : dataVector) {
215             std::filesystem::path dataPath = userPath / data;
216             dirs.emplace_back(dataPath.string());
217         }
218     }
219     return dirs;
220 }
221 
CreateDataGroupDir(const BundleInfo & bundleInfo,int32_t userId)222 void UpdateAppDataMgr::CreateDataGroupDir(const BundleInfo &bundleInfo, int32_t userId)
223 {
224     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
225     if (dataMgr == nullptr) {
226         APP_LOGE("CreateDataGroupDir failed for DataMgr is nullptr");
227         return;
228     }
229     if (!dataMgr->CreateAppGroupDir(bundleInfo.name, userId)) {
230         APP_LOGE("CreateAppGroupDir %{public}s in %{public}d failed", bundleInfo.name.c_str(), userId);
231         return;
232     }
233 }
234 
UpdateAppDataDirSelinuxLabel(int32_t userId)235 void UpdateAppDataMgr::UpdateAppDataDirSelinuxLabel(int32_t userId)
236 {
237     uint32_t tempTaskNum = CURRENT_TASK_NUM.fetch_add(1) + 1;
238     std::lock_guard<std::mutex> guard(TASK_MUTEX);
239     APP_LOGI("UpdateAppDataDirSelinuxLabel hold task_mutex_");
240     if (tempTaskNum != CURRENT_TASK_NUM) {
241         APP_LOGI("need stop current task, new first, -u %{public}d", userId);
242         return;
243     }
244     APP_LOGI("UpdateAppDataDirSelinuxLabel userId:%{public}d start", userId);
245     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
246     if (dataMgr == nullptr) {
247         APP_LOGE("UpdateAppDataDirSelinuxLabel DataMgr is nullptr");
248         return;
249     }
250     std::vector<BundleInfo> bundleInfos;
251     if (!dataMgr->GetBundleInfos(BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO |
252         BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION, bundleInfos, userId)) {
253         APP_LOGE("UpdateAppDataDirSelinuxLabel GetAllBundleInfos failed");
254         return;
255     }
256 
257     ReturnIfNewTask(ProcessUpdateAppDataDir, tempTaskNum, userId, bundleInfos, ServiceConstants::BUNDLE_EL[1]);
258 #ifdef CHECK_ELDIR_ENABLED
259     ReturnIfNewTask(ProcessUpdateAppDataDir, tempTaskNum, userId, bundleInfos, ServiceConstants::DIR_EL3);
260     ReturnIfNewTask(ProcessUpdateAppDataDir, tempTaskNum, userId, bundleInfos, ServiceConstants::DIR_EL4);
261 #endif
262     ReturnIfNewTask(ProcessUpdateAppDataDir, tempTaskNum, userId, bundleInfos, ServiceConstants::DIR_EL5);
263     ReturnIfNewTask(ProcessUpdateAppLogDir, tempTaskNum, bundleInfos, userId);
264     ReturnIfNewTask(ProcessFileManagerDir, tempTaskNum, bundleInfos, userId);
265     ReturnIfNewTask(ProcessNewBackupDir, tempTaskNum, bundleInfos, userId);
266     ReturnIfNewTask(CreateSharefilesSubDataDirs, tempTaskNum, bundleInfos, userId);
267     APP_LOGI("UpdateAppDataDirSelinuxLabel userId:%{public}d end", userId);
268 }
269 
ProcessUpdateAppDataDir(int32_t userId,const std::vector<BundleInfo> & bundleInfos,const std::string & elDir)270 void UpdateAppDataMgr::ProcessUpdateAppDataDir(
271     int32_t userId, const std::vector<BundleInfo> &bundleInfos, const std::string &elDir)
272 {
273     std::string baseBundleDataDir = ServiceConstants::BUNDLE_APP_DATA_BASE_DIR + elDir +
274         ServiceConstants::PATH_SEPARATOR + std::to_string(userId);
275     for (const auto &bundleInfo : bundleInfos) {
276         if (bundleInfo.appIndex > 0) {
277             APP_LOGI("bundleName:%{public}s appIndex:%{public}d clone app no need to change",
278                 bundleInfo.name.c_str(), bundleInfo.appIndex);
279             continue;
280         }
281         if (elDir == ServiceConstants::DIR_EL5) {
282             std::vector<std::string> reqPermissions = bundleInfo.reqPermissions;
283             auto it = std::find_if(reqPermissions.begin(), reqPermissions.end(), [](const std::string &permission) {
284                 return permission == ServiceConstants::PERMISSION_PROTECT_SCREEN_LOCK_DATA;
285             });
286             if (it == reqPermissions.end()) {
287                 continue;
288             }
289         }
290         if ((userId != Constants::DEFAULT_USERID && bundleInfo.singleton) ||
291             (userId != Constants::U1 && CheckU1EnableProcess(bundleInfo))) {
292             continue;
293         }
294         if (!CreateBundleDataDir(bundleInfo, userId, elDir)) {
295             continue;
296         }
297         std::string baseDir = baseBundleDataDir + ServiceConstants::BASE + bundleInfo.name;
298         if (InstalldClient::GetInstance()->SetDirApl(baseDir, bundleInfo.name,
299             bundleInfo.applicationInfo.appPrivilegeLevel, bundleInfo.isPreInstallApp,
300             bundleInfo.applicationInfo.appProvisionType == Constants::APP_PROVISION_TYPE_DEBUG,
301             bundleInfo.uid) != ERR_OK) {
302             APP_LOGW_NOFUNC("failed to SetDirApl baseDir dir");
303             continue;
304         }
305         std::string baseDataDir = baseBundleDataDir + ServiceConstants::DATABASE + bundleInfo.name;
306         if (InstalldClient::GetInstance()->SetDirApl(baseDataDir, bundleInfo.name,
307             bundleInfo.applicationInfo.appPrivilegeLevel, bundleInfo.isPreInstallApp,
308             bundleInfo.applicationInfo.appProvisionType == Constants::APP_PROVISION_TYPE_DEBUG,
309             bundleInfo.uid) != ERR_OK) {
310             APP_LOGW_NOFUNC("failed to SetDirApl baseDataDir dir");
311         }
312     }
313 }
314 
ProcessExtensionDir(const BundleInfo & bundleInfo,std::vector<std::string> & dirs)315 void UpdateAppDataMgr::ProcessExtensionDir(const BundleInfo &bundleInfo, std::vector<std::string> &dirs)
316 {
317     for (const ExtensionAbilityInfo &info : bundleInfo.extensionInfos) {
318         if (!info.needCreateSandbox) {
319             continue;
320         }
321         std::string extensionDir = ServiceConstants::EXTENSION_DIR + info.moduleName +
322             ServiceConstants::FILE_SEPARATOR_LINE + info.name +
323             ServiceConstants::FILE_SEPARATOR_PLUS + info.bundleName;
324         dirs.emplace_back(extensionDir);
325     }
326 }
327 
ProcessUpdateAppLogDir(const std::vector<BundleInfo> & bundleInfos,int32_t userId)328 void UpdateAppDataMgr::ProcessUpdateAppLogDir(const std::vector<BundleInfo> &bundleInfos, int32_t userId)
329 {
330     for (const auto &bundleInfo : bundleInfos) {
331         if (userId != Constants::DEFAULT_USERID && bundleInfo.singleton) {
332             continue;
333         }
334         if (userId != Constants::U1 && CheckU1EnableProcess(bundleInfo)) {
335             continue;
336         }
337         if (!CreateBundleLogDir(bundleInfo, userId)) {
338             APP_LOGD("log dir create failed or already exists");
339         }
340     }
341 }
342 
ProcessNewBackupDir(const std::vector<BundleInfo> & bundleInfos,int32_t userId)343 void UpdateAppDataMgr::ProcessNewBackupDir(const std::vector<BundleInfo> &bundleInfos, int32_t userId)
344 {
345     APP_LOGI_NOFUNC("process new back up dir, start");
346     for (const auto &bundleInfo : bundleInfos) {
347         if (bundleInfo.appIndex > 0) {
348             APP_LOGI("bundleName:%{public}s appIndex %{public}d clone app no need to create",
349                 bundleInfo.name.c_str(), bundleInfo.appIndex);
350             continue;
351         }
352         if (bundleInfo.singleton) {
353             CreateNewBackupDir(bundleInfo, Constants::DEFAULT_USERID);
354             continue;
355         }
356         if (userId != Constants::DEFAULT_USERID) {
357             CreateNewBackupDir(bundleInfo, userId);
358         }
359     }
360     APP_LOGI_NOFUNC("process new back up dir, end");
361 }
362 
CreateNewBackupDir(const BundleInfo & bundleInfo,int32_t userId)363 void UpdateAppDataMgr::CreateNewBackupDir(const BundleInfo &bundleInfo, int32_t userId)
364 {
365     std::string parentEl1Dir = BUNDLE_BACKUP_HOME_PATH_EL1_NEW;
366     parentEl1Dir = parentEl1Dir.replace(parentEl1Dir.find("%"), 1, std::to_string(userId)) + bundleInfo.name;
367     std::string parentEl2Dir = BUNDLE_BACKUP_HOME_PATH_EL2_NEW;
368     parentEl2Dir = parentEl2Dir.replace(parentEl2Dir.find("%"), 1, std::to_string(userId)) + bundleInfo.name;
369     bool isEl1Existed = false;
370     auto result = InstalldClient::GetInstance()->IsExistDir(parentEl1Dir, isEl1Existed);
371     if (result == ERR_OK && !isEl1Existed) {
372         APP_LOGE("parent dir(%{public}s) missing: backup", parentEl1Dir.c_str());
373         return;
374     }
375     bool isEl2Existed = false;
376     result = InstalldClient::GetInstance()->IsExistDir(parentEl2Dir, isEl2Existed);
377     if (result == ERR_OK && !isEl2Existed) {
378         APP_LOGE("parent dir(%{public}s) missing: backup", parentEl2Dir.c_str());
379         return;
380     }
381     std::string backupDirEl1 = parentEl1Dir + BUNDLE_BACKUP_INNER_DIR;
382     std::string backupDirEl2 = parentEl2Dir + BUNDLE_BACKUP_INNER_DIR;
383     std::vector<std::string> backupDirList;
384     backupDirList.emplace_back(backupDirEl1);
385     backupDirList.emplace_back(backupDirEl2);
386 
387     for (const std::string &dir : backupDirList) {
388         bool isDirExisted = false;
389         auto result = InstalldClient::GetInstance()->IsExistDir(dir, isDirExisted);
390         if (result != ERR_OK || isDirExisted) {
391             continue;
392         }
393         APP_LOGI("bundle %{public}s not exist backup dir", bundleInfo.name.c_str());
394         result = InstalldClient::GetInstance()->Mkdir(dir, S_IRWXU | S_IRWXG | S_ISGID,
395             bundleInfo.uid, ServiceConstants::BACKU_HOME_GID);
396         if (result != ERR_OK) {
397             APP_LOGW("bundle %{public}s create backup dir for user %{public}d failed",
398                 bundleInfo.name.c_str(), userId);
399         }
400     }
401 }
402 
CreateBundleLogDir(const BundleInfo & bundleInfo,int32_t userId)403 bool UpdateAppDataMgr::CreateBundleLogDir(const BundleInfo &bundleInfo, int32_t userId)
404 {
405     std::string parentDir = ServiceConstants::BUNDLE_APP_DATA_BASE_DIR + ServiceConstants::BUNDLE_EL[1] +
406         ServiceConstants::PATH_SEPARATOR + std::to_string(userId) + ServiceConstants::LOG;
407     if (!BundleUtil::IsExistDir(parentDir)) {
408         APP_LOGE("parent dir(%{public}s) missing: log", parentDir.c_str());
409         return false;
410     }
411     std::string bundleLogDir = parentDir + bundleInfo.name;
412     bool isExist = false;
413     if (InstalldClient::GetInstance()->IsExistDir(bundleLogDir, isExist) != ERR_OK) {
414         APP_LOGE("path: %{public}s IsExistDir failed", bundleLogDir.c_str());
415         return false;
416     }
417     if (isExist) {
418         APP_LOGD("path: %{public}s is exist", bundleLogDir.c_str());
419         return false;
420     }
421     if (InstalldClient::GetInstance()->Mkdir(
422         bundleLogDir, S_IRWXU | S_IRWXG, bundleInfo.uid, ServiceConstants::LOG_DIR_GID) != ERR_OK) {
423         APP_LOGE("CreateBundleLogDir failed");
424         return false;
425     }
426     return true;
427 }
428 
ProcessFileManagerDir(const std::vector<BundleInfo> & bundleInfos,int32_t userId)429 void UpdateAppDataMgr::ProcessFileManagerDir(const std::vector<BundleInfo> &bundleInfos, int32_t userId)
430 {
431     for (const auto &bundleInfo : bundleInfos) {
432         if (userId != Constants::DEFAULT_USERID && bundleInfo.singleton) {
433             continue;
434         }
435         CreateBundleCloudDir(bundleInfo, userId);
436     }
437 }
438 
CreateBundleCloudDir(const BundleInfo & bundleInfo,int32_t userId)439 bool UpdateAppDataMgr::CreateBundleCloudDir(const BundleInfo &bundleInfo, int32_t userId)
440 {
441     std::string parentDir = "/data/service/el2/%/hmdfs/cloud/data/";
442     parentDir = parentDir.replace(parentDir.find("%"), 1, std::to_string(userId));
443     if (!BundleUtil::IsExistDir(parentDir)) {
444         APP_LOGE("parent dir(%{public}s) missing: cloud", parentDir.c_str());
445         return false;
446     }
447     std::string bundleCloudDir = parentDir + bundleInfo.name;
448     bool isExist = false;
449     if (InstalldClient::GetInstance()->IsExistDir(bundleCloudDir, isExist) != ERR_OK) {
450         APP_LOGE("path: %{private}s IsExistDir failed", bundleCloudDir.c_str());
451         return false;
452     }
453     if (isExist) {
454         APP_LOGD("path: %{private}s is exist", bundleCloudDir.c_str());
455         return false;
456     }
457     if (InstalldClient::GetInstance()->Mkdir(bundleCloudDir, S_IRWXU | S_IRWXG | S_ISGID,
458         bundleInfo.uid, ServiceConstants::DFS_GID) != ERR_OK) {
459         APP_LOGW("CreateCloudDir failed for bundle %{private}s errno:%{public}d",
460             bundleInfo.name.c_str(), errno);
461     }
462     return true;
463 }
464 
CreateSharefilesSubDataDirs(const std::vector<BundleInfo> & bundleInfos,int32_t userId)465 void UpdateAppDataMgr::CreateSharefilesSubDataDirs(const std::vector<BundleInfo> &bundleInfos, int32_t userId)
466 {
467     APP_LOGD("begin for userid: [%{public}d]", userId);
468     std::string parentDir = ServiceConstants::BUNDLE_APP_DATA_BASE_DIR + ServiceConstants::BUNDLE_EL[1] +
469         ServiceConstants::PATH_SEPARATOR + std::to_string(userId) + ServiceConstants::SHAREFILES;
470     for (const auto &bundleInfo : bundleInfos) {
471         std::string sharefilesDataDir = parentDir + bundleInfo.name;
472         bool isExist = false;
473         if (InstalldClient::GetInstance()->IsExistDir(sharefilesDataDir, isExist) != ERR_OK) {
474             APP_LOGW("path: %{public}s IsExistDir failed",
475                 sharefilesDataDir.c_str());
476             continue;
477         }
478         if (InstalldClient::GetInstance()->Mkdir(sharefilesDataDir,
479             S_IRWXU, bundleInfo.uid, bundleInfo.gid) != ERR_OK) {
480             APP_LOGW("MkOwnerDir %{public}s failed: %{public}d",
481                 sharefilesDataDir.c_str(), errno);
482             continue;
483         }
484         for (const auto &dir : BUNDLE_DATA_DIR) {
485             std::string childBundleDataDir = sharefilesDataDir + dir;
486             if (InstalldClient::GetInstance()->Mkdir(childBundleDataDir,
487                 S_IRWXU, bundleInfo.uid, bundleInfo.gid) != ERR_OK) {
488                 APP_LOGW("MkOwnerDir [%{public}s] failed: %{public}d",
489                     childBundleDataDir.c_str(), errno);
490             }
491         }
492         if (InstalldClient::GetInstance()->SetDirApl(sharefilesDataDir, bundleInfo.name,
493             bundleInfo.applicationInfo.appPrivilegeLevel, bundleInfo.isPreInstallApp,
494             bundleInfo.applicationInfo.appProvisionType == Constants::APP_PROVISION_TYPE_DEBUG,
495             bundleInfo.uid) != ERR_OK) {
496             APP_LOGW("SetDirApl failed: %{public}s", sharefilesDataDir.c_str());
497             continue;
498         }
499         APP_LOGD("succeed for %{public}s", bundleInfo.name.c_str());
500     }
501     APP_LOGD("end for userid: [%{public}d]", userId);
502 }
503 
CheckU1EnableProcess(const BundleInfo & bundleInfo)504 bool UpdateAppDataMgr::CheckU1EnableProcess(const BundleInfo &bundleInfo)
505 {
506     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
507     if (dataMgr == nullptr) {
508         APP_LOGE("DataMgr is nullptr");
509         return false;
510     }
511     bool singleton = bundleInfo.singleton;
512     bool u1Enable = false;
513     InnerBundleInfo info;
514     dataMgr->FetchInnerBundleInfo(bundleInfo.name, info);
515     u1Enable = info.IsU1Enable();
516     if (u1Enable && !singleton) {
517         return true;
518     }
519     return false;
520 }
521 }  // namespace AppExecFwk
522 }  // namespace OHOS