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