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 BatchGetBundleStats(const std::vector<std::string> &bundleNames, const int32_t userId, 132 const std::unordered_map<std::string, int32_t> &uidMap, 133 std::vector<BundleStorageStats> &bundleStats); 134 135 ErrCode GetAllBundleStats(const int32_t userId, 136 std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids); 137 138 ErrCode MigrateData(const std::vector<std::string> &sourcePaths, const std::string &destinationPath); 139 140 /** 141 * @brief Reset the installd proxy object when installd service died. 142 * @return 143 */ 144 void ResetInstalldProxy(); 145 146 /** 147 * @brief Set dir apl. 148 * @param dir Indicates the data dir. 149 * @param bundleName Indicates the bundle name. 150 * @param apl Indicates the apl type. 151 * @param isPreInstallApp Indicates the bundle install type. 152 * @param debug Indicates the bundle debug mode. 153 * @param uid Indicates the uid. 154 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 155 */ 156 ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl, 157 bool isPreInstallApp, bool debug, int32_t uid); 158 159 /** 160 * @brief Set dir apl. 161 * @param dir Indicates the data dir. 162 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 163 */ 164 ErrCode SetArkStartupCacheApl(const std::string &dir); 165 166 /** 167 * @brief Get all cache file path. 168 * @param dir Indicates the data dir. 169 * @param cachesPath Indicates the cache file path. 170 * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise. 171 */ 172 ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath); 173 174 ErrCode ScanDir( 175 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 176 177 ErrCode MoveFile(const std::string &oldPath, const std::string &newPath); 178 179 ErrCode CopyFile(const std::string &oldPath, const std::string &newPath, 180 const std::string &signatureFilePath = ""); 181 182 ErrCode Mkdir( 183 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid); 184 185 ErrCode GetFileStat(const std::string &file, FileStat &fileStat); 186 187 ErrCode ChangeFileStat(const std::string &file, FileStat &fileStat); 188 189 ErrCode ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 190 const std::string &cpuAbi); 191 192 ErrCode ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 193 const std::string &newSoPath, int32_t uid = Constants::INVALID_UID); 194 195 ErrCode IsExistDir(const std::string &dir, bool &isExist); 196 197 ErrCode IsExistFile(const std::string &path, bool &isExist); 198 199 ErrCode IsExistApFile(const std::string &path, bool &isExist); 200 201 ErrCode IsDirEmpty(const std::string &dir, bool &isDirEmpty); 202 203 ErrCode ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec); 204 205 ErrCode CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 206 207 ErrCode ExtractFiles(const ExtractParam &extractParam); 208 209 ErrCode ExtractHnpFiles(const std::string &hnpPackageInfo, const ExtractParam &extractParam); 210 211 ErrCode ProcessBundleInstallNative(const std::string &userId, const std::string &hnpRootPath, 212 const std::string &hapPath, const std::string &cpuAbi, const std::string &packageName); 213 214 ErrCode ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName); 215 216 ErrCode GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 217 std::vector<std::string> &fileNames); 218 219 ErrCode ExecuteAOT(const AOTArgs &aotArgs, std::vector<uint8_t> &pendSignData); 220 221 ErrCode PendSignAOT(const std::string &anFileName, const std::vector<uint8_t> &signData); 222 223 ErrCode StopAOT(); 224 225 ErrCode DeleteUninstallTmpDirs(const std::vector<std::string> &dirs); 226 227 ErrCode VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam); 228 229 ErrCode VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam); 230 231 ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 232 233 ErrCode MoveFiles(const std::string &srcDir, const std::string &desDir); 234 235 ErrCode ExtractDriverSoFiles(const std::string &srcPath, 236 const std::unordered_multimap<std::string, std::string> &dirMap); 237 238 void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 239 240 void OnLoadSystemAbilityFail(); 241 242 bool StartInstalldService(); 243 244 ErrCode ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath, 245 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid); 246 247 ErrCode DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength, 248 const unsigned char *profileBlock); 249 250 ErrCode RemoveSignProfile(const std::string &bundleName); 251 252 ErrCode SetEncryptionPolicy(const EncryptionParam &encryptionParam, std::string &keyId); 253 254 ErrCode DeleteEncryptionKeyId(const EncryptionParam &encryptionParam); 255 256 ErrCode RemoveExtensionDir(int32_t userId, const std::vector<std::string> &extensionBundleDirs); 257 258 ErrCode IsExistExtensionDir(int32_t userId, const std::string &extensionBundleDir, bool &isExist); 259 260 ErrCode CreateExtensionDataDir(const CreateDirParam &createDirParam); 261 262 ErrCode GetExtensionSandboxTypeList(std::vector<std::string> &typeList); 263 264 ErrCode AddUserDirDeleteDfx(int32_t userId); 265 266 ErrCode MoveHapToCodeDir(const std::string &originPath, const std::string &targetPath); 267 268 ErrCode CreateDataGroupDirs(const std::vector<CreateDirParam> ¶ms); 269 270 ErrCode DeleteDataGroupDirs(const std::vector<std::string> &uuidList, int32_t userId); 271 272 ErrCode LoadInstalls(); 273 274 ErrCode ClearDir(const std::string &dir); 275 276 ErrCode RestoreconPath(const std::string &path); 277 278 private: 279 sptr<IInstalld> GetInstalldProxy(); 280 bool LoadInstalldService(); 281 282 template<typename F, typename... Args> CallService(F func,Args &&...args)283 ErrCode CallService(F func, Args&&... args) 284 { 285 int32_t maxRetryTimes = 2; 286 ErrCode errCode = ERR_APPEXECFWK_INSTALLD_SERVICE_DIED; 287 for (int32_t retryTimes = 0; retryTimes < maxRetryTimes; retryTimes++) { 288 auto proxy = GetInstalldProxy(); 289 if (proxy == nullptr) { 290 errCode = ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR; 291 continue; 292 } 293 errCode = (proxy->*func)(std::forward<Args>(args)...); 294 if (errCode == ERR_APPEXECFWK_INSTALLD_SERVICE_DIED) { 295 APP_LOGE("CallService failed, retry times: %{public}d", retryTimes + 1); 296 ResetInstalldProxy(); 297 } else { 298 return errCode; 299 } 300 } 301 return errCode; 302 } 303 private: 304 std::mutex mutex_; 305 std::mutex getProxyMutex_; 306 std::condition_variable loadSaCondition_; 307 sptr<IInstalld> installdProxy_; 308 sptr<IRemoteObject::DeathRecipient> recipient_; 309 }; 310 } // namespace AppExecFwk 311 } // namespace OHOS 312 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H