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 {
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      * @brief to obtain the hap paths of the input bundle path.
90      * @param currentBundlePath Indicates the current bundle path.
91      * @param hapFileList Indicates the hap paths.
92      * @return Returns true if the hap path obtained successfully; returns false otherwise.
93      */
94     static bool GetHapFilesFromBundlePath(const std::string& currentBundlePath, std::vector<std::string>& hapFileList);
95     /**
96      * @brief to obtain the current time.
97      * @return Returns current time.
98      */
99     static int64_t GetCurrentTime();
100     /**
101      * @brief to obtain the current time in ms.
102      * @return Returns current time.
103      */
104     static int64_t GetCurrentTimeMs();
105     /**
106      * @brief to obtain the current time in ns.
107      * @return Returns current time.
108      */
109     static int64_t GetCurrentTimeNs();
110     /**
111      * @brief key combination of deviceId and bundleName.
112      * @param deviceId Indicates the deviceId.
113      * @param bundleName Indicates the bundle name.
114      * @param key Indicates the key.
115      */
116     static void DeviceAndNameToKey(
117         const std::string &deviceId, const std::string &bundleName, std::string &key);
118     /**
119      * @brief The key is parsed into deviceId and bundleName.
120      * @param key Indicates the key.
121      * @param deviceId Indicates the deviceId.
122      * @param bundleName Indicates the bundle name.
123      * @return Returns result.
124      */
125     static bool KeyToDeviceAndName(
126         const std::string &key, std::string &deviceId, std::string &bundleName);
127     /**
128      * @brief get userId by callinguid.
129      * @return Returns userId.
130      */
131     static int32_t GetUserIdByCallingUid();
132     /**
133      * @brief get userId by uid.
134      * @param uid Indicates uid.
135      * @return Returns userId.
136      */
137     static int32_t GetUserIdByUid(int32_t uid);
138     /**
139      * @brief Is file exist.
140      * @param path Indicates path.
141      * @return Returns result.
142      */
143     static bool IsExistFile(const std::string &path);
144     /**
145      * @brief Is file exist.
146      * @param path Indicates path.
147      * @return Returns result.
148      */
149     static bool IsExistFileNoLog(const std::string &path);
150     /**
151      * @brief Is dir exist.
152      * @param path Indicates path.
153      * @return Returns result.
154      */
155     static bool IsExistDir(const std::string &path);
156     /**
157      * @brief Is dir exist.
158      * @param path Indicates path.
159      * @return Returns result.
160      */
161     static bool IsExistDirNoLog(const std::string &path);
162 
163     static bool IsPathInformationConsistent(const std::string &path, int32_t uid, int32_t gid);
164 
165     /**
166      * @brief Rename file from oldPath to newPath.
167      * @param oldPath Indicates oldPath.
168      * @param newPath Indicates newPath.
169      * @return Returns result.
170      */
171     static bool RenameFile(const std::string &oldPath, const std::string &newPath);
172     /**
173      * @brief Copy file from oldPath to newPath.
174      * @param oldPath Indicates oldPath.
175      * @param newPath Indicates newPath.
176      * @return Returns result.
177      */
178     static bool CopyFile(
179         const std::string &oldPath, const std::string &newPath);
180 
181     static bool CopyFileFast(const std::string &sourcePath, const std::string &destPath);
182     /**
183      * @brief Delete all dir or file.
184      * @param path Indicates sourceStr.
185      * @return Returns result.
186      */
187     static bool DeleteDir(const std::string &path);
188     static bool IsUtd(const std::string &param);
189     static bool IsSpecificUtd(const std::string &param);
190     static std::vector<std::string> GetUtdVectorByMimeType(const std::string &mimeType);
191     static std::string GetBoolStrVal(bool val);
192     static void MakeFsConfig(const std::string &bundleName, int32_t bundleId, const std::string &configPath);
193     static void RemoveFsConfig(const std::string &bundleName, const std::string &configPath);
194     static std::string CreateInstallTempDir(uint32_t installerId, const DirType &type);
195     static std::string CreateSharedBundleTempDir(uint32_t installerId, uint32_t index);
196     static int32_t CreateFileDescriptor(const std::string &bundlePath, long long offset);
197     static int32_t CreateFileDescriptorForReadOnly(const std::string &bundlePath, long long offset);
198     static void CloseFileDescriptor(std::vector<int32_t> &fdVec);
199     static Resource GetResource(const std::string &bundleName, const std::string &moduleName, uint32_t resId);
200     static bool CreateDir(const std::string &dir);
201     static bool RevertToRealPath(const std::string &sandBoxPath, const std::string &bundleName, std::string &realPath);
202     static bool StartWith(const std::string &source, const std::string &suffix);
203     static bool EndWith(const std::string &source, const std::string &suffix);
204     static int64_t GetFileSize(const std::string &filePath);
205     static int64_t CalculateFileSize(const std::string &bundlePath);
206     static std::string CreateTempDir(const std::string &tempDir);
207     static std::string CopyFileToSecurityDir(const std::string &filePath, const DirType &dirType,
208         std::vector<std::string> &toDeletePaths, bool rename = false);
209     static void DeleteTempDirs(const std::vector<std::string> &tempDirs);
210     static std::string GenerateUuid();
211     static std::string GetHexHash(const std::string &s);
212     static void RecursiveHash(std::string& s);
213     static std::string ExtractGroupIdByDevelopId(const std::string &developerId);
214     static std::string ToString(const std::vector<std::string> &vector);
215     static std::string GetNoDisablingConfigPath();
216     static std::string GenerateUuidByKey(const std::string &key);
217 private:
218     static std::mutex g_mutex;
219 };
220 }  // namespace AppExecFwk
221 }  // namespace OHOS
222 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_UTIL_H
223