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_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 "appexecfwk_errors.h" 24 #include "bundle_extractor.h" 25 #include "installd/installd_constants.h" 26 #include "ipc/extract_param.h" 27 #include "nocopyable.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 class InstalldOperator { 32 public: 33 /** 34 * @brief Check whether a file exist. 35 * @param path Indicates the file path to be checked. 36 * @return Returns true if the file exist; returns false otherwise. 37 */ 38 static bool IsExistFile(const std::string &path); 39 /** 40 * @brief Check whether a directory exist. 41 * @param path Indicates the directory path to be checked. 42 * @return Returns true if the directory exist; returns false otherwise. 43 */ 44 static bool IsExistDir(const std::string &path); 45 /** 46 * @brief Check whether a directory is empty. 47 * @param dir Indicates the directory path to be checked. 48 * @return Returns true if the directory is empty; returns false otherwise. 49 */ 50 static bool IsDirEmpty(const std::string &dir); 51 /** 52 * @brief Make a new directory including the parent path if not exist. 53 * @param path Indicates the directory path to be checked. 54 * @param isReadByOthers Indicates the directory whether read by other users. 55 * @return Returns true if the directory make successfully; returns false otherwise. 56 */ 57 static bool MkRecursiveDir(const std::string &path, bool isReadByOthers); 58 /** 59 * @brief Delete a directory. 60 * @param path Indicates the directory path to be deleted. 61 * @return Returns true if the directory deleted successfully; returns false otherwise. 62 */ 63 static bool DeleteDir(const std::string &path); 64 /** 65 * @brief Extract the files of a compressed package to a specific directory. 66 * @param srcModulePath Indicates the package file path. 67 * @param targetPath normal files decompression path. 68 * @param targetSoPath so files decompression path. 69 * @param cpuAbi cpuAbi. 70 * @return Returns true if the package extracted successfully; returns false otherwise. 71 */ 72 static bool ExtractFiles(const std::string &sourcePath, const std::string &targetPath, 73 const std::string &targetSoPath, const std::string &cpuAbi); 74 75 static bool IsNativeSo(const std::string &entryName, const std::string &targetSoPath, const std::string &cpuAbi); 76 77 static bool ExtractFiles(const ExtractParam &extractParam); 78 static void ExtractTargetFile( 79 const BundleExtractor &extractor, 80 const std::string &entryName, 81 const std::string &targetPath, 82 const std::string &cpuAbi, 83 const ExtractFileType &extractFileType = ExtractFileType::SO); 84 static bool IsNativeFile( 85 const std::string &entryName, const ExtractParam &extractParam); 86 87 /** 88 * @brief Rename a directory from old path to new path. 89 * @param oldPath Indicates the old path name. 90 * @param newPath Indicates the new path name. 91 * @return Returns true if the directory renamed successfully; returns false otherwise. 92 */ 93 static bool RenameDir(const std::string &oldPath, const std::string &newPath); 94 /** 95 * @brief Change the owner and group ID of a file or directory. 96 * @param filePath Indicates the file or directory path. 97 * @param uid Indicates the uid. 98 * @param uid Indicates the gid. 99 * @return Returns true if changed successfully; returns false otherwise. 100 */ 101 static bool ChangeFileAttr(const std::string &filePath, const int uid, const int gid); 102 /** 103 * @brief Rename a file 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 file renamed successfully; returns false otherwise. 107 */ 108 static bool RenameFile(const std::string &oldPath, const std::string &newPath); 109 /** 110 * @brief Check whether a path is valid under a root path. 111 * @param rootDir Indicates the root path name. 112 * @param path Indicates the path to be checked. 113 * @return Returns true if the path is valid successfully; returns false otherwise. 114 */ 115 static bool IsValidPath(const std::string &rootDir, const std::string &path); 116 /** 117 * @brief Check whether a path is valid code path. 118 * @param codePath Indicates the path to be checked. 119 * @return Returns true if the file renamed successfully; returns false otherwise. 120 */ 121 static bool IsValidCodePath(const std::string &codePath); 122 /** 123 * @brief Get the parent directory path of a file. 124 * @param codePath Indicates the file path. 125 * @return Returns the parent directory if get successfully; returns empty string otherwise. 126 */ 127 static std::string GetPathDir(const std::string &path); 128 /** 129 * @brief Delete files in a directory. 130 * @param path Indicates the directory path of the files to be deleted. 131 * @return Returns true if the files deleted successfully; returns false otherwise. 132 */ 133 static bool DeleteFiles(const std::string &dataPath); 134 /** 135 * @brief Make a directory and change the owner and group ID of it. 136 * @param path Indicates the directory path to be made. 137 * @param isReadByOthers Indicates the directory whether read by other users. 138 * @param uid Indicates the uid. 139 * @param uid Indicates the gid. 140 * @return Returns true if directory made successfully; returns false otherwise. 141 */ 142 static bool MkOwnerDir(const std::string &path, bool isReadByOthers, const int uid, const int gid); 143 /** 144 * @brief Make a directory and change the owner and group ID of it. 145 * @param path Indicates the directory path to be made. 146 * @param mode Indicates the directory mode. 147 * @param uid Indicates the uid. 148 * @param uid Indicates the gid. 149 * @return Returns true if directory made successfully; returns false otherwise. 150 */ 151 static bool MkOwnerDir(const std::string &path, int mode, const int uid, const int gid); 152 /** 153 * @brief Get disk usage for dir. 154 * @param dir Indicates the directory. 155 * @param size Indicates the disk size. 156 * @return Returns true if successfully; returns false otherwise. 157 */ 158 static int64_t GetDiskUsage(const std::string &dir); 159 /** 160 * @brief Traverse all cache directories. 161 * @param currentPath Indicates the current path. 162 * @param cacheDirs Indicates the cache directories. 163 * @return Returns true if successfully; returns false otherwise. 164 */ 165 static void TraverseCacheDirectory(const std::string ¤tPath, std::vector<std::string> &cacheDirs); 166 /** 167 * @brief Get disk usage from path. 168 * @param path Indicates the current path. 169 * @return Returns disk size. 170 */ 171 static int64_t GetDiskUsageFromPath(const std::vector<std::string> &path); 172 173 static bool ScanDir( 174 const std::string &dirPath, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths); 175 176 static bool CopyFile(const std::string &sourceFile, const std::string &destinationFile); 177 178 static bool ChangeDirOwnerRecursively(const std::string &path, const int uid, const int gid); 179 180 static bool IsDiffFiles(const std::string &entryName, 181 const std::string &targetPath, const std::string &cpuAbi); 182 183 static bool ExtractDiffFiles(const std::string &filePath, const std::string &targetPath, 184 const std::string &cpuAbi); 185 186 static bool ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath, 187 const std::string &newSoPath); 188 189 static bool ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &fileVec); 190 191 static bool CopyFiles(const std::string &sourceDir, const std::string &destinationDir); 192 193 private: 194 static bool OpenHandle(void **handle); 195 196 static void CloseHandle(void **handle); 197 198 static bool ProcessApplyDiffPatchPath(const std::string &oldSoPath, const std::string &diffFilePath, 199 const std::string &newSoPath, std::vector<std::string> &oldSoFileNames, 200 std::vector<std::string> &diffFileNames); 201 }; 202 } // namespace AppExecFwk 203 } // namespace OHOS 204 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H