From 8d5c2d427ded038d2d185e940098eecff3242d26 Mon Sep 17 00:00:00 2001 From: huaqingsimeng <1004904143@qq.com> Date: Thu, 13 Apr 2023 09:52:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbackup=5Ftool=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=94=A8=E4=BE=8B=E6=89=A7=E8=A1=8C=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huaqingsimeng --- .../backup_tool/tools_op_backup_test.cpp | 8 ++-- .../backup_tool/tools_op_restore_test.cpp | 7 +++- tools/backup_tool/src/tools_op_backup.cpp | 21 ++++++++-- tools/backup_tool/src/tools_op_restore.cpp | 40 ++++++++++--------- 4 files changed, 48 insertions(+), 28 deletions(-) diff --git a/tests/unittests/backup_tools/backup_tool/tools_op_backup_test.cpp b/tests/unittests/backup_tools/backup_tool/tools_op_backup_test.cpp index a1ebbfcdd..1eed3a4a9 100644 --- a/tests/unittests/backup_tools/backup_tool/tools_op_backup_test.cpp +++ b/tests/unittests/backup_tools/backup_tool/tools_op_backup_test.cpp @@ -19,6 +19,7 @@ #include +#include "b_resources/b_constants.h" #include "tools_op.h" #include "utils_mock_global_variable.h" @@ -48,17 +49,14 @@ HWTEST_F(ToolsOpBackupTest, SUB_backup_tools_op_backup_0100, testing::ext::TestS SetMockGetInstance(true); GTEST_LOG_(INFO) << "ToolsOpBackupTest-info"; map> mapArgToVal; - vector path = {"/data/backup/tmp"}; + string localCap = string(BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data()) + "/tmp"; + vector path = {localCap.data()}; mapArgToVal.insert(make_pair("pathCapFile", path)); vector bundles = {"com.example.app2backup"}; mapArgToVal.insert(make_pair("bundles", bundles)); vector local = {"true"}; mapArgToVal.insert(make_pair("isLocal", local)); - // 创建测试路径 - string strPath = "/data/backup"; - mkdir(strPath.data(), S_IRWXU); - // 尝试匹配当前命令,成功后执行 GTEST_LOG_(INFO) << "ToolsOpBackupTest-backup"; vector curOp; diff --git a/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp b/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp index 0ff779c2b..b71715f78 100644 --- a/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp +++ b/tests/unittests/backup_tools/backup_tool/tools_op_restore_test.cpp @@ -54,7 +54,8 @@ HWTEST_F(ToolsOpRestoreTest, SUB_backup_tools_op_restore_0100, testing::ext::Tes try { GTEST_LOG_(INFO) << "ToolsOpRestoreTest-info"; map> mapArgToVal; - vector path = {"/data/backup/tmp"}; + string localCap = string(BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data()) + "/tmp"; + vector path = {localCap.data()}; mapArgToVal.insert(make_pair("pathCapFile", path)); vector bundles = {"com.example.app2backup"}; mapArgToVal.insert(make_pair("bundles", bundles)); @@ -62,10 +63,14 @@ HWTEST_F(ToolsOpRestoreTest, SUB_backup_tools_op_restore_0100, testing::ext::Tes // 创建测试路径以及测试环境 string cmdMkdir = string("mkdir -p ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME; system(cmdMkdir.c_str()); + string cmdTool = string("mkdir -p ") + BConstants::SA_BUNDLE_BACKUP_TOOL_DIR.data(); + system(cmdTool.c_str()); string touchTar = string("touch ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME + FILE_NAME; system(touchTar.c_str()); string touchManage = string("touch ") + BConstants::BACKUP_TOOL_RECEIVE_DIR.data() + BUNDLE_NAME + MANAGE_JSON; system(touchManage.c_str()); + string touchTmp = string("touch ") + localCap; + system(touchTmp.c_str()); // 尝试匹配当前命令,成功后执行 GTEST_LOG_(INFO) << "ToolsOpRestoreTest-restore"; diff --git a/tools/backup_tool/src/tools_op_backup.cpp b/tools/backup_tool/src/tools_op_backup.cpp index 50462562f..d1f819240 100644 --- a/tools/backup_tool/src/tools_op_backup.cpp +++ b/tools/backup_tool/src/tools_op_backup.cpp @@ -212,7 +212,7 @@ static void BackupToolDirSoftlinkToBackupDir() } } -static int32_t InitPathCapFile(vector bundleNames) +static int32_t InitPathCapFile(const string &pathCapFile, vector bundleNames) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "InitPathCapFile"); // SELinux backup_tool工具/data/文件夹下创建文件夹 SA服务因root用户的自定义标签无写入权限 此处调整为软链接形式 @@ -222,6 +222,19 @@ static int32_t InitPathCapFile(vector bundleNames) mkdir((BConstants::BACKUP_TOOL_RECEIVE_DIR).data(), S_IRWXU) != 0) { throw BError(BError::Codes::TOOL_INVAL_ARG, generic_category().message(errno)); } + + UniqueFd fdLocal(open(pathCapFile.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)); + if (fdLocal < 0) { + fprintf(stderr, "Failed to open file. error: %d %s\n", errno, strerror(errno)); + return -EPERM; + } + auto proxy = ServiceProxy::GetInstance(); + if (!proxy) { + fprintf(stderr, "Get an empty backup sa proxy\n"); + return -EPERM; + } + BFile::SendFile(fdLocal, proxy->GetLocalCapabilities()); + auto ctx = make_shared(); ctx->session_ = BSessionBackup::Init( BSessionBackup::Callbacks {.onFileReady = bind(OnFileReady, ctx, placeholders::_1, placeholders::_2), @@ -230,13 +243,13 @@ static int32_t InitPathCapFile(vector bundleNames) .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { - printf("Failed to init backup"); + printf("Failed to init backup\n"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return -EPERM; } int ret = ctx->session_->AppendBundles(bundleNames); if (ret != 0) { - printf("backup append bundles error: %d", ret); + printf("backup append bundles error: %d\n", ret); throw BError(BError::Codes::TOOL_INVAL_ARG, "backup append bundles error"); } @@ -252,7 +265,7 @@ static int Exec(map> &mapArgToVal) mapArgToVal.find("isLocal") == mapArgToVal.end()) { return -EPERM; } - return InitPathCapFile(mapArgToVal["bundles"]); + return InitPathCapFile(*(mapArgToVal["pathCapFile"].begin()), mapArgToVal["bundles"]); } /** diff --git a/tools/backup_tool/src/tools_op_restore.cpp b/tools/backup_tool/src/tools_op_restore.cpp index 64a6ad29c..c02b6682a 100644 --- a/tools/backup_tool/src/tools_op_restore.cpp +++ b/tools/backup_tool/src/tools_op_restore.cpp @@ -164,6 +164,7 @@ static void OnBundleStarted(shared_ptr ctx, ErrCode err, const BundleNa printf("BundleStarted errCode = %d, BundleName = %s\n", err, name.c_str()); if (err != 0) { ctx->UpdateBundleFinishedCount(); + ctx->isAllBundelsFinished.store(true); ctx->ClearBundleOfMap(name); ctx->TryNotify(); } @@ -235,28 +236,31 @@ static void RestoreApp(shared_ptr restore, vector &bundleNa static int32_t InitPathCapFile(const string &pathCapFile, vector bundleNames) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Init"); + if (access(pathCapFile.data(), F_OK) != 0) { + return -errno; + } - UniqueFd fd(open(pathCapFile.data(), O_RDWR | O_CREAT | O_TRUNC, S_IRWXU)); + UniqueFd fd(open(pathCapFile.data(), O_RDWR, S_IRWXU)); if (fd < 0) { fprintf(stderr, "Failed to open file error: %d %s\n", errno, strerror(errno)); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return -errno; } - - BJsonCachedEntity cachedEntity(move(fd)); - auto cache = cachedEntity.Structuralize(); - vector bundleInfos; - for (auto name : bundleNames) { - string installPath = string(BConstants::BACKUP_TOOL_INSTALL_DIR) + name + ".hap"; - bool needToInstall = false; - if (access(installPath.data(), F_OK) == 0) { - needToInstall = true; + if (access(BConstants::BACKUP_TOOL_INSTALL_DIR.data(), F_OK) == 0) { + BJsonCachedEntity cachedEntity(move(fd)); + auto cache = cachedEntity.Structuralize(); + vector bundleInfos; + for (auto name : bundleNames) { + string installPath = string(BConstants::BACKUP_TOOL_INSTALL_DIR) + name + ".hap"; + if (access(installPath.data(), F_OK) == 0) { + bool needToInstall = true; + bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo {.name = name, .needToInstall = needToInstall}); + } } - bundleInfos.emplace_back(BJsonEntityCaps::BundleInfo {.name = name, .needToInstall = needToInstall}); + cache.SetBundleInfos(bundleInfos); + cachedEntity.Persist(); + fd = move(cachedEntity.GetFd()); } - cache.SetBundleInfos(bundleInfos); - cachedEntity.Persist(); - auto ctx = make_shared(); ctx->session_ = BSessionRestore::Init( BSessionRestore::Callbacks {.onFileReady = bind(OnFileReady, ctx, placeholders::_1, placeholders::_2), @@ -265,14 +269,14 @@ static int32_t InitPathCapFile(const string &pathCapFile, vector bundleN .onAllBundlesFinished = bind(OnAllBundlesFinished, ctx, placeholders::_1), .onBackupServiceDied = bind(OnBackupServiceDied, ctx)}); if (ctx->session_ == nullptr) { - printf("Failed to init restore"); + printf("Failed to init restore\n"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return -EPERM; } - int ret = ctx->session_->AppendBundles(move(cachedEntity.GetFd()), bundleNames); + int ret = ctx->session_->AppendBundles(move(fd), bundleNames); if (ret != 0) { - printf("restore append bundles error: %d", ret); - throw BError(BError::Codes::TOOL_INVAL_ARG, "restore append bundles error"); + printf("restore append bundles error: %d\n", ret); + return -ret; } ctx->SetBundleFinishedCount(bundleNames.size()); RestoreApp(ctx, bundleNames); -- Gitee