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_OPERATOR_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H 18 19 #include <mutex> 20 #include <string> 21 #include <vector> 22 23 #include "aot/aot_args.h" 24 #include "appexecfwk_errors.h" 25 #include "bundle_extractor.h" 26 #include "code_sign_helper.h" 27 #include "installd/installd_constants.h" 28 #include "ipc/check_encryption_param.h" 29 #include "ipc/code_signature_param.h" 30 #include "ipc/encryption_param.h" 31 #include "ipc/extract_param.h" 32 #include "nocopyable.h" 33 34 namespace OHOS { 35 namespace AppExecFwk { 36 using EnforceMetadataProcessForApp = int32_t (*)(const std::unordered_map<std::string, std::string> &, 37 const CodeCryptoHapInfo &, bool &); 38 39 class InstalldOperator { 40 public: 41 /** 42 * @brief Check link file and unlink. 43 * @param path Indicates the file path to be checked. 44 * @return Returns true if the file is link and unlink succeed; returns false otherwise. 45 */ 46 static bool CheckAndDeleteLinkFile(const std::string &path); 47 /** 48 * @brief Check whether a file exist. 49 * @param path Indicates the file path to be checked. 50 * @return Returns true if the file exist; returns false otherwise. 51 */ 52 static bool IsExistFile(const std::string &path); 53 /** 54 * @brief Check whether an AP file exists in the current directory of the file. 55 * @param path Indicates the file path to be checked. 56 * @return Returns true if the file exist; returns false otherwise. 57 */ 58 static bool IsExistApFile(const std::string &path); 59 /** 60 * @brief Check whether a directory exist. 61 * @param path Indicates the directory path to be checked. 62 * @return Returns true if the directory exist; returns false otherwise. 63 */ 64 static bool IsExistDir(const std::string &path); 65 /** 66 * @brief Check whether a directory is empty. 67 * @param dir Indicates the directory path to be checked. 68 * @return Returns true if the directory is empty; returns false otherwise. 69 */ 70 static bool IsDirEmpty(const std::string &dir); 71 /** 72 * @brief Check whether a directory is empty using a faster method. 73 * @param path Indicates the directory path to be checked. 74 * @return Returns true if the directory is empty or open failed; returns false otherwise. 75 */ 76 static bool IsDirEmptyFast(const std::string &path); 77 /** 78 * @brief Make a new directory including the parent path if not exist. 79 * @param path Indicates the directory path to be checked. 80 * @param isReadByOthers Indicates the directory whether read by other users. 81 * @return Returns true if the directory make successfully; returns false otherwise. 82 */ 83 static bool MkRecursiveDir(const std::string &path, bool isReadByOthers); 84 /** 85 * @brief Delete a directory. 86 * @param path Indicates the directory path to be deleted. 87 * @return Returns true if the directory deleted successfully; returns false otherwise. 88 */ 89 static bool DeleteDir(const std::string &path); 90 91 static bool DeleteDirFast(const std::string &path); 92 93 static bool DeleteDirFlexible(const std::string &path, const bool async); 94 95 static bool DeleteUninstallTmpDir(const std::string &path); 96 /** 97 * @brief Extract the files of a compressed package to a specific directory. 98 * @param srcModulePath Indicates the package file path. 99 * @param targetSoPath so files decompression path. 100 * @param cpuAbi cpuAbi. 101 * @return Returns true if the package extracted successfully; returns false otherwise. 102 */ 103 static bool ExtractFiles(const std::string &sourcePath, const std::string &targetSoPath, 104 const std::string &cpuAbi); 105 106 static bool IsNativeSo(const std::string &entryName, const std::string &cpuAbi); 107 108 static bool ExtractFiles(const ExtractParam &extractParam); 109 static bool ExtractFiles(const std::string hnpPackageInfo, const ExtractParam &extractParam); 110 static bool ExtractTargetFile( 111 const BundleExtractor &extractor, 112 const std::string &entryName, 113 const ExtractParam ¶m); 114 static void ExtractTargetHnpFile( 115 const BundleExtractor &extractor, 116 const std::string &entryName, 117 const std::string &targetPath, 118 const ExtractFileType &extractFileType = ExtractFileType::SO); 119 static bool ProcessBundleInstallNative( 120 const std::string &userId, 121 const std::string &hnpRootPath, 122 const std::string &hapPath, 123 const std::string &cpuAbi, 124 const std::string &packageName); 125 static bool ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName); 126 127 static bool DeterminePrefix(const ExtractFileType &extractFileType, const std::string &cpuAbi, 128 std::string &prefix); 129 130 static bool DetermineSuffix(const ExtractFileType &extractFileType, std::vector<std::string> &suffixes); 131 132 static bool IsNativeFile( 133 const std::string &entryName, const ExtractParam &extractParam); 134 135 /** 136 * @brief Rename a directory from old path to new path. 137 * @param oldPath Indicates the old path name. 138 * @param newPath Indicates the new path name. 139 * @return Returns true if the directory renamed successfully; returns false otherwise. 140 */ 141 static bool RenameDir(const std::string &oldPath, const std::string &newPath); 142 /** 143 * @brief Change the owner and group ID of a file or directory. 144 * @param filePath Indicates the file or directory path. 145 * @param uid Indicates the uid. 146 * @param uid Indicates the gid. 147 * @return Returns true if changed successfully; returns false otherwise. 148 */ 149 static bool ChangeFileAttr(const std::string &filePath, const int uid, const int gid); 150 /** 151 * @brief Rename a file from old path to new path. 152 * @param oldPath Indicates the old path name. 153 * @param newPath Indicates the new path name. 154 * @return Returns true if the file renamed successfully; returns false otherwise. 155 */ 156 static bool RenameFile(const std::string &oldPath, const std::string &newPath); 157 /** 158 * @brief Check whether a path is valid under a root path. 159 * @param rootDir Indicates the root path name. 160 * @param path Indicates the path to be checked. 161 * @return Returns true if the path is valid successfully; returns false otherwise. 162 */ 163 static bool IsValidPath(const std::string &rootDir, const std::string &path); 164 /** 165 * @brief Check whether a path is valid code path. 166 * @param codePath Indicates the path to be checked. 167 * @return Returns true if the file renamed successfully; returns false otherwise. 168 */ 169 static bool IsValidCodePath(const std::string &codePath); 170 /** 171 * @brief Get the parent directory path of a file. 172 * @param codePath Indicates the file path. 173 * @return Returns the parent directory if get successfully; returns empty string otherwise. 174 */ 175 static std::string GetPathDir(const std::string &path); 176 /** 177 * @brief Delete files in a directory. 178 * @param path Indicates the directory path of the files to be deleted. 179 * @return Returns true if the files deleted successfully; returns false otherwise. 180 */ 181 static bool DeleteFiles(const std::string &dataPath); 182 /** 183 * @brief Delete files in a directory except the directories to be kept. 184 * @param dataPath Indicates the directory path of the files to be deleted. 185 * @param dirsToKeep Indicates the directories to be kept. 186 * @return Returns true if the files deleted successfully; returns false otherwise 187 */ 188 static bool DeleteFilesExceptDirs(const std::string &dataPath, const std::vector<std::string> &dirsToKeep); 189 /** 190 * @brief Make a directory and change the owner and group ID of it. 191 * @param path Indicates the directory path to be made. 192 * @param isReadByOthers Indicates the directory whether read by other users. 193 * @param uid Indicates the uid. 194 * @param uid Indicates the gid. 195 * @return Returns true if directory made successfully; returns false otherwise. 196 */ 197 static bool MkOwnerDir(const std::string &path, bool isReadByOthers, const int uid, const int gid); 198 /** 199 * @brief Make a directory and change the owner and group ID of it. 200 * @param path Indicates the directory path to be made. 201 * @param mode Indicates the directory mode. 202 * @param uid Indicates the uid. 203 * @param uid Indicates the gid. 204 * @return Returns true if directory made successfully; returns false otherwise. 205 */ 206 static bool MkOwnerDir(const std::string &path, int mode, const int uid, const int gid); 207 /** 208 * @brief Get disk usage for dir. 209 * @param dir Indicates the directory. 210 * @param size Indicates the disk size. 211 * @return Returns true if successfully; returns false otherwise. 212 */ 213 static int64_t GetDiskUsage(const std::string &dir, bool isRealPath = false); 214 /** 215 * @brief Traverse all cache directories. 216 * @param currentPath Indicates the current path. 217 * @param cacheDirs Indicates the cache directories. 218 * @return Returns true if successfully; returns false otherwise. 219 */ 220 static void TraverseCacheDirectory(const std::string ¤tPath, std::vector<std::string> &cacheDirs); 221 /** 222 * @brief Get disk usage from path. 223 * @param path Indicates the current path. 224 * @return Returns disk size. 225 */ 226 static int64_t GetDiskUsageFromPath(const std::vector<std::string> &path); 227 228 static bool InitialiseQuotaMounts(); 229 230 static int64_t GetDiskUsageFromQuota(const int32_t uid); 231 232 static bool SetProjectIdForDir(const std::string &path, uint32_t projectId); 233 234 static int64_t GetProjectUsage(uint32_t projectId); 235 236 static bool ScanDir( 237 const std::string &dirPath, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 238 239 static bool ScanSoFiles(const std::string &newSoPath, const std::string &originPath, 240 const std::string ¤tPath, std::vector<std::string> &paths); 241 242 static bool CopyFile(const std::string &sourceFile, const std::string &destinationFile); 243 244 static bool CopyFileFast(const std::string &sourcePath, const std::string &destPath); 245 246 static bool ChangeDirOwnerRecursively(const std::string &path, const int uid, const int gid); 247 248 static bool IsDiffFiles(const std::string &entryName, 249 const std::string &targetPath, const std::string &cpuAbi); 250 251 static bool ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 252 const std::string &cpuAbi); 253 254 static bool ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 255 const std::string &newSoPath, int32_t uid); 256 257 static bool ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &fileVec); 258 259 static bool CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 260 261 static bool GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 262 std::vector<std::string> &fileNames); 263 264 static bool GetAtomicServiceBundleDataDir(const std::string &bundleName, 265 const int32_t userId, std::vector<std::string> &allPathNames); 266 267 #if defined(CODE_SIGNATURE_ENABLE) 268 static bool PrepareEntryMap(const CodeSignatureParam &codeSignatureParam, 269 const std::vector<std::string> &soEntryFiles, Security::CodeSign::EntryMap &entryMap); 270 static ErrCode PerformCodeSignatureCheck(const CodeSignatureParam &codeSignatureParam, 271 const Security::CodeSign::EntryMap &entryMap); 272 #endif 273 274 static ErrCode VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam); 275 276 #if defined(CODE_ENCRYPTION_ENABLE) 277 static ErrCode EnforceEncryption(std::unordered_map<std::string, std::string> &entryMap, 278 const CodeCryptoHapInfo &hapInfo, bool &isEncryption); 279 #endif 280 281 static ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 282 283 static ErrCode CheckHapEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 284 285 static bool MoveFiles(const std::string &srcDir, const std::string &desDir, bool isDesDirNeedCreated = false); 286 287 static bool MoveFileOrDir(const std::string &srcPath, const std::string &destPath, mode_t mode); 288 289 static bool MoveFile(const std::string &srcPath, const std::string &destPath); 290 291 static bool ExtractDriverSoFiles(const std::string &srcPath, 292 const std::unordered_multimap<std::string, std::string> &dirMap); 293 294 static bool CopyDriverSoFiles(const std::string &originalDir, const std::string &destinedDir); 295 296 #if defined(CODE_ENCRYPTION_ENABLE) 297 static ErrCode ExtractSoFilesToTmpHapPath(const std::string &hapPath, const std::string &cpuAbi, 298 const std::string &tmpSoPath, int32_t uid); 299 300 static ErrCode ExtractSoFilesToTmpSoPath(const std::string &hapPath, const std::string &realSoFilesPath, 301 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid); 302 303 static ErrCode DecryptSoFile(const std::string &hapPath, const std::string &tmpHapPath, int32_t uid, 304 uint32_t fileSize, uint32_t offset); 305 306 static ErrCode RemoveEncryptedKey(int32_t uid, const std::vector<std::string> &soList); 307 308 static int32_t CallIoctl(int32_t flag, int32_t associatedFlag, int32_t uid, int32_t &fd); 309 #endif 310 static int32_t MigrateData(const std::vector<std::string> &sourcePaths, const std::string &destinationPath); 311 312 static bool GenerateKeyIdAndSetPolicy(const EncryptionParam &encryptionParam, std::string &keyId); 313 314 static bool DeleteKeyId(const EncryptionParam &encryptionParam); 315 316 /** 317 * @brief Add file Delete dfx 318 * @param path Indicates the directory path to add dfx. 319 * @return 320 */ 321 static void AddDeleteDfx(const std::string &path); 322 323 /** 324 * @brief Rmv file Delete dfx 325 * @param path Indicates the directory path to add dfx. 326 * @return 327 */ 328 static void RmvDeleteDfx(const std::string &path); 329 330 static bool ClearDir(const std::string &dir); 331 332 static bool RestoreconPath(const std::string &path); 333 334 private: 335 static bool ObtainNativeSoFile(const BundleExtractor &extractor, const std::string &cpuAbi, 336 std::vector<std::string> &soEntryFiles); 337 338 static bool ProcessApplyDiffPatchPath(const std::string &oldSoPath, const std::string &diffFilePath, 339 const std::string &newSoPath, std::vector<std::string> &oldSoFileNames, 340 std::vector<std::string> &diffFileNames); 341 static bool ExtractResourceFiles(const ExtractParam &extractParam, const BundleExtractor &extractor); 342 static bool CheckPathIsSame(const std::string &path, int32_t mode, const int32_t uid, const int32_t gid, 343 bool &isPathExist); 344 static bool IsPathNeedChown(const std::string &path, int32_t mode, bool isPathExist); 345 static bool SetKeyIdPolicy(const EncryptionParam &encryptionParam, const std::string &keyId); 346 static bool GenerateKeyId(const EncryptionParam &encryptionParam, std::string &keyId); 347 #if defined(CODE_ENCRYPTION_ENABLE) 348 static std::mutex encryptionMutex_; 349 static void *encryptionHandle_; 350 static EnforceMetadataProcessForApp enforceMetadataProcessForApp_; 351 static bool OpenEncryptionHandle(); 352 #endif 353 static void FsyncFile(const std::string &path); 354 static std::string GetSameLevelTmpPath(const std::string &path); 355 356 struct OwnershipInfo { 357 int32_t uid { 0 }; 358 int32_t gid { 0 }; 359 int32_t mode { 0 }; 360 }; 361 static int32_t InnerMigrateData( 362 const std::string &sourcePaths, const std::string &destinationPath, const OwnershipInfo &info); 363 static int32_t MigrateDataCopyFile( 364 const std::string &sourceFile, const std::string &destinationFile, const OwnershipInfo &info); 365 static int32_t MigrateDataCopyDir( 366 const std::string &sourcePaths, const std::string &destinationPath, const OwnershipInfo &info); 367 static int32_t MigrateDataCheckPrmissions( 368 std::vector<std::string> &realSourcePaths, const std::string &destinationPath, OwnershipInfo &info); 369 static int32_t UpdateFileProperties(const std::string &newFile, const OwnershipInfo &info); 370 static int32_t ForceCreateDirectory(const std::string &path, const OwnershipInfo &info); 371 static int32_t MigrateDataCreateAhead( 372 const std::string &sourcePaths, std::string &destinationPath, const OwnershipInfo &info); 373 }; 374 } // namespace AppExecFwk 375 } // namespace OHOS 376 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H 377