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