From 4a7b24de9236e90ee94374407379e0ea987130f1 Mon Sep 17 00:00:00 2001 From: huhui Date: Mon, 9 Jun 2025 14:47:28 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- services/backup.cfg | 5 +- services/backup_sa/BUILD.gn | 5 + .../module_external/storage_manager_service.h | 181 ++++ .../src/module_external/sms_adapter.cpp | 17 +- .../storage_manager_service.cpp | 796 ++++++++++++++++++ .../backup_sa/module_external/BUILD.gn | 54 +- .../storage_manager_service_test.cpp | 469 +++++++++++ 7 files changed, 1516 insertions(+), 11 deletions(-) create mode 100644 services/backup_sa/include/module_external/storage_manager_service.h create mode 100644 services/backup_sa/src/module_external/storage_manager_service.cpp create mode 100644 tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp diff --git a/services/backup.cfg b/services/backup.cfg index 6fc61dab4..3c33ded8c 100644 --- a/services/backup.cfg +++ b/services/backup.cfg @@ -6,11 +6,14 @@ "uid" : "backup", "gid" : ["backup", "log"], "secon" : "u:r:backup_sa:s0", + "sandbox" : 0, + "caps" : ["DAC_READ_SEARCH", "SYS_ADMIN"], "permission" : [ "ohos.permission.STORAGE_MANAGER", "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", "ohos.permission.ACCESS_EXT_SYSTEM_ABILITY", - "ohos.permission.CONNECT_BACKUP_EXTENSION" + "ohos.permission.CONNECT_BACKUP_EXTENSION", + "ohos.permission.READ_MEDIA" ], "permission_acls" : ["ohos.permission.ACCESS_EXT_SYSTEM_ABILITY"] } diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index c5983d3a5..f781fca26 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -177,6 +177,7 @@ ohos_shared_library("backup_sa") { "src/module_ipc/svc_session_manager.cpp", "src/module_notify/notify_work_service.cpp", "src/module_sched/sched_scheduler.cpp", + "src/module_external/storage_manager_service.cpp", ] defines = [ @@ -194,6 +195,8 @@ ohos_shared_library("backup_sa") { ":backup_sa_ipc", "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", "${path_backup}/utils:backup_utils", + "${path_backup}/interfaces/innerkits/native:sandbox_helper_native", + "${path_backup}/interfaces/innerkits/native:fileuri_native", ] external_deps = [ @@ -215,6 +218,8 @@ ohos_shared_library("backup_sa") { "safwk:system_ability_fwk", "samgr:samgr_proxy", "storage_service:storage_manager_sa_proxy", + "data_share:datashare_consumer", + "data_share:datashare_common", ] cflags_cc = [ diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h new file mode 100644 index 000000000..3fda6fa47 --- /dev/null +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_STORAGE_MGR_SERVICE_H +#define OHOS_FILEMGMT_BACKUP_STORAGE_MGR_SERVICE_H + +#include +#include +#include + +#include "istorage_manager.h" +#include "datashare_abs_result_set.h" +#include "datashare_helper.h" +#include "datashare_predicates.h" + +namespace OHOS::FileManagement::Backup { +const uint64_t PATH_MAX_LEN = 4096; +const std::string PHY_APP = "/data/app/"; +const std::string WILDCARD_DEFAULT_INCLUDE = "*"; +constexpr char FILE_SEPARATOR_CHAR = '/'; +const std::string BASE_EL1 = "/data/storage/el1/base/"; +const std::string BASE_EL2 = "/data/storage/el2/base/"; +const std::string EL1 = "el1"; +const std::string EL2 = "el2"; +const std::string BASE = "/base/"; +const std::string Backup_INCLUDE = "INCLUDES"; +const std::string Backup_EXCLUDE = "EXCLUDES"; +const std::string BACKUP_PATH_PREFIX = "/data/service/el2"; +const std::string BACKUP_PATH_SURFFIX = "/backup/backup_sa/"; +const std::string BACKUP_INCEXC_SYMBOL = "incExc_"; +const std::string BACKUP_STAT_SYMBOL = "stat_"; +const std::string DEFAULT_INCLUDE_PATH_IN_HAP_FILES = "files"; +const std::string DEFAULT_INCLUDE_PATH_IN_HAP_DATABASE = "database"; +const std::string DEFAULT_INCLUDE_PATH_IN_HAP_PREFERENCE = "preferences"; +const std::string MEDIA_CLOUD_SAND_PREFIX = "/storage/cloud"; +const std::string MEDIA_SAND_PREFIX = "/storage/media"; +const std::string URI_PREFIX = "file://"; +const std::string NORMAL_SAND_PREFIX = "/data/storage/"; +const std::string FILE_SAND_PREFIX = "/storage/Users"; +const std::string FILE_AUTHORITY = "docs"; +const std::string DEFAULT_PATH_WITH_WILDCARD = "/haps/*"; +const std::string FILE_CONTENT_SEPARATOR = ";"; +const char LINE_SEP = '\n'; +const std::string VER_10_LINE1 = "version=1.0&attrNum=8"; +const std::string VER_10_LINE2 = "path;mode;dir;size;mtime;hash;inIncremental;encodeFlag"; +const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const std::string MEDIA_QUERYOPRN_QUERYVOLUME = "query_media_volume"; +const std::string MEDIA_TYPE = "media"; +const std::string FILE_TYPE = "file"; +const std::int32_t E_OK = 0; +constexpr int32_t E_ERR = -1; +constexpr int32_t STORAGE_SERVICE_SYS_CAP_TAG = 13600000; +constexpr const char *QUOTA_DEVICE_DATA_PATH = "/data"; +constexpr const char *PROC_MOUNTS_PATH = "/proc/mounts"; +constexpr const char *DEV_BLOCK_PATH = "/dev/block"; +const std::int32_t E_SA_IS_NULLPTR = STORAGE_SERVICE_SYS_CAP_TAG + 12; +const std::int32_t E_REMOTE_IS_NULLPTR = STORAGE_SERVICE_SYS_CAP_TAG + 13; +const std::int32_t E_MEDIALIBRARY_ERROR = STORAGE_SERVICE_SYS_CAP_TAG + 1202; +const std::int32_t E_QUERY = STORAGE_SERVICE_SYS_CAP_TAG + 1206; +const std::int32_t E_GETROWCOUNT = STORAGE_SERVICE_SYS_CAP_TAG + 1207; +const std::int32_t E_SYS_KERNEL_ERR = STORAGE_SERVICE_SYS_CAP_TAG + 8; +const int32_t E_SYS_ERR = -1; +const int MEDIA_TYPE_IMAGE = 1; +const int MEDIA_TYPE_VIDEO = 2; +const int MEDIA_TYPE_AUDIO = 3; +const int32_t GET_DATA_SHARE_HELPER_TIMES = 5; +const int UID_FILE_MANAGER = 1006; +const int32_t USER_ID_BASE = 200000; +static std::map mQuotaReverseMounts; + + +struct FileStat { + std::string filePath; + int64_t fileSize; + int64_t lastUpdateTime; + int32_t mode; + bool isDir; + bool isIncre; +}; +struct BundleStatsParas { + uint32_t userId; + std::string &bundleName; + int64_t lastBackupTime; + int64_t fileSizeSum; + int64_t incFileSizeSum; +}; + +class StorageManagerServie { +public: + StorageManagerServie(); + ~StorageManagerServie(); + static StorageManagerServie &GetInstance(); + /** + * @brief Get the bundle stats object + * + * @param bundleName bundle name + */ + StorageManager::BundleStats GetBundleStats(const std::string &bundleName); + + /** + * @brief Get the user storage stats object + * + * @param bundleName bundle name + * @param userId user id + */ + int32_t GetUserStorageStatsByType(int32_t userId, StorageManager::StorageStats &storageStats, std::string type); + + /** + * @brief update memory para + * + * @param size para data + */ + int32_t UpdateMemoryPara(int32_t size, int32_t oldSize); + + /** + * @brief Get the user storage stats object + * + * @param userId user id + * @param bundleNames + * @param incrementalBackTimes + * @param pkgFileSizes bundle backup file size + * @param incPkgFileSizes Incremental bundle backup file size + */ + int32_t GetBundleStatsForIncrease(uint32_t userId, const std::vector &bundleNames, + const std::vector &incrementalBackTimes, std::vector &pkgFileSizes, + std::vector &incPkgFileSizes); +private: + void GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, int64_t lastBackupTime, + std::vector &pkgFileSizes, std::vector &incPkgFileSizes); + std::tuple, std::vector> ReadIncludesExcludesPath( + const std::string &bundleName, const int64_t lastBackupTime, const uint32_t userId); + void DealWithIncludeFiles(const BundleStatsParas ¶s, const std::vector &includes, + std::vector &phyIncludes, std::map& pathMap); + void ConvertSandboxRealPath(const uint32_t userId, const std::string &bundleName, + const std::string &sandboxPathStr, std::vector &realPaths, + std::map& pathMap); + void DeduplicationPath(std::vector &configPaths); + void ScanExtensionPath(BundleStatsParas ¶s, + const std::vector &includes, const std::vector &excludes, + std::map &pathMap, std::ofstream &statFile); + void RecognizeSandboxWildCard(const uint32_t userId, const std::string &bundleName, + const std::string &sandboxPathStr, std::vector &phyIncludes, + std::map& pathMap); + void SetExcludePathMap(std::string &excludePath, std::map &excludesMap); + std::tuple CheckIfDirForIncludes(const std::string &path, BundleStatsParas ¶s, + std::map &pathMap, std::ofstream &statFile, std::map &excludesMap); + bool GetIncludesFileStats(const std::string &dir, BundleStatsParas ¶s, + std::map &pathMap, + std::ofstream &statFile, std::map &excludesMap); + bool GetPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &includeWildCard, + std::vector &includePathList, std::map &pathMap); + bool ExcludeFilter(std::map &excludesMap, const std::string &path); + void WriteFileList(std::ofstream &statFile, struct FileStat fileStat, BundleStatsParas ¶s); + bool AddOuterDirIntoFileStat(const std::string &dir, BundleStatsParas ¶s, const std::string &sandboxDir, + std::ofstream &statFile, std::map &excludesMap); + std::string PhysicalToSandboxPath(const std::string &dir, const std::string &sandboxDir, const std::string &path); + void InsertStatFile(const std::string &path, struct FileStat fileStat, + std::ofstream &statFile, std::map &excludesMap, BundleStatsParas ¶s); + bool AddPathMapForPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &phyPath, + std::map &pathMap); + uint32_t CheckOverLongPath(const std::string &path); + int32_t GetMediaStorageStats(StorageManager::StorageStats &storageStats); + void GetMediaTypeAndSize(const std::shared_ptr &resultSet, + StorageManager::StorageStats &storageStats); + int32_t GetFileStorageStats(int32_t userId, StorageManager::StorageStats &storageStats); + bool InitialiseQuotaMounts(); +} +} // namespace OHOS::FileManagement::Backup +#endif // OHOS_FILEMGMT_BACKUP_STORAGE_MGR_SERVICE_H \ No newline at end of file diff --git a/services/backup_sa/src/module_external/sms_adapter.cpp b/services/backup_sa/src/module_external/sms_adapter.cpp index a0b683afc..7884ce54a 100644 --- a/services/backup_sa/src/module_external/sms_adapter.cpp +++ b/services/backup_sa/src/module_external/sms_adapter.cpp @@ -22,6 +22,7 @@ #include "filemgmt_libhilog.h" #include "iservice_registry.h" #include "system_ability_definition.h" +#include "module_external/storage_manager_service.h" namespace OHOS::FileManagement::Backup { using namespace std; @@ -29,8 +30,6 @@ using namespace std; namespace { const string MEDIA_LIBRARY_HAP = "com.ohos.medialibrary.medialibrarydata"; const string EXTERNAL_FILE_HAP = "com.ohos.UserFile.ExternalFileManager"; -const string MEDIA_TYPE = "media"; -const string FILE_TYPE = "file"; const int64_t ERR_SIZE = -1; } // namespace @@ -62,15 +61,15 @@ StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bund int64_t StorageMgrAdapter::GetUserStorageStats(const std::string &bundleName, int32_t userId) { StorageManager::StorageStats bundleStats; - auto storageMgr = GetStorageManager(); + auto storageMgr = StorageManagerService::GetInstance(); if (bundleName == MEDIA_LIBRARY_HAP) { - if (storageMgr->GetUserStorageStatsByType(userId, bundleStats, MEDIA_TYPE)) { + if (storageMgr.GetUserStorageStatsByType(userId, bundleStats, MEDIA_TYPE)) { HILOGE("Failed to get user media storage stats"); return ERR_SIZE; } return bundleStats.image_ + bundleStats.video_; } else if (bundleName == EXTERNAL_FILE_HAP) { - if (storageMgr->GetUserStorageStatsByType(userId, bundleStats, FILE_TYPE)) { + if (storageMgr.GetUserStorageStatsByType(userId, bundleStats, FILE_TYPE)) { HILOGE("Failed to get user file storage stats"); return ERR_SIZE; } @@ -81,9 +80,9 @@ int64_t StorageMgrAdapter::GetUserStorageStats(const std::string &bundleName, in int32_t StorageMgrAdapter::UpdateMemPara(int32_t size) { - auto storageMgr = GetStorageManager(); + auto storageMgr = StorageManagerService::GetInstance(); int32_t oldSize = BConstants::DEFAULT_VFS_CACHE_PRESSURE; - if (storageMgr->UpdateMemoryPara(size, oldSize)) { + if (storageMgr.UpdateMemoryPara(size, oldSize)) { HILOGE("An error occured StorageMgrAdapter UpdateMemPara"); return BConstants::DEFAULT_VFS_CACHE_PRESSURE; } @@ -94,8 +93,8 @@ int32_t StorageMgrAdapter::GetBundleStatsForIncrease(uint32_t userId, const std: const std::vector &incrementalBackTimes, std::vector &pkgFileSizes, std::vector &incPkgFileSizes) { - auto storageMgr = GetStorageManager(); - if (storageMgr->GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, + auto storageMgr = StorageManagerService::GetInstance(); + if (storageMgr.GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, incPkgFileSizes)) { throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get user storage stats"); } diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp new file mode 100644 index 000000000..3f45368ce --- /dev/null +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -0,0 +1,796 @@ +/* + * Copyright (c) 2024-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + #include "module_external/storage_manager_service.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "b_error/b_error.h" +#include "b_resources/b_constants.h" +#include "filemgmt_helper.h" +#include "sandbox_helper.h" +#include "file_uri.h" + +#include +#include + +namespace OHOS::FileManagement::Backup { +using namespace std; +std:: recursive_mutex mMountsLock; + +static bool PathSortFunc(const std::string &path1, const std::string &path2) +{ + return path1 < path2; +} + +static std::string GetQuotaSrcMountPath(const std::string &target) +{ + std::lock_guard lock(mMountsLock); + if (mQuotaReverseMounts.find(target) != mQuotaReverseMounts.end()) { + return mQuotaReverseMounts[target]; + } else { + return ""; + } +} + +StorageManagerServie::StorageManagerServie() +{ +} +StorageManagerServie::~StorageManagerServie() +{ +} + +StorageManagerServie &StorageManagerServie::GetInstance() +{ + static StorageManagerServie instance{}; + return instance; +} + +StorageManager::BundleStats StorageManagerServie::GetBundleStats(const string &bundleName) +{ + StorageManager::BundleStats bundleStats; + return bundleStats; +} + +int32_t StorageStatusService::GetUserStorageStatsByType(int32_t userId, StorageStats &storageStats, std::string type) +{ + storageStats.video_ = 0; + storageStats.image_ = 0; + storageStats.file_ = 0; + int32_t err = E_OK; + if (type == MEDIA_TYPE) { + LOGI("GetUserStorageStatsByType media"); + err = GetMediaStorageStats(storageStats); + } else if (type == FILE_TYPE) { + LOGI("GetUserStorageStatsByType file"); + err = GetFileStorageStats(userId, storageStats); + } else { + LOGI("GetUserStorageStatsByType type: %{public}s", type.c_str()); + } + return err; +} + +int32_t StorageManagerServie::GetMediaStorageStats(StorageStats &storageStats) +{ + LOGE("GetMediaStorageStats start"); + auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sam == nullptr) { + LOGE("StorageStatusService::GetMediaStorageStats samgr == nullptr"); + return E_SA_IS_NULLPTR; + } + auto remoteObj = sam->GetSystemAbility(FILEMANAGEMENT_BACKUP_SERVICE_SA_ID); + if (remoteObj == nullptr) { + LOGE("StorageStatusService::GetMediaStorageStats remoteObj == nullptr"); + return E_REMOTE_IS_NULLPTR; + } + int32_t tryCount = 1; + LOGE("GetMediaStorageStats start Creator"); + auto dataShareHelper = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI); + while (dataShareHelper == nullptr && tryCount < GET_DATA_SHARE_HELPER_TIMES) { + LOGW("dataShareHelper is retrying, attempt %{public}d", tryCount); + dataShareHelper = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI); + tryCount++; + } + if (dataShareHelper == nullptr) { + LOGE("dataShareHelper is null!"); + return E_MEDIALIBRARY_ERROR; + } + vector columns; + Uri uri(MEDIALIBRARY_DATA_URI + "/" + MEDIA_QUERYOPRN_QUERYVOLUME + "/" + MEDIA_QUERYOPRN_QUERYVOLUME); + DataShare::DataSharePredicates predicates; + LOGE("GetMediaStorageStats start Query"); + auto queryResultSet = dataShareHelper->Query(uri, predicates, columns); + if (queryResultSet == nullptr) { + LOGE("queryResultSet is null!"); + return E_QUERY; + } + auto count = 0; + auto ret = queryResultSet->GetRowCount(count); + if ((ret != E_OK) || (count < 0)) { + LOGE("get row count from rdb failed"); + return E_GETROWCOUNT; + } + GetMediaTypeAndSize(queryResultSet, storageStats); + dataShareHelper->Release(); + LOGE("GetMediaStorageStats end"); + return E_OK; +} + +void StorageManagerServie::GetMediaTypeAndSize(const std::shared_ptr &resultSet, StorageStats &storageStats) +{ + if (resultSet == nullptr) { + LOGE("StorageStatusService::GetMediaTypeAndSize, input resultSet is nullptr."); + return; + } + int thumbnailType = -1; + while (resultSet->GoToNextRow() == E_OK) { + int32_t index = 0; + int mediatype = 0; + int64_t size = 0; + if (resultSet->GetColumnIndex("media_type", index) || resultSet->GetInt(index, mediatype)) { + LOGE("get media_type column index or int value err."); + continue; + } + if (resultSet->GetColumnIndex("size", index) || resultSet->GetLong(index, size)) { + LOGE("get size column index or long value err."); + continue; + } + LOGI("media type: %{public}d, size: %{public}lld", mediatype, static_cast(size)); + if (mediatype == MEDIA_TYPE_IMAGE || mediatype == thumbnailType) { + storageStats.image_ += size; + } else if (mediatype == MEDIA_TYPE_AUDIO) { + storageStats.audio_ = size; + } else if (mediatype == MEDIA_TYPE_VIDEO) { + storageStats.video_ = size; + } else { + LOGD("unsupprted media_type: %{public}d", mediatype); + } + } +} + +int32_t StorageManagerServie::GetFileStorageStats(int32_t userId, StorageStats &storageStats) +{ + int32_t uid = userId * USER_ID_BASE + UID_FILE_MANAGER; + HILOGE("GetOccupiedSpaceForUid uid:%{public}d", uid); + if (InitialiseQuotaMounts() != true) { + HILOGE("Failed to initialise quota mounts"); + reutrn E_SYS_KERNEL_ERR; + } + + std::string device = ""; + device = GetQuotaSrcMountPath(QUOTA_DEVICE_DATA_PATH); + if (device.empty()) { + HILOGE("skip when device no quotas present"); + return E_OK; + } + + struct dqblk dq; + if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, reinterpret_cast(&dq)) != 0) { + HILOGE("Failed to get quotactl, errno : %{public}d", errno); + return E_SYS_KERNEL_ERR + } + storageStats.file_ = static_cast(dq.dqb_curspace); + HILOGE("GetOccupiedSpaceForUid size:%{public}s", std::tostring(storageStats.file_).c_str()); + return E_OK; + + +} + +bool StorageManagerServie::InitialiseQuotaMounts() +{ + std::lock_guard lock(mMountsLock); + mQuotaReverseMounts.clear(); + std::ifstream in(PROC_MOUNTS_PATH); + + if (!in.is_open()) { + LOGE("Failed to open mounts file"); + return false; + } + std::string source; + std::string target; + std::string ignored; + + while (in.peek() != EOF) { + std::getline(in, source, ' '); + std::getline(in, target, ' '); + std::getline(in, ignored); + if (source.compare(0, strlen(DEV_BLOCK_PATH), DEV_BLOCK_PATH) == 0 + && target.compare(QUOTA_DEVICE_DATA_PATH) == 0) { + struct dqblk dq; + if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0, reinterpret_cast(&dq)) == 0) { + mQuotaReverseMounts[target] = source; + } + } + } + + return true; +} + +int32_t StorageManagerServie::UpdateMemoryPara(int32_t size, int32_t oldSize) +{ + return 0; +} + +int32_t StorageManagerServie::GetBundleStatsForIncrease(uint32_t userId, const std::vector &bundleNames, + const std::vector &incrementalBackTimes, std::vector &pkgFileSizes, + std::vector &incPkgFileSizes) +{ + LOGI("GetBundleStatsForIncrease start"); + if (bundleNames.size() != incrementalBackTimes.size()) { + LOGE("Invalid paramters, size of bundleNames should match incrementalBackTimes."); + return E_PARAMS_INVALID; + } + + for (size_t i = 0; i < bundleNames.size(); i++) { + std::string bundleName = bundleNames[i]; + int64_t lastBackupTime = incrementalBackTimes[i]; + GetBundleStatsForIncreaseEach(userId, bundleName, lastBackupTime, pkgFileSizes, incPkgFileSizes); + } + return E_OK; +} + +void StorageManagerServie::GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, int64_t lastBackupTime, + std::vector &pkgFileSizes, std::vector &incPkgFileSizes) +{ + // input parameters + BundleStatsParas paras = {.userId = userId, .bundleName = bundleName, + .lastBackupTime = lastBackupTime, .fileSizeSum = 0, .incFileSizeSum = 0}; + + // obtain includes, excludes in backup extension config + auto [includes, excludes] = ReadIncludesExcludesPath(bundleName, lastBackupTime, userId); + if (includes.empty()) { + pkgFileSizes.emplace_back(0); + incPkgFileSizes.emplace_back(0); + return; + } + // physical paths + std::vector phyIncludes; + // map about sandbox path to physical path + std::map pathMap; + + // recognize physical path for include directory + DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); + if (phyIncludes.empty()) { + LOGE("Incorrect convert for include sandbox path for %{private}s", bundleName.c_str()); + pkgFileSizes.emplace_back(0); + incPkgFileSizes.emplace_back(0); + return; + } + + // recognize physical path for exclude directory + std::vector phyExcludes; + for (const auto &exclude : excludes) { + std::string excludeStr = exclude; + if (excludeStr.front() != FILE_SEPARATOR_CHAR) { + excludeStr = FILE_SEPARATOR_CHAR + excludeStr; + } + // convert sandbox to physical path + ConvertSandboxRealPath(userId, bundleName, excludeStr, phyExcludes, pathMap); + } + + std::string filePath = BACKUP_PATH_PREFIX + std::to_string(userId) + BACKUP_PATH_SURFFIX + + bundleName + FILE_SEPARATOR_CHAR + BACKUP_STAT_SYMBOL + std::to_string(lastBackupTime); + std::ofstream statFile; + statFile.open(filePath.data(), std::ios::out | std::ios::trunc); + if (!statFile.is_open()) { + LOGE("creat file fail, errno:%{public}d.", errno); + pkgFileSizes.emplace_back(0); + incPkgFileSizes.emplace_back(0); + return; + } + statFile << VER_10_LINE1 << std::endl; + statFile << VER_10_LINE2 << std::endl; + + DeduplicationPath(phyIncludes); + ScanExtensionPath(paras, phyIncludes, phyExcludes, pathMap, statFile); + // calculate summary file sizes + pkgFileSizes.emplace_back(paras.fileSizeSum); + incPkgFileSizes.emplace_back(paras.incFileSizeSum); + LOGI("bundleName: %{public}s, size: %{public}lld", bundleName.c_str(), static_cast(paras.fileSizeSum)); + statFile.close(); +} + +std::tuple, std::vector> StorageManagerServie::ReadIncludesExcludesPath( + const std::string &bundleName, const int64_t lastBackupTime, const uint32_t userId) +{ + if (bundleName.empty()) { + LOGE("bundleName is empty"); + return { {}, {} }; + } + // 保存includeExclude的path + std::string filePath = BACKUP_PATH_PREFIX + std::to_string(userId) + BACKUP_PATH_SURFFIX + + bundleName + FILE_SEPARATOR_CHAR + BACKUP_INCEXC_SYMBOL + std::to_string(lastBackupTime); + std::ifstream incExcFile; + incExcFile.open(filePath.data()); + if (!incExcFile.is_open()) { + LOGE("Cannot open include/exclude file, fail errno:%{public}d", errno); + return { {}, {} }; + } + + std::vector includes; + std::vector excludes; + bool incOrExt = true; + while (incExcFile) { + std::string line; + std::getline(incExcFile, line); + if (line.empty()) { + LOGI("Read Complete"); + break; + } + if (line == BACKUP_INCLUDE) { + incOrExt = true; + } else if (line == BACKUP_EXCLUDE) { + incOrExt = false; + } + if (incOrExt && line != BACKUP_INCLUDE) { + includes.emplace_back(line); + } else if (!incOrExt && line != BACKUP_EXCLUDE) { + excludes.emplace_back(line); + } + } + incExcFile.close(); + return {includes, excludes}; +} + +void StorageManagerServie::DealWithIncludeFiles(const BundleStatsParas ¶s, const std::vector &includes, + std::vector &phyIncludes, std::map& pathMap) +{ + uint32_t userId = paras.userId; + std::string bundleName = paras.bundleName; + for (const auto &include : includes) { + std::string includeStr = include; + if (includeStr.front() != FILE_SEPARATOR_CHAR) { + includeStr = FILE_SEPARATOR_CHAR + includeStr; + } + if (includeStr.find(BASE_EL1 + DEFAULT_PATH_WITH_WILDCARD) == 0 || + includeStr.find(BASE_EL2 + DEFAULT_PATH_WITH_WILDCARD) == 0) { + // recognize sandbox path to physical path with wild card + RecognizeSandboxWildCard(userId, bundleName, includeStr, phyIncludes, pathMap); + if (phyIncludes.empty()) { + LOGE("DealWithIncludeFiles failed to recognize path with wildcard %{private}s", bundleName.c_str()); + continue; + } + } else { + // convert sandbox to physical path + ConvertSandboxRealPath(userId, bundleName, includeStr, phyIncludes, pathMap); + } + } +} + +void StorageManagerServie::ConvertSandboxRealPath(const uint32_t userId, const std::string &bundleName, + const std::string &sandboxPathStr, std::vector &realPaths, + std::map& pathMap) +{ + std::string uriString; + if (sandboxPathStr.find(NORMAL_SAND_PREFIX) == 0) { + // for normal hap, start with file://bundleName + uriString = URI_PREFIX + bundleName; + } else if (sandboxPathStr.find(FILE_SAND_PREFIX) == 0) { + // for public files, start with file://docs + uriString = URI_PREFIX + FILE_AUTHORITY; + } else if (sandboxPathStr.find(MEDIA_SAND_PREFIX) == 0) { + std::string physicalPath = sandboxPathStr; + physicalPath.insert(MEDIA_SAND_PREFIX.length(), FILE_SEPARATOR_CHAR + std::to_string(userId)); + realPaths.emplace_back(physicalPath); + pathMap.insert({physicalPath, sandboxPathStr}); + return; + } else if (sandboxPathStr.find(MEDIA_CLOUD_SAND_PREFIX) == 0) { + std::string physicalPath = sandboxPathStr; + physicalPath.insert(MEDIA_CLOUD_SAND_PREFIX.length(), FILE_SEPARATOR_CHAR + std::to_string(userId)); + realPaths.emplace_back(physicalPath); + pathMap.insert({physicalPath, sandboxPathStr}); + return; + } + + if (!uriString.empty()) { + std::string sandboxPathUriStr = AppFileService::SandboxHelper::Encode(sandboxPathStr); + uriString += sandboxPathUriStr; + AppFileService::ModuleFileUri::FileUri uri(uriString); + // files + std::string physicalPath; + int ret = AppFileService::SandboxHelper::GetBackupPhysicalPath(uri.ToString(), std::to_string(userId), + physicalPath); + if (ret != 0) { + LOGE("Get physical path failed with %{public}d", ret); + return; + } + realPaths.emplace_back(physicalPath); + pathMap.insert({physicalPath, sandboxPathStr}); + } +} + +void StorageManagerServie::DeduplicationPath(std::vector &configPaths) +{ + sort(configPaths.begin(), configPaths.end(), PathSortFunc); + auto it = unique(configPaths.begin(), configPaths.end(), [](const std::string &path1, const std::string &path2) { + return path1 == path2; + }); + configPaths.erase(it, configPaths.end()); +} + +void StorageManagerServie::ScanExtensionPath(BundleStatsParas ¶s, + const std::vector &includes, const std::vector &excludes, + std::map &pathMap, std::ofstream &statFile) +{ + std::map excludesMap; + for (auto exclude : excludes) { + SetExcludePathMap(exclude, excludesMap); + } + // all file with stats in include directory + for (const auto &includeDir : includes) { + // Check if includeDir is a file path + auto [isSucc, isDir] = CheckIfDirForIncludes(includeDir, paras, pathMap, statFile, excludesMap); + if (!isSucc) { + continue; + } + // recognize all file in include directory + if (isDir && !GetIncludesFileStats(includeDir, paras, pathMap, statFile, excludesMap)) { + LOGE("Faied to get include files for includeDir"); + } + } +} + +void StorageManagerServie::RecognizeSandboxWildCard(const uint32_t userId, const std::string &bundleName, + const std::string &sandboxPathStr, std::vector &phyIncludes, + std::map& pathMap) +{ + if (sandboxPathStr.find(BASE_EL1 + DEFAULT_PATH_WITH_WILDCARD) == 0) { + std::string physicalPrefix = PHY_APP + EL1 + FILE_SEPARATOR_CHAR + std::to_string(userId) + BASE + + bundleName + FILE_SEPARATOR_CHAR; + std::string relatePath = sandboxPathStr.substr(BASE_EL1.size()); + if (!GetPathWildCard(userId, bundleName, physicalPrefix + relatePath, phyIncludes, pathMap)) { + LOGE("el1 GetPathWildCard dir path invaild"); + } + } else if (sandboxPathStr.find(BASE_EL2 + DEFAULT_PATH_WITH_WILDCARD) == 0) { + std::string physicalPrefix = PHY_APP + EL2 + FILE_SEPARATOR_CHAR + std::to_string(userId) + BASE + + bundleName + FILE_SEPARATOR_CHAR; + std::string relatePath = sandboxPathStr.substr(BASE_EL2.size()); + if (!GetPathWildCard(userId, bundleName, physicalPrefix + relatePath, phyIncludes, pathMap)) { + LOGE("el2 GetPathWildCard dir path invaild"); + } + } +} + +void StorageManagerServie::SetExcludePathMap(std::string &excludePath, std::map &excludesMap) +{ + if (excludePath.empty()) { + LOGE("SetExcludePathMap Param failed"); + return; + } + struct stat fileStatInfo = {0}; + if (stat(excludePath.c_str(), &fileStatInfo) != 0) { + LOGE("SetExcludePathMap call stat error %{private}s, errno:%{public}d", excludePath.c_str(), errno); + return; + } + if (S_ISDIR(fileStatInfo.st_mode)) { + if (excludePath.back() != FILE_SEPARATOR_CHAR) { + excludePath.push_back(FILE_SEPARATOR_CHAR); + } + excludesMap.insert({excludePath, true}); + } else { + excludesMap.insert({excludePath, false}); + } +} + +std::tuple StorageManagerServie::CheckIfDirForIncludes(const std::string &path, BundleStatsParas ¶s, + std::map &pathMap, std::ofstream &statFile, std::map &excludesMap) +{ + if (!statFile.is_open() || path.empty()) { + LOGE("CheckIfDirForIncludes Param failed"); + return {false, false}; + } + // check whether the path exists + struct stat fileStatInfo = {0}; + // + if (stat(path.c_str(), &fileStatInfo) != 0) { + LOGE("CheckIfDirForIncludes call stat error %{private}s, fail errno:%{public}d", path.c_str(), errno); + return {false, false}; + } + if (S_ISDIR(fileStatInfo.st_mode)) { + LOGI("%{private}s exists and is a directory", path.c_str()); + return {true, true}; + } else { + std::string sandboxPath = path; + auto it = pathMap.find(path); + if (it != pathMap.end()) { + sandboxPath = it->second; + } + + struct FileStat fileStat; + fileStat.filePath = sandboxPath; + fileStat.fileSize = fileStatInfo.st_size; + // mode + fileStat.mode = static_cast(fileStatInfo.st_mode); + fileStat.isDir = false; + int64_t lastUpdateTime = static_cast(fileStatInfo.st_mtime); + fileStat.lastUpdateTime = lastUpdateTime; + if (paras.lastBackupTime == 0 || lastUpdateTime > paras.lastBackupTime) { + fileStat.isIncre = true; + } + if (ExcludeFilter(excludesMap, path) == false) { + WriteFileList(statFile, fileStat, paras); + } + return {true, false}; + } +} + +bool StorageManagerServie::GetIncludesFileStats(const std::string &dir, BundleStatsParas ¶s, + std::map &pathMap, + std::ofstream &statFile, std::map &excludesMap) +{ + std::string sandboxDir = dir; + auto it = pathMap.find(dir); + if (it != pathMap.end()) { + sandboxDir = it->second; + } + // stat current directory info + AddOuterDirIntoFileStat(dir, paras, sandboxDir, statFile, excludesMap); + + std::stack folderStack; + std::string filePath; + folderStack.push(dir); + // stat files and sub-directory in current directory info + while (!folderStack.empty()) { + filePath = folderStack.top(); + folderStack.pop(); + DIR *dirPtr = opendir(filePath.c_str()); + if (dirPtr == nullptr) { + LOGE("GetIncludesFileStats open file dir:%{private}s fail, errno:%{public}d", filePath.c_str(), errno); + continue; + } + if (filePath.back() != FILE_SEPARATOR_CHAR) { + filePath.push_back(FILE_SEPARATOR_CHAR); + } + + struct dirent *entry = nullptr; + while ((entry = readdir(dirPtr)) != nullptr) { + if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) { + continue; + } + std::string path = filePath + entry->d_name; + struct stat fileInfo = {0}; + if (stat(path.c_str(), &fileInfo) != 0) { + LOGE("GetIncludesFileStats call stat error %{private}s, errno:%{public}d", path.c_str(), errno); + fileInfo.st_size = 0; + } + struct FileStat fileStat = {}; + fileStat.filePath = PhysicalToSandboxPath(dir, sandboxDir, path); + fileStat.fileSize = fileInfo.st_size; + CheckOverLongPath(fileStat.filePath); + // mode + fileStat.mode = static_cast(fileInfo.st_mode); + int64_t lastUpdateTime = static_cast(fileInfo.st_mtime); + fileStat.lastUpdateTime = lastUpdateTime; + fileStat.isIncre = (paras.lastBackupTime == 0 || lastUpdateTime > paras.lastBackupTime) ? true : false; + if (entry->d_type == DT_DIR) { + fileStat.isDir = true; + folderStack.push(path); + } + InsertStatFile(path, fileStat, statFile, excludesMap, paras); + } + closedir(dirPtr); + } + return true; +} + +bool StorageManagerServie::GetPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &includeWildCard, + std::vector &includePathList, std::map &pathMap) +{ + size_t pos = includeWildCard.rfind(WILDCARD_DEFAULT_INCLUDE); + if (pos == std::string::npos) { + LOGE("GetPathWildCard: path should include *"); + return false; + } + std::string pathBeforeWildCard = includeWildCard.substr(0, pos); + DIR *dirPtr = opendir(pathBeforeWildCard.c_str()); + if (dirPtr == nullptr) { + LOGE("GetPathWildCard open file dir:%{private}s fail, errno:%{public}d", pathBeforeWildCard.c_str(), errno); + return false; + } + struct dirent *entry = nullptr; + std::vector subDirs; + while ((entry = readdir(dirPtr)) != nullptr) { + if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) { + continue; + } + std::string path = pathBeforeWildCard + entry->d_name; + if (entry->d_type == DT_DIR) { + subDirs.emplace_back(path); + } + } + closedir(dirPtr); + for (auto &subDir : subDirs) { + DIR *subDirPtr = opendir(subDir.c_str()); + if (subDirPtr == nullptr) { + LOGE("GetPathWildCard open file dir:%{private}s fail, errno:%{public}d", subDir.c_str(), errno); + return false; + } + while ((entry = readdir(subDirPtr)) != nullptr) { + if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) { + continue; + } + std::string dirName = std::string(entry->d_name); + + std::string path = subDir + FILE_SEPARATOR_CHAR + entry->d_name; + if (entry->d_type == DT_DIR && (dirName == DEFAULT_INCLUDE_PATH_IN_HAP_FILES || + dirName == DEFAULT_INCLUDE_PATH_IN_HAP_DATABASE || + dirName == DEFAULT_INCLUDE_PATH_IN_HAP_PREFERENCE)) { + includePathList.emplace_back(path); + AddPathMapForPathWildCard(userId, bundleName, path, pathMap); + } + } + closedir(subDirPtr); + } + return true; +} + +bool StorageManagerServie::ExcludeFilter(std::map &excludesMap, const std::string &path) +{ + if (path.empty()) { + LOGE("ExcludeFilter Param failed"); + return true; + } + std::string formatPath = path; + for (auto exclude = excludesMap.begin(); exclude != excludesMap.end(); exclude++) { + if (exclude->second != true) { + if (formatPath.compare(exclude->first) == 0) { + return true; + } + } else { + if (formatPath.compare(0, exclude->first.size(), exclude->first) == 0) { + return true; + } + } + } + return false; +} + +void StorageManagerServie::WriteFileList(std::ofstream &statFile, struct FileStat fileStat, BundleStatsParas ¶s) +{ + if (!statFile.is_open() || fileStat.filePath.empty()) { + LOGE("WriteFileList Param failed"); + return; + } + std::string fileLine = ""; + bool encodeFlag = false; + if (fileStat.filePath.find(LINE_SEP) != std::string::npos) { + fileLine += AppFileService::SandboxHelper::Encode(fileStat.filePath) + FILE_CONTENT_SEPARATOR; + encodeFlag = true; + } else { + fileLine += fileStat.filePath + FILE_CONTENT_SEPARATOR; + } + fileLine += std::to_string(fileStat.mode) + FILE_CONTENT_SEPARATOR; + if (fileStat.isDir) { + fileLine += std::to_string(1) + FILE_CONTENT_SEPARATOR; + } else { + fileLine += std::to_string(0) + FILE_CONTENT_SEPARATOR; + } + fileLine += std::to_string(fileStat.fileSize) + FILE_CONTENT_SEPARATOR; + fileLine += std::to_string(fileStat.lastUpdateTime) + FILE_CONTENT_SEPARATOR; + fileLine += FILE_CONTENT_SEPARATOR; + if (fileStat.isIncre) { + fileLine += std::to_string(1); + } else { + fileLine += std::to_string(0); + } + fileLine += FILE_CONTENT_SEPARATOR; + if (encodeFlag) { + fileLine += std::to_string(1); + } else { + fileLine += std::to_string(0); + } + // te file line + statFile << fileLine << std::endl; + if (fileStat.isIncre) { + paras.incFileSizeSum += fileStat.fileSize; + } + paras.fileSizeSum += fileStat.fileSize; +} + +bool StorageManagerServie::AddOuterDirIntoFileStat(const std::string &dir, BundleStatsParas ¶s, const std::string &sandboxDir, + std::ofstream &statFile, std::map &excludesMap) +{ + if (!statFile.is_open() || dir.empty()) { + LOGE("AddOuterDirIntoFileStat Param failed"); + return false; + } + struct stat fileInfo = {0}; + if (stat(dir.c_str(), &fileInfo) != 0) { + LOGE("AddOuterDirIntoFileStat call stat error %{private}s, fail errno:%{public}d", dir.c_str(), errno); + return false; + } + struct FileStat fileStat = {}; + fileStat.filePath = PhysicalToSandboxPath(dir, sandboxDir, dir); + fileStat.fileSize = fileInfo.st_size; + // + // mode + fileStat.mode = static_cast(fileInfo.st_mode); + int64_t lastUpdateTime = static_cast(fileInfo.st_mtime); + fileStat.lastUpdateTime = lastUpdateTime; + fileStat.isIncre = (paras.lastBackupTime == 0 || lastUpdateTime > paras.lastBackupTime) ? true : false; + fileStat.isDir = true; + std::string formatPath = dir; + if (formatPath.back() != FILE_SEPARATOR_CHAR) { + formatPath.push_back(FILE_SEPARATOR_CHAR); + } + if (ExcludeFilter(excludesMap, formatPath) == false) { + WriteFileList(statFile, fileStat, paras); + } + return true; +} + +std::string StorageManagerServie::PhysicalToSandboxPath(const std::string &dir, const std::string &sandboxDir, + const std::string &path) +{ + std::size_t dirPos = dir.size(); + std::string pathSurffix = path.substr(dirPos); + return sandboxDir + pathSurffix; +} + +void StorageManagerServie::InsertStatFile(const std::string &path, struct FileStat fileStat, + std::ofstream &statFile, std::map &excludesMap, BundleStatsParas ¶s) +{ + if (!statFile.is_open() || path.empty()) { + LOGE("InsertStatFile Param failed"); + return; + } + std::string formatPath = path; + if (fileStat.isDir == true && formatPath.back() != FILE_SEPARATOR_CHAR) { + formatPath.push_back(FILE_SEPARATOR_CHAR); + } + if (!ExcludeFilter(excludesMap, formatPath)) { + WriteFileList(statFile, fileStat, paras); + } +} + +bool StorageManagerServie::AddPathMapForPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &phyPath, + std::map &pathMap) +{ + std::string physicalPrefixEl1 = PHY_APP + EL1 + FILE_SEPARATOR_CHAR + std::to_string(userId) + BASE + + bundleName + FILE_SEPARATOR_CHAR; + std::string physicalPrefixEl2 = PHY_APP + EL2 + FILE_SEPARATOR_CHAR + std::to_string(userId) + BASE + + bundleName + FILE_SEPARATOR_CHAR; + if (phyPath.find(physicalPrefixEl1) == 0) { + std::string relatePath = phyPath.substr(physicalPrefixEl1.size()); + pathMap.insert({phyPath, BASE_EL1 + relatePath}); + } else if (phyPath.find(physicalPrefixEl2) == 0) { + std::string relatePath = phyPath.substr(physicalPrefixEl2.size()); + pathMap.insert({phyPath, BASE_EL2 + relatePath}); + } else { + LOGE("Invalid phyiscal path"); + return false; + } + return true; +} + +uint32_t CheckOverLongPath(const std::string &path) +{ + uint32_t len = path.length(); + if (len >= PATH_MAX_LEN) { + size_t found = path.find_last_of('/'); + std::string sub = path.substr(found + 1); + LOGE("Path over long, length:%{public}d, fileName:%{public}s.", len, sub.c_str()); + } + return len; +} + +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_external/BUILD.gn b/tests/unittests/backup_sa/module_external/BUILD.gn index a161cb536..0388ceb63 100644 --- a/tests/unittests/backup_sa/module_external/BUILD.gn +++ b/tests/unittests/backup_sa/module_external/BUILD.gn @@ -62,7 +62,59 @@ ohos_unittest("bms_adapter_test") { use_exceptions = true } +ohos_unittest("storage_manager_service_test") { + module_out_path = path_module_out_tests + + include_dirs = [ + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/services/backup_sa/include", + "${path_backup}/utils/include", + "${path_backup}/utils/include/b_hilog", + "${path_backup_mock}/utils_mock/include", + ] + + sources = [ + "${path_backup_mock}/utils_mock/src/b_jsonutil_mock.cpp", + "${path_backup_mock}/utils_mock/src/b_sa_utils_mock.cpp", + "storage_manager_service_test.cpp", + ] + + deps = [ + "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", + "${path_backup}/utils:backup_utils", + "${path_backup}/interfaces/innerkits/native:sandbox_helper_native", + "${path_backup}/interfaces/innerkits/native:fileuri_native", + "${path_backup}/services/backup_sa:backup_sa" + ] + + defines = [ + "LOG_TAG=\"app_file_service\"", + "LOG_DOMAIN=0xD200000", + "private = public", + "protected = public", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "hitrace:hitrace_meter", + "ipc:ipc_core", + "jsoncpp:jsoncpp", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "storage_service:storage_manager_sa_proxy", + ] + + use_exceptions = true +} + group("adapter_test") { testonly = true - deps = [ ":bms_adapter_test" ] + deps = [ + ":bms_adapter_test", + ":storage_manager_service_test" ] } diff --git a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp new file mode 100644 index 000000000..be9cd6b11 --- /dev/null +++ b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp @@ -0,0 +1,469 @@ +/*) + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include "b_jsonutil_mock.h" +#include "b_sa_util_mock.h" + +#include +#include +#include +#include "file_uri.h" +#include "sandbox_helper.h" +#include "module_external/storage_manager_service.h" +#include "module_external/storage_manager_service.cpp" + +namespace OHOS { +using namespace std; +using namespace testing; +using namespace FileManagement::Backup; +class StorageManagerServieTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override {}; + void TearDown() override {}; +}; + +void StorageManagerServieTest::SetUpTestCase(void) {} +void StorageManagerServieTest::TearDownTestCase(void) {} +void StorageManagerServieTest::SetUp(void) {} +void StorageManagerServieTest::TearDown(void) {} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsForIncrease_001 + * @tc.desc: check the GetBundleStatsForIncrease function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsForIncrease_001, + testing::ext::TestSize.Level1) +{ + uint32_t userId = 100; + std::vector bundleNames = {"com.example.app1", "com.example.app2"}; + std::vector incrementalBackTimes = {123456789}; + std::vector pkgFileSizes; + std::vector incPkgFileSizes; + int32_t result = StorageManagerServie::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, + incrementalBackTimes, pkgFileSizes, incPkgFileSizes); + EXPECT_EQ(result, -1); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsForIncrease_002 + * @tc.desc: check the GetBundleStatsForIncrease function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsForIncrease_002, + testing::ext::TestSize.Level1) +{ + uint32_t userId = 100; + std::vector bundleNames = {"com.example.app1", "com.example.app2"}; + std::vector incrementalBackTimes = {123456789, 987654321}; + std::vector pkgFileSizes; + std::vector incPkgFileSizes; + int32_t result = StorageManagerServie::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, + incrementalBackTimes, pkgFileSizes, incPkgFileSizes); + EXPECT_EQ(result, 0); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsForIncreaseEach_001 + * @tc.desc: check the GetBundleStatsForIncreaseEach function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsForIncreaseEach_001, + testing::ext::TestSize.Level1) +{ + uint32_t userId = 100; + std::string bundleName = "testBundle"; + int64_t lastBackupTime = 123456789; + std::vector pkgFileSizes; + std::vector incPkgFileSizes; + StorageManagerServie::GetInstance().GetBundleStatsForIncreaseEach(userId, bundleNames, incrementalBackTimes, + pkgFileSizes, incPkgFileSizes); + EXPECT_EQ(pkgFileSizes.size(), 1); + EXPECT_EQ(pkgFileSizes[0], 0); + EXPECT_EQ(incPkgFileSizes.size(), 1); + EXPECT_EQ(incPkgFileSizes[0], 0); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_ReadIncludesExcludesPath_001 + * @tc.desc: check the ReadIncludesExcludesPath function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_001, + testing::ext::TestSize.Level1) +{ + std::string bundleName = ""; + int64_t lastBackupTime = 123456789; + uint32_t userId = 100; + auto result = StorageManagerServie::GetInstance().ReadIncludesExcludesPath(bundleNames, lastBackupTime, userId); + EXPECT_TRUE(std::get<0>(result).empty()); + EXPECT_TRUE(std::get<1>(result).empty()); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_ReadIncludesExcludesPath_002 + * @tc.desc: check the ReadIncludesExcludesPath function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_002, + testing::ext::TestSize.Level1) +{ + std::string bundleName = "testBundle"; + int64_t lastBackupTime = 123456789; + uint32_t userId = 100; + // Assuming the file does not exist or cannot be opened + auto result = StorageManagerServie::GetInstance().ReadIncludesExcludesPath(bundleNames, lastBackupTime, userId); + EXPECT_TRUE(std::get<0>(result).empty()); + EXPECT_TRUE(std::get<1>(result).empty()); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_DealWithIncludeFiles_001 + * @tc.desc: check the DealWithIncludeFiles function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_001, + testing::ext::TestSize.Level1) +{ + std::string bundleName = "com.huawei.hmos.camera"; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::vector includes = {"data/storage/el1/base/" + DEFAULT_PATH_WITH_WILDCARD}; + std::vector phyIncludes; + std::map pathMap; + StorageManagerServie::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); + EXPECT_TRUE(phyIncludes.empty()); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_DealWithIncludeFiles_002 + * @tc.desc: check the DealWithIncludeFiles function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_002, + testing::ext::TestSize.Level1) +{ + std::string bundleName = "com.huawei.hmos.camera"; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::vector includes = {NORMAL_SAND_PREFIX}; + std::vector phyIncludes; + std::map pathMap; + StorageManagerServie::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); + EXPECT_TRUE(phyIncludes.empty()); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_DealWithIncludeFiles_003 + * @tc.desc: check the DealWithIncludeFiles function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_003, + testing::ext::TestSize.Level1) +{ + std::string bundleName = "testBundle"; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::vector includes = {}; + std::vector phyIncludes; + std::map pathMap; + StorageManagerServie::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); + EXPECT_TRUE(phyIncludes.empty()); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_ConvertSandboxRealPath_001 + * @tc.desc: check the ConvertSandboxRealPath function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_001, + testing::ext::TestSize.Level1) +{ + uint32_t userId = 100; + std::string bundleName = "com.example.app"; + std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/path/to/file"; + std::vector realPaths; + std::map pathMap; + StorageManagerServie::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap); + EXPECT_TRUE(realPaths.empty()); + EXPECT_TRUE(pathMap.empty()); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_ConvertSandboxRealPath_002 + * @tc.desc: check the ConvertSandboxRealPath function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_002, + testing::ext::TestSize.Level1) +{ + uint32_t userId = 100; + std::string bundleName = "com.huawei.hmos.camera"; + std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/el2/database/"; + std::vector realPaths; + std::map pathMap; + StorageManagerServie::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap); + std::string uriString = URI_PREFIX + bundleName + AppFileService::SandboxHelper::Encode(sandboxPathStr); + AppFileService::ModuleFileUri::FileUri uri(uriString); + std::string physicalPath; + AppFileService::SandboxHelper::GetBackupPhysicalPath(uri.ToString, std::to_string(userId), physicalPath); + EXPECT_FALSE(realPaths.empty()); + EXPECT_FALSE(pathMap.empty()); + EXPECT_EQ(realPaths[0], physicalPath); + EXPECT_EQ(pathMap[realPaths[0]], sandboxPathStr); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_CheckIfDirForIncludes_001 + * @tc.desc: check the CheckIfDirForIncludes function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_001, + testing::ext::TestSize.Level1) +{ + ofstream closedStatFile; + std::string bundleName = "testBundle"; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::map pathMap; + std::map excludesMap; + auto result = StorageManagerServie::GetInstance().CheckIfDirForIncludes("test_path", paras, + pathMap, closedStatFile, excludesMap); + EXPECT_EQ(result, std::make_tuple(false, false)); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_CheckIfDirForIncludes_002 + * @tc.desc: check the CheckIfDirForIncludes function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_002, + testing::ext::TestSize.Level1) +{ + ofstream statFile; + std::string bundleName = "testBundle"; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::map pathMap; + std::map excludesMap; + auto result = StorageManagerServie::GetInstance().CheckIfDirForIncludes("", paras, pathMap, closedStatFile, excludesMap); + EXPECT_EQ(result, std::make_tuple(false, false)); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_CheckIfDirForIncludes_003 + * @tc.desc: check the CheckIfDirForIncludes function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_003, + testing::ext::TestSize.Level1) +{ + ofstream statFile; + statFile.open("/system/etc/NOTICE.TXT", ios::out | ios::trunc); + EXPECT_FALSE(!statFile.is_open()) << "file is not open" + std::string bundleName = "testBundle"; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::map pathMap; + std::map excludesMap; + auto result = StorageManagerServie::GetInstance().CheckIfDirForIncludes("/data/service", paras, pathMap, + statFile, excludesMap); + EXPECT_EQ(result, std::make_tuple(true, true)); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_ExcludeFilter_001 + * @tc.desc: check the ExcludeFilter function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ExcludeFilter_001, + testing::ext::TestSize.Level1) +{ + std::map excludesMap; + std::string path = ""; + auto result = StorageManagerServie::GetInstance().ExcludeFilter(excludePath, path); + EXPECT_TRUE(result); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_ExcludeFilter_002 + * @tc.desc: check the ExcludeFilter function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ExcludeFilter_002, + testing::ext::TestSize.Level1) +{ + std::map excludesMap; + std::string path = "/path/to/file"; + auto result = StorageManagerServie::GetInstance().ExcludeFilter(excludePath, path); + EXPECT_FALSE(result); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetUserStorageStatsByType_001 + * @tc.desc: check the GetUserStorageStatsByType function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_001, + testing::ext::TestSize.Level1) +{ + StorageManager::StorageStats storageStats; + std::string type = "MEDIA_TYPE"; + int32_t userId = 100; + int64_t result = StorageManagerServie::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); + EXPECT_EQ(result, E_OK); + EXPECT_EQ(storageStats.video_, 0); + EXPECT_EQ(storageStats.image_, 0); + EXPECT_EQ(storageStats.file_, 0); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetUserStorageStatsByType_002 + * @tc.desc: check the GetUserStorageStatsByType function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_002, + testing::ext::TestSize.Level1) +{ + StorageManager::StorageStats storageStats; + std::string type = "FILE_TYPE"; + int32_t userId = 100; + int64_t result = StorageManagerServie::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); + EXPECT_EQ(result, E_OK); + EXPECT_EQ(storageStats.video_, 0); + EXPECT_EQ(storageStats.image_, 0); + EXPECT_EQ(storageStats.file_, 0); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetUserStorageStatsByType_003 + * @tc.desc: check the GetUserStorageStatsByType function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_003, + testing::ext::TestSize.Level1) +{ + StorageManager::StorageStats storageStats; + std::string type = "OTHER_TYPE"; + int32_t userId = 100; + int64_t result = StorageManagerServie::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); + EXPECT_EQ(result, E_OK); + EXPECT_EQ(storageStats.video_, 0); + EXPECT_EQ(storageStats.image_, 0); + EXPECT_EQ(storageStats.file_, 0); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetMediaStorageStats_001 + * @tc.desc: check the GetMediaStorageStats function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetMediaStorageStats_001, + testing::ext::TestSize.Level1) +{ + StorageManager::StorageStats storageStats; + StorageManagerServie::GetInstance().GetMediaStorageStats(nullptr, storageStats); + EXPECT_EQ(storageStats.video_, 0); + EXPECT_EQ(storageStats.image_, 0); + EXPECT_EQ(storageStats.file_, 0); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetMediaStorageStats_002 + * @tc.desc: check the GetMediaStorageStats function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetMediaStorageStats_002, + testing::ext::TestSize.Level1) +{ + auto resultSet = std::make_shared(); + StorageManager::StorageStats storageStats; + StorageManagerServie::GetInstance().GetMediaStorageStats(nullptr, storageStats); + EXPECT_EQ(storageStats.video_, 0); + EXPECT_EQ(storageStats.image_, 0); + EXPECT_EQ(storageStats.file_, 0); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001 + * @tc.desc: check the GetQuotaSrcMountPath function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001, + testing::ext::TestSize.Level1) +{ + mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1")); + mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1")); + std::string target = "/path1"; + std::string expectedPath = "/mount1"; + std::string actualPath = GetQuotaSrcMountPath(target); + EXPECT_EQ(expectedPath, actualPath); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetQuotaSrcMountPath_002 + * @tc.desc: check the GetQuotaSrcMountPath function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_002, + testing::ext::TestSize.Level1) +{ + mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1")); + mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1")); + std::string target = "/path3"; + std::string expectedPath = ""; + std::string actualPath = GetQuotaSrcMountPath(target); + EXPECT_EQ(expectedPath, actualPath); +} + +/** + * @tc.name: Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001 + * @tc.desc: check the GetQuotaSrcMountPath function + * @tc.type: FUNC + * @tc.require: AR000IGCR7 + */ +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001, + testing::ext::TestSize.Level1) +{ + std::string path1 = "AAA"; + std::string path2 = "BBB"; + EXPECT_TRUE(PathSortFunc(path1, path2)); +} +} -- Gitee From b2486d086ba608647650284b491a972a5fdd98f6 Mon Sep 17 00:00:00 2001 From: huhui Date: Mon, 9 Jun 2025 14:58:52 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../backup_sa/include/module_external/storage_manager_service.h | 1 - .../backup_sa/src/module_external/storage_manager_service.cpp | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h index 3fda6fa47..eb204e0d1 100644 --- a/services/backup_sa/include/module_external/storage_manager_service.h +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -71,7 +71,6 @@ const std::int32_t E_MEDIALIBRARY_ERROR = STORAGE_SERVICE_SYS_CAP_TAG + 1202; const std::int32_t E_QUERY = STORAGE_SERVICE_SYS_CAP_TAG + 1206; const std::int32_t E_GETROWCOUNT = STORAGE_SERVICE_SYS_CAP_TAG + 1207; const std::int32_t E_SYS_KERNEL_ERR = STORAGE_SERVICE_SYS_CAP_TAG + 8; -const int32_t E_SYS_ERR = -1; const int MEDIA_TYPE_IMAGE = 1; const int MEDIA_TYPE_VIDEO = 2; const int MEDIA_TYPE_AUDIO = 3; diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index 3f45368ce..51efaf430 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -227,7 +227,7 @@ bool StorageManagerServie::InitialiseQuotaMounts() int32_t StorageManagerServie::UpdateMemoryPara(int32_t size, int32_t oldSize) { - return 0; + return E_OK; } int32_t StorageManagerServie::GetBundleStatsForIncrease(uint32_t userId, const std::vector &bundleNames, -- Gitee From 2bb2a9934be1b918579ba3d1717e78bc9f917486 Mon Sep 17 00:00:00 2001 From: huhui Date: Tue, 10 Jun 2025 20:13:46 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../src/module_external/sms_adapter.cpp | 5 ++- .../storage_manager_service.cpp | 43 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/services/backup_sa/src/module_external/sms_adapter.cpp b/services/backup_sa/src/module_external/sms_adapter.cpp index 7884ce54a..5bb8ccb3a 100644 --- a/services/backup_sa/src/module_external/sms_adapter.cpp +++ b/services/backup_sa/src/module_external/sms_adapter.cpp @@ -96,8 +96,9 @@ int32_t StorageMgrAdapter::GetBundleStatsForIncrease(uint32_t userId, const std: auto storageMgr = StorageManagerService::GetInstance(); if (storageMgr.GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, incPkgFileSizes)) { - throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get user storage stats"); + HILOGE("An error occured StorageManagerService GetBundleStatsForIncrease"); + return E_ERR; } - return 0; + return E_OK; } } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index 51efaf430..8aa05ed1e 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -24,6 +24,7 @@ #include #include #include + #include "b_error/b_error.h" #include "b_resources/b_constants.h" #include "filemgmt_helper.h" @@ -71,7 +72,8 @@ StorageManager::BundleStats StorageManagerServie::GetBundleStats(const string &b return bundleStats; } -int32_t StorageStatusService::GetUserStorageStatsByType(int32_t userId, StorageStats &storageStats, std::string type) +int32_t StorageStatusService::GetUserStorageStatsByType(int32_t userId, StorageStats &storageStats, + std::string type) { storageStats.video_ = 0; storageStats.image_ = 0; @@ -135,7 +137,8 @@ int32_t StorageManagerServie::GetMediaStorageStats(StorageStats &storageStats) return E_OK; } -void StorageManagerServie::GetMediaTypeAndSize(const std::shared_ptr &resultSet, StorageStats &storageStats) +void StorageManagerServie::GetMediaTypeAndSize(const std::shared_ptr &resultSet, + StorageStats &storageStats) { if (resultSet == nullptr) { LOGE("StorageStatusService::GetMediaTypeAndSize, input resultSet is nullptr."); @@ -191,8 +194,6 @@ int32_t StorageManagerServie::GetFileStorageStats(int32_t userId, StorageStats & storageStats.file_ = static_cast(dq.dqb_curspace); HILOGE("GetOccupiedSpaceForUid size:%{public}s", std::tostring(storageStats.file_).c_str()); return E_OK; - - } bool StorageManagerServie::InitialiseQuotaMounts() @@ -221,7 +222,6 @@ bool StorageManagerServie::InitialiseQuotaMounts() } } } - return true; } @@ -239,7 +239,6 @@ int32_t StorageManagerServie::GetBundleStatsForIncrease(uint32_t userId, const s LOGE("Invalid paramters, size of bundleNames should match incrementalBackTimes."); return E_PARAMS_INVALID; } - for (size_t i = 0; i < bundleNames.size(); i++) { std::string bundleName = bundleNames[i]; int64_t lastBackupTime = incrementalBackTimes[i]; @@ -248,8 +247,8 @@ int32_t StorageManagerServie::GetBundleStatsForIncrease(uint32_t userId, const s return E_OK; } -void StorageManagerServie::GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, int64_t lastBackupTime, - std::vector &pkgFileSizes, std::vector &incPkgFileSizes) +void StorageManagerServie::GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, + int64_t lastBackupTime, std::vector &pkgFileSizes, std::vector &incPkgFileSizes) { // input parameters BundleStatsParas paras = {.userId = userId, .bundleName = bundleName, @@ -351,8 +350,9 @@ std::tuple, std::vector> StorageManagerSer return {includes, excludes}; } -void StorageManagerServie::DealWithIncludeFiles(const BundleStatsParas ¶s, const std::vector &includes, - std::vector &phyIncludes, std::map& pathMap) +void StorageManagerServie::DealWithIncludeFiles(const BundleStatsParas ¶s, + const std::vector &includes, std::vector &phyIncludes, + std::map& pathMap) { uint32_t userId = paras.userId; std::string bundleName = paras.bundleName; @@ -500,13 +500,12 @@ std::tuple StorageManagerServie::CheckIfDirForIncludes(const std::st } // check whether the path exists struct stat fileStatInfo = {0}; - // if (stat(path.c_str(), &fileStatInfo) != 0) { - LOGE("CheckIfDirForIncludes call stat error %{private}s, fail errno:%{public}d", path.c_str(), errno); + LOGE("CheckIfDirForIncludes call stat error %{public}s, fail errno:%{public}d", path.c_str(), errno); return {false, false}; } if (S_ISDIR(fileStatInfo.st_mode)) { - LOGI("%{private}s exists and is a directory", path.c_str()); + LOGI("%{public}s exists and is a directory", path.c_str()); return {true, true}; } else { std::string sandboxPath = path; @@ -592,8 +591,9 @@ bool StorageManagerServie::GetIncludesFileStats(const std::string &dir, BundleSt return true; } -bool StorageManagerServie::GetPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &includeWildCard, - std::vector &includePathList, std::map &pathMap) +bool StorageManagerServie::GetPathWildCard(uint32_t userId, const std::string &bundleName, + const std::string &includeWildCard, std::vector &includePathList, + std::map &pathMap) { size_t pos = includeWildCard.rfind(WILDCARD_DEFAULT_INCLUDE); if (pos == std::string::npos) { @@ -603,11 +603,11 @@ bool StorageManagerServie::GetPathWildCard(uint32_t userId, const std::string &b std::string pathBeforeWildCard = includeWildCard.substr(0, pos); DIR *dirPtr = opendir(pathBeforeWildCard.c_str()); if (dirPtr == nullptr) { - LOGE("GetPathWildCard open file dir:%{private}s fail, errno:%{public}d", pathBeforeWildCard.c_str(), errno); + LOGE("GetPathWildCard open file dir:%{public}s fail, errno:%{public}d", pathBeforeWildCard.c_str(), errno); return false; } struct dirent *entry = nullptr; - std::vector subDirs; + while ((entry = readdir(dirPtr)) != nullptr) { if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) { continue; @@ -706,8 +706,8 @@ void StorageManagerServie::WriteFileList(std::ofstream &statFile, struct FileSta paras.fileSizeSum += fileStat.fileSize; } -bool StorageManagerServie::AddOuterDirIntoFileStat(const std::string &dir, BundleStatsParas ¶s, const std::string &sandboxDir, - std::ofstream &statFile, std::map &excludesMap) +bool StorageManagerServie::AddOuterDirIntoFileStat(const std::string &dir, BundleStatsParas ¶s, + const std::string &sandboxDir, std::ofstream &statFile, std::map &excludesMap) { if (!statFile.is_open() || dir.empty()) { LOGE("AddOuterDirIntoFileStat Param failed"); @@ -721,7 +721,6 @@ bool StorageManagerServie::AddOuterDirIntoFileStat(const std::string &dir, Bundl struct FileStat fileStat = {}; fileStat.filePath = PhysicalToSandboxPath(dir, sandboxDir, dir); fileStat.fileSize = fileInfo.st_size; - // // mode fileStat.mode = static_cast(fileInfo.st_mode); int64_t lastUpdateTime = static_cast(fileInfo.st_mtime); @@ -762,8 +761,8 @@ void StorageManagerServie::InsertStatFile(const std::string &path, struct FileSt } } -bool StorageManagerServie::AddPathMapForPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &phyPath, - std::map &pathMap) +bool StorageManagerServie::AddPathMapForPathWildCard(uint32_t userId, const std::string &bundleName, + const std::string &phyPath, std::map &pathMap) { std::string physicalPrefixEl1 = PHY_APP + EL1 + FILE_SEPARATOR_CHAR + std::to_string(userId) + BASE + bundleName + FILE_SEPARATOR_CHAR; -- Gitee From 0ea4c286accaac05c9d2d0a1eceb1b02180151e2 Mon Sep 17 00:00:00 2001 From: huhui Date: Wed, 11 Jun 2025 11:10:13 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../module_external/storage_manager_service.h | 24 +-- .../src/module_external/sms_adapter.cpp | 2 +- .../storage_manager_service.cpp | 172 +++++++++--------- .../backup_sa/module_external/BUILD.gn | 15 +- .../storage_manager_service_test.cpp | 128 ++++++------- 5 files changed, 176 insertions(+), 165 deletions(-) diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h index eb204e0d1..cad751ad2 100644 --- a/services/backup_sa/include/module_external/storage_manager_service.h +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -35,9 +35,9 @@ const std::string BASE_EL2 = "/data/storage/el2/base/"; const std::string EL1 = "el1"; const std::string EL2 = "el2"; const std::string BASE = "/base/"; -const std::string Backup_INCLUDE = "INCLUDES"; -const std::string Backup_EXCLUDE = "EXCLUDES"; -const std::string BACKUP_PATH_PREFIX = "/data/service/el2"; +const std::string BACKUP_INCLUDE = "INCLUDES"; +const std::string BACKUP_EXCLUDE = "EXCLUDES"; +const std::string BACKUP_PATH_PREFIX = "/data/service/el2/"; const std::string BACKUP_PATH_SURFFIX = "/backup/backup_sa/"; const std::string BACKUP_INCEXC_SYMBOL = "incExc_"; const std::string BACKUP_STAT_SYMBOL = "stat_"; @@ -47,14 +47,14 @@ const std::string DEFAULT_INCLUDE_PATH_IN_HAP_PREFERENCE = "preferences"; const std::string MEDIA_CLOUD_SAND_PREFIX = "/storage/cloud"; const std::string MEDIA_SAND_PREFIX = "/storage/media"; const std::string URI_PREFIX = "file://"; -const std::string NORMAL_SAND_PREFIX = "/data/storage/"; +const std::string NORMAL_SAND_PREFIX = "/data/storage"; const std::string FILE_SAND_PREFIX = "/storage/Users"; const std::string FILE_AUTHORITY = "docs"; -const std::string DEFAULT_PATH_WITH_WILDCARD = "/haps/*"; +const std::string DEFAULT_PATH_WITH_WILDCARD = "haps/*"; const std::string FILE_CONTENT_SEPARATOR = ";"; const char LINE_SEP = '\n'; const std::string VER_10_LINE1 = "version=1.0&attrNum=8"; -const std::string VER_10_LINE2 = "path;mode;dir;size;mtime;hash;inIncremental;encodeFlag"; +const std::string VER_10_LINE2 = "path;mode;dir;size;mtime;hash;isIncremental;encodeFlag"; const std::string MEDIALIBRARY_DATA_URI = "datashare:///media"; const std::string MEDIA_QUERYOPRN_QUERYVOLUME = "query_media_volume"; const std::string MEDIA_TYPE = "media"; @@ -64,7 +64,7 @@ constexpr int32_t E_ERR = -1; constexpr int32_t STORAGE_SERVICE_SYS_CAP_TAG = 13600000; constexpr const char *QUOTA_DEVICE_DATA_PATH = "/data"; constexpr const char *PROC_MOUNTS_PATH = "/proc/mounts"; -constexpr const char *DEV_BLOCK_PATH = "/dev/block"; +constexpr const char *DEV_BLOCK_PATH = "/dev/block/"; const std::int32_t E_SA_IS_NULLPTR = STORAGE_SERVICE_SYS_CAP_TAG + 12; const std::int32_t E_REMOTE_IS_NULLPTR = STORAGE_SERVICE_SYS_CAP_TAG + 13; const std::int32_t E_MEDIALIBRARY_ERROR = STORAGE_SERVICE_SYS_CAP_TAG + 1202; @@ -96,11 +96,11 @@ struct BundleStatsParas { int64_t incFileSizeSum; }; -class StorageManagerServie { +class StorageManagerServcie { public: - StorageManagerServie(); - ~StorageManagerServie(); - static StorageManagerServie &GetInstance(); + StorageManagerServcie(); + ~StorageManagerServcie(); + static StorageManagerServcie &GetInstance(); /** * @brief Get the bundle stats object * @@ -175,6 +175,6 @@ private: StorageManager::StorageStats &storageStats); int32_t GetFileStorageStats(int32_t userId, StorageManager::StorageStats &storageStats); bool InitialiseQuotaMounts(); -} +}; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_STORAGE_MGR_SERVICE_H \ No newline at end of file diff --git a/services/backup_sa/src/module_external/sms_adapter.cpp b/services/backup_sa/src/module_external/sms_adapter.cpp index 5bb8ccb3a..2919a5060 100644 --- a/services/backup_sa/src/module_external/sms_adapter.cpp +++ b/services/backup_sa/src/module_external/sms_adapter.cpp @@ -53,7 +53,7 @@ StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bund StorageManager::BundleStats bundleStats; auto storageMgr = GetStorageManager(); if (storageMgr->GetBundleStats(bundleName, bundleStats, 0, 0)) { - throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get bundle stats"); + HILOGE("An error occured StorageMgrAdapter GetBundleStats"); } return bundleStats; } diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index 8aa05ed1e..b9060b85b 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -27,7 +27,7 @@ #include "b_error/b_error.h" #include "b_resources/b_constants.h" -#include "filemgmt_helper.h" +#include "filemgmt_libhilog.h" #include "sandbox_helper.h" #include "file_uri.h" @@ -53,26 +53,26 @@ static std::string GetQuotaSrcMountPath(const std::string &target) } } -StorageManagerServie::StorageManagerServie() +StorageManagerService::StorageManagerService() { } -StorageManagerServie::~StorageManagerServie() +StorageManagerService::~StorageManagerService() { } -StorageManagerServie &StorageManagerServie::GetInstance() +StorageManagerService &StorageManagerService::GetInstance() { - static StorageManagerServie instance{}; + static StorageManagerService instance{}; return instance; } -StorageManager::BundleStats StorageManagerServie::GetBundleStats(const string &bundleName) +StorageManager::BundleStats StorageManagerService::GetBundleStats(const string &bundleName) { StorageManager::BundleStats bundleStats; return bundleStats; } -int32_t StorageStatusService::GetUserStorageStatsByType(int32_t userId, StorageStats &storageStats, +int32_t StorageManagerService::GetUserStorageStatsByType(int32_t userId, StorageManager::StorageStats &storageStats, std::string type) { storageStats.video_ = 0; @@ -80,68 +80,68 @@ int32_t StorageStatusService::GetUserStorageStatsByType(int32_t userId, StorageS storageStats.file_ = 0; int32_t err = E_OK; if (type == MEDIA_TYPE) { - LOGI("GetUserStorageStatsByType media"); + HILOGI("GetUserStorageStatsByType media"); err = GetMediaStorageStats(storageStats); } else if (type == FILE_TYPE) { - LOGI("GetUserStorageStatsByType file"); + HILOGI("GetUserStorageStatsByType file"); err = GetFileStorageStats(userId, storageStats); } else { - LOGI("GetUserStorageStatsByType type: %{public}s", type.c_str()); + HILOGI("GetUserStorageStatsByType type: %{public}s", type.c_str()); } return err; } -int32_t StorageManagerServie::GetMediaStorageStats(StorageStats &storageStats) +int32_t StorageManagerServie::GetMediaStorageStats(StorageManager::StorageStats &storageStats) { - LOGE("GetMediaStorageStats start"); + HILOGE("GetMediaStorageStats start"); auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (sam == nullptr) { - LOGE("StorageStatusService::GetMediaStorageStats samgr == nullptr"); + HILOGE("StorageStatusService::GetMediaStorageStats samgr == nullptr"); return E_SA_IS_NULLPTR; } auto remoteObj = sam->GetSystemAbility(FILEMANAGEMENT_BACKUP_SERVICE_SA_ID); if (remoteObj == nullptr) { - LOGE("StorageStatusService::GetMediaStorageStats remoteObj == nullptr"); + HILOGE("StorageStatusService::GetMediaStorageStats remoteObj == nullptr"); return E_REMOTE_IS_NULLPTR; } int32_t tryCount = 1; - LOGE("GetMediaStorageStats start Creator"); + HILOGE("GetMediaStorageStats start Creator"); auto dataShareHelper = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI); while (dataShareHelper == nullptr && tryCount < GET_DATA_SHARE_HELPER_TIMES) { - LOGW("dataShareHelper is retrying, attempt %{public}d", tryCount); + HILOGW("dataShareHelper is retrying, attempt %{public}d", tryCount); dataShareHelper = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI); tryCount++; } if (dataShareHelper == nullptr) { - LOGE("dataShareHelper is null!"); + HILOGE("dataShareHelper is null!"); return E_MEDIALIBRARY_ERROR; } vector columns; Uri uri(MEDIALIBRARY_DATA_URI + "/" + MEDIA_QUERYOPRN_QUERYVOLUME + "/" + MEDIA_QUERYOPRN_QUERYVOLUME); DataShare::DataSharePredicates predicates; - LOGE("GetMediaStorageStats start Query"); + HILOGE("GetMediaStorageStats start Query"); auto queryResultSet = dataShareHelper->Query(uri, predicates, columns); if (queryResultSet == nullptr) { - LOGE("queryResultSet is null!"); + HILOGE("queryResultSet is null!"); return E_QUERY; } auto count = 0; auto ret = queryResultSet->GetRowCount(count); if ((ret != E_OK) || (count < 0)) { - LOGE("get row count from rdb failed"); + HILOGE("get row count from rdb failed"); return E_GETROWCOUNT; } GetMediaTypeAndSize(queryResultSet, storageStats); dataShareHelper->Release(); - LOGE("GetMediaStorageStats end"); + HILOGE("GetMediaStorageStats end"); return E_OK; } -void StorageManagerServie::GetMediaTypeAndSize(const std::shared_ptr &resultSet, - StorageStats &storageStats) +void StorageManagerService::GetMediaTypeAndSize(const std::shared_ptr &resultSet, + StorageManager::StorageStats &storageStats) { if (resultSet == nullptr) { - LOGE("StorageStatusService::GetMediaTypeAndSize, input resultSet is nullptr."); + HILOGE("StorageStatusService::GetMediaTypeAndSize, input resultSet is nullptr."); return; } int thumbnailType = -1; @@ -150,14 +150,14 @@ void StorageManagerServie::GetMediaTypeAndSize(const std::shared_ptrGetColumnIndex("media_type", index) || resultSet->GetInt(index, mediatype)) { - LOGE("get media_type column index or int value err."); + HILOGE("get media_type column index or int value err."); continue; } if (resultSet->GetColumnIndex("size", index) || resultSet->GetLong(index, size)) { - LOGE("get size column index or long value err."); + HILOGE("get size column index or long value err."); continue; } - LOGI("media type: %{public}d, size: %{public}lld", mediatype, static_cast(size)); + HILOGI("media type: %{public}d, size: %{public}lld", mediatype, static_cast(size)); if (mediatype == MEDIA_TYPE_IMAGE || mediatype == thumbnailType) { storageStats.image_ += size; } else if (mediatype == MEDIA_TYPE_AUDIO) { @@ -165,18 +165,18 @@ void StorageManagerServie::GetMediaTypeAndSize(const std::shared_ptr(&dq)) != 0) { HILOGE("Failed to get quotactl, errno : %{public}d", errno); - return E_SYS_KERNEL_ERR + return E_SYS_KERNEL_ERR; } storageStats.file_ = static_cast(dq.dqb_curspace); - HILOGE("GetOccupiedSpaceForUid size:%{public}s", std::tostring(storageStats.file_).c_str()); + HILOGE("GetOccupiedSpaceForUid size:%{public}s", std::to_string(storageStats.file_).c_str()); return E_OK; } -bool StorageManagerServie::InitialiseQuotaMounts() +bool StorageManagerService::InitialiseQuotaMounts() { std::lock_guard lock(mMountsLock); mQuotaReverseMounts.clear(); std::ifstream in(PROC_MOUNTS_PATH); if (!in.is_open()) { - LOGE("Failed to open mounts file"); + HILOGE("Failed to open mounts file"); return false; } std::string source; @@ -225,19 +225,19 @@ bool StorageManagerServie::InitialiseQuotaMounts() return true; } -int32_t StorageManagerServie::UpdateMemoryPara(int32_t size, int32_t oldSize) +int32_t StorageManagerService::UpdateMemoryPara(int32_t size, int32_t oldSize) { return E_OK; } -int32_t StorageManagerServie::GetBundleStatsForIncrease(uint32_t userId, const std::vector &bundleNames, +int32_t StorageManagerService::GetBundleStatsForIncrease(uint32_t userId, const std::vector &bundleNames, const std::vector &incrementalBackTimes, std::vector &pkgFileSizes, std::vector &incPkgFileSizes) { - LOGI("GetBundleStatsForIncrease start"); + HILOGI("GetBundleStatsForIncrease start"); if (bundleNames.size() != incrementalBackTimes.size()) { - LOGE("Invalid paramters, size of bundleNames should match incrementalBackTimes."); - return E_PARAMS_INVALID; + HILOGE("Invalid paramters, size of bundleNames should match incrementalBackTimes."); + return E_ERR; } for (size_t i = 0; i < bundleNames.size(); i++) { std::string bundleName = bundleNames[i]; @@ -247,7 +247,7 @@ int32_t StorageManagerServie::GetBundleStatsForIncrease(uint32_t userId, const s return E_OK; } -void StorageManagerServie::GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, +void StorageManagerService::GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, int64_t lastBackupTime, std::vector &pkgFileSizes, std::vector &incPkgFileSizes) { // input parameters @@ -269,7 +269,7 @@ void StorageManagerServie::GetBundleStatsForIncreaseEach(uint32_t userId, std::s // recognize physical path for include directory DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); if (phyIncludes.empty()) { - LOGE("Incorrect convert for include sandbox path for %{private}s", bundleName.c_str()); + HILOGE("Incorrect convert for include sandbox path for %{private}s", bundleName.c_str()); pkgFileSizes.emplace_back(0); incPkgFileSizes.emplace_back(0); return; @@ -291,7 +291,7 @@ void StorageManagerServie::GetBundleStatsForIncreaseEach(uint32_t userId, std::s std::ofstream statFile; statFile.open(filePath.data(), std::ios::out | std::ios::trunc); if (!statFile.is_open()) { - LOGE("creat file fail, errno:%{public}d.", errno); + HILOGE("creat file fail, errno:%{public}d.", errno); pkgFileSizes.emplace_back(0); incPkgFileSizes.emplace_back(0); return; @@ -304,15 +304,15 @@ void StorageManagerServie::GetBundleStatsForIncreaseEach(uint32_t userId, std::s // calculate summary file sizes pkgFileSizes.emplace_back(paras.fileSizeSum); incPkgFileSizes.emplace_back(paras.incFileSizeSum); - LOGI("bundleName: %{public}s, size: %{public}lld", bundleName.c_str(), static_cast(paras.fileSizeSum)); + HILOGI("bundleName: %{public}s, size: %{public}lld", bundleName.c_str(), static_cast(paras.fileSizeSum)); statFile.close(); } -std::tuple, std::vector> StorageManagerServie::ReadIncludesExcludesPath( +std::tuple, std::vector> StorageManagerService::ReadIncludesExcludesPath( const std::string &bundleName, const int64_t lastBackupTime, const uint32_t userId) { if (bundleName.empty()) { - LOGE("bundleName is empty"); + HILOGE("bundleName is empty"); return { {}, {} }; } // 保存includeExclude的path @@ -321,7 +321,7 @@ std::tuple, std::vector> StorageManagerSer std::ifstream incExcFile; incExcFile.open(filePath.data()); if (!incExcFile.is_open()) { - LOGE("Cannot open include/exclude file, fail errno:%{public}d", errno); + HILOGE("Cannot open include/exclude file, fail errno:%{public}d", errno); return { {}, {} }; } @@ -332,7 +332,7 @@ std::tuple, std::vector> StorageManagerSer std::string line; std::getline(incExcFile, line); if (line.empty()) { - LOGI("Read Complete"); + HILOGI("Read Complete"); break; } if (line == BACKUP_INCLUDE) { @@ -350,7 +350,7 @@ std::tuple, std::vector> StorageManagerSer return {includes, excludes}; } -void StorageManagerServie::DealWithIncludeFiles(const BundleStatsParas ¶s, +void StorageManagerService::DealWithIncludeFiles(const BundleStatsParas ¶s, const std::vector &includes, std::vector &phyIncludes, std::map& pathMap) { @@ -366,7 +366,7 @@ void StorageManagerServie::DealWithIncludeFiles(const BundleStatsParas ¶s, // recognize sandbox path to physical path with wild card RecognizeSandboxWildCard(userId, bundleName, includeStr, phyIncludes, pathMap); if (phyIncludes.empty()) { - LOGE("DealWithIncludeFiles failed to recognize path with wildcard %{private}s", bundleName.c_str()); + HILOGE("DealWithIncludeFiles failed to recognize path with wildcard %{private}s", bundleName.c_str()); continue; } } else { @@ -376,7 +376,7 @@ void StorageManagerServie::DealWithIncludeFiles(const BundleStatsParas ¶s, } } -void StorageManagerServie::ConvertSandboxRealPath(const uint32_t userId, const std::string &bundleName, +void StorageManagerService::ConvertSandboxRealPath(const uint32_t userId, const std::string &bundleName, const std::string &sandboxPathStr, std::vector &realPaths, std::map& pathMap) { @@ -410,7 +410,7 @@ void StorageManagerServie::ConvertSandboxRealPath(const uint32_t userId, const s int ret = AppFileService::SandboxHelper::GetBackupPhysicalPath(uri.ToString(), std::to_string(userId), physicalPath); if (ret != 0) { - LOGE("Get physical path failed with %{public}d", ret); + HILOGE("Get physical path failed with %{public}d", ret); return; } realPaths.emplace_back(physicalPath); @@ -418,7 +418,7 @@ void StorageManagerServie::ConvertSandboxRealPath(const uint32_t userId, const s } } -void StorageManagerServie::DeduplicationPath(std::vector &configPaths) +void StorageManagerService::DeduplicationPath(std::vector &configPaths) { sort(configPaths.begin(), configPaths.end(), PathSortFunc); auto it = unique(configPaths.begin(), configPaths.end(), [](const std::string &path1, const std::string &path2) { @@ -427,7 +427,7 @@ void StorageManagerServie::DeduplicationPath(std::vector &configPat configPaths.erase(it, configPaths.end()); } -void StorageManagerServie::ScanExtensionPath(BundleStatsParas ¶s, +void StorageManagerService::ScanExtensionPath(BundleStatsParas ¶s, const std::vector &includes, const std::vector &excludes, std::map &pathMap, std::ofstream &statFile) { @@ -444,12 +444,12 @@ void StorageManagerServie::ScanExtensionPath(BundleStatsParas ¶s, } // recognize all file in include directory if (isDir && !GetIncludesFileStats(includeDir, paras, pathMap, statFile, excludesMap)) { - LOGE("Faied to get include files for includeDir"); + HILOGE("Faied to get include files for includeDir"); } } } -void StorageManagerServie::RecognizeSandboxWildCard(const uint32_t userId, const std::string &bundleName, +void StorageManagerService::RecognizeSandboxWildCard(const uint32_t userId, const std::string &bundleName, const std::string &sandboxPathStr, std::vector &phyIncludes, std::map& pathMap) { @@ -458,27 +458,27 @@ void StorageManagerServie::RecognizeSandboxWildCard(const uint32_t userId, const bundleName + FILE_SEPARATOR_CHAR; std::string relatePath = sandboxPathStr.substr(BASE_EL1.size()); if (!GetPathWildCard(userId, bundleName, physicalPrefix + relatePath, phyIncludes, pathMap)) { - LOGE("el1 GetPathWildCard dir path invaild"); + HILOGE("el1 GetPathWildCard dir path invaild"); } } else if (sandboxPathStr.find(BASE_EL2 + DEFAULT_PATH_WITH_WILDCARD) == 0) { std::string physicalPrefix = PHY_APP + EL2 + FILE_SEPARATOR_CHAR + std::to_string(userId) + BASE + bundleName + FILE_SEPARATOR_CHAR; std::string relatePath = sandboxPathStr.substr(BASE_EL2.size()); if (!GetPathWildCard(userId, bundleName, physicalPrefix + relatePath, phyIncludes, pathMap)) { - LOGE("el2 GetPathWildCard dir path invaild"); + HILOGE("el2 GetPathWildCard dir path invaild"); } } } -void StorageManagerServie::SetExcludePathMap(std::string &excludePath, std::map &excludesMap) +void StorageManagerService::SetExcludePathMap(std::string &excludePath, std::map &excludesMap) { if (excludePath.empty()) { - LOGE("SetExcludePathMap Param failed"); + HILOGE("SetExcludePathMap Param failed"); return; } struct stat fileStatInfo = {0}; if (stat(excludePath.c_str(), &fileStatInfo) != 0) { - LOGE("SetExcludePathMap call stat error %{private}s, errno:%{public}d", excludePath.c_str(), errno); + HILOGE("SetExcludePathMap call stat error %{private}s, errno:%{public}d", excludePath.c_str(), errno); return; } if (S_ISDIR(fileStatInfo.st_mode)) { @@ -491,21 +491,21 @@ void StorageManagerServie::SetExcludePathMap(std::string &excludePath, std::map< } } -std::tuple StorageManagerServie::CheckIfDirForIncludes(const std::string &path, BundleStatsParas ¶s, +std::tuple StorageManagerService::CheckIfDirForIncludes(const std::string &path, BundleStatsParas ¶s, std::map &pathMap, std::ofstream &statFile, std::map &excludesMap) { if (!statFile.is_open() || path.empty()) { - LOGE("CheckIfDirForIncludes Param failed"); + HILOGE("CheckIfDirForIncludes Param failed"); return {false, false}; } // check whether the path exists struct stat fileStatInfo = {0}; if (stat(path.c_str(), &fileStatInfo) != 0) { - LOGE("CheckIfDirForIncludes call stat error %{public}s, fail errno:%{public}d", path.c_str(), errno); + HILOGE("CheckIfDirForIncludes call stat error %{public}s, fail errno:%{public}d", path.c_str(), errno); return {false, false}; } if (S_ISDIR(fileStatInfo.st_mode)) { - LOGI("%{public}s exists and is a directory", path.c_str()); + HILOGI("%{public}s exists and is a directory", path.c_str()); return {true, true}; } else { std::string sandboxPath = path; @@ -532,7 +532,7 @@ std::tuple StorageManagerServie::CheckIfDirForIncludes(const std::st } } -bool StorageManagerServie::GetIncludesFileStats(const std::string &dir, BundleStatsParas ¶s, +bool StorageManagerService::GetIncludesFileStats(const std::string &dir, BundleStatsParas ¶s, std::map &pathMap, std::ofstream &statFile, std::map &excludesMap) { @@ -553,7 +553,7 @@ bool StorageManagerServie::GetIncludesFileStats(const std::string &dir, BundleSt folderStack.pop(); DIR *dirPtr = opendir(filePath.c_str()); if (dirPtr == nullptr) { - LOGE("GetIncludesFileStats open file dir:%{private}s fail, errno:%{public}d", filePath.c_str(), errno); + HILOGE("GetIncludesFileStats open file dir:%{private}s fail, errno:%{public}d", filePath.c_str(), errno); continue; } if (filePath.back() != FILE_SEPARATOR_CHAR) { @@ -568,7 +568,7 @@ bool StorageManagerServie::GetIncludesFileStats(const std::string &dir, BundleSt std::string path = filePath + entry->d_name; struct stat fileInfo = {0}; if (stat(path.c_str(), &fileInfo) != 0) { - LOGE("GetIncludesFileStats call stat error %{private}s, errno:%{public}d", path.c_str(), errno); + HILOGE("GetIncludesFileStats call stat error %{private}s, errno:%{public}d", path.c_str(), errno); fileInfo.st_size = 0; } struct FileStat fileStat = {}; @@ -591,23 +591,23 @@ bool StorageManagerServie::GetIncludesFileStats(const std::string &dir, BundleSt return true; } -bool StorageManagerServie::GetPathWildCard(uint32_t userId, const std::string &bundleName, +bool StorageManagerService::GetPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &includeWildCard, std::vector &includePathList, std::map &pathMap) { size_t pos = includeWildCard.rfind(WILDCARD_DEFAULT_INCLUDE); if (pos == std::string::npos) { - LOGE("GetPathWildCard: path should include *"); + HILOGE("GetPathWildCard: path should include *"); return false; } std::string pathBeforeWildCard = includeWildCard.substr(0, pos); DIR *dirPtr = opendir(pathBeforeWildCard.c_str()); if (dirPtr == nullptr) { - LOGE("GetPathWildCard open file dir:%{public}s fail, errno:%{public}d", pathBeforeWildCard.c_str(), errno); + HILOGE("GetPathWildCard open file dir:%{public}s fail, errno:%{public}d", pathBeforeWildCard.c_str(), errno); return false; } struct dirent *entry = nullptr; - + std::vector subDirs; while ((entry = readdir(dirPtr)) != nullptr) { if ((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) { continue; @@ -621,7 +621,7 @@ bool StorageManagerServie::GetPathWildCard(uint32_t userId, const std::string &b for (auto &subDir : subDirs) { DIR *subDirPtr = opendir(subDir.c_str()); if (subDirPtr == nullptr) { - LOGE("GetPathWildCard open file dir:%{private}s fail, errno:%{public}d", subDir.c_str(), errno); + HILOGE("GetPathWildCard open file dir:%{private}s fail, errno:%{public}d", subDir.c_str(), errno); return false; } while ((entry = readdir(subDirPtr)) != nullptr) { @@ -643,10 +643,10 @@ bool StorageManagerServie::GetPathWildCard(uint32_t userId, const std::string &b return true; } -bool StorageManagerServie::ExcludeFilter(std::map &excludesMap, const std::string &path) +bool StorageManagerService::ExcludeFilter(std::map &excludesMap, const std::string &path) { if (path.empty()) { - LOGE("ExcludeFilter Param failed"); + HILOGE("ExcludeFilter Param failed"); return true; } std::string formatPath = path; @@ -664,10 +664,10 @@ bool StorageManagerServie::ExcludeFilter(std::map &excludesMa return false; } -void StorageManagerServie::WriteFileList(std::ofstream &statFile, struct FileStat fileStat, BundleStatsParas ¶s) +void StorageManagerService::WriteFileList(std::ofstream &statFile, struct FileStat fileStat, BundleStatsParas ¶s) { if (!statFile.is_open() || fileStat.filePath.empty()) { - LOGE("WriteFileList Param failed"); + HILOGE("WriteFileList Param failed"); return; } std::string fileLine = ""; @@ -706,16 +706,16 @@ void StorageManagerServie::WriteFileList(std::ofstream &statFile, struct FileSta paras.fileSizeSum += fileStat.fileSize; } -bool StorageManagerServie::AddOuterDirIntoFileStat(const std::string &dir, BundleStatsParas ¶s, +bool StorageManagerService::AddOuterDirIntoFileStat(const std::string &dir, BundleStatsParas ¶s, const std::string &sandboxDir, std::ofstream &statFile, std::map &excludesMap) { if (!statFile.is_open() || dir.empty()) { - LOGE("AddOuterDirIntoFileStat Param failed"); + HILOGE("AddOuterDirIntoFileStat Param failed"); return false; } struct stat fileInfo = {0}; if (stat(dir.c_str(), &fileInfo) != 0) { - LOGE("AddOuterDirIntoFileStat call stat error %{private}s, fail errno:%{public}d", dir.c_str(), errno); + HILOGE("AddOuterDirIntoFileStat call stat error %{private}s, fail errno:%{public}d", dir.c_str(), errno); return false; } struct FileStat fileStat = {}; @@ -737,7 +737,7 @@ bool StorageManagerServie::AddOuterDirIntoFileStat(const std::string &dir, Bundl return true; } -std::string StorageManagerServie::PhysicalToSandboxPath(const std::string &dir, const std::string &sandboxDir, +std::string StorageManagerService::PhysicalToSandboxPath(const std::string &dir, const std::string &sandboxDir, const std::string &path) { std::size_t dirPos = dir.size(); @@ -745,11 +745,11 @@ std::string StorageManagerServie::PhysicalToSandboxPath(const std::string &dir, return sandboxDir + pathSurffix; } -void StorageManagerServie::InsertStatFile(const std::string &path, struct FileStat fileStat, +void StorageManagerService::InsertStatFile(const std::string &path, struct FileStat fileStat, std::ofstream &statFile, std::map &excludesMap, BundleStatsParas ¶s) { if (!statFile.is_open() || path.empty()) { - LOGE("InsertStatFile Param failed"); + HILOGE("InsertStatFile Param failed"); return; } std::string formatPath = path; @@ -761,7 +761,7 @@ void StorageManagerServie::InsertStatFile(const std::string &path, struct FileSt } } -bool StorageManagerServie::AddPathMapForPathWildCard(uint32_t userId, const std::string &bundleName, +bool StorageManagerService::AddPathMapForPathWildCard(uint32_t userId, const std::string &bundleName, const std::string &phyPath, std::map &pathMap) { std::string physicalPrefixEl1 = PHY_APP + EL1 + FILE_SEPARATOR_CHAR + std::to_string(userId) + BASE + @@ -775,19 +775,19 @@ bool StorageManagerServie::AddPathMapForPathWildCard(uint32_t userId, const std: std::string relatePath = phyPath.substr(physicalPrefixEl2.size()); pathMap.insert({phyPath, BASE_EL2 + relatePath}); } else { - LOGE("Invalid phyiscal path"); + HILOGE("Invalid phyiscal path"); return false; } return true; } -uint32_t CheckOverLongPath(const std::string &path) +uint32_t StorageManagerService::CheckOverLongPath(const std::string &path) { uint32_t len = path.length(); if (len >= PATH_MAX_LEN) { size_t found = path.find_last_of('/'); std::string sub = path.substr(found + 1); - LOGE("Path over long, length:%{public}d, fileName:%{public}s.", len, sub.c_str()); + HILOGE("Path over long, length:%{public}d, fileName:%{public}s.", len, sub.c_str()); } return len; } diff --git a/tests/unittests/backup_sa/module_external/BUILD.gn b/tests/unittests/backup_sa/module_external/BUILD.gn index 0388ceb63..778c59e93 100644 --- a/tests/unittests/backup_sa/module_external/BUILD.gn +++ b/tests/unittests/backup_sa/module_external/BUILD.gn @@ -68,6 +68,7 @@ ohos_unittest("storage_manager_service_test") { include_dirs = [ "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", "${path_backup}/services/backup_sa/include", + "${path_backup}/services/backup_sa/src", "${path_backup}/utils/include", "${path_backup}/utils/include/b_hilog", "${path_backup_mock}/utils_mock/include", @@ -95,18 +96,28 @@ ohos_unittest("storage_manager_service_test") { ] external_deps = [ + "ability_base:want", + "ability_runtime:ability_connect_callback_stub", + "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "aaccess_token:libtokenid_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", - "googletest:gmock_main", - "googletest:gtest_main", + "common_event_service:cesfwk_innerkits", "hilog:libhilog", + "hisysevent:libhisysevent", "hitrace:hitrace_meter", + "init:libbegetutil", "ipc:ipc_core", "jsoncpp:jsoncpp", "safwk:system_ability_fwk", "samgr:samgr_proxy", "storage_service:storage_manager_sa_proxy", + "data_share:datashare_consumer", + "data_share:datashare_common", + "googletest:gmock_main", + "googletest:gtest_main", ] use_exceptions = true diff --git a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp index be9cd6b11..334eeef1a 100644 --- a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp +++ b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp @@ -15,7 +15,7 @@ #include #include #include "b_jsonutil_mock.h" -#include "b_sa_util_mock.h" +#include "b_sa_utils_mock.h" #include #include @@ -29,18 +29,18 @@ namespace OHOS { using namespace std; using namespace testing; using namespace FileManagement::Backup; -class StorageManagerServieTest : public testing::Test { +class StorageManagerServiceTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); - void SetUp() override {}; - void TearDown() override {}; + void SetUp() override; + void TearDown() override; }; -void StorageManagerServieTest::SetUpTestCase(void) {} -void StorageManagerServieTest::TearDownTestCase(void) {} -void StorageManagerServieTest::SetUp(void) {} -void StorageManagerServieTest::TearDown(void) {} +void StorageManagerServiceTest::SetUpTestCase(void) {} +void StorageManagerServiceTest::TearDownTestCase(void) {} +void StorageManagerServiceTest::SetUp(void) {} +void StorageManagerServiceTest::TearDown(void) {} /** * @tc.name: Storage_Manager_ServiceTest_GetBundleStatsForIncrease_001 @@ -48,7 +48,7 @@ void StorageManagerServieTest::TearDown(void) {} * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsForIncrease_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsForIncrease_001, testing::ext::TestSize.Level1) { uint32_t userId = 100; @@ -56,7 +56,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsFor std::vector incrementalBackTimes = {123456789}; std::vector pkgFileSizes; std::vector incPkgFileSizes; - int32_t result = StorageManagerServie::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, + int32_t result = StorageManagerService::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, incPkgFileSizes); EXPECT_EQ(result, -1); } @@ -67,7 +67,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsFor * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsForIncrease_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsForIncrease_002, testing::ext::TestSize.Level1) { uint32_t userId = 100; @@ -75,7 +75,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsFor std::vector incrementalBackTimes = {123456789, 987654321}; std::vector pkgFileSizes; std::vector incPkgFileSizes; - int32_t result = StorageManagerServie::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, + int32_t result = StorageManagerService::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, incPkgFileSizes); EXPECT_EQ(result, 0); } @@ -86,7 +86,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsFor * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsForIncreaseEach_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsForIncreaseEach_001, testing::ext::TestSize.Level1) { uint32_t userId = 100; @@ -94,7 +94,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsFor int64_t lastBackupTime = 123456789; std::vector pkgFileSizes; std::vector incPkgFileSizes; - StorageManagerServie::GetInstance().GetBundleStatsForIncreaseEach(userId, bundleNames, incrementalBackTimes, + StorageManagerService::GetInstance().GetBundleStatsForIncreaseEach(userId, bundleName, lastBackupTime, pkgFileSizes, incPkgFileSizes); EXPECT_EQ(pkgFileSizes.size(), 1); EXPECT_EQ(pkgFileSizes[0], 0); @@ -108,13 +108,13 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetBundleStatsFor * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_001, testing::ext::TestSize.Level1) { std::string bundleName = ""; int64_t lastBackupTime = 123456789; uint32_t userId = 100; - auto result = StorageManagerServie::GetInstance().ReadIncludesExcludesPath(bundleNames, lastBackupTime, userId); + auto result = StorageManagerService::GetInstance().ReadIncludesExcludesPath(bundleNames, lastBackupTime, userId); EXPECT_TRUE(std::get<0>(result).empty()); EXPECT_TRUE(std::get<1>(result).empty()); } @@ -125,14 +125,14 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ReadIncludesExclu * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcludesPath_002, testing::ext::TestSize.Level1) { std::string bundleName = "testBundle"; int64_t lastBackupTime = 123456789; uint32_t userId = 100; // Assuming the file does not exist or cannot be opened - auto result = StorageManagerServie::GetInstance().ReadIncludesExcludesPath(bundleNames, lastBackupTime, userId); + auto result = StorageManagerService::GetInstance().ReadIncludesExcludesPath(bundleNames, lastBackupTime, userId); EXPECT_TRUE(std::get<0>(result).empty()); EXPECT_TRUE(std::get<1>(result).empty()); } @@ -143,7 +143,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ReadIncludesExclu * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_001, testing::ext::TestSize.Level1) { std::string bundleName = "com.huawei.hmos.camera"; @@ -152,7 +152,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFi std::vector includes = {"data/storage/el1/base/" + DEFAULT_PATH_WITH_WILDCARD}; std::vector phyIncludes; std::map pathMap; - StorageManagerServie::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); + StorageManagerService::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); EXPECT_TRUE(phyIncludes.empty()); } @@ -162,7 +162,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFi * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_002, testing::ext::TestSize.Level1) { std::string bundleName = "com.huawei.hmos.camera"; @@ -171,7 +171,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFi std::vector includes = {NORMAL_SAND_PREFIX}; std::vector phyIncludes; std::map pathMap; - StorageManagerServie::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); + StorageManagerService::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); EXPECT_TRUE(phyIncludes.empty()); } @@ -181,7 +181,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFi * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_003, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_003, testing::ext::TestSize.Level1) { std::string bundleName = "testBundle"; @@ -190,7 +190,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFi std::vector includes = {}; std::vector phyIncludes; std::map pathMap; - StorageManagerServie::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); + StorageManagerService::GetInstance().DealWithIncludeFiles(paras, includes, phyIncludes, pathMap); EXPECT_TRUE(phyIncludes.empty()); } @@ -200,7 +200,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_DealWithIncludeFi * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_001, testing::ext::TestSize.Level1) { uint32_t userId = 100; @@ -208,7 +208,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ConvertSandboxRea std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/path/to/file"; std::vector realPaths; std::map pathMap; - StorageManagerServie::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap); + StorageManagerService::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap); EXPECT_TRUE(realPaths.empty()); EXPECT_TRUE(pathMap.empty()); } @@ -219,7 +219,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ConvertSandboxRea * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRealPath_002, testing::ext::TestSize.Level1) { uint32_t userId = 100; @@ -227,11 +227,11 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ConvertSandboxRea std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/el2/database/"; std::vector realPaths; std::map pathMap; - StorageManagerServie::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap); + StorageManagerService::GetInstance().ConvertSandboxRealPath(userId, bundleName, sandboxPathStr, realPaths, pathMap); std::string uriString = URI_PREFIX + bundleName + AppFileService::SandboxHelper::Encode(sandboxPathStr); AppFileService::ModuleFileUri::FileUri uri(uriString); std::string physicalPath; - AppFileService::SandboxHelper::GetBackupPhysicalPath(uri.ToString, std::to_string(userId), physicalPath); + AppFileService::SandboxHelper::GetBackupPhysicalPath(uri.ToString(), std::to_string(userId), physicalPath); EXPECT_FALSE(realPaths.empty()); EXPECT_FALSE(pathMap.empty()); EXPECT_EQ(realPaths[0], physicalPath); @@ -244,7 +244,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ConvertSandboxRea * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_001, testing::ext::TestSize.Level1) { ofstream closedStatFile; @@ -253,7 +253,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncl .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::map pathMap; std::map excludesMap; - auto result = StorageManagerServie::GetInstance().CheckIfDirForIncludes("test_path", paras, + auto result = StorageManagerService::GetInstance().CheckIfDirForIncludes("test_path", paras, pathMap, closedStatFile, excludesMap); EXPECT_EQ(result, std::make_tuple(false, false)); } @@ -264,7 +264,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncl * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_002, testing::ext::TestSize.Level1) { ofstream statFile; @@ -273,7 +273,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncl .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::map pathMap; std::map excludesMap; - auto result = StorageManagerServie::GetInstance().CheckIfDirForIncludes("", paras, pathMap, closedStatFile, excludesMap); + auto result = StorageManagerService::GetInstance().CheckIfDirForIncludes("", paras, pathMap, statFile, excludesMap); EXPECT_EQ(result, std::make_tuple(false, false)); } @@ -283,18 +283,18 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncl * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_003, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_003, testing::ext::TestSize.Level1) { ofstream statFile; statFile.open("/system/etc/NOTICE.TXT", ios::out | ios::trunc); - EXPECT_FALSE(!statFile.is_open()) << "file is not open" + EXPECT_FALSE(!statFile.is_open()) << "file can not open" std::string bundleName = "testBundle"; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::map pathMap; std::map excludesMap; - auto result = StorageManagerServie::GetInstance().CheckIfDirForIncludes("/data/service", paras, pathMap, + auto result = StorageManagerService::GetInstance().CheckIfDirForIncludes("/data/service", paras, pathMap, statFile, excludesMap); EXPECT_EQ(result, std::make_tuple(true, true)); } @@ -305,12 +305,12 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_CheckIfDirForIncl * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ExcludeFilter_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ExcludeFilter_001, testing::ext::TestSize.Level1) { std::map excludesMap; std::string path = ""; - auto result = StorageManagerServie::GetInstance().ExcludeFilter(excludePath, path); + bool result = StorageManagerService::GetInstance().ExcludeFilter(excludesMap, path); EXPECT_TRUE(result); } @@ -320,12 +320,12 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ExcludeFilter_001 * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ExcludeFilter_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ExcludeFilter_002, testing::ext::TestSize.Level1) { std::map excludesMap; std::string path = "/path/to/file"; - auto result = StorageManagerServie::GetInstance().ExcludeFilter(excludePath, path); + bool result = StorageManagerService::GetInstance().ExcludeFilter(excludesMap, path); EXPECT_FALSE(result); } @@ -335,13 +335,13 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_ExcludeFilter_002 * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_001, testing::ext::TestSize.Level1) { StorageManager::StorageStats storageStats; std::string type = "MEDIA_TYPE"; int32_t userId = 100; - int64_t result = StorageManagerServie::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); + int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); EXPECT_EQ(result, E_OK); EXPECT_EQ(storageStats.video_, 0); EXPECT_EQ(storageStats.image_, 0); @@ -354,13 +354,13 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageSta * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_002, testing::ext::TestSize.Level1) { StorageManager::StorageStats storageStats; std::string type = "FILE_TYPE"; int32_t userId = 100; - int64_t result = StorageManagerServie::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); + int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); EXPECT_EQ(result, E_OK); EXPECT_EQ(storageStats.video_, 0); EXPECT_EQ(storageStats.image_, 0); @@ -373,13 +373,13 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageSta * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_003, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageStatsByType_003, testing::ext::TestSize.Level1) { StorageManager::StorageStats storageStats; std::string type = "OTHER_TYPE"; int32_t userId = 100; - int64_t result = StorageManagerServie::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); + int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); EXPECT_EQ(result, E_OK); EXPECT_EQ(storageStats.video_, 0); EXPECT_EQ(storageStats.image_, 0); @@ -387,36 +387,36 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetUserStorageSta } /** - * @tc.name: Storage_Manager_ServiceTest_GetMediaStorageStats_001 - * @tc.desc: check the GetMediaStorageStats function + * @tc.name: Storage_Manager_ServiceTest_GetMediaTypeAndSize_001 + * @tc.desc: check the GetMediaTypeAndSize function * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetMediaStorageStats_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetMediaTypeAndSize_001, testing::ext::TestSize.Level1) { StorageManager::StorageStats storageStats; - StorageManagerServie::GetInstance().GetMediaStorageStats(nullptr, storageStats); - EXPECT_EQ(storageStats.video_, 0); + StorageManagerService::GetInstance().GetMediaTypeAndSize(nullptr, storageStats); EXPECT_EQ(storageStats.image_, 0); - EXPECT_EQ(storageStats.file_, 0); + EXPECT_EQ(storageStats.audio_, 0); + EXPECT_EQ(storageStats.video_, 0); } /** - * @tc.name: Storage_Manager_ServiceTest_GetMediaStorageStats_002 - * @tc.desc: check the GetMediaStorageStats function + * @tc.name: Storage_Manager_ServiceTest_GetMediaTypeAndSize_002 + * @tc.desc: check the GetMediaTypeAndSize function * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetMediaStorageStats_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetMediaTypeAndSize_002, testing::ext::TestSize.Level1) { auto resultSet = std::make_shared(); StorageManager::StorageStats storageStats; - StorageManagerServie::GetInstance().GetMediaStorageStats(nullptr, storageStats); - EXPECT_EQ(storageStats.video_, 0); + StorageManagerService::GetInstance().GetMediaTypeAndSize(nullptr, storageStats); EXPECT_EQ(storageStats.image_, 0); - EXPECT_EQ(storageStats.file_, 0); + EXPECT_EQ(storageStats.audio_, 0); + EXPECT_EQ(storageStats.video_, 0); } /** @@ -425,11 +425,11 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetMediaStorageSt * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001, testing::ext::TestSize.Level1) { mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1")); - mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1")); + mQuotaReverseMounts.insert(std::make_pair("/path2", "/mount2")); std::string target = "/path1"; std::string expectedPath = "/mount1"; std::string actualPath = GetQuotaSrcMountPath(target); @@ -442,7 +442,7 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetQuotaSrcMountP * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_002, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_002, testing::ext::TestSize.Level1) { mQuotaReverseMounts.insert(std::make_pair("/path1", "/mount1")); @@ -454,12 +454,12 @@ HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetQuotaSrcMountP } /** - * @tc.name: Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001 - * @tc.desc: check the GetQuotaSrcMountPath function + * @tc.name: Storage_Manager_ServiceTest_PathSortFunc_001 + * @tc.desc: check the PathSortFunc function * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_GetQuotaSrcMountPath_001, +HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_PathSortFunc_001, testing::ext::TestSize.Level1) { std::string path1 = "AAA"; -- Gitee From 0941d851eab528a7be877c18d9f8209ad0c64f39 Mon Sep 17 00:00:00 2001 From: huhui Date: Wed, 11 Jun 2025 13:56:55 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../module_external/storage_manager_service.h | 12 ++++++------ .../module_external/storage_manager_service.cpp | 10 +++++----- .../unittests/backup_sa/module_external/BUILD.gn | 2 +- .../storage_manager_service_test.cpp | 15 ++++++++------- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h index cad751ad2..e3bed2c7c 100644 --- a/services/backup_sa/include/module_external/storage_manager_service.h +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -96,11 +96,11 @@ struct BundleStatsParas { int64_t incFileSizeSum; }; -class StorageManagerServcie { +class StorageManagerService { public: - StorageManagerServcie(); - ~StorageManagerServcie(); - static StorageManagerServcie &GetInstance(); + StorageManagerService(); + ~StorageManagerService(); + static StorageManagerService &GetInstance(); /** * @brief Get the bundle stats object * @@ -130,11 +130,11 @@ public: * @param bundleNames * @param incrementalBackTimes * @param pkgFileSizes bundle backup file size - * @param incPkgFileSizes Incremental bundle backup file size + * @param incPkgFileSizes Incremental bundle backup file size */ int32_t GetBundleStatsForIncrease(uint32_t userId, const std::vector &bundleNames, const std::vector &incrementalBackTimes, std::vector &pkgFileSizes, - std::vector &incPkgFileSizes); + std::vector &incPkgFileSizes); private: void GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, int64_t lastBackupTime, std::vector &pkgFileSizes, std::vector &incPkgFileSizes); diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index b9060b85b..93c2fb9c9 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ - #include "module_external/storage_manager_service.h" +#include "module_external/storage_manager_service.h" #include #include @@ -36,7 +36,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; -std:: recursive_mutex mMountsLock; +std::recursive_mutex mMountsLock; static bool PathSortFunc(const std::string &path1, const std::string &path2) { @@ -91,7 +91,7 @@ int32_t StorageManagerService::GetUserStorageStatsByType(int32_t userId, Storage return err; } -int32_t StorageManagerServie::GetMediaStorageStats(StorageManager::StorageStats &storageStats) +int32_t StorageManagerService::GetMediaStorageStats(StorageManager::StorageStats &storageStats) { HILOGE("GetMediaStorageStats start"); auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -170,7 +170,7 @@ void StorageManagerService::GetMediaTypeAndSize(const std::shared_ptr(result).empty()); EXPECT_TRUE(std::get<1>(result).empty()); } @@ -132,7 +133,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcl int64_t lastBackupTime = 123456789; uint32_t userId = 100; // Assuming the file does not exist or cannot be opened - auto result = StorageManagerService::GetInstance().ReadIncludesExcludesPath(bundleNames, lastBackupTime, userId); + auto result = StorageManagerService::GetInstance().ReadIncludesExcludesPath(bundleName, lastBackupTime, userId); EXPECT_TRUE(std::get<0>(result).empty()); EXPECT_TRUE(std::get<1>(result).empty()); } @@ -146,7 +147,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ReadIncludesExcl HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_001, testing::ext::TestSize.Level1) { - std::string bundleName = "com.huawei.hmos.camera"; + std::string bundleName = CAMERA_BUNDLENAME; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::vector includes = {"data/storage/el1/base/" + DEFAULT_PATH_WITH_WILDCARD}; @@ -165,7 +166,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeF HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_DealWithIncludeFiles_002, testing::ext::TestSize.Level1) { - std::string bundleName = "com.huawei.hmos.camera"; + std::string bundleName = CAMERA_BUNDLENAME; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; std::vector includes = {NORMAL_SAND_PREFIX}; @@ -223,7 +224,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_ConvertSandboxRe testing::ext::TestSize.Level1) { uint32_t userId = 100; - std::string bundleName = "com.huawei.hmos.camera"; + std::string bundleName = CAMERA_BUNDLENAME; std::string sandboxPathStr = NORMAL_SAND_PREFIX + "/el2/database/"; std::vector realPaths; std::map pathMap; @@ -288,7 +289,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForInc { ofstream statFile; statFile.open("/system/etc/NOTICE.TXT", ios::out | ios::trunc); - EXPECT_FALSE(!statFile.is_open()) << "file can not open" + EXPECT_FALSE(!statFile.is_open()) << "file can not open"; std::string bundleName = "testBundle"; BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; @@ -459,7 +460,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetQuotaSrcMount * @tc.type: FUNC * @tc.require: AR000IGCR7 */ -HWTEST_F(StorageManagerServieTest, Storage_Manager_ServiceTest_PathSortFunc_001, +HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_PathSortFunc_001, testing::ext::TestSize.Level1) { std::string path1 = "AAA"; -- Gitee From 26f2f965fd78883b6d9001cc32a7a438a3184d42 Mon Sep 17 00:00:00 2001 From: huhui Date: Wed, 11 Jun 2025 15:49:33 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../module_external/storage_manager_service.h | 8 ++++---- .../backup_sa/src/module_external/sms_adapter.cpp | 15 --------------- .../module_external/storage_manager_service.cpp | 4 ++-- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h index e3bed2c7c..c17a7246b 100644 --- a/services/backup_sa/include/module_external/storage_manager_service.h +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -106,11 +106,11 @@ public: * * @param bundleName bundle name */ - StorageManager::BundleStats GetBundleStats(const std::string &bundleName); + StorageManager::BundleStats GetBundleStats(const std::string &bundleName, StorageManager::BundleStats bundleStats); /** * @brief Get the user storage stats object - * + * * @param bundleName bundle name * @param userId user id */ @@ -118,14 +118,14 @@ public: /** * @brief update memory para - * + * * @param size para data */ int32_t UpdateMemoryPara(int32_t size, int32_t oldSize); /** * @brief Get the user storage stats object - * + * * @param userId user id * @param bundleNames * @param incrementalBackTimes diff --git a/services/backup_sa/src/module_external/sms_adapter.cpp b/services/backup_sa/src/module_external/sms_adapter.cpp index 2919a5060..0c375d6b0 100644 --- a/services/backup_sa/src/module_external/sms_adapter.cpp +++ b/services/backup_sa/src/module_external/sms_adapter.cpp @@ -33,21 +33,6 @@ const string EXTERNAL_FILE_HAP = "com.ohos.UserFile.ExternalFileManager"; const int64_t ERR_SIZE = -1; } // namespace -static sptr GetStorageManager() -{ - auto saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (saMgr == nullptr) { - throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get system ability manager"); - } - - auto storageObj = saMgr->GetSystemAbility(STORAGE_MANAGER_MANAGER_ID); - if (storageObj == nullptr) { - throw BError(BError::Codes::SA_BROKEN_IPC, "Failed to get storage manager service"); - } - - return iface_cast(storageObj); -} - StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bundleName) { StorageManager::BundleStats bundleStats; diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index 93c2fb9c9..bce2865cf 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -66,9 +66,9 @@ StorageManagerService &StorageManagerService::GetInstance() return instance; } -StorageManager::BundleStats StorageManagerService::GetBundleStats(const string &bundleName) +StorageManager::BundleStats StorageManagerService::GetBundleStats(const string &bundleName, + StorageManager::StorageStats storageStats) { - StorageManager::BundleStats bundleStats; return bundleStats; } -- Gitee From a7852fe019d39b4d0495cceda9b3e48006172580 Mon Sep 17 00:00:00 2001 From: huhui Date: Wed, 11 Jun 2025 16:00:08 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../backup_sa/include/module_external/storage_manager_service.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h index c17a7246b..fb1149aea 100644 --- a/services/backup_sa/include/module_external/storage_manager_service.h +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -103,7 +103,7 @@ public: static StorageManagerService &GetInstance(); /** * @brief Get the bundle stats object - * + * * @param bundleName bundle name */ StorageManager::BundleStats GetBundleStats(const std::string &bundleName, StorageManager::BundleStats bundleStats); -- Gitee From f312b7c2f54677cac56669def44eaf310caaa6fc Mon Sep 17 00:00:00 2001 From: huhui Date: Wed, 11 Jun 2025 16:16:26 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../include/module_external/storage_manager_service.h | 2 +- services/backup_sa/src/module_external/sms_adapter.cpp | 4 ++-- .../backup_sa/src/module_external/storage_manager_service.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h index fb1149aea..2dee44731 100644 --- a/services/backup_sa/include/module_external/storage_manager_service.h +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -106,7 +106,7 @@ public: * * @param bundleName bundle name */ - StorageManager::BundleStats GetBundleStats(const std::string &bundleName, StorageManager::BundleStats bundleStats); + bool GetBundleStats(const std::string &bundleName, StorageManager::BundleStats bundleStats); /** * @brief Get the user storage stats object diff --git a/services/backup_sa/src/module_external/sms_adapter.cpp b/services/backup_sa/src/module_external/sms_adapter.cpp index 0c375d6b0..9173d5110 100644 --- a/services/backup_sa/src/module_external/sms_adapter.cpp +++ b/services/backup_sa/src/module_external/sms_adapter.cpp @@ -36,8 +36,8 @@ const int64_t ERR_SIZE = -1; StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bundleName) { StorageManager::BundleStats bundleStats; - auto storageMgr = GetStorageManager(); - if (storageMgr->GetBundleStats(bundleName, bundleStats, 0, 0)) { + auto storageMgr = StorageManagerService::GetInstance(); + if (storageMgr.GetBundleStats(bundleName, bundleStats)) { HILOGE("An error occured StorageMgrAdapter GetBundleStats"); } return bundleStats; diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index bce2865cf..1d4373477 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -66,10 +66,10 @@ StorageManagerService &StorageManagerService::GetInstance() return instance; } -StorageManager::BundleStats StorageManagerService::GetBundleStats(const string &bundleName, +bool StorageManagerService::GetBundleStats(const string &bundleName, StorageManager::StorageStats storageStats) { - return bundleStats; + return true; } int32_t StorageManagerService::GetUserStorageStatsByType(int32_t userId, StorageManager::StorageStats &storageStats, -- Gitee From 42fc959f1d4c7e3e1a4ff76414f9ee1d4cf15a68 Mon Sep 17 00:00:00 2001 From: huhui Date: Wed, 11 Jun 2025 16:34:07 +0800 Subject: [PATCH 09/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../backup_sa/src/module_external/storage_manager_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index 1d4373477..4e561eefd 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -67,7 +67,7 @@ StorageManagerService &StorageManagerService::GetInstance() } bool StorageManagerService::GetBundleStats(const string &bundleName, - StorageManager::StorageStats storageStats) + StorageManager::BundleStats storageStats) { return true; } -- Gitee From a4ad566a3b8665efe34ed6477e8afd1da4259ba8 Mon Sep 17 00:00:00 2001 From: huhui Date: Thu, 12 Jun 2025 21:01:23 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../module_external/storage_manager_service.h | 12 ++++++++---- .../src/module_external/sms_adapter.cpp | 16 ++++++---------- .../storage_manager_service.cpp | 18 +++--------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h index 2dee44731..0899723a6 100644 --- a/services/backup_sa/include/module_external/storage_manager_service.h +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -98,15 +98,17 @@ struct BundleStatsParas { class StorageManagerService { public: - StorageManagerService(); - ~StorageManagerService(); - static StorageManagerService &GetInstance(); + static StorageManagerService &GetInstance() + { + static StorageManagerService {}; + return instance; + } /** * @brief Get the bundle stats object * * @param bundleName bundle name */ - bool GetBundleStats(const std::string &bundleName, StorageManager::BundleStats bundleStats); + bool GetBundleStats(const std::string &bundleName, StorageManager::BundleStats &bundleStats); /** * @brief Get the user storage stats object @@ -136,6 +138,8 @@ public: const std::vector &incrementalBackTimes, std::vector &pkgFileSizes, std::vector &incPkgFileSizes); private: + StorageManagerService() = default; + ~StorageManagerService() = default; void GetBundleStatsForIncreaseEach(uint32_t userId, std::string &bundleName, int64_t lastBackupTime, std::vector &pkgFileSizes, std::vector &incPkgFileSizes); std::tuple, std::vector> ReadIncludesExcludesPath( diff --git a/services/backup_sa/src/module_external/sms_adapter.cpp b/services/backup_sa/src/module_external/sms_adapter.cpp index 9173d5110..032699a1d 100644 --- a/services/backup_sa/src/module_external/sms_adapter.cpp +++ b/services/backup_sa/src/module_external/sms_adapter.cpp @@ -36,8 +36,7 @@ const int64_t ERR_SIZE = -1; StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bundleName) { StorageManager::BundleStats bundleStats; - auto storageMgr = StorageManagerService::GetInstance(); - if (storageMgr.GetBundleStats(bundleName, bundleStats)) { + if (StorageManagerService::GetInstance().GetBundleStats(bundleName, bundleStats)) { HILOGE("An error occured StorageMgrAdapter GetBundleStats"); } return bundleStats; @@ -46,15 +45,14 @@ StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bund int64_t StorageMgrAdapter::GetUserStorageStats(const std::string &bundleName, int32_t userId) { StorageManager::StorageStats bundleStats; - auto storageMgr = StorageManagerService::GetInstance(); if (bundleName == MEDIA_LIBRARY_HAP) { - if (storageMgr.GetUserStorageStatsByType(userId, bundleStats, MEDIA_TYPE)) { + if (StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, bundleStats, MEDIA_TYPE)) { HILOGE("Failed to get user media storage stats"); return ERR_SIZE; } return bundleStats.image_ + bundleStats.video_; } else if (bundleName == EXTERNAL_FILE_HAP) { - if (storageMgr.GetUserStorageStatsByType(userId, bundleStats, FILE_TYPE)) { + if (StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, bundleStats, FILE_TYPE)) { HILOGE("Failed to get user file storage stats"); return ERR_SIZE; } @@ -65,9 +63,8 @@ int64_t StorageMgrAdapter::GetUserStorageStats(const std::string &bundleName, in int32_t StorageMgrAdapter::UpdateMemPara(int32_t size) { - auto storageMgr = StorageManagerService::GetInstance(); int32_t oldSize = BConstants::DEFAULT_VFS_CACHE_PRESSURE; - if (storageMgr.UpdateMemoryPara(size, oldSize)) { + if (StorageManagerService::GetInstance().UpdateMemoryPara(size, oldSize)) { HILOGE("An error occured StorageMgrAdapter UpdateMemPara"); return BConstants::DEFAULT_VFS_CACHE_PRESSURE; } @@ -78,9 +75,8 @@ int32_t StorageMgrAdapter::GetBundleStatsForIncrease(uint32_t userId, const std: const std::vector &incrementalBackTimes, std::vector &pkgFileSizes, std::vector &incPkgFileSizes) { - auto storageMgr = StorageManagerService::GetInstance(); - if (storageMgr.GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, - incPkgFileSizes)) { + if (StorageManagerService::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, + pkgFileSizes, incPkgFileSizes)) { HILOGE("An error occured StorageManagerService GetBundleStatsForIncrease"); return E_ERR; } diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index 4e561eefd..189432d68 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -53,21 +53,8 @@ static std::string GetQuotaSrcMountPath(const std::string &target) } } -StorageManagerService::StorageManagerService() -{ -} -StorageManagerService::~StorageManagerService() -{ -} - -StorageManagerService &StorageManagerService::GetInstance() -{ - static StorageManagerService instance{}; - return instance; -} - bool StorageManagerService::GetBundleStats(const string &bundleName, - StorageManager::BundleStats storageStats) + StorageManager::BundleStats &storageStats) { return true; } @@ -656,7 +643,8 @@ bool StorageManagerService::ExcludeFilter(std::map &excludesM return true; } } else { - if (formatPath.compare(0, exclude->first.size(), exclude->first) == 0) { + if (formatPath.compare(0, exclude->first.size(), exclude->first) == 0 && + (formatPath.size() == exclude->first.size() || formatPath[exclude->first.size()] == '/')) { return true; } } -- Gitee From ab8c5c064a373f001fb2fb5c5d67daf8d21fd3b2 Mon Sep 17 00:00:00 2001 From: huhui Date: Fri, 13 Jun 2025 10:37:01 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../src/module_external/sms_adapter.cpp | 10 +++--- .../storage_manager_service.cpp | 2 +- .../storage_manager_service_test.cpp | 36 ++++++++++++++++--- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/services/backup_sa/src/module_external/sms_adapter.cpp b/services/backup_sa/src/module_external/sms_adapter.cpp index 032699a1d..e2e0fded0 100644 --- a/services/backup_sa/src/module_external/sms_adapter.cpp +++ b/services/backup_sa/src/module_external/sms_adapter.cpp @@ -36,7 +36,7 @@ const int64_t ERR_SIZE = -1; StorageManager::BundleStats StorageMgrAdapter::GetBundleStats(const string &bundleName) { StorageManager::BundleStats bundleStats; - if (StorageManagerService::GetInstance().GetBundleStats(bundleName, bundleStats)) { + if (!StorageManagerService::GetInstance().GetBundleStats(bundleName, bundleStats)) { HILOGE("An error occured StorageMgrAdapter GetBundleStats"); } return bundleStats; @@ -46,13 +46,13 @@ int64_t StorageMgrAdapter::GetUserStorageStats(const std::string &bundleName, in { StorageManager::StorageStats bundleStats; if (bundleName == MEDIA_LIBRARY_HAP) { - if (StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, bundleStats, MEDIA_TYPE)) { + if (StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, bundleStats, MEDIA_TYPE) != E_OK) { HILOGE("Failed to get user media storage stats"); return ERR_SIZE; } return bundleStats.image_ + bundleStats.video_; } else if (bundleName == EXTERNAL_FILE_HAP) { - if (StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, bundleStats, FILE_TYPE)) { + if (StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, bundleStats, FILE_TYPE) != E_OK) { HILOGE("Failed to get user file storage stats"); return ERR_SIZE; } @@ -64,7 +64,7 @@ int64_t StorageMgrAdapter::GetUserStorageStats(const std::string &bundleName, in int32_t StorageMgrAdapter::UpdateMemPara(int32_t size) { int32_t oldSize = BConstants::DEFAULT_VFS_CACHE_PRESSURE; - if (StorageManagerService::GetInstance().UpdateMemoryPara(size, oldSize)) { + if (StorageManagerService::GetInstance().UpdateMemoryPara(size, oldSize) != E_OK) { HILOGE("An error occured StorageMgrAdapter UpdateMemPara"); return BConstants::DEFAULT_VFS_CACHE_PRESSURE; } @@ -76,7 +76,7 @@ int32_t StorageMgrAdapter::GetBundleStatsForIncrease(uint32_t userId, const std: std::vector &incPkgFileSizes) { if (StorageManagerService::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, - pkgFileSizes, incPkgFileSizes)) { + pkgFileSizes, incPkgFileSizes) != E_OK) { HILOGE("An error occured StorageManagerService GetBundleStatsForIncrease"); return E_ERR; } diff --git a/services/backup_sa/src/module_external/storage_manager_service.cpp b/services/backup_sa/src/module_external/storage_manager_service.cpp index 189432d68..22a78759d 100644 --- a/services/backup_sa/src/module_external/storage_manager_service.cpp +++ b/services/backup_sa/src/module_external/storage_manager_service.cpp @@ -65,7 +65,7 @@ int32_t StorageManagerService::GetUserStorageStatsByType(int32_t userId, Storage storageStats.video_ = 0; storageStats.image_ = 0; storageStats.file_ = 0; - int32_t err = E_OK; + int32_t err = E_ERR; if (type == MEDIA_TYPE) { HILOGI("GetUserStorageStatsByType media"); err = GetMediaStorageStats(storageStats); diff --git a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp index 3960b28b6..631c586f2 100644 --- a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp +++ b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp @@ -30,6 +30,7 @@ const std::string CAMERA_BUNDLENAME = "file"; using namespace std; using namespace testing; using namespace FileManagement::Backup; +namespace fs = std::filesystem; class StorageManagerServiceTest : public testing::Test { public: static void SetUpTestCase(); @@ -59,7 +60,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetBundleStatsFo std::vector incPkgFileSizes; int32_t result = StorageManagerService::GetInstance().GetBundleStatsForIncrease(userId, bundleNames, incrementalBackTimes, pkgFileSizes, incPkgFileSizes); - EXPECT_EQ(result, -1); + EXPECT_EQ(result, E_ERR); } /** @@ -287,7 +288,30 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForInc HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForIncludes_003, testing::ext::TestSize.Level1) { - ofstream statFile; + std::ofstream statFile; + fs::path tempPath = "/data/temp.txt"; + + try { + fs::path dirPath = tempPath.parent_path(); + if (!fs::exists(dirPath)) { + fs::create_directories(dirPath); + } + fs:path canonicalPath = fs::canonical(fs::absolute(tempPath)); + statFile.open(canonicalPath, std::ios::out | std::ios::trunc); + EXPECT_FALSE(!statFile.is_open()) << "file can not open"; + std::string bundleName = "testBundle"; + BundleStatsParas paras = {.userId = 100, .bundleName = bundleName, + .lastBackupTime = 0, .fileSizeSum = 0, .incFileSizeSum = 0}; + std::map pathMap; + std::map excludesMap; + auto result = StorageManagerService::GetInstance().CheckIfDirForIncludes("/data/service", paras, pathMap, + statFile, excludesMap); + EXPECT_EQ(result, std::make_tuple(true, true)); + fs::remove(canonicalPath); + } catch (const fs::filesystem_error& e) { + std::cerr << "Filesystem error: " << e.what() << '\n'; + } + statFile.open("/system/etc/NOTICE.TXT", ios::out | ios::trunc); EXPECT_FALSE(!statFile.is_open()) << "file can not open"; std::string bundleName = "testBundle"; @@ -343,7 +367,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageSt std::string type = "MEDIA_TYPE"; int32_t userId = 100; int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); - EXPECT_EQ(result, E_OK); + EXPECT_EQ(result, E_ERR); EXPECT_EQ(storageStats.video_, 0); EXPECT_EQ(storageStats.image_, 0); EXPECT_EQ(storageStats.file_, 0); @@ -362,7 +386,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageSt std::string type = "FILE_TYPE"; int32_t userId = 100; int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); - EXPECT_EQ(result, E_OK); + EXPECT_EQ(result, E_ERR); EXPECT_EQ(storageStats.video_, 0); EXPECT_EQ(storageStats.image_, 0); EXPECT_EQ(storageStats.file_, 0); @@ -381,7 +405,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetUserStorageSt std::string type = "OTHER_TYPE"; int32_t userId = 100; int64_t result = StorageManagerService::GetInstance().GetUserStorageStatsByType(userId, storageStats, type); - EXPECT_EQ(result, E_OK); + EXPECT_EQ(result, E_ERR); EXPECT_EQ(storageStats.video_, 0); EXPECT_EQ(storageStats.image_, 0); EXPECT_EQ(storageStats.file_, 0); @@ -435,6 +459,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetQuotaSrcMount std::string expectedPath = "/mount1"; std::string actualPath = GetQuotaSrcMountPath(target); EXPECT_EQ(expectedPath, actualPath); + mQuotaReverseMounts.clear(); } /** @@ -452,6 +477,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_GetQuotaSrcMount std::string expectedPath = ""; std::string actualPath = GetQuotaSrcMountPath(target); EXPECT_EQ(expectedPath, actualPath); + mQuotaReverseMounts.clear(); } /** -- Gitee From 7a9f7c0bee53b3e2298d2104fab8b68ff9e46d5e Mon Sep 17 00:00:00 2001 From: huhui Date: Fri, 13 Jun 2025 15:52:01 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=A1=86=E6=9E=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6=E6=89=AB=E6=8F=8F=E8=83=BD?= =?UTF-8?q?=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huhui --- .../backup_sa/include/module_external/storage_manager_service.h | 2 +- .../backup_sa/module_external/storage_manager_service_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/backup_sa/include/module_external/storage_manager_service.h b/services/backup_sa/include/module_external/storage_manager_service.h index 0899723a6..ac57da922 100644 --- a/services/backup_sa/include/module_external/storage_manager_service.h +++ b/services/backup_sa/include/module_external/storage_manager_service.h @@ -100,7 +100,7 @@ class StorageManagerService { public: static StorageManagerService &GetInstance() { - static StorageManagerService {}; + static StorageManagerService instance {}; return instance; } /** diff --git a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp index 631c586f2..b7e57d4ee 100644 --- a/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp +++ b/tests/unittests/backup_sa/module_external/storage_manager_service_test.cpp @@ -296,7 +296,7 @@ HWTEST_F(StorageManagerServiceTest, Storage_Manager_ServiceTest_CheckIfDirForInc if (!fs::exists(dirPath)) { fs::create_directories(dirPath); } - fs:path canonicalPath = fs::canonical(fs::absolute(tempPath)); + fs::path canonicalPath = fs::canonical(fs::absolute(tempPath)); statFile.open(canonicalPath, std::ios::out | std::ios::trunc); EXPECT_FALSE(!statFile.is_open()) << "file can not open"; std::string bundleName = "testBundle"; -- Gitee