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