1 /* 2 * Copyright (c) 2021-2023 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 <string> 23 24 #include "nocopyable.h" 25 #include "singleton.h" 26 27 #include "appexecfwk_errors.h" 28 #include "bundle_constants.h" 29 #include "ipc/installd_interface.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 class InstalldClient : public DelayedSingleton<InstalldClient> { 34 public: 35 /** 36 * @brief Create a bundle code directory through an installd proxy object. 37 * @param bundleDir Indicates the bundle code directory path that to be created. 38 * @return Returns ERR_OK if the bundle directory created successfully; returns error code otherwise. 39 */ 40 ErrCode CreateBundleDir(const std::string &bundleDir); 41 /** 42 * @brief Extract the files of a HAP module to the code directory. 43 * @param srcModulePath Indicates the HAP file path. 44 * @param targetPath normal files decompression path. 45 * @param targetSoPath so files decompression path. 46 * @param cpuAbi cpuAbi. 47 * @return Returns ERR_OK if the HAP file extracted successfully; returns error code otherwise. 48 */ 49 ErrCode ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath, 50 const std::string &targetSoPath, const std::string &cpuAbi); 51 /** 52 * @brief Rename the module directory from temporaily path to the real path. 53 * @param oldPath Indicates the old path name. 54 * @param newPath Indicates the new path name. 55 * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise. 56 */ 57 ErrCode RenameModuleDir(const std::string &oldPath, const std::string &newPath); 58 /** 59 * @brief Create a bundle data directory. 60 * @param createDirParam Indicates param to be set to the directory. 61 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 62 */ 63 ErrCode CreateBundleDataDir(const CreateDirParam &createDirParam); 64 65 ErrCode CreateBundleDataDirWithVector(const std::vector<CreateDirParam> &createDirParams); 66 67 /** 68 * @brief Remove a bundle data directory. 69 * @param bundleName Indicates the bundleName data directory path that to be created. 70 * @param userid Indicates userid to be set to the directory. 71 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 72 */ 73 ErrCode RemoveBundleDataDir(const std::string &bundleName, const int userid); 74 /** 75 * @brief Remove a module data directory. 76 * @param ModuleDir Indicates the module data directory path that to be created. 77 * @param userid Indicates userid to be set to the directory. 78 * @return Returns ERR_OK if the data directories created successfully; returns error code otherwise. 79 */ 80 ErrCode RemoveModuleDataDir(const std::string &ModuleDir, const int userid); 81 /** 82 * @brief Remove a directory. 83 * @param dir Indicates the directory path that to be removed. 84 * @return Returns ERR_OK if the directory removed successfully; returns error code otherwise. 85 */ 86 ErrCode RemoveDir(const std::string &dir); 87 /** 88 * @brief Clean all files in a bundle data directory. 89 * @param bundleDir Indicates the data directory path that to be cleaned. 90 * @return Returns ERR_OK if the data directory cleaned successfully; returns error code otherwise. 91 */ 92 ErrCode CleanBundleDataDir(const std::string &bundleDir); 93 /** 94 * @brief Clean a bundle data directory. 95 * @param bundleName Indicates the bundleName data directory path that to be cleaned. 96 * @param userid Indicates userid to be set to the directory. 97 * @return Returns ERR_OK if the bundle data directory cleaned successfully; returns error code otherwise. 98 */ 99 ErrCode CleanBundleDataDirByName(const std::string &bundleName, const int userid); 100 /** 101 * @brief Get bundle Stats. 102 * @param bundleName Indicates the bundle name. 103 * @param userId Indicates the user Id. 104 * @param bundleStats Indicates the bundle Stats. 105 * @return Returns ERR_OK if get stats successfully; returns error code otherwise. 106 */ 107 ErrCode GetBundleStats(const std::string &bundleName, const int32_t userId, 108 std::vector<int64_t> &bundleStats, const int32_t uid = Constants::INVALID_UID); 109 110 ErrCode GetAllBundleStats(const std::vector<std::string> &bundleNames, const int32_t userId, 111 std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids); 112 113 /** 114 * @brief Reset the installd proxy object when installd service died. 115 * @return 116 */ 117 void ResetInstalldProxy(); 118 119 /** 120 * @brief Set dir apl. 121 * @param dir Indicates the data dir. 122 * @param bundleName Indicates the bundle name. 123 * @param apl Indicates the apl type. 124 * @param isPreInstallApp Indicates the bundle install type. 125 * @param debug Indicates the bundle debug mode. 126 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 127 */ 128 ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl, 129 bool isPreInstallApp, bool debug); 130 131 /** 132 * @brief Get all cache file path. 133 * @param dir Indicates the data dir. 134 * @param cachesPath Indicates the cache file path. 135 * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise. 136 */ 137 ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath); 138 139 ErrCode ScanDir( 140 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 141 142 ErrCode MoveFile(const std::string &oldPath, const std::string &newPath); 143 144 ErrCode CopyFile(const std::string &oldPath, const std::string &newPath, 145 const std::string &signatureFilePath = ""); 146 147 ErrCode Mkdir( 148 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid); 149 150 ErrCode GetFileStat(const std::string &file, FileStat &fileStat); 151 152 ErrCode ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 153 const std::string &cpuAbi); 154 155 ErrCode ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 156 const std::string &newSoPath, int32_t uid = Constants::INVALID_UID); 157 158 ErrCode IsExistDir(const std::string &dir, bool &isExist); 159 160 ErrCode IsExistFile(const std::string &path, bool &isExist); 161 162 ErrCode IsExistApFile(const std::string &path, bool &isExist); 163 164 ErrCode IsDirEmpty(const std::string &dir, bool &isDirEmpty); 165 166 ErrCode ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec); 167 168 ErrCode CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 169 170 ErrCode ExtractFiles(const ExtractParam &extractParam); 171 172 ErrCode GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 173 std::vector<std::string> &fileNames); 174 175 ErrCode ExecuteAOT(const AOTArgs &aotArgs); 176 177 ErrCode VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam); 178 179 ErrCode VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam); 180 181 ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 182 183 ErrCode MoveFiles(const std::string &srcDir, const std::string &desDir); 184 185 ErrCode ExtractDriverSoFiles(const std::string &srcPath, 186 const std::unordered_multimap<std::string, std::string> &dirMap); 187 188 void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 189 190 void OnLoadSystemAbilityFail(); 191 192 bool StartInstalldService(); 193 194 ErrCode ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath, 195 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid); 196 197 ErrCode DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength, 198 const unsigned char *profileBlock); 199 200 ErrCode RemoveSignProfile(const std::string &bundleName); 201 202 private: 203 /** 204 * @brief Get the installd proxy object. 205 * @return Returns true if the installd proxy object got successfully; returns false otherwise. 206 */ 207 bool GetInstalldProxy(); 208 bool LoadInstalldService(); 209 210 template<typename F, typename... Args> CallService(F func,Args &&...args)211 ErrCode CallService(F func, Args&&... args) 212 { 213 if (!GetInstalldProxy()) { 214 return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR; 215 } 216 return (installdProxy_->*func)(std::forward<Args>(args)...); 217 } 218 219 private: 220 bool loadSaFinished_; 221 std::mutex mutex_; 222 std::mutex loadSaMutex_; 223 std::mutex getProxyMutex_; 224 std::condition_variable loadSaCondition_; 225 sptr<IInstalld> installdProxy_; 226 sptr<IRemoteObject::DeathRecipient> recipient_; 227 }; 228 } // namespace AppExecFwk 229 } // namespace OHOS 230 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H