1600cc4afSopenharmony_ci/* 2600cc4afSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3600cc4afSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4600cc4afSopenharmony_ci * you may not use this file except in compliance with the License. 5600cc4afSopenharmony_ci * You may obtain a copy of the License at 6600cc4afSopenharmony_ci * 7600cc4afSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8600cc4afSopenharmony_ci * 9600cc4afSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10600cc4afSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11600cc4afSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12600cc4afSopenharmony_ci * See the License for the specific language governing permissions and 13600cc4afSopenharmony_ci * limitations under the License. 14600cc4afSopenharmony_ci */ 15600cc4afSopenharmony_ci 16600cc4afSopenharmony_ci#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_UTIL_H 17600cc4afSopenharmony_ci#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_UTIL_H 18600cc4afSopenharmony_ci 19600cc4afSopenharmony_ci#include <mutex> 20600cc4afSopenharmony_ci#include <string> 21600cc4afSopenharmony_ci#include <vector> 22600cc4afSopenharmony_ci 23600cc4afSopenharmony_ci#include "appexecfwk_errors.h" 24600cc4afSopenharmony_ci#include "application_info.h" 25600cc4afSopenharmony_ci 26600cc4afSopenharmony_cinamespace OHOS { 27600cc4afSopenharmony_cinamespace AppExecFwk { 28600cc4afSopenharmony_cienum class DirType : uint8_t { 29600cc4afSopenharmony_ci STREAM_INSTALL_DIR = 0, 30600cc4afSopenharmony_ci QUICK_FIX_DIR = 1, 31600cc4afSopenharmony_ci SIG_FILE_DIR = 2, 32600cc4afSopenharmony_ci ABC_FILE_DIR = 3, 33600cc4afSopenharmony_ci PGO_FILE_DIR = 4, 34600cc4afSopenharmony_ci EXT_RESOURCE_FILE_DIR = 5, 35600cc4afSopenharmony_ci UNKNOWN 36600cc4afSopenharmony_ci}; 37600cc4afSopenharmony_ci 38600cc4afSopenharmony_ciclass BundleUtil { 39600cc4afSopenharmony_cipublic: 40600cc4afSopenharmony_ci /** 41600cc4afSopenharmony_ci * @brief Check whether a file is valid HAP file. 42600cc4afSopenharmony_ci * @param bundlePath Indicates the HAP file path. 43600cc4afSopenharmony_ci * @return Returns ERR_OK if the file checked successfully; returns error code otherwise. 44600cc4afSopenharmony_ci */ 45600cc4afSopenharmony_ci static ErrCode CheckFilePath(const std::string &bundlePath, std::string &realPath); 46600cc4afSopenharmony_ci /** 47600cc4afSopenharmony_ci * @brief Check whether an array of files are valid HAP files. 48600cc4afSopenharmony_ci * @param bundlePaths Indicates the HAP file paths. 49600cc4afSopenharmony_ci * @param realPaths Indicates the real paths of HAP files. 50600cc4afSopenharmony_ci * @return Returns ERR_OK if the file checked successfully; returns error code otherwise. 51600cc4afSopenharmony_ci */ 52600cc4afSopenharmony_ci static ErrCode CheckFilePath(const std::vector<std::string> &bundlePaths, std::vector<std::string> &realPaths); 53600cc4afSopenharmony_ci /** 54600cc4afSopenharmony_ci * @brief Check whether a file is the specific type file. 55600cc4afSopenharmony_ci * @param fileName Indicates the file path. 56600cc4afSopenharmony_ci * @param extensionName Indicates the type to be checked. 57600cc4afSopenharmony_ci * @return Returns true if the file type checked successfully; returns false otherwise. 58600cc4afSopenharmony_ci */ 59600cc4afSopenharmony_ci static bool CheckFileType(const std::string &fileName, const std::string &extensionName); 60600cc4afSopenharmony_ci /** 61600cc4afSopenharmony_ci * @brief Check whether a file name is valid. 62600cc4afSopenharmony_ci * @param fileName Indicates the file path. 63600cc4afSopenharmony_ci * @return Returns true if the file name checked successfully; returns false otherwise. 64600cc4afSopenharmony_ci */ 65600cc4afSopenharmony_ci static bool CheckFileName(const std::string &fileName); 66600cc4afSopenharmony_ci /** 67600cc4afSopenharmony_ci * @brief Check whether a Hap size is valid. 68600cc4afSopenharmony_ci * @param fileName Indicates the file path. 69600cc4afSopenharmony_ci * @return Returns true if the file size checked successfully; returns false otherwise. 70600cc4afSopenharmony_ci */ 71600cc4afSopenharmony_ci static bool CheckFileSize(const std::string &bundlePath, const int64_t fileSize); 72600cc4afSopenharmony_ci /** 73600cc4afSopenharmony_ci * @brief Check whether the disk path memory is available for installing the hap. 74600cc4afSopenharmony_ci * @param bundlePath Indicates the file path. 75600cc4afSopenharmony_ci * @param diskPath Indicates disk path in the system. 76600cc4afSopenharmony_ci * @return Returns true if the file size checked successfully; returns false otherwise. 77600cc4afSopenharmony_ci */ 78600cc4afSopenharmony_ci static bool CheckSystemSize(const std::string &bundlePath, const std::string &diskPath); 79600cc4afSopenharmony_ci 80600cc4afSopenharmony_ci static bool CheckSystemFreeSize(const std::string &path, int64_t size); 81600cc4afSopenharmony_ci 82600cc4afSopenharmony_ci /** 83600cc4afSopenharmony_ci * @brief Insufficient disk space reported 84600cc4afSopenharmony_ci * @param path Indicates the file path. 85600cc4afSopenharmony_ci * @param fileName Indicates the file path. 86600cc4afSopenharmony_ci */ 87600cc4afSopenharmony_ci static bool CheckSystemSizeAndHisysEvent(const std::string &path, const std::string &fileName); 88600cc4afSopenharmony_ci 89600cc4afSopenharmony_ci /** 90600cc4afSopenharmony_ci * @brief to obtain the hap paths of the input bundle path. 91600cc4afSopenharmony_ci * @param currentBundlePath Indicates the current bundle path. 92600cc4afSopenharmony_ci * @param hapFileList Indicates the hap paths. 93600cc4afSopenharmony_ci * @return Returns true if the hap path obtained successfully; returns false otherwise. 94600cc4afSopenharmony_ci */ 95600cc4afSopenharmony_ci static bool GetHapFilesFromBundlePath(const std::string& currentBundlePath, std::vector<std::string>& hapFileList); 96600cc4afSopenharmony_ci /** 97600cc4afSopenharmony_ci * @brief to obtain the current time. 98600cc4afSopenharmony_ci * @return Returns current time. 99600cc4afSopenharmony_ci */ 100600cc4afSopenharmony_ci static int64_t GetCurrentTime(); 101600cc4afSopenharmony_ci /** 102600cc4afSopenharmony_ci * @brief to obtain the current time in ms. 103600cc4afSopenharmony_ci * @return Returns current time. 104600cc4afSopenharmony_ci */ 105600cc4afSopenharmony_ci static int64_t GetCurrentTimeMs(); 106600cc4afSopenharmony_ci /** 107600cc4afSopenharmony_ci * @brief to obtain the current time in ns. 108600cc4afSopenharmony_ci * @return Returns current time. 109600cc4afSopenharmony_ci */ 110600cc4afSopenharmony_ci static int64_t GetCurrentTimeNs(); 111600cc4afSopenharmony_ci /** 112600cc4afSopenharmony_ci * @brief key combination of deviceId and bundleName. 113600cc4afSopenharmony_ci * @param deviceId Indicates the deviceId. 114600cc4afSopenharmony_ci * @param bundleName Indicates the bundle name. 115600cc4afSopenharmony_ci * @param key Indicates the key. 116600cc4afSopenharmony_ci */ 117600cc4afSopenharmony_ci static void DeviceAndNameToKey( 118600cc4afSopenharmony_ci const std::string &deviceId, const std::string &bundleName, std::string &key); 119600cc4afSopenharmony_ci /** 120600cc4afSopenharmony_ci * @brief The key is parsed into deviceId and bundleName. 121600cc4afSopenharmony_ci * @param key Indicates the key. 122600cc4afSopenharmony_ci * @param deviceId Indicates the deviceId. 123600cc4afSopenharmony_ci * @param bundleName Indicates the bundle name. 124600cc4afSopenharmony_ci * @return Returns result. 125600cc4afSopenharmony_ci */ 126600cc4afSopenharmony_ci static bool KeyToDeviceAndName( 127600cc4afSopenharmony_ci const std::string &key, std::string &deviceId, std::string &bundleName); 128600cc4afSopenharmony_ci /** 129600cc4afSopenharmony_ci * @brief get userId by callinguid. 130600cc4afSopenharmony_ci * @return Returns userId. 131600cc4afSopenharmony_ci */ 132600cc4afSopenharmony_ci static int32_t GetUserIdByCallingUid(); 133600cc4afSopenharmony_ci /** 134600cc4afSopenharmony_ci * @brief get userId by uid. 135600cc4afSopenharmony_ci * @param uid Indicates uid. 136600cc4afSopenharmony_ci * @return Returns userId. 137600cc4afSopenharmony_ci */ 138600cc4afSopenharmony_ci static int32_t GetUserIdByUid(int32_t uid); 139600cc4afSopenharmony_ci /** 140600cc4afSopenharmony_ci * @brief Is file exist. 141600cc4afSopenharmony_ci * @param path Indicates path. 142600cc4afSopenharmony_ci * @return Returns result. 143600cc4afSopenharmony_ci */ 144600cc4afSopenharmony_ci static bool IsExistFile(const std::string &path); 145600cc4afSopenharmony_ci /** 146600cc4afSopenharmony_ci * @brief Is file exist. 147600cc4afSopenharmony_ci * @param path Indicates path. 148600cc4afSopenharmony_ci * @return Returns result. 149600cc4afSopenharmony_ci */ 150600cc4afSopenharmony_ci static bool IsExistFileNoLog(const std::string &path); 151600cc4afSopenharmony_ci /** 152600cc4afSopenharmony_ci * @brief Is dir exist. 153600cc4afSopenharmony_ci * @param path Indicates path. 154600cc4afSopenharmony_ci * @return Returns result. 155600cc4afSopenharmony_ci */ 156600cc4afSopenharmony_ci static bool IsExistDir(const std::string &path); 157600cc4afSopenharmony_ci /** 158600cc4afSopenharmony_ci * @brief Is dir exist. 159600cc4afSopenharmony_ci * @param path Indicates path. 160600cc4afSopenharmony_ci * @return Returns result. 161600cc4afSopenharmony_ci */ 162600cc4afSopenharmony_ci static bool IsExistDirNoLog(const std::string &path); 163600cc4afSopenharmony_ci /** 164600cc4afSopenharmony_ci * @brief Rename file from oldPath to newPath. 165600cc4afSopenharmony_ci * @param oldPath Indicates oldPath. 166600cc4afSopenharmony_ci * @param newPath Indicates newPath. 167600cc4afSopenharmony_ci * @return Returns result. 168600cc4afSopenharmony_ci */ 169600cc4afSopenharmony_ci static bool RenameFile(const std::string &oldPath, const std::string &newPath); 170600cc4afSopenharmony_ci /** 171600cc4afSopenharmony_ci * @brief Copy file from oldPath to newPath. 172600cc4afSopenharmony_ci * @param oldPath Indicates oldPath. 173600cc4afSopenharmony_ci * @param newPath Indicates newPath. 174600cc4afSopenharmony_ci * @return Returns result. 175600cc4afSopenharmony_ci */ 176600cc4afSopenharmony_ci static bool CopyFile( 177600cc4afSopenharmony_ci const std::string &oldPath, const std::string &newPath); 178600cc4afSopenharmony_ci 179600cc4afSopenharmony_ci static bool CopyFileFast(const std::string &sourcePath, const std::string &destPath); 180600cc4afSopenharmony_ci /** 181600cc4afSopenharmony_ci * @brief Delete all dir or file. 182600cc4afSopenharmony_ci * @param path Indicates sourceStr. 183600cc4afSopenharmony_ci * @return Returns result. 184600cc4afSopenharmony_ci */ 185600cc4afSopenharmony_ci static bool DeleteDir(const std::string &path); 186600cc4afSopenharmony_ci static bool IsUtd(const std::string ¶m); 187600cc4afSopenharmony_ci static bool IsSpecificUtd(const std::string ¶m); 188600cc4afSopenharmony_ci static std::vector<std::string> GetUtdVectorByMimeType(const std::string &mimeType); 189600cc4afSopenharmony_ci static std::string GetBoolStrVal(bool val); 190600cc4afSopenharmony_ci static void MakeFsConfig(const std::string &bundleName, int32_t bundleId, const std::string &configPath); 191600cc4afSopenharmony_ci static void RemoveFsConfig(const std::string &bundleName, const std::string &configPath); 192600cc4afSopenharmony_ci static std::string CreateInstallTempDir(uint32_t installerId, const DirType &type); 193600cc4afSopenharmony_ci static std::string CreateSharedBundleTempDir(uint32_t installerId, uint32_t index); 194600cc4afSopenharmony_ci static int32_t CreateFileDescriptor(const std::string &bundlePath, long long offset); 195600cc4afSopenharmony_ci static int32_t CreateFileDescriptorForReadOnly(const std::string &bundlePath, long long offset); 196600cc4afSopenharmony_ci static void CloseFileDescriptor(std::vector<int32_t> &fdVec); 197600cc4afSopenharmony_ci static Resource GetResource(const std::string &bundleName, const std::string &moduleName, uint32_t resId); 198600cc4afSopenharmony_ci static bool CreateDir(const std::string &dir); 199600cc4afSopenharmony_ci static bool RevertToRealPath(const std::string &sandBoxPath, const std::string &bundleName, std::string &realPath); 200600cc4afSopenharmony_ci static bool StartWith(const std::string &source, const std::string &suffix); 201600cc4afSopenharmony_ci static bool EndWith(const std::string &source, const std::string &suffix); 202600cc4afSopenharmony_ci static int64_t GetFileSize(const std::string &filePath); 203600cc4afSopenharmony_ci static int64_t CalculateFileSize(const std::string &bundlePath); 204600cc4afSopenharmony_ci static std::string CreateTempDir(const std::string &tempDir); 205600cc4afSopenharmony_ci static std::string CopyFileToSecurityDir(const std::string &filePath, const DirType &dirType, 206600cc4afSopenharmony_ci std::vector<std::string> &toDeletePaths); 207600cc4afSopenharmony_ci static void DeleteTempDirs(const std::vector<std::string> &tempDirs); 208600cc4afSopenharmony_ci static std::string GenerateUuid(); 209600cc4afSopenharmony_ci static std::string GetHexHash(const std::string &s); 210600cc4afSopenharmony_ci static void RecursiveHash(std::string& s); 211600cc4afSopenharmony_ci static std::string ExtractGroupIdByDevelopId(const std::string &developerId); 212600cc4afSopenharmony_ci static std::string ToString(const std::vector<std::string> &vector); 213600cc4afSopenharmony_ci static std::string GetNoDisablingConfigPath(); 214600cc4afSopenharmony_ci static std::string GenerateUuidByKey(const std::string &key); 215600cc4afSopenharmony_ciprivate: 216600cc4afSopenharmony_ci static std::mutex g_mutex; 217600cc4afSopenharmony_ci}; 218600cc4afSopenharmony_ci} // namespace AppExecFwk 219600cc4afSopenharmony_ci} // namespace OHOS 220600cc4afSopenharmony_ci#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_UTIL_H 221