1 /* 2 * Copyright (c) 2021-2023 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/extract_param.h" 31 #include "nocopyable.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 class InstalldOperator { 36 public: 37 /** 38 * @brief Check whether a file exist. 39 * @param path Indicates the file path to be checked. 40 * @return Returns true if the file exist; returns false otherwise. 41 */ 42 static bool IsExistFile(const std::string &path); 43 /** 44 * @brief Check whether an AP file exists in the current directory of the file. 45 * @param path Indicates the file path to be checked. 46 * @return Returns true if the file exist; returns false otherwise. 47 */ 48 static bool IsExistApFile(const std::string &path); 49 /** 50 * @brief Check whether a directory exist. 51 * @param path Indicates the directory path to be checked. 52 * @return Returns true if the directory exist; returns false otherwise. 53 */ 54 static bool IsExistDir(const std::string &path); 55 /** 56 * @brief Check whether a directory is empty. 57 * @param dir Indicates the directory path to be checked. 58 * @return Returns true if the directory is empty; returns false otherwise. 59 */ 60 static bool IsDirEmpty(const std::string &dir); 61 /** 62 * @brief Make a new directory including the parent path if not exist. 63 * @param path Indicates the directory path to be checked. 64 * @param isReadByOthers Indicates the directory whether read by other users. 65 * @return Returns true if the directory make successfully; returns false otherwise. 66 */ 67 static bool MkRecursiveDir(const std::string &path, bool isReadByOthers); 68 /** 69 * @brief Delete a directory. 70 * @param path Indicates the directory path to be deleted. 71 * @return Returns true if the directory deleted successfully; returns false otherwise. 72 */ 73 static bool DeleteDir(const std::string &path); 74 /** 75 * @brief Extract the files of a compressed package to a specific directory. 76 * @param srcModulePath Indicates the package file path. 77 * @param targetSoPath so files decompression path. 78 * @param cpuAbi cpuAbi. 79 * @return Returns true if the package extracted successfully; returns false otherwise. 80 */ 81 static bool ExtractFiles(const std::string &sourcePath, const std::string &targetSoPath, 82 const std::string &cpuAbi); 83 84 static bool IsNativeSo(const std::string &entryName, const std::string &cpuAbi); 85 86 static bool ExtractFiles(const ExtractParam &extractParam); 87 static void ExtractTargetFile( 88 const BundleExtractor &extractor, 89 const std::string &entryName, 90 const std::string &targetPath, 91 const std::string &cpuAbi, 92 const ExtractFileType &extractFileType = ExtractFileType::SO); 93 94 static bool DeterminePrefix(const ExtractFileType &extractFileType, const std::string &cpuAbi, 95 std::string &prefix); 96 97 static bool DetermineSuffix(const ExtractFileType &extractFileType, std::vector<std::string> &suffixes); 98 99 static bool IsNativeFile( 100 const std::string &entryName, const ExtractParam &extractParam); 101 102 /** 103 * @brief Rename a directory from old path to new path. 104 * @param oldPath Indicates the old path name. 105 * @param newPath Indicates the new path name. 106 * @return Returns true if the directory renamed successfully; returns false otherwise. 107 */ 108 static bool RenameDir(const std::string &oldPath, const std::string &newPath); 109 /** 110 * @brief Change the owner and group ID of a file or directory. 111 * @param filePath Indicates the file or directory path. 112 * @param uid Indicates the uid. 113 * @param uid Indicates the gid. 114 * @return Returns true if changed successfully; returns false otherwise. 115 */ 116 static bool ChangeFileAttr(const std::string &filePath, const int uid, const int gid); 117 /** 118 * @brief Rename a file from old path to new path. 119 * @param oldPath Indicates the old path name. 120 * @param newPath Indicates the new path name. 121 * @return Returns true if the file renamed successfully; returns false otherwise. 122 */ 123 static bool RenameFile(const std::string &oldPath, const std::string &newPath); 124 /** 125 * @brief Check whether a path is valid under a root path. 126 * @param rootDir Indicates the root path name. 127 * @param path Indicates the path to be checked. 128 * @return Returns true if the path is valid successfully; returns false otherwise. 129 */ 130 static bool IsValidPath(const std::string &rootDir, const std::string &path); 131 /** 132 * @brief Check whether a path is valid code path. 133 * @param codePath Indicates the path to be checked. 134 * @return Returns true if the file renamed successfully; returns false otherwise. 135 */ 136 static bool IsValidCodePath(const std::string &codePath); 137 /** 138 * @brief Get the parent directory path of a file. 139 * @param codePath Indicates the file path. 140 * @return Returns the parent directory if get successfully; returns empty string otherwise. 141 */ 142 static std::string GetPathDir(const std::string &path); 143 /** 144 * @brief Delete files in a directory. 145 * @param path Indicates the directory path of the files to be deleted. 146 * @return Returns true if the files deleted successfully; returns false otherwise. 147 */ 148 static bool DeleteFiles(const std::string &dataPath); 149 /** 150 * @brief Delete files in a directory except the directories to be kept. 151 * @param dataPath Indicates the directory path of the files to be deleted. 152 * @param dirsToKeep Indicates the directories to be kept. 153 * @return Returns true if the files deleted successfully; returns false otherwise 154 */ 155 static bool DeleteFilesExceptDirs(const std::string &dataPath, const std::vector<std::string> &dirsToKeep); 156 /** 157 * @brief Make a directory and change the owner and group ID of it. 158 * @param path Indicates the directory path to be made. 159 * @param isReadByOthers Indicates the directory whether read by other users. 160 * @param uid Indicates the uid. 161 * @param uid Indicates the gid. 162 * @return Returns true if directory made successfully; returns false otherwise. 163 */ 164 static bool MkOwnerDir(const std::string &path, bool isReadByOthers, const int uid, const int gid); 165 /** 166 * @brief Make a directory and change the owner and group ID of it. 167 * @param path Indicates the directory path to be made. 168 * @param mode Indicates the directory mode. 169 * @param uid Indicates the uid. 170 * @param uid Indicates the gid. 171 * @return Returns true if directory made successfully; returns false otherwise. 172 */ 173 static bool MkOwnerDir(const std::string &path, int mode, const int uid, const int gid); 174 /** 175 * @brief Get disk usage for dir. 176 * @param dir Indicates the directory. 177 * @param size Indicates the disk size. 178 * @return Returns true if successfully; returns false otherwise. 179 */ 180 static int64_t GetDiskUsage(const std::string &dir, bool isRealPath = false); 181 /** 182 * @brief Traverse all cache directories. 183 * @param currentPath Indicates the current path. 184 * @param cacheDirs Indicates the cache directories. 185 * @return Returns true if successfully; returns false otherwise. 186 */ 187 static void TraverseCacheDirectory(const std::string ¤tPath, std::vector<std::string> &cacheDirs); 188 /** 189 * @brief Get disk usage from path. 190 * @param path Indicates the current path. 191 * @return Returns disk size. 192 */ 193 static int64_t GetDiskUsageFromPath(const std::vector<std::string> &path); 194 195 static bool InitialiseQuotaMounts(); 196 197 static int64_t GetDiskUsageFromQuota(const int32_t uid); 198 199 static bool ScanDir( 200 const std::string &dirPath, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 201 202 static bool ScanSoFiles(const std::string &newSoPath, const std::string &originPath, 203 const std::string ¤tPath, std::vector<std::string> &paths); 204 205 static bool CopyFile(const std::string &sourceFile, const std::string &destinationFile); 206 207 static bool CopyFileFast(const std::string &sourcePath, const std::string &destPath); 208 209 static bool ChangeDirOwnerRecursively(const std::string &path, const int uid, const int gid); 210 211 static bool IsDiffFiles(const std::string &entryName, 212 const std::string &targetPath, const std::string &cpuAbi); 213 214 static bool ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 215 const std::string &cpuAbi); 216 217 static bool ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 218 const std::string &newSoPath, int32_t uid); 219 220 static bool ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &fileVec); 221 222 static bool CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 223 224 static bool GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi, 225 std::vector<std::string> &fileNames); 226 227 #if defined(CODE_SIGNATURE_ENABLE) 228 static bool PrepareEntryMap(const CodeSignatureParam &codeSignatureParam, 229 const std::vector<std::string> &soEntryFiles, Security::CodeSign::EntryMap &entryMap); 230 static ErrCode PerformCodeSignatureCheck(const CodeSignatureParam &codeSignatureParam, 231 const Security::CodeSign::EntryMap &entryMap); 232 #endif 233 234 static bool VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam); 235 236 static bool CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 237 238 static bool CheckHapEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption); 239 240 static bool MoveFiles(const std::string &srcDir, const std::string &desDir, bool isDesDirNeedCreated = false); 241 242 static bool MoveFileOrDir(const std::string &srcPath, const std::string &destPath, mode_t mode); 243 244 static bool MoveFile(const std::string &srcPath, const std::string &destPath); 245 246 static bool ExtractDriverSoFiles(const std::string &srcPath, 247 const std::unordered_multimap<std::string, std::string> &dirMap); 248 249 static bool CopyDriverSoFiles(const BundleExtractor &extractor, const std::string &originalDir, 250 const std::string &destinedDir); 251 252 #if defined(CODE_ENCRYPTION_ENABLE) 253 static ErrCode ExtractSoFilesToTmpHapPath(const std::string &hapPath, const std::string &cpuAbi, 254 const std::string &tmpSoPath, int32_t uid); 255 256 static ErrCode ExtractSoFilesToTmpSoPath(const std::string &hapPath, const std::string &realSoFilesPath, 257 const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid); 258 259 static ErrCode DecryptSoFile(const std::string &hapPath, const std::string &tmpHapPath, int32_t uid, 260 uint32_t fileSize, uint32_t offset); 261 262 static ErrCode RemoveEncryptedKey(int32_t uid, const std::vector<std::string> &soList); 263 264 static int32_t CallIoctl(int32_t flag, int32_t associatedFlag, int32_t uid, int32_t &fd); 265 #endif 266 267 private: 268 static bool OpenHandle(void **handle); 269 270 static void CloseHandle(void **handle); 271 272 static bool ObtainNativeSoFile(const BundleExtractor &extractor, const std::string &cpuAbi, 273 std::vector<std::string> &soEntryFiles); 274 275 static bool ProcessApplyDiffPatchPath(const std::string &oldSoPath, const std::string &diffFilePath, 276 const std::string &newSoPath, std::vector<std::string> &oldSoFileNames, 277 std::vector<std::string> &diffFileNames); 278 static bool ExtractResourceFiles(const ExtractParam &extractParam, const BundleExtractor &extractor); 279 static bool CheckPathIsSame(const std::string &path, int32_t mode, const int32_t uid, const int32_t gid, 280 bool &isPathExist); 281 }; 282 } // namespace AppExecFwk 283 } // namespace OHOS 284 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H