diff --git a/frameworks/native/backup_ext/include/ext_backup_js.h b/frameworks/native/backup_ext/include/ext_backup_js.h index 7f26d84b367877c4c450b86328996b438dfbc92c..c69cc77a0c9e3bfcf02d46cfb9f8337cd71a41f7 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 4de154106d5b2cd827d2e619d349290203d1c3f6..99df9a387e69b8b5a795ad5d966fdc2794046f0b 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 aa453316a67552482df2fab1f9357c9da936ffd1..4953cae6d7ec75b0cb9ffee3c2b0395b28f098da 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 3863761c7c09fd2713dacd438f0f477b1ce9945a..6de44f35f40698b19f2bc0ec86860e3865c9057d 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,