/* * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "installd_client.h" #include "bundle_constants.h" #include "installd_death_recipient.h" #include "system_ability_definition.h" #include "system_ability_helper.h" namespace OHOS { namespace AppExecFwk { ErrCode InstalldClient::CreateBundleDir(const std::string &bundleDir) { if (bundleDir.empty()) { APP_LOGE("bundle dir is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::CreateBundleDir, bundleDir); } ErrCode InstalldClient::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath, const std::string &targetSoPath, const std::string &cpuAbi) { if (srcModulePath.empty() || targetPath.empty()) { APP_LOGE("src module path or target path is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::ExtractModuleFiles, srcModulePath, targetPath, targetSoPath, cpuAbi); } ErrCode InstalldClient::ExtractFiles(const ExtractParam &extractParam) { if (extractParam.srcPath.empty() || extractParam.targetPath.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::ExtractFiles, extractParam); } ErrCode InstalldClient::ExtractHnpFiles(const std::string &hnpPackageInfo, const ExtractParam &extractParam) { if (extractParam.srcPath.empty() || extractParam.targetPath.empty() || hnpPackageInfo.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::ExtractHnpFiles, hnpPackageInfo, extractParam); } ErrCode InstalldClient::ProcessBundleInstallNative(const std::string &userId, const std::string &hnpRootPath, const std::string &hapPath, const std::string &cpuAbi, const std::string &packageName) { return CallService(&IInstalld::ProcessBundleInstallNative, userId, hnpRootPath, hapPath, cpuAbi, packageName); } ErrCode InstalldClient::ProcessBundleUnInstallNative(const std::string &userId, const std::string &packageName) { return CallService(&IInstalld::ProcessBundleUnInstallNative, userId, packageName); } ErrCode InstalldClient::ExecuteAOT(const AOTArgs &aotArgs, std::vector &pendSignData) { return CallService(&IInstalld::ExecuteAOT, aotArgs, pendSignData); } ErrCode InstalldClient::PendSignAOT(const std::string &anFileName, const std::vector &signData) { return CallService(&IInstalld::PendSignAOT, anFileName, signData); } ErrCode InstalldClient::StopAOT() { return CallService(&IInstalld::StopAOT); } ErrCode InstalldClient::DeleteUninstallTmpDirs(const std::vector &dirs) { return CallService(&IInstalld::DeleteUninstallTmpDirs, dirs); } ErrCode InstalldClient::RenameModuleDir(const std::string &oldPath, const std::string &newPath) { if (oldPath.empty() || newPath.empty()) { APP_LOGE("rename path is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::RenameModuleDir, oldPath, newPath); } ErrCode InstalldClient::CreateBundleDataDir(const CreateDirParam &createDirParam) { if (createDirParam.bundleName.empty() || createDirParam.userId < 0 || createDirParam.uid < 0 || createDirParam.gid < 0) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::CreateBundleDataDir, createDirParam); } ErrCode InstalldClient::CreateBundleDataDirWithVector(const std::vector &createDirParams) { return ERR_OK; } ErrCode InstalldClient::RemoveBundleDataDir( const std::string &bundleName, const int userId, bool isAtomicService, const bool async) { if (bundleName.empty() || userId < 0) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::RemoveBundleDataDir, bundleName, userId, isAtomicService, async); } ErrCode InstalldClient::RemoveModuleDataDir(const std::string &ModuleName, const int userid) { if (ModuleName.empty() || userid < 0) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::RemoveModuleDataDir, ModuleName, userid); } ErrCode InstalldClient::RemoveDir(const std::string &dir) { if (dir.empty()) { APP_LOGE("dir removed is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::RemoveDir, dir); } ErrCode InstalldClient::CleanBundleDataDir(const std::string &bundleDir) { if (bundleDir.empty()) { APP_LOGE("bundle dir is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::CleanBundleDataDir, bundleDir); } ErrCode InstalldClient::CleanBundleDataDirByName( const std::string &bundleName, const int userid, const int appIndex) { if (bundleName.empty() || userid < 0 || appIndex < 0 || appIndex > Constants::INITIAL_SANDBOX_APP_INDEX) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::CleanBundleDataDirByName, bundleName, userid, appIndex); } ErrCode InstalldClient::GetBundleStats(const std::string &bundleName, const int32_t userId, std::vector &bundleStats, const int32_t uid, const int32_t appIndex, const uint32_t statFlag, const std::vector &moduleNameList) { if (bundleName.empty()) { APP_LOGE("bundleName is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::GetBundleStats, bundleName, userId, bundleStats, uid, appIndex, statFlag, moduleNameList); } ErrCode InstalldClient::GetAllBundleStats(const int32_t userId, std::vector &bundleStats, const std::vector &uids) { if (uids.empty()) { APP_LOGE("uids is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::GetAllBundleStats, userId, bundleStats, uids); } ErrCode InstalldClient::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl, bool isPreInstallApp, bool debug) { if (dir.empty() || bundleName.empty() || apl.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::SetDirApl, dir, bundleName, apl, isPreInstallApp, debug); } ErrCode InstalldClient::GetBundleCachePath(const std::string &dir, std::vector &cachePath) { if (dir.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::GetBundleCachePath, dir, cachePath); } void InstalldClient::ResetInstalldProxy() { if ((installdProxy_ != nullptr) && (installdProxy_->AsObject() != nullptr)) { installdProxy_->AsObject()->RemoveDeathRecipient(recipient_); } installdProxy_ = nullptr; } sptr InstalldClient::GetInstalldProxy() { std::lock_guard lock(mutex_); if (installdProxy_ == nullptr) { sptr tempProxy = iface_cast(SystemAbilityHelper::GetSystemAbility(INSTALLD_SERVICE_ID)); if ((tempProxy == nullptr) || (tempProxy->AsObject() == nullptr)) { APP_LOGE("the installd proxy or remote object is null"); return nullptr; } recipient_ = new (std::nothrow) InstalldDeathRecipient(); if (recipient_ == nullptr) { APP_LOGE("the death recipient is nullptr"); return nullptr; } tempProxy->AsObject()->AddDeathRecipient(recipient_); installdProxy_ = tempProxy; } return installdProxy_; } ErrCode InstalldClient::ScanDir( const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector &paths) { if (dir.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::ScanDir, dir, scanMode, resultMode, paths); } ErrCode InstalldClient::MoveFile(const std::string &oldPath, const std::string &newPath) { if (oldPath.empty() || newPath.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::MoveFile, oldPath, newPath); } ErrCode InstalldClient::CopyFile(const std::string &oldPath, const std::string &newPath, const std::string &signatureFilePath) { if (oldPath.empty() || newPath.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::CopyFile, oldPath, newPath, signatureFilePath); } ErrCode InstalldClient::Mkdir( const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid) { if (dir.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::Mkdir, dir, mode, uid, gid); } ErrCode InstalldClient::GetFileStat(const std::string &file, FileStat &fileStat) { if (file.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::GetFileStat, file, fileStat); } ErrCode InstalldClient::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, const std::string &cpuAbi) { if (filePath.empty() || targetPath.empty() || cpuAbi.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::ExtractDiffFiles, filePath, targetPath, cpuAbi); } ErrCode InstalldClient::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, const std::string &newSoPath, int32_t uid) { if (oldSoPath.empty() || diffFilePath.empty() || newSoPath.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::ApplyDiffPatch, oldSoPath, diffFilePath, newSoPath, uid); } ErrCode InstalldClient::IsExistDir(const std::string &dir, bool &isExist) { return CallService(&IInstalld::IsExistDir, dir, isExist); } ErrCode InstalldClient::IsExistFile(const std::string &path, bool &isExist) { return CallService(&IInstalld::IsExistFile, path, isExist); } ErrCode InstalldClient::IsExistApFile(const std::string &path, bool &isExist) { return CallService(&IInstalld::IsExistApFile, path, isExist); } ErrCode InstalldClient::IsDirEmpty(const std::string &dir, bool &isDirEmpty) { return CallService(&IInstalld::IsDirEmpty, dir, isDirEmpty); } ErrCode InstalldClient::ObtainQuickFixFileDir(const std::string &dir, std::vector &dirVec) { return CallService(&IInstalld::ObtainQuickFixFileDir, dir, dirVec); } ErrCode InstalldClient::CopyFiles(const std::string &sourceDir, const std::string &destinationDir) { return CallService(&IInstalld::CopyFiles, sourceDir, destinationDir); } ErrCode InstalldClient::GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, std::vector &fileNames) { return CallService(&IInstalld::GetNativeLibraryFileNames, filePath, cpuAbi, fileNames); } ErrCode InstalldClient::VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam) { if (codeSignatureParam.modulePath.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::VerifyCodeSignature, codeSignatureParam); } ErrCode InstalldClient::CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption) { if (checkEncryptionParam.modulePath.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::CheckEncryption, checkEncryptionParam, isEncryption); } ErrCode InstalldClient::MoveFiles(const std::string &srcDir, const std::string &desDir) { if (srcDir.empty() || desDir.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::MoveFiles, srcDir, desDir); } ErrCode InstalldClient::ExtractDriverSoFiles(const std::string &srcPath, const std::unordered_multimap &dirMap) { if (srcPath.empty() || dirMap.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::ExtractDriverSoFiles, srcPath, dirMap); } ErrCode InstalldClient::ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath, const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid) { if (hapPath.empty() || tmpSoPath.empty() || cpuAbi.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::ExtractEncryptedSoFiles, hapPath, realSoFilesPath, cpuAbi, tmpSoPath, uid); } ErrCode InstalldClient::VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam) { if (codeSignatureParam.modulePath.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::VerifyCodeSignatureForHap, codeSignatureParam); } ErrCode InstalldClient::DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength, const unsigned char *profileBlock) { if (bundleName.empty() || profileBlock == nullptr) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::DeliverySignProfile, bundleName, profileBlockLength, profileBlock); } ErrCode InstalldClient::RemoveSignProfile(const std::string &bundleName) { if (bundleName.empty()) { return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::RemoveSignProfile, bundleName); } ErrCode InstalldClient::SetEncryptionPolicy(int32_t uid, const std::string &bundleName, const int32_t userId, std::string &keyId) { if (bundleName.empty()) { APP_LOGE("bundleName is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::SetEncryptionPolicy, uid, bundleName, userId, keyId); } ErrCode InstalldClient::RemoveExtensionDir(int32_t userId, const std::vector &extensionBundleDirs) { if (extensionBundleDirs.empty() || userId < 0) { APP_LOGE("extensionBundleDirs is empty or userId is invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::RemoveExtensionDir, userId, extensionBundleDirs); } ErrCode InstalldClient::IsExistExtensionDir(int32_t userId, const std::string &extensionBundleDir, bool &isExist) { if (extensionBundleDir.empty() || userId < 0) { APP_LOGE("extensionBundleDir is empty or userId is invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::IsExistExtensionDir, userId, extensionBundleDir, isExist); } ErrCode InstalldClient::CreateExtensionDataDir(const CreateDirParam &createDirParam) { if (createDirParam.bundleName.empty() || createDirParam.userId < 0 || createDirParam.uid < 0 || createDirParam.gid < 0 || createDirParam.extensionDirs.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::CreateExtensionDataDir, createDirParam); } ErrCode InstalldClient::DeleteEncryptionKeyId(const std::string &bundleName, const int32_t userId) { if (bundleName.empty()) { APP_LOGE("bundleName is empty"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::DeleteEncryptionKeyId, bundleName, userId); } bool InstalldClient::StartInstalldService() { return GetInstalldProxy() != nullptr; } ErrCode InstalldClient::GetExtensionSandboxTypeList(std::vector &typeList) { return CallService(&IInstalld::GetExtensionSandboxTypeList, typeList); } int64_t InstalldClient::GetDiskUsage(const std::string& dir, bool isRealPath) { return 0; } ErrCode InstalldClient::MoveHapToCodeDir(const std::string &originPath, const std::string &targetPath) { if (originPath.empty() || targetPath.empty()) { APP_LOGE("params are invalid"); return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR; } return CallService(&IInstalld::MoveHapToCodeDir, originPath, targetPath); } } // namespace AppExecFwk } // namespace OHOS