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