From 99f6deebf867c74d6ebc971dde2d8b07d2162db6 Mon Sep 17 00:00:00 2001 From: hunili Date: Mon, 17 Mar 2025 10:30:54 +0800 Subject: [PATCH] =?UTF-8?q?[Bug]:=20Create=20.backup=20when=20it's=20not?= =?UTF-8?q?=20exists=EF=BC=885.0.2=EF=BC=89=20issue:=20https://gitee.com/o?= =?UTF-8?q?penharmony/filemanagement=5Fapp=5Ffile=5Fservice/issues/IBTTIR?= =?UTF-8?q?=20Signed-off-by:=20hunili=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../native/backup_ext/include/ext_backup_js.h | 3 +- .../native/backup_ext/src/ext_backup_js.cpp | 31 ++++++++++++++- .../backup_ext/ext_backup_js_test.cpp | 39 ++++++++++++++++++- utils/include/b_error/b_error.h | 2 + 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/frameworks/native/backup_ext/include/ext_backup_js.h b/frameworks/native/backup_ext/include/ext_backup_js.h index 7f26d84b3..c69cc77a0 100644 --- a/frameworks/native/backup_ext/include/ext_backup_js.h +++ b/frameworks/native/backup_ext/include/ext_backup_js.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -158,6 +158,7 @@ private: ErrCode CallJsOnBackup(); void ExportJsContext(void); + void InitTempPath(const std::string &bundleName); AbilityRuntime::JsRuntime &jsRuntime_; std::unique_ptr jsObj_; diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index 4de154106..99df9a387 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -40,7 +40,9 @@ #include "b_error/b_excep_utils.h" #include "b_json/b_json_cached_entity.h" #include "b_json/b_json_entity_extension_config.h" +#include "b_radar/b_radar.h" #include "b_resources/b_constants.h" +#include "directory_ex.h" #include "ext_extension.h" #include "filemgmt_libhilog.h" @@ -382,6 +384,7 @@ void ExtBackupJs::Init(const shared_ptr &record, // 获取应用扩展的 BackupExtensionAbility 的路径 const AppExecFwk::AbilityInfo &info = *abilityInfo_; string bundleName = info.bundleName; + InitTempPath(bundleName); string moduleName(info.moduleName + "::" + info.name); string modulePath = GetSrcPath(info); int moduleType = static_cast(info.type); @@ -950,4 +953,30 @@ ErrCode ExtBackupJs::OnProcess(std::function c HILOGI("BackupExtensionAbulity(JS) OnProcess end."); return errCode; } + +void ExtBackupJs::InitTempPath(const std::string &bundleName) +{ + std::string el2BackupDir(BConstants::PATH_BUNDLE_BACKUP_HOME); + if (access(el2BackupDir.c_str(), F_OK) != 0) { + HILOGW("backup home el2 dir not exits, try to create"); + if (!ForceCreateDirectory(el2BackupDir.data())) { + HILOGE("Failed to create directory, err = %{public}d", errno); + AppRadar::Info info(bundleName, "", "Create backup home el2 dir failed"); + AppRadar::GetInstance().RecordDefaultFuncRes(info, "ExtBackupJs::InitTempPath", + AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_DEFAULT, + static_cast(BError::Codes::EXT_CREATE_DIR_ERROR)); + } + } + std::string el1BackupDir(BConstants::PATH_BUNDLE_BACKUP_HOME_EL1); + if (access(el1BackupDir.c_str(), F_OK) != 0) { + HILOGW("backup home el1 dir not exits, try to create"); + if (!ForceCreateDirectory(el1BackupDir.data())) { + HILOGE("Failed to create home el1 dir, err = %{public}d", errno); + AppRadar::Info info(bundleName, "", "Create backup home el1 dir failed"); + AppRadar::GetInstance().RecordDefaultFuncRes(info, "ExtBackupJs::InitTempPath", + AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_DEFAULT, + static_cast(BError::Codes::EXT_CREATE_DIR_ERROR)); + } + } +} } // namespace OHOS::FileManagement::Backup diff --git a/tests/unittests/backup_ext/ext_backup_js_test.cpp b/tests/unittests/backup_ext/ext_backup_js_test.cpp index aa453316a..4953cae6d 100644 --- a/tests/unittests/backup_ext/ext_backup_js_test.cpp +++ b/tests/unittests/backup_ext/ext_backup_js_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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 @@ -1456,4 +1456,41 @@ HWTEST_F(ExtBackupJsTest, SUB_backup_ext_js_GetBackupInfo_0200, testing::ext::Te } GTEST_LOG_(INFO) << "ExtBackupJsTest-end SUB_backup_ext_js_GetBackupInfo_0200"; } + + +/** + * @tc.number: SUB_backup_ext_js_InitTempPath_0100 + * @tc.name: SUB_backup_ext_js_InitTempPath_0100 + * @tc.desc: 测试 InitTempPath 各个分支成功与失败 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issuesIAFBOS + */ +HWTEST_F(ExtBackupJsTest, SUB_backup_ext_js_InitTempPath_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ExtBackupJsTest-begin SUB_backup_ext_js_InitTempPath_0100"; + try { + std::string el2BackupDir(BConstants::PATH_BUNDLE_BACKUP_HOME); + int ret = access(el2BackupDir.c_str(), F_OK); + EXPECT_TRUE(ret != F_OK); + std::string el1BackupDir(BConstants::PATH_BUNDLE_BACKUP_HOME_EL1); + ret = access(el1BackupDir.c_str(), F_OK); + EXPECT_TRUE(ret != F_OK); + std::string bundleName = "testBundleName"; + extBackupJs->InitTempPath(bundleName); + ret = access(el2BackupDir.c_str(), F_OK); + EXPECT_EQ(ret, F_OK); + ret = access(el1BackupDir.c_str(), F_OK); + EXPECT_EQ(ret, F_OK); + std::string el2RootPath = BConstants::BACKUP_DIR_PRE + BConstants::CONTEXT_ELS[0]; // 0: {"el1", "el2"}中的el1 + std::string el1RootPath = BConstants::BACKUP_DIR_PRE + BConstants::CONTEXT_ELS[1]; // 1: {"el1", "el2"}中的el2 + ForceRemoveDirectoryBMS(el2RootPath); + ForceRemoveDirectoryBMS(el1RootPath); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ExtBackupJsTest-an exception occurred by OnProcess."; + } + GTEST_LOG_(INFO) << "ExtBackupJsTest-end SUB_backup_ext_js_InitTempPath_0100"; +} } // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/utils/include/b_error/b_error.h b/utils/include/b_error/b_error.h index 3863761c7..6de44f35f 100644 --- a/utils/include/b_error/b_error.h +++ b/utils/include/b_error/b_error.h @@ -91,6 +91,8 @@ public: EXT_METHOD_NOT_EXIST = 0x5008, EXT_THROW_EXCEPTION = 0x5009, EXT_BACKUP_UNPACKET_ERROR = 0x5010, + EXT_TIMER_ERROR = 0x5011, + EXT_CREATE_DIR_ERROR = 0x5012, // 0x6000~0x6999 sa_ext错误 SA_EXT_ERR_CALL = 0x6000, -- Gitee