From deb549b45edc1c1aacd723d11491a51a8cb14703 Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Thu, 1 Feb 2024 11:29:59 +0800 Subject: [PATCH] =?UTF-8?q?backup=5Ftool=E5=85=8B=E9=9A=86=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E5=A4=8D=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I52fcf44950a2d49d516e363846233aebfa3bc947 --- .../src/tools_op_restore_async.cpp | 83 +++++++++++++++++-- 1 file changed, 76 insertions(+), 7 deletions(-) diff --git a/tools/backup_tool/src/tools_op_restore_async.cpp b/tools/backup_tool/src/tools_op_restore_async.cpp index bea070535..5546450c0 100644 --- a/tools/backup_tool/src/tools_op_restore_async.cpp +++ b/tools/backup_tool/src/tools_op_restore_async.cpp @@ -145,9 +145,70 @@ static void OnBackupServiceDied(shared_ptr ctx) ctx->TryNotify(true); } +static void AdapteCloneOptimize(const string &path) +{ + printf("!!!!!!!!!! AdapteCloneOptimize fileName %s\n", path.data()); + string manageJosnStr = path; + manageJosnStr = manageJosnStr + BConstants::FILE_SEPARATOR_CHAR + string(BConstants::EXT_BACKUP_MANAGE); + printf("!!!!!!!!!!AdapteCloneOptimize manageJosnStr %s\n", manageJosnStr.data()); + BJsonCachedEntity cachedEntityOld(UniqueFd(open(manageJosnStr.data(), O_RDWR))); + auto cacheOld = cachedEntityOld.Structuralize(); + auto pkgInfo = cacheOld.GetExtManageInfo(); + close(cachedEntityOld.GetFd().Release()); + printf("!!!!!!!!!!AdapteCloneOptimize openOld manageJSON %s\n", manageJosnStr.data()); + + map> info; + for (auto &item : pkgInfo) { + string fileName = item.hashName; + string filePath = item.fileName; + if (fileName.empty() || filePath.empty()) { + continue; + } + + // rename big file with real name in backup/receive directory + string realNameInReceive = path + BConstants::FILE_SEPARATOR_CHAR + filePath.substr(filePath.rfind("/") + 1); + string currentNameInReceive = path + BConstants::FILE_SEPARATOR_CHAR + fileName; + + printf("!!!!!!!!!!fileName %s\n", fileName.data()); + printf("!!!!!!!!!!filePath %s\n", filePath.data()); + printf("!!!!!!!!!!realNameInReceive %s\n", realNameInReceive.data()); + printf("!!!!!!!!!!currentNameInReceive %s\n", currentNameInReceive.data()); + + if (item.isBigFile && rename(currentNameInReceive.data(), realNameInReceive.data()) != 0) { + printf("err rename %d %s\n", errno, strerror(errno)); + } + + // update fileName with filePath according to clone optimize + item.hashName = filePath; + + // update filePath + item.fileName = filePath.substr(0, filePath.rfind("/")); + + // sta + struct stat sta = {}; + + bool isBeforeTar = false; + if (item.isBigFile || item.isUserTar) { + isBeforeTar = true; + } + info.emplace(item.hashName, make_tuple(item.fileName, sta, isBeforeTar)); + } + + BJsonCachedEntity cachedEntity(UniqueFd(open(manageJosnStr.data(), + O_RDWR | O_CREAT, S_IRUSR | S_IWUSR))); + auto cache = cachedEntity.Structuralize(); + + printf("!!!!!!!!!!AdapteCloneOptimize openNew manageJSON %s\n", manageJosnStr.data()); + + cache.SetExtManage(info); + cachedEntity.Persist(); + close(cachedEntity.GetFd().Release()); +} + static void RestoreApp(shared_ptr restore, vector &bundleNames) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RestoreApp"); + printf("!!!!!!!!!! RestoreApp\n"); if (!restore || !restore->session_) { throw BError(BError::Codes::TOOL_INVAL_ARG, generic_category().message(errno)); } @@ -159,13 +220,21 @@ static void RestoreApp(shared_ptr restore, vector &bun if (access(path.data(), F_OK) != 0) { throw BError(BError::Codes::TOOL_INVAL_ARG, generic_category().message(errno)); } - const auto [err, filePaths] = BDir::GetDirFiles(path); - if (err != 0) { - throw BError(BError::Codes::TOOL_INVAL_ARG, "error path"); - } - for (auto &filePath : filePaths) { - string fileName = filePath.substr(filePath.rfind("/") + 1); - restore->session_->GetFileHandle(bundleName, fileName); + + printf("!!!!!!!!!! RestoreApp. Before AdapteCloneOptimize\n"); + // update manage.json and fileName + AdapteCloneOptimize(path); + + // manage.json first + string manageJsonPath = path + BConstants::FILE_SEPARATOR_CHAR + string(BConstants::EXT_BACKUP_MANAGE); + restore->session_->GetFileHandle(bundleName, manageJsonPath); + + BJsonCachedEntity cachedEntity(UniqueFd(open(path.data(), O_RDONLY))); + auto cache = cachedEntity.Structuralize(); + auto pkgInfo = cache.GetExtManageInfo(); + for (auto &item : pkgInfo) { + printf("New FileName %s\n", item.hashName.data()); + restore->session_->GetFileHandle(bundleName, item.hashName); } } FinishTrace(HITRACE_TAG_FILEMANAGEMENT); -- Gitee