• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_INSTALLD_OPERATOR_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H
18 
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "aot/aot_args.h"
24 #include "appexecfwk_errors.h"
25 #include "bundle_extractor.h"
26 #include "code_sign_helper.h"
27 #include "installd/installd_constants.h"
28 #include "ipc/check_encryption_param.h"
29 #include "ipc/code_signature_param.h"
30 #include "ipc/encryption_param.h"
31 #include "ipc/extract_param.h"
32 #include "nocopyable.h"
33 
34 namespace OHOS {
35 namespace AppExecFwk {
36 using EnforceMetadataProcessForApp = int32_t (*)(const std::unordered_map<std::string, std::string> &,
37     const CodeCryptoHapInfo &, bool &);
38 
39 class InstalldOperator {
40 public:
41     /**
42      * @brief Check link file and unlink.
43      * @param path Indicates the file path to be checked.
44      * @return Returns true if the file is link and unlink succeed; returns false otherwise.
45      */
46     static bool CheckAndDeleteLinkFile(const std::string &path);
47     /**
48      * @brief Check whether a file exist.
49      * @param path Indicates the file path to be checked.
50      * @return Returns true if the file exist; returns false otherwise.
51      */
52     static bool IsExistFile(const std::string &path);
53     /**
54      * @brief Check whether an AP file exists in the current directory of the file.
55      * @param path Indicates the file path to be checked.
56      * @return Returns true if the file exist; returns false otherwise.
57      */
58     static bool IsExistApFile(const std::string &path);
59     /**
60      * @brief Check whether a directory exist.
61      * @param path Indicates the directory path to be checked.
62      * @return Returns true if the directory exist; returns false otherwise.
63      */
64     static bool IsExistDir(const std::string &path);
65     /**
66      * @brief Check whether a directory is empty.
67      * @param dir Indicates the directory path to be checked.
68      * @return Returns true if the directory is empty; returns false otherwise.
69      */
70     static bool IsDirEmpty(const std::string &dir);
71     /**
72      * @brief Make a new directory including the parent path if not exist.
73      * @param path Indicates the directory path to be checked.
74      * @param isReadByOthers Indicates the directory whether read by other users.
75      * @return Returns true if the directory make successfully; returns false otherwise.
76      */
77     static bool MkRecursiveDir(const std::string &path, bool isReadByOthers);
78     /**
79      * @brief Delete a directory.
80      * @param path Indicates the directory path to be deleted.
81      * @return Returns true if the directory deleted successfully; returns false otherwise.
82      */
83     static bool DeleteDir(const std::string &path);
84 
85     static bool DeleteDirFast(const std::string &path);
86 
87     static bool DeleteDirFlexible(const std::string &path, const bool async);
88 
89     static bool DeleteUninstallTmpDir(const std::string &path);
90     /**
91      * @brief Extract the files of a compressed package to a specific directory.
92      * @param srcModulePath Indicates the package file path.
93      * @param targetSoPath so files decompression path.
94      * @param cpuAbi cpuAbi.
95      * @return Returns true if the package extracted successfully; returns false otherwise.
96      */
97     static bool ExtractFiles(const std::string &sourcePath, const std::string &targetSoPath,
98         const std::string &cpuAbi);
99 
100     static bool IsNativeSo(const std::string &entryName, const std::string &cpuAbi);
101 
102     static bool ExtractFiles(const ExtractParam &extractParam);
103     static bool ExtractFiles(const std::string hnpPackageInfo, const ExtractParam &extractParam);
104     static void ExtractTargetFile(
105         const BundleExtractor &extractor,
106         const std::string &entryName,
107         const ExtractParam &param);
108     static void ExtractTargetHnpFile(
109         const BundleExtractor &extractor,
110         const std::string &entryName,
111         const std::string &targetPath,
112         const ExtractFileType &extractFileType = ExtractFileType::SO);
113     static bool ProcessBundleInstallNative(
114         const std::string &userId,
115         const std::string &hnpRootPath,
116         const std::string &hapPath,
117         const std::string &cpuAbi,
118         const std::string &packageName);
119     static bool ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName);
120 
121     static bool DeterminePrefix(const ExtractFileType &extractFileType, const std::string &cpuAbi,
122         std::string &prefix);
123 
124     static bool DetermineSuffix(const ExtractFileType &extractFileType, std::vector<std::string> &suffixes);
125 
126     static bool IsNativeFile(
127         const std::string &entryName, const ExtractParam &extractParam);
128 
129     /**
130      * @brief Rename a directory from old path to new path.
131      * @param oldPath Indicates the old path name.
132      * @param newPath Indicates the new path name.
133      * @return Returns true if the directory renamed successfully; returns false otherwise.
134      */
135     static bool RenameDir(const std::string &oldPath, const std::string &newPath);
136     /**
137      * @brief Change the owner and group ID of a file or directory.
138      * @param filePath Indicates the file or directory path.
139      * @param uid Indicates the uid.
140      * @param uid Indicates the gid.
141      * @return Returns true if changed successfully; returns false otherwise.
142      */
143     static bool ChangeFileAttr(const std::string &filePath, const int uid, const int gid);
144     /**
145      * @brief Rename a file from old path to new path.
146      * @param oldPath Indicates the old path name.
147      * @param newPath Indicates the new path name.
148      * @return Returns true if the file renamed successfully; returns false otherwise.
149      */
150     static bool RenameFile(const std::string &oldPath, const std::string &newPath);
151     /**
152      * @brief Check whether a path is valid under a root path.
153      * @param rootDir Indicates the root path name.
154      * @param path Indicates the path to be checked.
155      * @return Returns true if the path is valid successfully; returns false otherwise.
156      */
157     static bool IsValidPath(const std::string &rootDir, const std::string &path);
158     /**
159      * @brief Check whether a path is valid code path.
160      * @param codePath Indicates the path to be checked.
161      * @return Returns true if the file renamed successfully; returns false otherwise.
162      */
163     static bool IsValidCodePath(const std::string &codePath);
164     /**
165      * @brief Get the parent directory path of a file.
166      * @param codePath Indicates the file path.
167      * @return Returns the parent directory if get successfully; returns empty string otherwise.
168      */
169     static std::string GetPathDir(const std::string &path);
170     /**
171      * @brief Delete files in a directory.
172      * @param path Indicates the directory path of the files to be deleted.
173      * @return Returns true if the files deleted successfully; returns false otherwise.
174      */
175     static bool DeleteFiles(const std::string &dataPath);
176     /**
177      * @brief Delete files in a directory except the directories to be kept.
178      * @param dataPath Indicates the directory path of the files to be deleted.
179      * @param dirsToKeep Indicates the directories to be kept.
180      * @return Returns true if the files deleted successfully; returns false otherwise
181      */
182     static bool DeleteFilesExceptDirs(const std::string &dataPath, const std::vector<std::string> &dirsToKeep);
183     /**
184      * @brief Make a directory and change the owner and group ID of it.
185      * @param path Indicates the directory path to be made.
186      * @param isReadByOthers Indicates the directory whether read by other users.
187      * @param uid Indicates the uid.
188      * @param uid Indicates the gid.
189      * @return Returns true if directory made successfully; returns false otherwise.
190      */
191     static bool MkOwnerDir(const std::string &path, bool isReadByOthers, const int uid, const int gid);
192     /**
193      * @brief Make a directory and change the owner and group ID of it.
194      * @param path Indicates the directory path to be made.
195      * @param mode Indicates the directory mode.
196      * @param uid Indicates the uid.
197      * @param uid Indicates the gid.
198      * @return Returns true if directory made successfully; returns false otherwise.
199      */
200     static bool MkOwnerDir(const std::string &path,  int mode, const int uid, const int gid);
201     /**
202      * @brief Get disk usage for dir.
203      * @param dir Indicates the directory.
204      * @param size Indicates the disk size.
205      * @return Returns true if successfully; returns false otherwise.
206      */
207     static int64_t GetDiskUsage(const std::string &dir, bool isRealPath = false);
208     /**
209      * @brief Traverse all cache directories.
210      * @param currentPath Indicates the current path.
211      * @param cacheDirs Indicates the cache directories.
212      * @return Returns true if successfully; returns false otherwise.
213      */
214     static void TraverseCacheDirectory(const std::string &currentPath, std::vector<std::string> &cacheDirs);
215     /**
216      * @brief Get disk usage from path.
217      * @param path Indicates the current path.
218      * @return Returns disk size.
219      */
220     static int64_t GetDiskUsageFromPath(const std::vector<std::string> &path);
221 
222     static bool InitialiseQuotaMounts();
223 
224     static int64_t GetDiskUsageFromQuota(const int32_t uid);
225 
226     static bool ScanDir(
227         const std::string &dirPath, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths);
228 
229     static bool ScanSoFiles(const std::string &newSoPath, const std::string &originPath,
230         const std::string &currentPath, std::vector<std::string> &paths);
231 
232     static bool CopyFile(const std::string &sourceFile, const std::string &destinationFile);
233 
234     static bool CopyFileFast(const std::string &sourcePath, const std::string &destPath);
235 
236     static bool ChangeDirOwnerRecursively(const std::string &path, const int uid, const int gid);
237 
238     static bool IsDiffFiles(const std::string &entryName,
239         const std::string &targetPath, const std::string &cpuAbi);
240 
241     static bool ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
242         const std::string &cpuAbi);
243 
244     static bool ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
245         const std::string &newSoPath, int32_t uid);
246 
247     static bool ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &fileVec);
248 
249     static bool CopyFiles(const std::string &sourceDir, const std::string &destinationDir);
250 
251     static bool GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi,
252         std::vector<std::string> &fileNames);
253 
254     static bool GetAtomicServiceBundleDataDir(const std::string &bundleName,
255         const int32_t userId, std::vector<std::string> &allPathNames);
256 
257 #if defined(CODE_SIGNATURE_ENABLE)
258     static bool PrepareEntryMap(const CodeSignatureParam &codeSignatureParam,
259         const std::vector<std::string> &soEntryFiles, Security::CodeSign::EntryMap &entryMap);
260     static ErrCode PerformCodeSignatureCheck(const CodeSignatureParam &codeSignatureParam,
261         const Security::CodeSign::EntryMap &entryMap);
262 #endif
263 
264     static bool VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam);
265 
266 #if defined(CODE_ENCRYPTION_ENABLE)
267     static ErrCode EnforceEncryption(std::unordered_map<std::string, std::string> &entryMap,
268         const CodeCryptoHapInfo &hapInfo, bool &isEncryption);
269 #endif
270 
271     static ErrCode CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption);
272 
273     static ErrCode CheckHapEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption);
274 
275     static bool MoveFiles(const std::string &srcDir, const std::string &desDir, bool isDesDirNeedCreated = false);
276 
277     static bool MoveFileOrDir(const std::string &srcPath, const std::string &destPath, mode_t mode);
278 
279     static bool MoveFile(const std::string &srcPath, const std::string &destPath);
280 
281     static bool ExtractDriverSoFiles(const std::string &srcPath,
282         const std::unordered_multimap<std::string, std::string> &dirMap);
283 
284     static bool CopyDriverSoFiles(const std::string &originalDir, const std::string &destinedDir);
285 
286 #if defined(CODE_ENCRYPTION_ENABLE)
287     static ErrCode ExtractSoFilesToTmpHapPath(const std::string &hapPath, const std::string &cpuAbi,
288         const std::string &tmpSoPath, int32_t uid);
289 
290     static ErrCode ExtractSoFilesToTmpSoPath(const std::string &hapPath, const std::string &realSoFilesPath,
291         const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid);
292 
293     static ErrCode DecryptSoFile(const std::string &hapPath, const std::string &tmpHapPath, int32_t uid,
294         uint32_t fileSize, uint32_t offset);
295 
296     static ErrCode RemoveEncryptedKey(int32_t uid, const std::vector<std::string> &soList);
297 
298     static int32_t CallIoctl(int32_t flag, int32_t associatedFlag, int32_t uid, int32_t &fd);
299 #endif
300     static int32_t MigrateData(const std::vector<std::string> &sourcePaths, const std::string &destinationPath);
301 
302     static bool GenerateKeyIdAndSetPolicy(const EncryptionParam &encryptionParam, std::string &keyId);
303 
304     static bool DeleteKeyId(const EncryptionParam &encryptionParam);
305 
306     /**
307      * @brief Add file Delete dfx
308      * @param path Indicates the directory path to add dfx.
309      * @return
310      */
311     static void AddDeleteDfx(const std::string &path);
312 
313    /**
314      * @brief Rmv file Delete dfx
315      * @param path Indicates the directory path to add dfx.
316      * @return
317      */
318     static void RmvDeleteDfx(const std::string &path);
319 
320     static std::vector<std::string> GetLogPath(const std::string& logDir, const std::vector<std::string>& fileHeads);
321     static void GetDirFiles(const std::string& path, std::vector<std::string>& files, bool isRecursive = true);
322     static std::string GetFileName(const std::string &sourcePath);
323     static std::string IncludeTrailingPathDelimiter(const std::string& path);
324     static std::vector<std::string> GetFirstBootLogFile();
325 
326 private:
327     static bool ObtainNativeSoFile(const BundleExtractor &extractor, const std::string &cpuAbi,
328         std::vector<std::string> &soEntryFiles);
329 
330     static bool ProcessApplyDiffPatchPath(const std::string &oldSoPath, const std::string &diffFilePath,
331         const std::string &newSoPath, std::vector<std::string> &oldSoFileNames,
332         std::vector<std::string> &diffFileNames);
333     static bool ExtractResourceFiles(const ExtractParam &extractParam, const BundleExtractor &extractor);
334     static bool CheckPathIsSame(const std::string &path, int32_t mode, const int32_t uid, const int32_t gid,
335         bool &isPathExist);
336     static bool SetKeyIdPolicy(const EncryptionParam &encryptionParam, const std::string &keyId);
337     static bool GenerateKeyId(const EncryptionParam &encryptionParam, std::string &keyId);
338 #if defined(CODE_ENCRYPTION_ENABLE)
339     static std::mutex encryptionMutex_;
340     static void *encryptionHandle_;
341     static EnforceMetadataProcessForApp enforceMetadataProcessForApp_;
342     static bool OpenEncryptionHandle();
343 #endif
344     static void FsyncFile(const std::string &path);
345     static std::string GetSameLevelTmpPath(const std::string &path);
346 
347     struct OwnershipInfo {
348         int32_t uid { 0 };
349         int32_t gid { 0 };
350         int32_t mode { 0 };
351     };
352     static int32_t InnerMigrateData(
353         const std::string &sourcePaths, const std::string &destinationPath, const OwnershipInfo &info);
354     static int32_t MigrateDataCopyFile(
355         const std::string &sourceFile, const std::string &destinationFile, const OwnershipInfo &info);
356     static int32_t MigrateDataCopyDir(
357         const std::string &sourcePaths, const std::string &destinationPath, const OwnershipInfo &info);
358     static int32_t MigrateDataCheckPrmissions(
359         std::vector<std::string> &realSourcePaths, const std::string &destinationPath, OwnershipInfo &info);
360     static int32_t UpdateFileProperties(const std::string &newFile, const OwnershipInfo &info);
361     static int32_t ForceCreateDirectory(const std::string &path, const OwnershipInfo &info);
362 };
363 }  // namespace AppExecFwk
364 }  // namespace OHOS
365 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H
366