• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "installd/installd_host_impl.h"
17 
18 #include <cstdio>
19 #include <fstream>
20 #include <map>
21 #include <memory>
22 #include <sstream>
23 #include <string>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 
28 #include "app_log_wrapper.h"
29 #include "bundle_constants.h"
30 #include "common_profile.h"
31 #include "directory_ex.h"
32 #ifdef WITH_SELINUX
33 #include "hap_restorecon.h"
34 #endif // WITH_SELINUX
35 #include "installd/installd_operator.h"
36 #include "installd/installd_permission_mgr.h"
37 #include "parameters.h"
38 
39 namespace OHOS {
40 namespace AppExecFwk {
41 namespace {
42 const std::string ARK_CACHE_PATH = "/data/local/ark-cache/";
43 const std::string ARK_PROFILE_PATH = "/data/local/ark-profile/";
44 }
45 
InstalldHostImpl()46 InstalldHostImpl::InstalldHostImpl()
47 {
48     APP_LOGI("installd service instance is created");
49 }
50 
~InstalldHostImpl()51 InstalldHostImpl::~InstalldHostImpl()
52 {
53     APP_LOGI("installd service instance is destroyed");
54 }
55 
CreateBundleDir(const std::string & bundleDir)56 ErrCode InstalldHostImpl::CreateBundleDir(const std::string &bundleDir)
57 {
58     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
59         APP_LOGE("installd permission denied, only used for foundation process");
60         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
61     }
62     if (bundleDir.empty()) {
63         APP_LOGE("Calling the function CreateBundleDir with invalid param");
64         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
65     }
66     if (InstalldOperator::IsExistDir(bundleDir)) {
67         APP_LOGW("bundleDir %{public}s is exist", bundleDir.c_str());
68         OHOS::ForceRemoveDirectory(bundleDir);
69     }
70     if (!InstalldOperator::MkRecursiveDir(bundleDir, true)) {
71         APP_LOGE("create bundle dir %{public}s failed", bundleDir.c_str());
72         return ERR_APPEXECFWK_INSTALLD_CREATE_DIR_FAILED;
73     }
74     return ERR_OK;
75 }
76 
ExtractModuleFiles(const std::string & srcModulePath,const std::string & targetPath,const std::string & targetSoPath,const std::string & cpuAbi)77 ErrCode InstalldHostImpl::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath,
78     const std::string &targetSoPath, const std::string &cpuAbi)
79 {
80     APP_LOGD("ExtractModuleFiles extract original src %{public}s and target src %{public}s",
81         srcModulePath.c_str(), targetPath.c_str());
82     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
83         APP_LOGE("installd permission denied, only used for foundation process");
84         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
85     }
86     if (srcModulePath.empty() || targetPath.empty()) {
87         APP_LOGE("Calling the function ExtractModuleFiles with invalid param");
88         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
89     }
90     if (!InstalldOperator::MkRecursiveDir(targetPath, true)) {
91         APP_LOGE("create target dir %{private}s failed", targetPath.c_str());
92         return ERR_APPEXECFWK_INSTALLD_CREATE_DIR_FAILED;
93     }
94     if (!InstalldOperator::ExtractFiles(srcModulePath, targetPath, targetSoPath, cpuAbi)) {
95         APP_LOGE("extract %{private}s to %{private}s failed", srcModulePath.c_str(), targetPath.c_str());
96         InstalldOperator::DeleteDir(targetPath);
97         return ERR_APPEXECFWK_INSTALL_DISK_MEM_INSUFFICIENT;
98     }
99     return ERR_OK;
100 }
101 
ExtractFiles(const ExtractParam & extractParam)102 ErrCode InstalldHostImpl::ExtractFiles(const ExtractParam &extractParam)
103 {
104     APP_LOGD("ExtractFiles extractParam %{public}s", extractParam.ToString().c_str());
105     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
106         APP_LOGE("installd permission denied, only used for foundation process");
107         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
108     }
109 
110     if (extractParam.srcPath.empty() || extractParam.targetPath.empty()) {
111         APP_LOGE("Calling the function ExtractFiles with invalid param");
112         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
113     }
114 
115     if (!InstalldOperator::ExtractFiles(extractParam)) {
116         APP_LOGE("extract failed");
117         return ERR_APPEXECFWK_INSTALL_DISK_MEM_INSUFFICIENT;
118     }
119 
120     return ERR_OK;
121 }
122 
RenameModuleDir(const std::string & oldPath,const std::string & newPath)123 ErrCode InstalldHostImpl::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
124 {
125     APP_LOGD("rename %{private}s to %{private}s", oldPath.c_str(), newPath.c_str());
126     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
127         APP_LOGE("installd permission denied, only used for foundation process");
128         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
129     }
130     if (oldPath.empty() || newPath.empty()) {
131         APP_LOGE("Calling the function RenameModuleDir with invalid param");
132         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
133     }
134     if (!InstalldOperator::RenameDir(oldPath, newPath)) {
135         APP_LOGE("rename module dir %{private}s to %{private}s failed", oldPath.c_str(), newPath.c_str());
136         return ERR_APPEXECFWK_INSTALLD_RNAME_DIR_FAILED;
137     }
138     return ERR_OK;
139 }
140 
CreateBackupExtHomeDir(const std::string & bundleName,const int userid,const int uid)141 static void CreateBackupExtHomeDir(const std::string &bundleName, const int userid, const int uid)
142 {
143     // Setup BackupExtensionAbility's home directory in a harmless way
144     std::string bundleBackupDir = Constants::BUNDLE_BACKUP_HOME_PATH + bundleName;
145     bundleBackupDir = bundleBackupDir.replace(bundleBackupDir.find("%"), 1, std::to_string(userid));
146     if (!InstalldOperator::MkOwnerDir(bundleBackupDir, S_IRWXU | S_IRWXG | S_ISGID, uid, Constants::BACKU_HOME_GID)) {
147         static std::once_flag logOnce;
148         std::call_once(logOnce, []() {
149             APP_LOGW("CreateBundledatadir MkOwnerDir(backup's home dir) failed");
150         });
151     }
152 }
153 
CreateShareDir(const std::string & bundleName,const int userid,const int uid,const int gid)154 static void CreateShareDir(const std::string &bundleName, const int userid, const int uid, const int gid)
155 {
156     std::string bundleShareDir = Constants::SHARE_FILE_PATH + bundleName;
157     bundleShareDir = bundleShareDir.replace(bundleShareDir.find("%"), 1, std::to_string(userid));
158     if (!InstalldOperator::MkOwnerDir(bundleShareDir, S_IRWXU | S_IRWXG | S_ISGID, uid, gid)) {
159         static std::once_flag logOnce;
160         std::call_once(logOnce, []() {
161             APP_LOGW("CreateBundledatadir MkOwnerDir(share's home dir) failed");
162         });
163     }
164 }
165 
CreateBundleDataDir(const std::string & bundleName,const int userid,const int uid,const int gid,const std::string & apl)166 ErrCode InstalldHostImpl::CreateBundleDataDir(const std::string &bundleName,
167     const int userid, const int uid, const int gid, const std::string &apl)
168 {
169     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
170         APP_LOGE("installd permission denied, only used for foundation process");
171         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
172     }
173     if (bundleName.empty() || userid < 0 || uid < 0 || gid < 0) {
174         APP_LOGE("Calling the function CreateBundleDataDir with invalid param");
175         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
176     }
177     for (const auto &el : Constants::BUNDLE_EL) {
178         std::string bundleDataDir = GetBundleDataDir(el, userid) + Constants::BASE;
179         if (access(bundleDataDir.c_str(), F_OK) != 0) {
180             APP_LOGW("CreateBundleDataDir base directory does not existed.");
181             return ERR_OK;
182         }
183         bundleDataDir += bundleName;
184         if (!InstalldOperator::MkOwnerDir(bundleDataDir, S_IRWXU, uid, gid)) {
185             APP_LOGE("CreateBundledatadir MkOwnerDir failed");
186             return ERR_APPEXECFWK_INSTALLD_CREATE_DIR_FAILED;
187         }
188         if (el == Constants::BUNDLE_EL[1]) {
189             for (const auto &dir : Constants::BUNDLE_DATA_DIR) {
190                 if (!InstalldOperator::MkOwnerDir(bundleDataDir + dir, S_IRWXU, uid, gid)) {
191                     APP_LOGE("CreateBundledatadir MkOwnerDir el2 failed");
192                     return ERR_APPEXECFWK_INSTALLD_CREATE_DIR_FAILED;
193                 }
194             }
195         }
196         ErrCode ret = SetDirApl(bundleDataDir, bundleName, apl);
197         if (ret != ERR_OK) {
198             APP_LOGE("CreateBundleDataDir SetDirApl failed");
199             return ret;
200         }
201         std::string databaseDir = GetBundleDataDir(el, userid) + Constants::DATABASE + bundleName;
202         if (!InstalldOperator::MkOwnerDir(
203             databaseDir, S_IRWXU | S_IRWXG | S_ISGID, uid, Constants::DATABASE_DIR_GID)) {
204             APP_LOGE("CreateBundle databaseDir MkOwnerDir failed");
205             return ERR_APPEXECFWK_INSTALLD_CREATE_DIR_FAILED;
206         }
207         ret = SetDirApl(databaseDir, bundleName, apl);
208         if (ret != ERR_OK) {
209             APP_LOGE("CreateBundleDataDir SetDirApl failed");
210             return ret;
211         }
212     }
213     std::string distributedfile = Constants::DISTRIBUTED_FILE;
214     distributedfile = distributedfile.replace(distributedfile.find("%"), 1, std::to_string(userid));
215     if (!InstalldOperator::MkOwnerDir(distributedfile + bundleName,
216         S_IRWXU | S_IRWXG | S_ISGID, uid, Constants::DFS_GID)) {
217         APP_LOGE("Failed to mk dir for distributedfile");
218     }
219 
220     distributedfile = Constants::DISTRIBUTED_FILE_NON_ACCOUNT;
221     distributedfile = distributedfile.replace(distributedfile.find("%"), 1, std::to_string(userid));
222     if (!InstalldOperator::MkOwnerDir(distributedfile + bundleName,
223         S_IRWXU | S_IRWXG | S_ISGID, uid, Constants::DFS_GID)) {
224         APP_LOGE("Failed to mk dir for non account distributedfile");
225     }
226 
227     CreateBackupExtHomeDir(bundleName, userid, uid);
228     CreateShareDir(bundleName, userid, uid, gid);
229     return ERR_OK;
230 }
231 
RemoveShareDir(const std::string & bundleName,const int userid)232 static ErrCode RemoveShareDir(const std::string &bundleName, const int userid)
233 {
234     std::string shareFileDir = Constants::SHARE_FILE_PATH + bundleName;
235     shareFileDir = shareFileDir.replace(shareFileDir.find("%"), 1, std::to_string(userid));
236     if (!InstalldOperator::DeleteDir(shareFileDir)) {
237         APP_LOGE("remove dir %{public}s failed", shareFileDir.c_str());
238         return ERR_APPEXECFWK_INSTALLD_REMOVE_DIR_FAILED;
239     }
240     return ERR_OK;
241 }
242 
RemoveBundleDataDir(const std::string & bundleName,const int userid)243 ErrCode InstalldHostImpl::RemoveBundleDataDir(const std::string &bundleName, const int userid)
244 {
245     APP_LOGD("InstalldHostImpl::RemoveBundleDataDir bundleName:%{public}s", bundleName.c_str());
246     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
247         APP_LOGE("installd permission denied, only used for foundation process");
248         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
249     }
250     if (bundleName.empty() || userid < 0) {
251         APP_LOGE("Calling the function CreateBundleDataDir with invalid param");
252         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
253     }
254     for (const auto &el : Constants::BUNDLE_EL) {
255         std::string bundleDataDir = GetBundleDataDir(el, userid) + Constants::BASE + bundleName;
256         if (!InstalldOperator::DeleteDir(bundleDataDir)) {
257             APP_LOGE("remove dir %{public}s failed", bundleDataDir.c_str());
258             return ERR_APPEXECFWK_INSTALLD_REMOVE_DIR_FAILED;
259         }
260         std::string databaseDir = GetBundleDataDir(el, userid) + Constants::DATABASE + bundleName;
261         if (!InstalldOperator::DeleteDir(databaseDir)) {
262             APP_LOGE("remove dir %{public}s failed", databaseDir.c_str());
263             return ERR_APPEXECFWK_INSTALLD_REMOVE_DIR_FAILED;
264         }
265     }
266     if (RemoveShareDir(bundleName, userid) != ERR_OK) {
267         APP_LOGE("failed to remove share dir");
268         return ERR_APPEXECFWK_INSTALLD_REMOVE_DIR_FAILED;
269     }
270     return ERR_OK;
271 }
272 
RemoveModuleDataDir(const std::string & ModuleDir,const int userid)273 ErrCode InstalldHostImpl::RemoveModuleDataDir(const std::string &ModuleDir, const int userid)
274 {
275     APP_LOGD("InstalldHostImpl::RemoveModuleDataDir ModuleDir:%{public}s", ModuleDir.c_str());
276     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
277         APP_LOGE("installd permission denied, only used for foundation process");
278         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
279     }
280     if (ModuleDir.empty() || userid < 0) {
281         APP_LOGE("Calling the function CreateModuleDataDir with invalid param");
282         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
283     }
284 
285     for (const auto &el : Constants::BUNDLE_EL) {
286         std::string moduleDataDir = GetBundleDataDir(el, userid) + Constants::BASE + ModuleDir;
287         if (!InstalldOperator::DeleteDir(moduleDataDir)) {
288             APP_LOGE("remove dir %{public}s failed", moduleDataDir.c_str());
289         }
290     }
291     return ERR_OK;
292 }
293 
RemoveDir(const std::string & dir)294 ErrCode InstalldHostImpl::RemoveDir(const std::string &dir)
295 {
296     APP_LOGD("InstalldHostImpl::RemoveDir:%{public}s", dir.c_str());
297     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
298         APP_LOGE("installd permission denied, only used for foundation process");
299         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
300     }
301     if (dir.empty()) {
302         APP_LOGE("Calling the function RemoveDir with invalid param");
303         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
304     }
305     if (!InstalldOperator::DeleteDir(dir)) {
306         APP_LOGE("remove dir %{public}s failed", dir.c_str());
307         return ERR_APPEXECFWK_INSTALLD_REMOVE_DIR_FAILED;
308     }
309     return ERR_OK;
310 }
311 
CleanBundleDataDir(const std::string & dataDir)312 ErrCode InstalldHostImpl::CleanBundleDataDir(const std::string &dataDir)
313 {
314     APP_LOGD("InstalldHostImpl::CleanBundleDataDir start");
315     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
316         APP_LOGE("installd permission denied, only used for foundation process");
317         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
318     }
319     if (dataDir.empty()) {
320         APP_LOGE("Calling the function CleanBundleDataDir with invalid param");
321         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
322     }
323 
324     if (!InstalldOperator::DeleteFiles(dataDir)) {
325         APP_LOGE("CleanBundleDataDir delete files failed");
326         return ERR_APPEXECFWK_INSTALLD_CLEAN_DIR_FAILED;
327     }
328     return ERR_OK;
329 }
330 
GetBundleDataDir(const std::string & el,const int userid) const331 std::string InstalldHostImpl::GetBundleDataDir(const std::string &el, const int userid) const
332 {
333     std::string dataDir = Constants::BUNDLE_APP_DATA_BASE_DIR +
334                           el +
335                           Constants::PATH_SEPARATOR +
336                           std::to_string(userid);
337     return dataDir;
338 }
339 
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats)340 ErrCode InstalldHostImpl::GetBundleStats(
341     const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats)
342 {
343     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
344         APP_LOGE("installd permission denied, only used for foundation process");
345         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
346     }
347     if (bundleName.empty()) {
348         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
349     }
350     std::vector<std::string> bundlePath;
351     bundlePath.push_back(Constants::BUNDLE_CODE_DIR + Constants::PATH_SEPARATOR + bundleName); // bundle code
352     bundlePath.push_back(ARK_CACHE_PATH + bundleName); // ark cache file
353     // ark profile
354     bundlePath.push_back(ARK_PROFILE_PATH + std::to_string(userId) + Constants::PATH_SEPARATOR + bundleName);
355     int64_t fileSize = InstalldOperator::GetDiskUsageFromPath(bundlePath);
356     bundlePath.clear();
357     std::vector<std::string> cachePath;
358     int64_t allBundleLocalSize = 0;
359     for (const auto &el : Constants::BUNDLE_EL) {
360         std::string filePath = Constants::BUNDLE_APP_DATA_BASE_DIR + el + Constants::PATH_SEPARATOR +
361             std::to_string(userId) + Constants::BASE + bundleName;
362         allBundleLocalSize += InstalldOperator::GetDiskUsage(filePath);
363         if (el == Constants::BUNDLE_EL[1]) {
364             for (const auto &dataDir : Constants::BUNDLE_DATA_DIR) {
365                 bundlePath.push_back(filePath + dataDir);
366             }
367         } else {
368             bundlePath.push_back(filePath);
369         }
370         InstalldOperator::TraverseCacheDirectory(filePath, cachePath);
371     }
372     int64_t bundleLocalSize = InstalldOperator::GetDiskUsageFromPath(bundlePath);
373     int64_t systemFolderSize = allBundleLocalSize - bundleLocalSize;
374     // index 0 : bundle data size
375     bundleStats.push_back(fileSize + systemFolderSize);
376     int64_t cacheSize = InstalldOperator::GetDiskUsageFromPath(cachePath);
377     bundleLocalSize -= cacheSize;
378     // index 1 : local bundle data size
379     bundleStats.push_back(bundleLocalSize);
380 
381     // index 2 : distributed data size
382     std::string distributedfilePath = Constants::DISTRIBUTED_FILE;
383     distributedfilePath = distributedfilePath.replace(distributedfilePath.find("%"), 1, std::to_string(userId)) +
384         bundleName;
385     int64_t distributedFileSize = InstalldOperator::GetDiskUsage(distributedfilePath);
386     bundleStats.push_back(distributedFileSize);
387 
388     // index 3 : database size
389     std::vector<std::string> dataBasePath;
390     for (const auto &el : Constants::BUNDLE_EL) {
391         std::string filePath = Constants::BUNDLE_APP_DATA_BASE_DIR + el + Constants::PATH_SEPARATOR +
392             std::to_string(userId) + Constants::DATABASE + bundleName;
393         dataBasePath.push_back(filePath);
394     }
395     int64_t databaseFileSize = InstalldOperator::GetDiskUsageFromPath(dataBasePath);
396     bundleStats.push_back(databaseFileSize);
397 
398     // index 4 : cache size
399     bundleStats.push_back(cacheSize);
400     return ERR_OK;
401 }
402 
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl)403 ErrCode InstalldHostImpl::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl)
404 {
405 #ifdef WITH_SELINUX
406     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
407         APP_LOGE("installd permission denied, only used for foundation process");
408         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
409     }
410     if (dir.empty() || bundleName.empty()) {
411         APP_LOGE("Calling the function SetDirApl with invalid param");
412         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
413     }
414     std::string aplLevel = Profile::AVAILABLELEVEL_NORMAL;
415     if (!apl.empty()) {
416         aplLevel = apl;
417     }
418     HapContext hapContext;
419     int ret = hapContext.HapFileRestorecon(dir, aplLevel, bundleName, SELINUX_HAP_RESTORECON_RECURSE);
420     if (ret != 0) {
421         APP_LOGE("HapFileRestorecon path: %{private}s failed, ret:%{public}d", dir.c_str(), ret);
422     }
423     return ret;
424 #else
425     return ERR_OK;
426 #endif // WITH_SELINUX
427 }
428 
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)429 ErrCode InstalldHostImpl::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
430 {
431     APP_LOGD("InstalldHostImpl::GetBundleCachePath start");
432     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
433         APP_LOGE("installd permission denied, only used for foundation process");
434         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
435     }
436     if (dir.empty()) {
437         APP_LOGE("Calling the function GetBundleCachePath with invalid param");
438         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
439     }
440     InstalldOperator::TraverseCacheDirectory(dir, cachePath);
441     return ERR_OK;
442 }
443 
ScanDir(const std::string & dir,ScanMode scanMode,ResultMode resultMode,std::vector<std::string> & paths)444 ErrCode InstalldHostImpl::ScanDir(
445     const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
446 {
447     APP_LOGD("InstalldHostImpl::Scan start %{public}s", dir.c_str());
448     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
449         APP_LOGE("installd permission denied, only used for foundation process");
450         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
451     }
452     if (dir.empty()) {
453         APP_LOGE("Calling the function Scan with invalid param");
454         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
455     }
456 
457     InstalldOperator::ScanDir(dir, scanMode, resultMode, paths);
458     return ERR_OK;
459 }
460 
MoveFile(const std::string & oldPath,const std::string & newPath)461 ErrCode InstalldHostImpl::MoveFile(const std::string &oldPath, const std::string &newPath)
462 {
463     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
464         APP_LOGE("installd permission denied, only used for foundation process");
465         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
466     }
467     if (!InstalldOperator::RenameFile(oldPath, newPath)) {
468         APP_LOGE("Move file %{public}s to %{public}s failed",
469             oldPath.c_str(), newPath.c_str());
470         return ERR_APPEXECFWK_INSTALLD_MOVE_FILE_FAILED;
471     }
472     mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
473     if (!OHOS::ChangeModeFile(newPath, mode)) {
474         APP_LOGE("change mode failed");
475         return ERR_APPEXECFWK_INSTALLD_MOVE_FILE_FAILED;
476     }
477     return ERR_OK;
478 }
479 
CopyFile(const std::string & oldPath,const std::string & newPath)480 ErrCode InstalldHostImpl::CopyFile(const std::string &oldPath, const std::string &newPath)
481 {
482     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
483         APP_LOGE("installd permission denied, only used for foundation process");
484         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
485     }
486     if (!InstalldOperator::CopyFile(oldPath, newPath)) {
487         APP_LOGE("Copy file %{public}s to %{public}s failed",
488             oldPath.c_str(), newPath.c_str());
489         return ERR_APPEXECFWK_INSTALLD_COPY_FILE_FAILED;
490     }
491     mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
492     if (!OHOS::ChangeModeFile(newPath, mode)) {
493         APP_LOGE("change mode failed");
494         return ERR_APPEXECFWK_INSTALLD_COPY_FILE_FAILED;
495     }
496     return ERR_OK;
497 }
498 
Mkdir(const std::string & dir,const int32_t mode,const int32_t uid,const int32_t gid)499 ErrCode InstalldHostImpl::Mkdir(
500     const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid)
501 {
502     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
503         APP_LOGE("installd permission denied, only used for foundation process");
504         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
505     }
506     APP_LOGD("Mkdir start %{public}s", dir.c_str());
507     if (dir.empty()) {
508         APP_LOGE("Calling the function Mkdir with invalid param");
509         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
510     }
511 
512     if (!InstalldOperator::MkOwnerDir(dir, mode, uid, gid)) {
513         APP_LOGE("Mkdir %{public}s failed", dir.c_str());
514         return ERR_APPEXECFWK_INSTALLD_MKDIR_FAILED;
515     }
516 
517     return ERR_OK;
518 }
519 
GetFileStat(const std::string & file,FileStat & fileStat)520 ErrCode InstalldHostImpl::GetFileStat(const std::string &file, FileStat &fileStat)
521 {
522     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
523         APP_LOGE("installd permission denied, only used for foundation process");
524         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
525     }
526 
527     APP_LOGD("GetFileStat start %{public}s", file.c_str());
528     struct stat s;
529     if (stat(file.c_str(), &s) != 0) {
530         APP_LOGE("Stat file(%{public}s) failed", file.c_str());
531         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
532     }
533 
534     fileStat.uid = static_cast<int32_t>(s.st_uid);
535     fileStat.gid = static_cast<int32_t>(s.st_gid);
536     fileStat.lastModifyTime = static_cast<int64_t>(s.st_mtime);
537     fileStat.isDir = s.st_mode & S_IFDIR;
538     return ERR_OK;
539 }
540 
ExtractDiffFiles(const std::string & filePath,const std::string & targetPath,const std::string & cpuAbi)541 ErrCode InstalldHostImpl::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
542     const std::string &cpuAbi)
543 {
544     if (filePath.empty() || targetPath.empty()) {
545         APP_LOGE("Calling the function ExtractDiffFiles with invalid param");
546         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
547     }
548     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
549         APP_LOGE("installd permission denied, only used for foundation process");
550         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
551     }
552     if (!InstalldOperator::ExtractDiffFiles(filePath, targetPath, cpuAbi)) {
553         return ERR_BUNDLEMANAGER_QUICK_FIX_EXTRACT_DIFF_FILES_FAILED;
554     }
555     return ERR_OK;
556 }
557 
ApplyDiffPatch(const std::string & oldSoPath,const std::string & diffFilePath,const std::string & newSoPath)558 ErrCode InstalldHostImpl::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
559     const std::string &newSoPath)
560 {
561     if (oldSoPath.empty() || diffFilePath.empty() || newSoPath.empty()) {
562         APP_LOGE("Calling the function ExtractDiffFiles with invalid param");
563         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
564     }
565     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
566         APP_LOGE("installd permission denied, only used for foundation process");
567         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
568     }
569     if (!InstalldOperator::ApplyDiffPatch(oldSoPath, diffFilePath, newSoPath)) {
570         return ERR_BUNDLEMANAGER_QUICK_FIX_APPLY_DIFF_PATCH_FAILED;
571     }
572     return ERR_OK;
573 }
574 
IsExistDir(const std::string & dir,bool & isExist)575 ErrCode InstalldHostImpl::IsExistDir(const std::string &dir, bool &isExist)
576 {
577     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
578         APP_LOGE("installd permission denied, only used for foundation process");
579         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
580     }
581     isExist = InstalldOperator::IsExistDir(dir);
582     return ERR_OK;
583 }
584 
IsDirEmpty(const std::string & dir,bool & isDirEmpty)585 ErrCode InstalldHostImpl::IsDirEmpty(const std::string &dir, bool &isDirEmpty)
586 {
587     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
588         APP_LOGE("installd permission denied, only used for foundation process");
589         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
590     }
591     isDirEmpty = InstalldOperator::IsDirEmpty(dir);
592     return ERR_OK;
593 }
594 
ObtainQuickFixFileDir(const std::string & dir,std::vector<std::string> & dirVec)595 ErrCode InstalldHostImpl::ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec)
596 {
597     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
598         APP_LOGE("installd permission denied, only used for foundation process");
599         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
600     }
601     InstalldOperator::ObtainQuickFixFileDir(dir, dirVec);
602     return ERR_OK;
603 }
604 
CopyFiles(const std::string & sourceDir,const std::string & destinationDir)605 ErrCode InstalldHostImpl::CopyFiles(const std::string &sourceDir, const std::string &destinationDir)
606 {
607     if (!InstalldPermissionMgr::VerifyCallingPermission(Constants::FOUNDATION_UID)) {
608         APP_LOGE("installd permission denied, only used for foundation process");
609         return ERR_APPEXECFWK_INSTALLD_PERMISSION_DENIED;
610     }
611 
612     InstalldOperator::CopyFiles(sourceDir, destinationDir);
613     return ERR_OK;
614 }
615 }  // namespace AppExecFwk
616 }  // namespace OHOS
617