1 /* 2 * Copyright (c) 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 OHOS_FILEMGMT_BACKUP_STORAGE_MGR_SERVICE_H 17 #define OHOS_FILEMGMT_BACKUP_STORAGE_MGR_SERVICE_H 18 19 #include <string> 20 #include <vector> 21 #include <unistd.h> 22 23 #include "istorage_manager.h" 24 #include "datashare_abs_result_set.h" 25 #include "datashare_helper.h" 26 #include "datashare_predicates.h" 27 28 namespace OHOS::FileManagement::Backup { 29 const uint64_t PATH_MAX_LEN = 4096; 30 const std::string PHY_APP = "/data/app/"; 31 const std::string WILDCARD_DEFAULT_INCLUDE = "*"; 32 constexpr char FILE_SEPARATOR_CHAR = '/'; 33 const std::string BASE_EL1 = "/data/storage/el1/base/"; 34 const std::string BASE_EL2 = "/data/storage/el2/base/"; 35 const std::string EL1 = "el1"; 36 const std::string EL2 = "el2"; 37 const std::string BASE = "/base/"; 38 const std::string BACKUP_INCLUDE = "INCLUDES"; 39 const std::string BACKUP_EXCLUDE = "EXCLUDES"; 40 const std::string BACKUP_PATH_PREFIX = "/data/service/el2/"; 41 const std::string BACKUP_PATH_SURFFIX = "/backup/backup_sa/"; 42 const std::string BACKUP_INCEXC_SYMBOL = "incExc_"; 43 const std::string BACKUP_STAT_SYMBOL = "stat_"; 44 const std::string DEFAULT_INCLUDE_PATH_IN_HAP_FILES = "files"; 45 const std::string DEFAULT_INCLUDE_PATH_IN_HAP_DATABASE = "database"; 46 const std::string DEFAULT_INCLUDE_PATH_IN_HAP_PREFERENCE = "preferences"; 47 const std::string MEDIA_CLOUD_SAND_PREFIX = "/storage/cloud"; 48 const std::string MEDIA_SAND_PREFIX = "/storage/media"; 49 const std::string URI_PREFIX = "file://"; 50 const std::string NORMAL_SAND_PREFIX = "/data/storage"; 51 const std::string FILE_SAND_PREFIX = "/storage/Users"; 52 const std::string FILE_AUTHORITY = "docs"; 53 const std::string DEFAULT_PATH_WITH_WILDCARD = "haps/*"; 54 const std::string FILE_CONTENT_SEPARATOR = ";"; 55 const char LINE_SEP = '\n'; 56 const std::string VER_10_LINE1 = "version=1.0&attrNum=8"; 57 const std::string VER_10_LINE2 = "path;mode;dir;size;mtime;hash;isIncremental;encodeFlag"; 58 const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; 59 const std::string MEDIA_QUERYOPRN_QUERYVOLUME = "query_media_volume"; 60 const std::string MEDIA_TYPE = "media"; 61 const std::string FILE_TYPE = "file"; 62 const std::int32_t E_OK = 0; 63 constexpr int32_t E_ERR = -1; 64 constexpr int32_t STORAGE_SERVICE_SYS_CAP_TAG = 13600000; 65 constexpr const char *QUOTA_DEVICE_DATA_PATH = "/data"; 66 constexpr const char *PROC_MOUNTS_PATH = "/proc/mounts"; 67 constexpr const char *DEV_BLOCK_PATH = "/dev/block/"; 68 const std::int32_t E_SA_IS_NULLPTR = STORAGE_SERVICE_SYS_CAP_TAG + 12; 69 const std::int32_t E_REMOTE_IS_NULLPTR = STORAGE_SERVICE_SYS_CAP_TAG + 13; 70 const std::int32_t E_MEDIALIBRARY_ERROR = STORAGE_SERVICE_SYS_CAP_TAG + 1202; 71 const std::int32_t E_QUERY = STORAGE_SERVICE_SYS_CAP_TAG + 1206; 72 const std::int32_t E_GETROWCOUNT = STORAGE_SERVICE_SYS_CAP_TAG + 1207; 73 const std::int32_t E_SYS_KERNEL_ERR = STORAGE_SERVICE_SYS_CAP_TAG + 8; 74 const int MEDIA_TYPE_IMAGE = 1; 75 const int MEDIA_TYPE_VIDEO = 2; 76 const int MEDIA_TYPE_AUDIO = 3; 77 const int32_t GET_DATA_SHARE_HELPER_TIMES = 5; 78 const int UID_FILE_MANAGER = 1006; 79 const int32_t USER_ID_BASE = 200000; 80 static std::map<std::string, std::string> mQuotaReverseMounts; 81 82 83 struct FileStat { 84 std::string filePath; 85 int64_t fileSize; 86 int64_t lastUpdateTime; 87 int32_t mode; 88 bool isDir; 89 bool isIncre; 90 }; 91 struct BundleStatsParas { 92 uint32_t userId; 93 std::string &bundleName; 94 int64_t lastBackupTime; 95 int64_t fileSizeSum; 96 int64_t incFileSizeSum; 97 }; 98 99 class StorageManagerService { 100 public: GetInstance()101 static StorageManagerService &GetInstance() 102 { 103 static StorageManagerService instance {}; 104 return instance; 105 } 106 /** 107 * @brief Get the bundle stats object 108 * 109 * @param bundleName bundle name 110 */ 111 bool GetBundleStats(const std::string &bundleName, StorageManager::BundleStats &bundleStats); 112 113 /** 114 * @brief Get the user storage stats object 115 * 116 * @param bundleName bundle name 117 * @param userId user id 118 */ 119 int32_t GetUserStorageStatsByType(int32_t userId, StorageManager::StorageStats &storageStats, std::string type); 120 121 /** 122 * @brief update memory para 123 * 124 * @param size para data 125 */ 126 int32_t UpdateMemoryPara(int32_t size, int32_t oldSize); 127 128 /** 129 * @brief Get the user storage stats object 130 * 131 * @param userId user id 132 * @param bundleNames 133 * @param incrementalBackTimes 134 * @param pkgFileSizes bundle backup file size 135 * @param incPkgFileSizes Incremental bundle backup file size 136 */ 137 int32_t GetBundleStatsForIncrease(uint32_t userId, const std::vector<std::string> &bundleNames, 138 const std::vector<int64_t> &incrementalBackTimes, std::vector<int64_t> &pkgFileSizes, 139 std::vector<int64_t> &incPkgFileSizes); 140 private: 141 StorageManagerService() = default; 142 ~StorageManagerService() = default; 143 void GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, int64_t lastBackupTime, 144 std::vector<int64_t> &pkgFileSizes, std::vector<int64_t> &incPkgFileSizes); 145 std::tuple<std::vector<std::string>, std::vector<std::string>> ReadIncludesExcludesPath( 146 const std::string &bundleName, const int64_t lastBackupTime, const uint32_t userId); 147 void DealWithIncludeFiles(const BundleStatsParas ¶s, const std::vector<std::string> &includes, 148 std::vector<std::string> &phyIncludes, std::map<std::string, std::string>& pathMap); 149 void ConvertSandboxRealPath(const uint32_t userId, const std::string &bundleName, 150 const std::string &sandboxPathStr, std::vector<std::string> &realPaths, 151 std::map<std::string, std::string>& pathMap); 152 void DeduplicationPath(std::vector<std::string> &configPaths); 153 void ScanExtensionPath(BundleStatsParas ¶s, 154 const std::vector<std::string> &includes, const std::vector<std::string> &excludes, 155 std::map<std::string, std::string> &pathMap, std::ofstream &statFile); 156 void RecognizeSandboxWildCard(const uint32_t userId, const std::string &bundleName, 157 const std::string &sandboxPathStr, std::vector<std::string> &phyIncludes, 158 std::map<std::string, std::string>& pathMap); 159 void SetExcludePathMap(std::string &excludePath, std::map<std::string, bool> &excludesMap); 160 std::tuple<bool, bool> CheckIfDirForIncludes(const std::string &path, BundleStatsParas ¶s, 161 std::map<std::string, std::string> &pathMap, std::ofstream &statFile, std::map<std::string, bool> &excludesMap); 162 bool GetIncludesFileStats(const std::string &dir, BundleStatsParas ¶s, 163 std::map<std::string, std::string> &pathMap, 164 std::ofstream &statFile, std::map<std::string, bool> &excludesMap); 165 bool GetPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &includeWildCard, 166 std::vector<std::string> &includePathList, std::map<std::string, std::string> &pathMap); 167 bool ExcludeFilter(std::map<std::string, bool> &excludesMap, const std::string &path); 168 void WriteFileList(std::ofstream &statFile, struct FileStat fileStat, BundleStatsParas ¶s); 169 bool AddOuterDirIntoFileStat(const std::string &dir, BundleStatsParas ¶s, const std::string &sandboxDir, 170 std::ofstream &statFile, std::map<std::string, bool> &excludesMap); 171 std::string PhysicalToSandboxPath(const std::string &dir, const std::string &sandboxDir, const std::string &path); 172 void InsertStatFile(const std::string &path, struct FileStat fileStat, 173 std::ofstream &statFile, std::map<std::string, bool> &excludesMap, BundleStatsParas ¶s); 174 bool AddPathMapForPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &phyPath, 175 std::map<std::string, std::string> &pathMap); 176 uint32_t CheckOverLongPath(const std::string &path); 177 int32_t GetMediaStorageStats(StorageManager::StorageStats &storageStats); 178 void GetMediaTypeAndSize(const std::shared_ptr<DataShare::DataShareResultSet> &resultSet, 179 StorageManager::StorageStats &storageStats); 180 int32_t GetFileStorageStats(int32_t userId, StorageManager::StorageStats &storageStats); 181 bool InitialiseQuotaMounts(); 182 }; 183 } // namespace OHOS::FileManagement::Backup 184 #endif // OHOS_FILEMGMT_BACKUP_STORAGE_MGR_SERVICE_H