1 /* 2 * Copyright (c) 2021-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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H 18 19 #include <condition_variable> 20 #include <memory> 21 #include <mutex> 22 #include <shared_mutex> 23 #include <string> 24 25 #include "nocopyable.h" 26 #include "singleton.h" 27 28 #include "app_log_wrapper.h" 29 #include "appexecfwk_errors.h" 30 #include "bundle_constants.h" 31 #include "ipc/installd_interface.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 class InstalldClient : public DelayedSingleton<InstalldClient> { 36 public: 37 /** 38 * @brief Create a bundle code directory through an installd proxy object. 39 * @param bundleDir Indicates the bundle code directory path that to be created. 40 * @return Returns ERR_OK if the bundle directory created successfully; returns error code otherwise. 41 */ 42 ErrCode CreateBundleDir(const std::string &bundleDir); 43 /** 44 * @brief Extract the files of a HAP module to the code directory. 45 * @param srcModulePath Indicates the HAP file path. 46 * @param targetPath normal files decompression path. 47 * @param targetSoPath so files decompression path. 48 * @param cpuAbi cpuAbi. 49 * @return Returns ERR_OK if the HAP file extracted successfully; returns error code otherwise. 50 */ 51 ErrCode ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath, 52 const std::string &targetSoPath, const std::string &cpuAbi); 53 /** 54 * @brief Rename the module directory from temporaily path to the real path. 55 * @param oldPath Indicates the old path name. 56 * @param newPath Indicates the new path name. 57 * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise. 58 */ 59 ErrCode RenameModuleDir(const std::string &oldPath, const std::string &newPath); 60 /** 61 * @brief Create a bundle data directory. 62 * @param createDirParam Indicates param to be set to the directory. 63 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 64 */ 65 ErrCode CreateBundleDataDir(const CreateDirParam &createDirParam); 66 67 ErrCode CreateBundleDataDirWithVector(const std::vector<CreateDirParam> &createDirParams); 68 69 /** 70 * @brief Remove a bundle data directory. 71 * @param bundleName Indicates the bundleName data directory path that to be created. 72 * @param userid Indicates userid to be set to the directory. 73 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 74 */ 75 ErrCode RemoveBundleDataDir(const std::string &bundleName, const int32_t userId, 76 bool isAtomicService = false, const bool async = false); 77 /** 78 * @brief Remove a module data directory. 79 * @param ModuleDir Indicates the module data directory path that to be created. 80 * @param userid Indicates userid to be set to the directory. 81 * @return Returns ERR_OK if the data directories created successfully; returns error code otherwise. 82 */ 83 ErrCode RemoveModuleDataDir(const std::string &ModuleDir, const int userid); 84 /** 85 * @brief Remove a directory. 86 * @param dir Indicates the directory path that to be removed. 87 * @return Returns ERR_OK if the directory removed successfully; returns error code otherwise. 88 */ 89 ErrCode RemoveDir(const std::string &dir); 90 /** 91 * @brief Get disk usage for dir. 92 * @param dir Indicates the directory. 93 * @param isRealPath Indicates isRealPath. 94 * @param statSize Indicates size of dir. 95 * @return Returns true if successfully; returns false otherwise. 96 */ 97 ErrCode GetDiskUsage(const std::string &dir, int64_t &statSize, bool isRealPath = false); 98 /** 99 * @brief Get disk usage for dir. 100 * @param path Indicates the directory vector. 101 * @param statSize Indicates size of path. 102 * @return Returns true if successfully; returns false otherwise. 103 */ 104 ErrCode GetDiskUsageFromPath(const std::vector<std::string> &path, int64_t &statSize); 105 /** 106 * @brief Clean all files in a bundle data directory. 107 * @param bundleDir Indicates the data directory path that to be cleaned. 108 * @return Returns ERR_OK if the data directory cleaned successfully; returns error code otherwise. 109 */ 110 ErrCode CleanBundleDataDir(const std::string &bundleDir); 111 /** 112 * @brief Clean a bundle data directory. 113 * @param bundleName Indicates the bundleName data directory path that to be cleaned. 114 * @param userid Indicates userid to be set to the directory. 115 * @param appIndex Indicates app index to be set to the directory. 116 * @return Returns ERR_OK if the bundle data directory cleaned successfully; returns error code otherwise. 117 */ 118 ErrCode CleanBundleDataDirByName(const std::string &bundleName, const int userid, const int appIndex = 0); 119 /** 120 * @brief Get bundle Stats. 121 * @param bundleName Indicates the bundle name. 122 * @param userId Indicates the user Id. 123 * @param bundleStats Indicates the bundle Stats. 124 * @return Returns ERR_OK if get stats successfully; returns error code otherwise. 125 */ 126 ErrCode GetBundleStats(const std::string &bundleName, const int32_t userId, 127 std::vector<int64_t> &bundleStats, const int32_t uid = Constants::INVALID_UID, 128 const int32_t appIndex = 0, const uint32_t statFlag = 0, 129 const std::vector<std::string> &moduleNameList = {}); 130 131 ErrCode GetAllBundleStats(const int32_t userId, 132 std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids); 133 134 ErrCode MigrateData(const std::vector<std::string> &sourcePaths, const std::string &destinationPath); 135 136 /** 137 * @brief Reset the installd proxy object when installd service died. 138 * @return 139 */ 140 void ResetInstalldProxy(); 141 142 /** 143 * @brief Set dir apl. 144 * @param dir Indicates the data dir. 145 * @param bundleName Indicates the bundle name. 146 * @param apl Indicates the apl type. 147 * @param isPreInstallApp Indicates the bundle install type. 148 * @param debug Indicates the bundle debug mode. 149 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 150 */ 151 ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl, 152 bool isPreInstallApp, bool debug); 153 154 /** 155 * @brief Get all cache file path. 156 * @param dir Indicates the data dir. 157 * @param cachesPath Indicates the cache file path. 158 * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise. 159 */ 160 ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath); 161 162 ErrCode ScanDir( 163 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 164 165 ErrCode MoveFile(const std::string &oldPath, const std::string &newPath); 166 167 ErrCode CopyFile(const std::string &oldPath, const std::string &newPath, 168 const std::string &signatureFilePath = ""); 169 170 ErrCode Mkdir( 171 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid); 172 173 ErrCode GetFileStat(const std::string &file, FileStat &fileStat); 174 175 ErrCode ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 176 const std::string &cpuAbi); 177 178 ErrCode ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 179 const std::string &newSoPath, int32_t uid = Constants::INVALID_UID); 180 181 ErrCode IsExistDir(const std::string &dir, bool &isExist); 182 183 ErrCode IsExistFile(const std::string &path, bool &isExist); 184 185 ErrCode IsExistApFile(const std::string &path, bool &isExist); 186 187 ErrCode IsDirEmpty(const std::string &dir, bool &isDirEmpty); 188 189 ErrCode ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec); 190 191 ErrCode CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 192 193 ErrCode ExtractFiles(const ExtractParam &extractParam); 194 195 ErrCode ExtractHnpFiles(const std::string &hnpPackageInfo, const ExtractParam &extractParam); 196 197 ErrCode ProcessBundleInstallNative(const std::string &userId, const std::string &hnpRootPath, 198 const std::string &hapPath, const std::string &cpuAbi, const std::string &packageName); 199 200 ErrCode ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName); 201 202 ErrCode GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 203 std::vector<std::string> &fileNames); 204 205 ErrCode ExecuteAOT(const AOTArgs &aotArgs, std::vector<uint8_t> &pendSignData); 206 207 ErrCode PendSignAOT(const std::string &anFileName, const std::vector<uint8_t> &signData); 208 209 ErrCode StopAOT(); 210 211 ErrCode DeleteUninstallTmpDirs(const std::vector<std::string> &dirs); 212 213 ErrCode VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam); 214 215 ErrCode VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam); 216 217 ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 218 219 ErrCode MoveFiles(const std::string &srcDir, const std::string &desDir); 220 221 ErrCode ExtractDriverSoFiles(const std::string &srcPath, 222 const std::unordered_multimap<std::string, std::string> &dirMap); 223 224 void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 225 226 void OnLoadSystemAbilityFail(); 227 228 bool StartInstalldService(); 229 230 ErrCode ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath, 231 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid); 232 233 ErrCode DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength, 234 const unsigned char *profileBlock); 235 236 ErrCode RemoveSignProfile(const std::string &bundleName); 237 238 ErrCode SetEncryptionPolicy(const EncryptionParam &encryptionParam, std::string &keyId); 239 240 ErrCode DeleteEncryptionKeyId(const EncryptionParam &encryptionParam); 241 242 ErrCode RemoveExtensionDir(int32_t userId, const std::vector<std::string> &extensionBundleDirs); 243 244 ErrCode IsExistExtensionDir(int32_t userId, const std::string &extensionBundleDir, bool &isExist); 245 246 ErrCode CreateExtensionDataDir(const CreateDirParam &createDirParam); 247 248 ErrCode GetExtensionSandboxTypeList(std::vector<std::string> &typeList); 249 250 ErrCode AddUserDirDeleteDfx(int32_t userId); 251 252 ErrCode MoveHapToCodeDir(const std::string &originPath, const std::string &targetPath); 253 254 ErrCode CreateDataGroupDirs(const std::vector<CreateDirParam> ¶ms); 255 256 ErrCode DeleteDataGroupDirs(const std::vector<std::string> &uuidList, int32_t userId); 257 258 ErrCode BackUpFirstBootLog(); 259 260 private: 261 sptr<IInstalld> GetInstalldProxy(); 262 bool LoadInstalldService(); 263 264 template<typename F, typename... Args> CallService(F func,Args &&...args)265 ErrCode CallService(F func, Args&&... args) 266 { 267 int32_t maxRetryTimes = 2; 268 ErrCode errCode = ERR_APPEXECFWK_INSTALLD_SERVICE_DIED; 269 for (int32_t retryTimes = 0; retryTimes < maxRetryTimes; retryTimes++) { 270 auto proxy = GetInstalldProxy(); 271 if (proxy == nullptr) { 272 errCode = ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR; 273 continue; 274 } 275 errCode = (proxy->*func)(std::forward<Args>(args)...); 276 if (errCode == ERR_APPEXECFWK_INSTALLD_SERVICE_DIED) { 277 APP_LOGE("CallService failed, retry times: %{public}d", retryTimes + 1); 278 ResetInstalldProxy(); 279 } else { 280 return errCode; 281 } 282 } 283 return errCode; 284 } 285 private: 286 std::mutex mutex_; 287 std::mutex getProxyMutex_; 288 std::condition_variable loadSaCondition_; 289 sptr<IInstalld> installdProxy_; 290 sptr<IRemoteObject::DeathRecipient> recipient_; 291 }; 292 } // namespace AppExecFwk 293 } // namespace OHOS 294 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H