1 /*
2  * Copyright (c) 2020 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 OHOS_BUNDLE_UTIL_H
17 #define OHOS_BUNDLE_UTIL_H
18 
19 #include "bundle_common.h"
20 #include "bundle_info.h"
21 #include "utils_list.h"
22 #include "stdint.h"
23 #ifndef __LITEOS_M__
24 #ifdef OHOS_APPEXECFWK_BMS_BUNDLEMANAGER
25 #include <string>
26 #include <vector>
27 #endif
28 #endif
29 #include "cJSON.h"
30 
31 #ifdef __LITEOS_M__
32 #define S_IREAD S_IRUSR
33 #define S_IWRITE S_IWUSR
34 #endif
35 
36 namespace OHOS {
37 class BundleUtil {
38 public:
39     static bool CheckRealPath(const char *path);
40     static bool IsFile(const char *path);
41     static bool IsDir(const char *path);
42     static bool EndWith(const char *str, const char *subStr);
43     static bool StartWith(const char *str, const char *subStr);
44     static cJSON *GetJsonStream(const char *path);
45     static uint32_t GetFileSize(const char *filePath);
46     static uint32_t GetFileFolderSize(const char *filePath);
47     static uint32_t GetCurrentFolderSize(const char *dirPath, List<char *>* list);
48     static cJSON *ConvertInstallRecordToJson(const InstallRecord &installRecord);
49     static char *GetValueFromBundleJson(const char *bundleName, const char *key);
50     static int32_t GetValueFromBundleJson(const char *bundleName, const char *key, int32_t defaultValue);
51     static bool IsDigitStr(const char *str);
52     static void DeleteJsonFile(const char *bundleName, const char *randStr);
53     static bool CheckBundleJsonIsValid(const char *bundleName, char **codePath, char **appId, int32_t &versionCode);
54     static char *Strscat(char *str[], uint32_t len);
55     static void CreateRandStr(char *str, uint32_t len);
56 #ifdef OHOS_APPEXECFWK_BMS_BUNDLEMANAGER
57     static cJSON *ConvertUidAndGidToJson(const InstallRecord &installRecord);
58     static bool DeleteUidInfoFromJson(const char *bundleName);
59 #else
60     static bool MkDirs(const char *dir);
61     static bool RemoveDir(const char *dir);
62     static bool RenameDir(const char *oldDir, const char *newDir);
63     static bool RenameFile(const char *oldFile, const char *newFile);
64     static bool StoreJsonContentToFile(const char *packageJson, const cJSON *object);
65     static void FreeStrArrayMemory(char **pointer, uint32_t len);
66 #endif
67 private:
68     BundleUtil() = default;
69     ~BundleUtil() = default;
70 
71 #ifdef OHOS_APPEXECFWK_BMS_BUNDLEMANAGER
72     static cJSON *ObtainUidAndGidJson(bool flag);
73     static bool AddUidAndGidInfo(const InstallRecord &installRecord, cJSON *size, cJSON *uids);
74     static bool DeleteInnerUidInfoFromUidArray(const char *bundleName, cJSON *size, cJSON *uids);
75 #else
76     static char *GetRootDir(const char *dir, int32_t index);
77     static bool CheckDirIsEmpty(const char *dir, List<char *>* list);
78 #endif
79 }; // namespace OHOS
80 }
81 
82 extern "C" {
83 void RefreshAllServiceTimeStamp(void);
84 }
85 #endif // OHOS_BUNDLE_UTIL_H
86