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 <memory> 20 #include <mutex> 21 #include <string> 22 23 #include "nocopyable.h" 24 #include "singleton.h" 25 26 #include "appexecfwk_errors.h" 27 #include "ipc/installd_interface.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 class InstalldClient : public DelayedSingleton<InstalldClient> { 32 public: 33 /** 34 * @brief Create a bundle code directory through an installd proxy object. 35 * @param bundleDir Indicates the bundle code directory path that to be created. 36 * @return Returns ERR_OK if the bundle directory created successfully; returns error code otherwise. 37 */ 38 ErrCode CreateBundleDir(const std::string &bundleDir); 39 /** 40 * @brief Extract the files of a HAP module to the code directory. 41 * @param srcModulePath Indicates the HAP file path. 42 * @param targetPath normal files decompression path. 43 * @param targetSoPath so files decompression path. 44 * @param cpuAbi cpuAbi. 45 * @return Returns ERR_OK if the HAP file extracted successfully; returns error code otherwise. 46 */ 47 ErrCode ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath, 48 const std::string &targetSoPath, const std::string &cpuAbi); 49 /** 50 * @brief Rename the module directory from temporaily path to the real path. 51 * @param oldPath Indicates the old path name. 52 * @param newPath Indicates the new path name. 53 * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise. 54 */ 55 ErrCode RenameModuleDir(const std::string &oldPath, const std::string &newPath); 56 /** 57 * @brief Create a bundle data directory. 58 * @param bundleName Indicates bundleName to be set to the directory. 59 * @param userid Indicates userid to be set to the directory. 60 * @param uid Indicates uid to be set to the directory. 61 * @param gid Indicates gid to be set to the directory. 62 * @param apl Indicates apl 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 std::string &bundleName, 66 const int userid, const int uid, const int gid, const std::string &apl); 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 Get bundle Stats. 95 * @param bundleName Indicates the bundle name. 96 * @param userId Indicates the user Id. 97 * @param bundleStats Indicates the bundle Stats. 98 * @return Returns ERR_OK if get stats successfully; returns error code otherwise. 99 */ 100 ErrCode GetBundleStats(const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats); 101 102 /** 103 * @brief Reset the installd proxy object when installd service died. 104 * @return 105 */ 106 void ResetInstalldProxy(); 107 108 /** 109 * @brief Set dir apl. 110 * @param dir Indicates the data dir. 111 * @param bundleName Indicates the bundle name. 112 * @param apl Indicates the apl type. 113 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 114 */ 115 ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl); 116 117 /** 118 * @brief Get all cache file path. 119 * @param dir Indicates the data dir. 120 * @param cachesPath Indicates the cache file path. 121 * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise. 122 */ 123 ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath); 124 125 ErrCode ScanDir( 126 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 127 128 ErrCode MoveFile(const std::string &oldPath, const std::string &newPath); 129 130 ErrCode CopyFile(const std::string &oldPath, const std::string &newPath); 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 IsDirEmpty(const std::string &dir, bool &isDirEmpty); 146 147 ErrCode ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec); 148 149 ErrCode CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 150 151 ErrCode ExtractFiles(const ExtractParam &extractParam); 152 153 private: 154 /** 155 * @brief Get the installd proxy object. 156 * @return Returns true if the installd proxy object got successfully; returns false otherwise. 157 */ 158 bool GetInstalldProxy(); 159 160 template<typename F, typename... Args> CallService(F func,Args &&...args)161 ErrCode CallService(F func, Args&&... args) 162 { 163 if (!GetInstalldProxy()) { 164 return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR; 165 } 166 return (installdProxy_->*func)(std::forward<Args>(args)...); 167 } 168 169 private: 170 std::mutex mutex_; 171 sptr<IInstalld> installdProxy_; 172 sptr<IRemoteObject::DeathRecipient> recipient_; 173 }; 174 } // namespace AppExecFwk 175 } // namespace OHOS 176 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H