From ad745385aa4221a17d5d675c2a18a5f30e4cbddb Mon Sep 17 00:00:00 2001 From: lizhengxing Date: Wed, 16 Jul 2025 11:58:40 +0800 Subject: [PATCH] clear temp dir && errCode trans bug fix Signed-off-by: lizhengxing --- .../backup_sa/include/module_ipc/service.h | 1 + .../src/module_ipc/service_incremental.cpp | 29 ++++++ .../backup_sa/src/module_ipc/sub_service.cpp | 1 - tests/unittests/backup_sa/module_ipc/BUILD.gn | 1 + .../module_ipc/service_incremental_test.cpp | 98 ++++++++++++++++++- 5 files changed, 126 insertions(+), 4 deletions(-) diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 8bb77e1ad..1884e94cd 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -720,6 +720,7 @@ private: const vector &bundleNames, std::string &oldBackupVersion); void AppStatReportErr(const string &bundleName, const string &func, RadarError err); void SaStatReport(const string &bundleName, const string &func, RadarError err); + void ClearIncrementalStatFile(int32_t userId, const string &bundleName); void TotalStart() { if (totalStatistic_ != nullptr) { diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index f223767e3..3c50934f3 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -170,6 +170,9 @@ UniqueFd Service::GetLocalCapabilitiesIncremental(const std::vectorDecreaseSessionCnt(__PRETTY_FUNCTION__); + for (const auto &bundleInfo : bundleInfos) { + ClearIncrementalStatFile(GetUserIdDefault(), bundleInfo.name); + } HILOGI("End, bundleInfos size:%{public}zu", bundleInfos.size()); return move(cachedEntity.GetFd()); } catch (const BError &e) { @@ -712,6 +715,7 @@ ErrCode Service::AppIncrementalDone(ErrCode errCode) } HILOGI("Service AppIncrementalDone start, callerName is %{public}s, errCode is: %{public}d", callerName.c_str(), errCode); + ClearIncrementalStatFile(GetUserIdDefault(), callerName); if (session_->OnBundleFileReady(callerName) || errCode != BError(BError::Codes::OK)) { std::shared_ptr mutexPtr = GetExtensionMutex(callerName); if (mutexPtr == nullptr) { @@ -1119,4 +1123,29 @@ ErrCode Service::Cancel(const std::string& bundleName, int32_t &result) } return BError(BError::Codes::OK); } + +void Service::ClearIncrementalStatFile(int32_t userId, const string &bundleName) +{ + BJsonUtil::BundleDetailInfo bundleDetail = BJsonUtil::ParseBundleNameIndexStr(bundleName); + string backupSaBundleDir; + if (bundleDetail.bundleIndex > 0) { + std::string bundleNameIndex = "+clone-" + std::to_string(bundleDetail.bundleIndex) + "+" + + bundleDetail.bundleName; + backupSaBundleDir = BConstants::BACKUP_PATH_PREFIX + to_string(userId) + BConstants::BACKUP_PATH_SURFFIX + + bundleNameIndex + BConstants::FILE_SEPARATOR_CHAR; + } else { + backupSaBundleDir = BConstants::BACKUP_PATH_PREFIX + to_string(userId) + BConstants::BACKUP_PATH_SURFFIX + + bundleDetail.bundleName + BConstants::FILE_SEPARATOR_CHAR; + } + if (access(backupSaBundleDir.c_str(), F_OK) != ERR_OK) { + HILOGD("ClearIncrementalStatFile, access dir failed errno = %{public}d", errno); + return; + } + if (!ForceRemoveDirectoryBMS(backupSaBundleDir.c_str())) { + HILOGE("Failed to delete SaBundleBackupDir cache: %{public}s", + backupSaBundleDir.c_str()); + return; + } +} + } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 7c83fd08f..94254c6fd 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -1100,7 +1100,6 @@ ErrCode Service::GetBackupDataSize(bool isPreciseScan, const std::vectorDecreaseSessionCnt(__PRETTY_FUNCTION__); return BError(BError::Codes::SA_INVAL_ARG, "verify caller failed"); } - BundleMgrAdapter::CreatBackupEnv(bundleNameList, GetUserIdDefault()); CyclicSendScannedInfo(isPreciseScan, bundleNameList); return BError(BError::Codes::OK); } catch (...) { diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index 3c8a43658..71c82107b 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -464,6 +464,7 @@ ohos_unittest("backup_service_incremental_test") { "${path_backup_mock}/module_external/include", "${path_backup_mock}/module_ipc/include", "${path_backup_mock}/utils_mock/include", + "${path_backup_mock}/library_func_mock", ] deps = [ diff --git a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp index ce96dcb76..ebccead67 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -14,12 +14,14 @@ */ #include +#include #include "backup_para_mock.h" #include "bms_adapter_mock.h" #include "b_json/b_json_entity_caps.h" #include "b_jsonutil_mock.h" #include "ipc_skeleton_mock.h" +#include "library_func_mock.h" #include "sa_backup_connection_mock.h" #include "service_reverse_proxy_mock.h" #include "svc_backup_connection_mock.h" @@ -30,6 +32,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; +using namespace OHOS::AppFileService; class BService { public: @@ -513,6 +516,7 @@ public: static inline shared_ptr skeleton = nullptr; static inline sptr srProxy = nullptr; static inline shared_ptr srvMock = nullptr; + static inline shared_ptr funcMock = nullptr; }; void ServiceIncrementalTest::SetUpTestCase(void) @@ -537,6 +541,8 @@ void ServiceIncrementalTest::SetUpTestCase(void) skeleton = make_shared(); IPCSkeletonMock::skeleton = skeleton; srProxy = sptr(new ServiceReverseProxyMock()); + funcMock = make_shared(); + LibraryFuncMock::libraryFunc_ = funcMock; } void ServiceIncrementalTest::TearDownTestCase() @@ -561,6 +567,7 @@ void ServiceIncrementalTest::TearDownTestCase() srProxy = nullptr; ServiceMock::serviceMock = nullptr; srvMock = nullptr; + LibraryFuncMock::libraryFunc_ = nullptr; } /** @@ -592,11 +599,18 @@ HWTEST_F(ServiceIncrementalTest, SUB_ServiceIncremental_GetLocalCapabilitiesIncr fd = service->GetLocalCapabilitiesIncremental({}); EXPECT_EQ(static_cast(fd), -ENOENT); - vector info; + vector infos; + infos.emplace_back(BJsonEntityCaps::BundleInfo{.name = "bundleName", .appIndex = 0}); + EXPECT_CALL(*funcMock, access(_,_)).WillRepeatedly(Return(0)); EXPECT_CALL(*srvMock, VerifyCaller()).WillOnce(Return(BError(BError::Codes::OK).GetCode())); - EXPECT_CALL(*srvMock, GetUserIdDefault()).WillOnce(Return(0)).WillOnce(Return(0)); + EXPECT_CALL(*srvMock, GetUserIdDefault()).WillOnce(Return(0)).WillOnce(Return(0)).WillOnce(Return(0)); EXPECT_CALL(*bms, GetBundleInfosForIncremental(An(), An&>())) - .WillOnce(Return(info)); + .WillOnce(Return(infos)); + + BJsonUtil::BundleDetailInfo bundleInfo; + bundleInfo.bundleIndex = 1; + bundleInfo.bundleName = "bundleName"; + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); fd = service->GetLocalCapabilitiesIncremental({}); EXPECT_TRUE(static_cast(fd) > 0); } catch (...) { @@ -1342,28 +1356,37 @@ HWTEST_F(ServiceIncrementalTest, SUB_ServiceIncremental_AppIncrementalDone_0000, int32_t errCode = BError(BError::Codes::OK).GetCode(); auto session_ = service->session_; service->session_ = nullptr; + EXPECT_CALL(*srvMock, GetUserIdDefault()).WillRepeatedly(Return(100)); + BJsonUtil::BundleDetailInfo bundleInfo; + bundleInfo.bundleIndex = 0; + bundleInfo.bundleName = "bundleName"; + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); auto ret = service->AppIncrementalDone(errCode); EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); service->session_ = session_; EXPECT_CALL(*srvMock, VerifyCallerAndGetCallerName(_)) .WillOnce(Return(BError(BError::Codes::SA_INVAL_ARG).GetCode())); + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); ret = service->AppIncrementalDone(errCode); EXPECT_EQ(ret, BError(BError::Codes::SA_INVAL_ARG).GetCode()); EXPECT_CALL(*srvMock, VerifyCallerAndGetCallerName(_)).WillOnce(Return(BError(BError::Codes::OK).GetCode())); EXPECT_CALL(*session, OnBundleFileReady(_, _)).WillOnce(Return(false)); + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); ret = service->AppIncrementalDone(errCode); EXPECT_EQ(ret, BError(BError::Codes::OK).GetCode()); service->backupExtMutexMap_.clear(); EXPECT_CALL(*srvMock, VerifyCallerAndGetCallerName(_)).WillOnce(Return(BError(BError::Codes::OK).GetCode())); EXPECT_CALL(*session, OnBundleFileReady(_, _)).WillOnce(Return(false)); + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); ret = service->AppIncrementalDone(BError(BError::Codes::SA_INVAL_ARG).GetCode()); EXPECT_EQ(ret, BError(BError::Codes::OK).GetCode()); EXPECT_CALL(*srvMock, VerifyCallerAndGetCallerName(_)).WillOnce(Return(BError(BError::Codes::OK).GetCode())); EXPECT_CALL(*session, OnBundleFileReady(_, _)).WillOnce(Return(true)); + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); ret = service->AppIncrementalDone(errCode); EXPECT_EQ(ret, BError(BError::Codes::OK).GetCode()); } catch (...) { @@ -2096,4 +2119,73 @@ HWTEST_F(ServiceIncrementalTest, SUB_ServiceIncremental_RemoveExtensionMutex_000 } GTEST_LOG_(INFO) << "ServiceIncrementalTest-end SUB_ServiceIncremental_RemoveExtensionMutex_0000"; } + +/** + * @tc.number: SUB_ServiceIncremental_ClearIncrementalStatFile_0000 + * @tc.name: SUB_ServiceIncremental_ClearIncrementalStatFile_0000 + * @tc.desc: 测试 ClearIncrementalStatFile 分身与一般应用分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(ServiceIncrementalTest, SUB_ServiceIncremental_ClearIncrementalStatFile_0000, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceIncrementalTest-begin SUB_ServiceIncremental_ClearIncrementalStatFile_0000"; + try { + + int userId = BConstants::DEFAULT_USER_ID; + ASSERT_TRUE(service != nullptr); + BJsonUtil::BundleDetailInfo bundleInfo; + bundleInfo.bundleIndex = 1; + bundleInfo.bundleName = "com.example.app2backup"; + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); + service->ClearIncrementalStatFile(userId, bundleInfo.bundleName); + + bundleInfo.bundleIndex = 0; + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); + service->ClearIncrementalStatFile(userId, bundleInfo.bundleName); + EXPECT_EQ(bundleInfo.bundleIndex, 0); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceIncrementalTest-an exception occurred by ClearIncrementalStatFile."; + } + GTEST_LOG_(INFO) << "ServiceIncrementalTest-end SUB_ServiceIncremental_ClearIncrementalStatFile_0000"; +} + +/** + * @tc.number: SUB_ServiceIncremental_ClearIncrementalStatFile_0100 + * @tc.name: SUB_ServiceIncremental_ClearIncrementalStatFile_0100 + * @tc.desc: 测试 ClearIncrementalStatFile 的文件删除异常分支 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(ServiceIncrementalTest, SUB_ServiceIncremental_ClearIncrementalStatFile_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceIncrementalTest-begin SUB_ServiceIncremental_ClearIncrementalStatFile_0100"; + try { + + int userId = BConstants::DEFAULT_USER_ID; + ASSERT_TRUE(service != nullptr); + BJsonUtil::BundleDetailInfo bundleInfo; + bundleInfo.bundleIndex = 0; + bundleInfo.bundleName = "com.example.app2backup"; + string path = BConstants::GetSaBundleBackupRootDir.data() + bundleInfo.bundleName; + int ret = mkdir(path.data(), S_IRWXU); + EXPECT_EQ(ret, 0); + + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); + service->ClearIncrementalStatFile(userId, bundleInfo.bundleName); + + EXPECT_CALL(*jsonUtil, ParseBundleNameIndexStr(_)).WillOnce(Return(bundleInfo)); + service->ClearIncrementalStatFile(userId, bundleInfo.bundleName); + EXPECT_EQ(bundleInfo.bundleIndex, 0); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceIncrementalTest-an exception occurred by ClearIncrementalStatFile."; + } + GTEST_LOG_(INFO) << "ServiceIncrementalTest-end SUB_ServiceIncremental_ClearIncrementalStatFile_0100"; +} } \ No newline at end of file -- Gitee