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