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 bundleDir Indicates the bundle data directory path that to be created. 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 * @param onlyOneUser Indicates is only one user or not. 64 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 65 */ 66 ErrCode CreateBundleDataDir(const std::string &bundleDir, 67 const int userid, const int uid, const int gid, const std::string &apl, bool onlyOneUser = true); 68 /** 69 * @brief Remove a bundle data directory. 70 * @param bundleName Indicates the bundleName data directory path that to be created. 71 * @param userid Indicates userid to be set to the directory. 72 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 73 */ 74 ErrCode RemoveBundleDataDir(const std::string &bundleName, const int userid); 75 76 /** 77 * @brief Create a module and it's abilities data directory. 78 * @param bundleDir Indicates the module data directory path that to be created. 79 * @param abilityDirs Indicates the abilities data directory name that to be created. 80 * @param uid Indicates uid to be set to the directory. 81 * @param gid Indicates gid to be set to the directory. 82 * @return Returns ERR_OK if the data directories created successfully; returns error code otherwise. 83 */ 84 ErrCode CreateModuleDataDir( 85 const std::string &ModuleDir, const std::vector<std::string> &abilityDirs, const int uid, const int gid); 86 87 /** 88 * @brief Remove a module data directory. 89 * @param ModuleDir Indicates the module data directory path that to be created. 90 * @param userid Indicates userid to be set to the directory. 91 * @return Returns ERR_OK if the data directories created successfully; returns error code otherwise. 92 */ 93 ErrCode RemoveModuleDataDir(const std::string &ModuleDir, const int userid); 94 /** 95 * @brief Remove a directory. 96 * @param dir Indicates the directory path that to be removed. 97 * @return Returns ERR_OK if the directory removed successfully; returns error code otherwise. 98 */ 99 ErrCode RemoveDir(const std::string &dir); 100 /** 101 * @brief Clean all files in a bundle data directory. 102 * @param bundleDir Indicates the data directory path that to be cleaned. 103 * @return Returns ERR_OK if the data directory cleaned successfully; returns error code otherwise. 104 */ 105 ErrCode CleanBundleDataDir(const std::string &bundleDir); 106 /** 107 * @brief Get bundle Stats. 108 * @param bundleName Indicates the bundle name. 109 * @param userId Indicates the user Id. 110 * @param bundleStats Indicates the bundle Stats. 111 * @return Returns ERR_OK if get stats successfully; returns error code otherwise. 112 */ 113 ErrCode GetBundleStats(const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats); 114 115 /** 116 * @brief Reset the installd proxy object when installd service died. 117 * @return 118 */ 119 void ResetInstalldProxy(); 120 121 /** 122 * @brief Set dir apl. 123 * @param dir Indicates the data dir. 124 * @param bundleName Indicates the bundle name. 125 * @param apl Indicates the apl type. 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 130 /** 131 * @brief Get all cache file path. 132 * @param dir Indicates the data dir. 133 * @param cachesPath Indicates the cache file path. 134 * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise. 135 */ 136 ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath); 137 138 private: 139 /** 140 * @brief Get the installd proxy object. 141 * @return Returns true if the installd proxy object got successfully; returns false otherwise. 142 */ 143 bool GetInstalldProxy(); 144 145 template<typename F, typename... Args> CallService(F func,Args &&...args)146 ErrCode CallService(F func, Args&&... args) 147 { 148 if (!GetInstalldProxy()) { 149 return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR; 150 } 151 return (installdProxy_->*func)(std::forward<Args>(args)...); 152 } 153 154 private: 155 std::mutex mutex_; 156 sptr<IInstalld> installdProxy_; 157 sptr<IRemoteObject::DeathRecipient> recipient_; 158 }; 159 } // namespace AppExecFwk 160 } // namespace OHOS 161 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_CLIENT_H