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_BUNDLE_UTIL_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_UTIL_H 18 19 #include <mutex> 20 #include <string> 21 #include <unordered_map> 22 #include <unordered_set> 23 #include <vector> 24 25 #include "appexecfwk_errors.h" 26 #include "application_info.h" 27 #include "nlohmann/json.hpp" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 enum class DirType : uint8_t { 32 STREAM_INSTALL_DIR = 0, 33 QUICK_FIX_DIR = 1, 34 SIG_FILE_DIR = 2, 35 ABC_FILE_DIR = 3, 36 PGO_FILE_DIR = 4, 37 EXT_RESOURCE_FILE_DIR = 5, 38 EXT_PROFILE_DIR = 6, 39 UNKNOWN 40 }; 41 42 class BundleUtil { 43 public: 44 /** 45 * @brief Check whether a file is valid HAP file. 46 * @param bundlePath Indicates the HAP file path. 47 * @return Returns ERR_OK if the file checked successfully; returns error code otherwise. 48 */ 49 static ErrCode CheckFilePath(const std::string &bundlePath, std::string &realPath); 50 /** 51 * @brief Check whether an array of files are valid HAP files. 52 * @param bundlePaths Indicates the HAP file paths. 53 * @param realPaths Indicates the real paths of HAP files. 54 * @return Returns ERR_OK if the file checked successfully; returns error code otherwise. 55 */ 56 static ErrCode CheckFilePath(const std::vector<std::string> &bundlePaths, std::vector<std::string> &realPaths); 57 /** 58 * @brief Check whether a file is the specific type file. 59 * @param fileName Indicates the file path. 60 * @param extensionName Indicates the type to be checked. 61 * @return Returns true if the file type checked successfully; returns false otherwise. 62 */ 63 static bool CheckFileType(const std::string &fileName, const std::string &extensionName); 64 /** 65 * @brief Check whether a file name is valid. 66 * @param fileName Indicates the file path. 67 * @return Returns true if the file name checked successfully; returns false otherwise. 68 */ 69 static bool CheckFileName(const std::string &fileName); 70 /** 71 * @brief Check whether a Hap size is valid. 72 * @param fileName Indicates the file path. 73 * @return Returns true if the file size checked successfully; returns false otherwise. 74 */ 75 static bool CheckFileSize(const std::string &bundlePath, const int64_t fileSize); 76 /** 77 * @brief Check whether the disk path memory is available for installing the hap. 78 * @param bundlePath Indicates the file path. 79 * @param diskPath Indicates disk path in the system. 80 * @return Returns true if the file size checked successfully; returns false otherwise. 81 */ 82 static bool CheckSystemSize(const std::string &bundlePath, const std::string &diskPath); 83 84 static bool CheckSystemFreeSize(const std::string &path, int64_t size); 85 86 /** 87 * @brief Insufficient disk space reported 88 * @param path Indicates the file path. 89 * @param fileName Indicates the file path. 90 */ 91 static bool CheckSystemSizeAndHisysEvent(const std::string &path, const std::string &fileName); 92 93 /** 94 * @brief to obtain the hap paths of the input bundle path. 95 * @param currentBundlePath Indicates the current bundle path. 96 * @param hapFileList Indicates the hap paths. 97 * @return Returns true if the hap path obtained successfully; returns false otherwise. 98 */ 99 static bool GetHapFilesFromBundlePath(const std::string& currentBundlePath, std::vector<std::string>& hapFileList); 100 /** 101 * @brief to obtain the current time. 102 * @return Returns current time. 103 */ 104 static int64_t GetCurrentTime(); 105 /** 106 * @brief to obtain the current time in ms. 107 * @return Returns current time. 108 */ 109 static int64_t GetCurrentTimeMs(); 110 /** 111 * @brief to obtain the current time in ns. 112 * @return Returns current time. 113 */ 114 static int64_t GetCurrentTimeNs(); 115 /** 116 * @brief key combination of deviceId and bundleName. 117 * @param deviceId Indicates the deviceId. 118 * @param bundleName Indicates the bundle name. 119 * @param key Indicates the key. 120 */ 121 static void DeviceAndNameToKey( 122 const std::string &deviceId, const std::string &bundleName, std::string &key); 123 /** 124 * @brief The key is parsed into deviceId and bundleName. 125 * @param key Indicates the key. 126 * @param deviceId Indicates the deviceId. 127 * @param bundleName Indicates the bundle name. 128 * @return Returns result. 129 */ 130 static bool KeyToDeviceAndName( 131 const std::string &key, std::string &deviceId, std::string &bundleName); 132 /** 133 * @brief get userId by callinguid. 134 * @return Returns userId. 135 */ 136 static int32_t GetUserIdByCallingUid(); 137 /** 138 * @brief get userId by uid. 139 * @param uid Indicates uid. 140 * @return Returns userId. 141 */ 142 static int32_t GetUserIdByUid(int32_t uid); 143 /** 144 * @brief Is file exist. 145 * @param path Indicates path. 146 * @return Returns result. 147 */ 148 static bool IsExistFile(const std::string &path); 149 /** 150 * @brief Is file exist. 151 * @param path Indicates path. 152 * @return Returns result. 153 */ 154 static bool IsExistFileNoLog(const std::string &path); 155 /** 156 * @brief Is dir exist. 157 * @param path Indicates path. 158 * @return Returns result. 159 */ 160 static bool IsExistDir(const std::string &path); 161 /** 162 * @brief Is dir exist. 163 * @param path Indicates path. 164 * @return Returns result. 165 */ 166 static bool IsExistDirNoLog(const std::string &path); 167 168 static bool IsPathInformationConsistent(const std::string &path, int32_t uid, int32_t gid); 169 170 /** 171 * @brief Rename file from oldPath to newPath. 172 * @param oldPath Indicates oldPath. 173 * @param newPath Indicates newPath. 174 * @return Returns result. 175 */ 176 static bool RenameFile(const std::string &oldPath, const std::string &newPath); 177 /** 178 * @brief Copy file from oldPath to newPath. 179 * @param oldPath Indicates oldPath. 180 * @param newPath Indicates newPath. 181 * @return Returns result. 182 */ 183 static bool CopyFile( 184 const std::string &oldPath, const std::string &newPath); 185 186 static bool CopyFileFast(const std::string &sourcePath, const std::string &destPath, const bool needFsync = false); 187 /** 188 * @brief Delete all dir or file. 189 * @param path Indicates sourceStr. 190 * @return Returns result. 191 */ 192 static bool DeleteDir(const std::string &path); 193 /** 194 * @brief set the bit to 1. 195 * @param pos Indicates the position. 196 * @param num Indicates the value to be processed. 197 */ 198 static void SetBit(const uint8_t pos, uint8_t &num); 199 /** 200 * @brief set the bit to 0. 201 * @param pos Indicates the position. 202 * @param num Indicates the value to be processed. 203 */ 204 static void ResetBit(const uint8_t pos, uint8_t &num); 205 /** 206 * @brief get the bit value. 207 * @param pos Indicates the position. 208 * @param num Indicates the value to be processed. 209 * @return If the bit value is 1, return true; return false otherwise. 210 */ 211 static bool GetBitValue(const uint8_t num, const uint8_t pos); 212 static bool IsUtd(const std::string ¶m); 213 static bool IsSpecificUtd(const std::string ¶m); 214 static std::vector<std::string> GetUtdVectorByMimeType(const std::string &mimeType); 215 static std::string GetBoolStrVal(bool val); 216 static void MakeFsConfig(const std::string &bundleName, int32_t bundleId, const std::string &configPath); 217 static void MakeFsConfig(const std::string &bundleName, const std::string &configPath, 218 const std::string labelValue, const std::string labelPath); 219 static void RemoveFsConfig(const std::string &bundleName, const std::string &configPath); 220 static std::string CreateInstallTempDir(uint32_t installerId, const DirType &type); 221 static std::string CreateSharedBundleTempDir(uint32_t installerId, uint32_t index); 222 static int32_t CreateFileDescriptor(const std::string &bundlePath, long long offset); 223 static int32_t CreateFileDescriptorForReadOnly(const std::string &bundlePath, long long offset); 224 static void CloseFileDescriptor(std::vector<int32_t> &fdVec); 225 static Resource GetResource(const std::string &bundleName, const std::string &moduleName, uint32_t resId); 226 static bool CreateDir(const std::string &dir); 227 static bool RevertToRealPath(const std::string &sandBoxPath, const std::string &bundleName, std::string &realPath); 228 static bool StartWith(const std::string &source, const std::string &suffix); 229 static bool EndWith(const std::string &source, const std::string &suffix); 230 static int64_t GetFileSize(const std::string &filePath); 231 static int64_t CalculateFileSize(const std::string &bundlePath); 232 static std::string CreateTempDir(const std::string &tempDir); 233 static std::string CopyFileToSecurityDir(const std::string &filePath, const DirType &dirType, 234 std::vector<std::string> &toDeletePaths, bool rename = false); 235 static std::string GetAppInstallPrefix(const std::string &bundleName, bool rename); 236 static void RestoreAppInstallHaps(); 237 static void RestoreHaps(const std::string &sourcePath, const std::string &bundleName, const std::string &userId); 238 static void DeleteTempDirs(const std::vector<std::string> &tempDirs); 239 static std::string ExtractGroupIdByDevelopId(const std::string &developerId); 240 static std::string ToString(const std::vector<std::string> &vector); 241 static std::string GetNoDisablingConfigPath(); 242 static uint32_t ExtractNumberFromString(nlohmann::json &jsonObject, const std::string &key); 243 static std::vector<uint8_t> GenerateRandomNumbers(uint8_t size, uint8_t lRange, uint8_t rRange); 244 static bool IsSandBoxPath(const std::string &path); 245 static bool StrToUint32(const std::string &str, uint32_t &value); 246 static std::string ExtractStringFromJson(nlohmann::json &jsonObject, const std::string &key); 247 static std::unordered_map<std::string, std::string> ParseMapFromJson(const std::string &jsonStr); 248 private: 249 static std::mutex g_mutex; 250 }; 251 } // namespace AppExecFwk 252 } // namespace OHOS 253 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_UTIL_H 254