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_HOST_IMPL_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_HOST_IMPL_H 18 19 #include "bundle_constants.h" 20 #include "code_sign_helper.h" 21 #include "ipc/installd_host.h" 22 #include "installd/installd_operator.h" 23 #include "nlohmann/json.hpp" 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 class InstalldHostImpl : public InstalldHost { 28 public: 29 InstalldHostImpl(); 30 virtual ~InstalldHostImpl(); 31 /** 32 * @brief Create a bundle code directory. 33 * @param bundleDir Indicates the bundle code directory path that to be created. 34 * @return Returns ERR_OK if the bundle directory created successfully; returns error code otherwise. 35 */ 36 virtual ErrCode CreateBundleDir(const std::string &bundleDir) override; 37 /** 38 * @brief Extract the files of a HAP module to the code directory. 39 * @param srcModulePath Indicates the HAP file path. 40 * @param targetPath normal files decompression path. 41 * @param targetSoPath so files decompression path. 42 * @param cpuAbi cpuAbi. 43 * @return Returns ERR_OK if the HAP file extracted successfully; returns error code otherwise. 44 */ 45 virtual ErrCode ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath, 46 const std::string &targetSoPath, const std::string &cpuAbi) override; 47 /** 48 * @brief Extract the files. 49 * @param extractParam Indicates the extractParam. 50 * @return Returns ERR_OK if the HAP file extracted successfully; returns error code otherwise. 51 */ 52 virtual ErrCode ExtractFiles(const ExtractParam &extractParam) override; 53 54 /** 55 * @brief Extract the hnpFiles. 56 * @param hnpPackageInfo Indicates the hnpPackageInfo. 57 * @param extractParam Indicates the extractParam. 58 * @return Returns ERR_OK if the HAP file extracted successfully; returns error code otherwise. 59 */ 60 virtual ErrCode ExtractHnpFiles(const std::string &hnpPackageInfo, const ExtractParam &extractParam) override; 61 62 virtual ErrCode ProcessBundleInstallNative(const std::string &userId, const std::string &hnpRootPath, 63 const std::string &hapPath, const std::string &cpuAbi, const std::string &packageName) override; 64 65 virtual ErrCode ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName) override; 66 67 virtual ErrCode ExecuteAOT(const AOTArgs &aotArgs, std::vector<uint8_t> &pendSignData) override; 68 69 virtual ErrCode PendSignAOT(const std::string &anFileName, const std::vector<uint8_t> &signData) override; 70 71 virtual ErrCode StopAOT() override; 72 73 virtual ErrCode DeleteUninstallTmpDirs(const std::vector<std::string> &dirs) override; 74 /** 75 * @brief Rename the module directory from temporaily path to the real path. 76 * @param oldPath Indicates the old path name. 77 * @param newPath Indicates the new path name. 78 * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise. 79 */ 80 virtual ErrCode RenameModuleDir(const std::string &oldPath, const std::string &newPath) override; 81 /** 82 * @brief Create a bundle data directory. 83 * @param createDirParam Indicates param to be set to the directory. 84 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 85 */ 86 virtual ErrCode CreateBundleDataDir(const CreateDirParam &createDirParam) override; 87 88 virtual ErrCode CreateBundleDataDirWithVector(const std::vector<CreateDirParam> &createDirParams) override; 89 /** 90 * @brief Remove a bundle data directory. 91 * @param bundleName Indicates the bundleName data directory path that to be created. 92 * @param userid Indicates userid to be set to the directory. 93 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 94 */ 95 virtual ErrCode RemoveBundleDataDir(const std::string &bundleName, const int32_t userId, 96 bool isAtomicService = false, const bool async = false) override; 97 /** 98 * @brief Remove a module data directory. 99 * @param ModuleDir Indicates the module data directory path that to be created. 100 * @param userid Indicates userid to be set to the directory. 101 * @return Returns ERR_OK if the data directories created successfully; returns error code otherwise. 102 */ 103 virtual ErrCode RemoveModuleDataDir(const std::string &ModuleDir, const int userid) override; 104 /** 105 * @brief Remove a directory. 106 * @param dir Indicates the directory path that to be removed. 107 * @return Returns ERR_OK if the directory removed successfully; returns error code otherwise. 108 */ 109 virtual ErrCode RemoveDir(const std::string &dir) override; 110 /** 111 * @brief Get disk usage for dir. 112 * @param dir Indicates the directory. 113 * @param isRealPath Indicates isRealPath. 114 * * @param statSize Indicates size of dir. 115 * @return Returns true if successfully; returns false otherwise. 116 */ 117 virtual ErrCode GetDiskUsage(const std::string &dir, int64_t &statSize, bool isRealPath = false) override; 118 /** 119 * @brief Get disk usage for dir. 120 * @param path Indicates the directory vector. 121 * * @param statSize Indicates size of path. 122 * @return Returns true if successfully; returns false otherwise. 123 */ 124 virtual ErrCode GetDiskUsageFromPath(const std::vector<std::string> &path, int64_t &statSize) override; 125 /** 126 * @brief Clean all files in a bundle data directory. 127 * @param bundleDir Indicates the data directory path that to be cleaned. 128 * @return Returns ERR_OK if the data directory cleaned successfully; returns error code otherwise. 129 */ 130 virtual ErrCode CleanBundleDataDir(const std::string &bundleDir) override; 131 /** 132 * @brief Clean a bundle data directory. 133 * @param bundleName Indicates the bundleName data directory path that to be cleaned. 134 * @param userid Indicates userid to be set to the directory. 135 * @param appIndex Indicates app index to be set to the directory. 136 * @return Returns ERR_OK if the bundle data directory cleaned successfully; returns error code otherwise. 137 */ 138 virtual ErrCode CleanBundleDataDirByName(const std::string &bundleName, const int userid, 139 const int appIndex = 0) override; 140 /** 141 * @brief Get bundle Stats. 142 * @param bundleName Indicates the bundle name. 143 * @param userId Indicates the user Id. 144 * @param bundleStats Indicates the bundle Stats. 145 * @return Returns ERR_OK if get stats successfully; returns error code otherwise. 146 */ 147 virtual ErrCode GetBundleStats(const std::string &bundleName, const int32_t userId, 148 std::vector<int64_t> &bundleStats, const int32_t uid = Constants::INVALID_UID, 149 const int32_t appIndex = 0, const uint32_t statFlag = 0, 150 const std::vector<std::string> &moduleNameList = {}) override; 151 152 virtual ErrCode BatchGetBundleStats(const std::vector<std::string> &bundleNames, const int32_t userId, 153 const std::unordered_map<std::string, int32_t> &uidMap, 154 std::vector<BundleStorageStats> &bundleStats) override; 155 156 virtual ErrCode GetAllBundleStats(const int32_t userId, 157 std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids) override; 158 /** 159 * @brief Set dir apl. 160 * @param dir Indicates the data dir. 161 * @param bundleName Indicates the bundle name. 162 * @param apl Indicates the apl type. 163 * @param isPreInstallApp Indicates the bundle install type. 164 * @param debug Indicates the bundle debug mode. 165 * @param uid Indicates the uid. 166 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 167 */ 168 virtual ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl, 169 bool isPreInstallApp, bool debug, int32_t uid) override; 170 171 /** 172 * @brief Set dir apl. 173 * @param dir Indicates the data dir. 174 * @return Returns ERR_OK if set apl successfully; returns error code otherwise. 175 */ 176 virtual ErrCode SetArkStartupCacheApl(const std::string &dir) override; 177 178 std::string GetAppDataPath(const std::string &bundleName, const std::string &el, 179 const int32_t userId, const int32_t appIndex); 180 181 /** 182 * @brief Get all cache file path. 183 * @param dir Indicates the data dir. 184 * @param cachesPath Indicates the cache file path. 185 * @return Returns ERR_OK if get cache file path successfully; returns error code otherwise. 186 */ 187 virtual ErrCode GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath) override; 188 189 virtual ErrCode ScanDir( 190 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths) override; 191 192 virtual ErrCode MoveFile(const std::string &oldPath, const std::string &newPath) override; 193 194 virtual ErrCode CopyFile(const std::string &oldPath, const std::string &newPath, 195 const std::string &signatureFilePath = "") override; 196 197 virtual ErrCode Mkdir( 198 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid) override; 199 200 virtual ErrCode GetFileStat(const std::string &file, FileStat &fileStat) override; 201 202 virtual ErrCode ChangeFileStat(const std::string &file, FileStat &fileStat) override; 203 204 virtual ErrCode ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 205 const std::string &cpuAbi) override; 206 207 virtual ErrCode ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 208 const std::string &newSoPath, int32_t uid) override; 209 210 virtual ErrCode IsExistDir(const std::string &dir, bool &isExist) override; 211 212 virtual ErrCode IsExistFile(const std::string &path, bool &isExist) override; 213 214 virtual ErrCode IsExistApFile(const std::string &path, bool &isExist) override; 215 216 virtual ErrCode IsDirEmpty(const std::string &dir, bool &isDirEmpty) override; 217 218 virtual ErrCode ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec) override; 219 220 virtual ErrCode CopyFiles(const std::string &sourceDir, const std::string &destinationDir) override; 221 222 virtual ErrCode GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 223 std::vector<std::string> &fileNames) override; 224 225 virtual ErrCode VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam) override; 226 227 virtual ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption) override; 228 229 virtual ErrCode MoveFiles(const std::string &srcDir, const std::string &desDir) override; 230 231 virtual ErrCode ExtractDriverSoFiles(const std::string &srcPath, 232 const std::unordered_multimap<std::string, std::string> &dirMap) override; 233 234 virtual ErrCode ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath, 235 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid) override; 236 237 #if defined(CODE_SIGNATURE_ENABLE) 238 ErrCode PrepareEntryMap(const CodeSignatureParam &codeSignatureParam, Security::CodeSign::EntryMap &entryMap); 239 #endif 240 241 virtual ErrCode VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam) override; 242 243 virtual ErrCode DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength, 244 const unsigned char *profileBlock) override; 245 246 virtual ErrCode RemoveSignProfile(const std::string &bundleName) override; 247 248 virtual ErrCode SetEncryptionPolicy(const EncryptionParam &encryptionParam, std::string &keyId) override; 249 250 virtual ErrCode DeleteEncryptionKeyId(const EncryptionParam &encryptionParam) override; 251 252 virtual ErrCode RemoveExtensionDir(int32_t userId, const std::vector<std::string> &extensionBundleDirs) override; 253 254 virtual ErrCode IsExistExtensionDir(int32_t userId, const std::string &extensionBundleDir, bool &isExist) override; 255 256 virtual ErrCode CreateExtensionDataDir(const CreateDirParam &createDirParam) override; 257 258 virtual ErrCode GetExtensionSandboxTypeList(std::vector<std::string> &typeList) override; 259 260 virtual ErrCode AddUserDirDeleteDfx(int32_t userId) override; 261 262 virtual ErrCode MoveHapToCodeDir(const std::string &originPath, const std::string &targetPath) override; 263 264 virtual ErrCode CreateDataGroupDirs(const std::vector<CreateDirParam> ¶ms) override; 265 266 virtual ErrCode MigrateData( 267 const std::vector<std::string> &sourcePaths, const std::string &destinationPath) override; 268 269 virtual ErrCode DeleteDataGroupDirs(const std::vector<std::string> &uuidList, int32_t userId) override; 270 271 virtual ErrCode ClearDir(const std::string &dir) override; 272 273 virtual ErrCode RestoreconPath(const std::string &path) override; 274 275 private: 276 static std::string GetGroupDirPath(const std::string &el, int32_t userId, const std::string &uuid); 277 std::string GetExtensionConfigPath() const; 278 /** 279 * @brief Create /data/app/el2/userid/sharefiles/ bundle data directory. 280 * @param createDirParam Indicates param to be set to the directory. 281 * @return Returns ERR_OK if the bundle data directory created successfully; returns error code otherwise. 282 */ 283 ErrCode CreateSharefilesDataDirEl2(const CreateDirParam &createDirParam); 284 void LoadNeedCreateSandbox(const nlohmann::json &object, std::vector<std::string> &typeList); 285 bool LoadExtensionNeedCreateSandbox(const nlohmann::json &object, std::string extensionTypeName); 286 bool ReadFileIntoJson(const std::string &filePath, nlohmann::json &jsonBuf); 287 ErrCode CreateExtensionDir(const CreateDirParam &createDirParam, const std::string& parentDir, 288 int32_t mode, int32_t gid, bool isLog = false); 289 ErrCode RemoveExtensionDir(int32_t userId, const std::string &extensionBundleDir); 290 std::string GetBundleDataDir(const std::string &el, const int userid) const; 291 bool CheckPathValid(const std::string &path, const std::string &prefix); 292 ErrCode SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl, 293 unsigned int hapFlags, int32_t uid); 294 unsigned int GetHapFlags(const bool isPreInstallApp, const bool debug, const bool isDlpSandbox); 295 ErrCode InnerRemoveAtomicServiceBundleDataDir( 296 const std::string &bundleName, const int32_t userId, const bool async); 297 ErrCode InnerRemoveBundleDataDir(const std::string &bundleName, const int32_t userId, const bool async); 298 ErrCode AclSetDir(bool debug, const std::string &dir, bool setAccess, bool setDefault); 299 ErrCode AclSetExtensionDirs(bool debug, const std::string &parentDir, 300 const std::vector<std::string> &extensionDirs, bool setAccess, bool setDefault); 301 int64_t GetAppCacheSize(const std::string &bundleName, const int32_t userId, 302 const int32_t appIndex, const std::vector<std::string> &moduleNames = {}); 303 int64_t GetEl2CacheSize(const int32_t projectId, const std::string &bundleName, 304 const int32_t userId, const int32_t appIndex); 305 ErrCode CreateDataGroupDir(const CreateDirParam ¶m); 306 ErrCode DeleteEl5DataGroupDirs(const std::vector<std::string> &uuidList, int32_t userId); 307 ErrCode CreateBundleDataDirWithEl(const CreateDirParam &createDirParam); 308 ErrCode CreateCommonDataDir(const CreateDirParam &createDirParam, const std::string &el); 309 ErrCode CreateEl2DataDir(const CreateDirParam &createDirParam); 310 }; 311 } // namespace AppExecFwk 312 } // namespace OHOS 313 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_HOST_IMPL_H