diff --git a/frameworks/native/backup_ext/src/ext_extension.cpp b/frameworks/native/backup_ext/src/ext_extension.cpp index 40b1f73b1d5b11d0622a52625279e93f3c6ae98e..5bf369cceffa745cf5e5e2a090f45555da1351fd 100644 --- a/frameworks/native/backup_ext/src/ext_extension.cpp +++ b/frameworks/native/backup_ext/src/ext_extension.cpp @@ -49,6 +49,7 @@ #include "b_jsonutil/b_jsonutil.h" #include "b_ohos/startup/backup_para.h" #include "b_tarball/b_tarball_factory.h" +#include "b_utils/b_time.h" #include "filemgmt_libhilog.h" #include "hitrace_meter.h" #include "i_service.h" @@ -2106,130 +2107,4 @@ int BackupExtExtension::DoIncrementalBackup(const vector smallFiles.size(), allFiles.size()); return err; } - -bool BackupExtExtension::IfAllowToBackupRestore() -{ - if (extension_ == nullptr) { - HILOGE("Failed to handle backup, extension is nullptr"); - return false; - } - string usrConfig = extension_->GetUsrConfig(); - BJsonCachedEntity cachedEntity(usrConfig); - auto cache = cachedEntity.Structuralize(); - if (!cache.GetAllowToBackupRestore()) { - HILOGE("Application does not allow backup or restore"); - return false; - } - return true; -} - -ErrCode BackupExtExtension::User0OnBackup() -{ - HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - if (!IfAllowToBackupRestore()) { - return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE, "Application does not allow backup or restore") - .GetCode(); - } - AsyncTaskUser0Backup(); - return ERR_OK; -} - -void BackupExtExtension::AsyncTaskUser0Backup() -{ - HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - auto task = [obj {wptr(this)}]() { - auto ptr = obj.promote(); - BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released"); - const string config = ptr->extension_->GetUsrConfig(); - try { - HILOGI("Do backup, start fwk timer begin."); - bool isFwkStart; - ptr->StartFwkTimer(isFwkStart); - if (!isFwkStart) { - HILOGE("Do backup, start fwk timer fail."); - return; - } - HILOGI("Do backup, start fwk timer end."); - BJsonCachedEntity cachedEntity(config); - auto cache = cachedEntity.Structuralize(); - auto ret = ptr->User0DoBackup(cache); - if (ret != ERR_OK) { - HILOGE("User0DoBackup, err = %{pubilc}d", ret); - ptr->AppIncrementalDone(BError::GetCodeByErrno(ret)); - } - } catch (const BError &e) { - HILOGE("extension: AsyncTaskBackup error, err code:%{public}d", e.GetCode()); - ptr->AppIncrementalDone(e.GetCode()); - } catch (const exception &e) { - HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); - ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); - } catch (...) { - HILOGE("Failed to restore the ext bundle"); - ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); - } - }; - - threadPool_.AddTask([task]() { - try { - task(); - } catch (...) { - HILOGE("Failed to add task to thread pool"); - } - }); -} - -void BackupExtExtension::DoUser0Backup(const BJsonEntityExtensionConfig &usrConfig) -{ - HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_BACKUP); - if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { - throw BError(errno); - } - vector includes = usrConfig.GetIncludes(); - vector excludes = usrConfig.GetExcludes(); - auto task = [obj {wptr(this)}, includes, excludes]() { - auto ptr = obj.promote(); - BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released"); - try { - auto [bigFile, smallFile] = BDir::GetBackupList(includes, excludes); - vector allFiles; - vector smallFiles; - vector bigFiles; - BDir::GetUser0FileStat(move(bigFile), move(smallFile), allFiles, smallFiles, bigFiles); - auto ret = ptr->DoIncrementalBackup(allFiles, smallFiles, bigFiles); - ptr->AppIncrementalDone(ret); - HILOGI("User0 backup app done %{public}d", ret); - } catch (const BError &e) { - ptr->AppIncrementalDone(e.GetCode()); - } catch (const exception &e) { - ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); - } catch (...) { - HILOGE("Failed to restore the ext bundle"); - ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); - } - }; - - threadPool_.AddTask([task]() { - try { - task(); - } catch (...) { - HILOGE("Failed to add task to thread pool"); - } - }); -} - -int BackupExtExtension::User0DoBackup(const BJsonEntityExtensionConfig &usrConfig) -{ - HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - HILOGI("Start Do User0Backup"); - if (extension_ == nullptr) { - HILOGE("Failed to do backup, extension is nullptr"); - return BError(BError::Codes::EXT_INVAL_ARG); - } - if (extension_->GetExtensionAction() != BConstants::ExtensionAction::BACKUP) { - return EPERM; - } - DoUser0Backup(usrConfig); - return ERR_OK; -} } // namespace OHOS::FileManagement::Backup diff --git a/frameworks/native/backup_ext/src/sub_ext_extension.cpp b/frameworks/native/backup_ext/src/sub_ext_extension.cpp index 926780571049e23a51225d073618ee18d2b5492f..e1225f60ecd6a3625709b165e33fdbb80d0689e5 100644 --- a/frameworks/native/backup_ext/src/sub_ext_extension.cpp +++ b/frameworks/native/backup_ext/src/sub_ext_extension.cpp @@ -781,4 +781,130 @@ bool BackupExtExtension::SetStagingPathProperties() } return true; } + +bool BackupExtExtension::IfAllowToBackupRestore() +{ + if (extension_ == nullptr) { + HILOGE("Failed to handle backup, extension is nullptr"); + return false; + } + string usrConfig = extension_->GetUsrConfig(); + BJsonCachedEntity cachedEntity(usrConfig); + auto cache = cachedEntity.Structuralize(); + if (!cache.GetAllowToBackupRestore()) { + HILOGE("Application does not allow backup or restore"); + return false; + } + return true; +} + +ErrCode BackupExtExtension::User0OnBackup() +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + if (!IfAllowToBackupRestore()) { + return BError(BError::Codes::EXT_FORBID_BACKUP_RESTORE, "Application does not allow backup or restore") + .GetCode(); + } + AsyncTaskUser0Backup(); + return ERR_OK; +} + +void BackupExtExtension::AsyncTaskUser0Backup() +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + auto task = [obj {wptr(this)}]() { + auto ptr = obj.promote(); + BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released"); + const string config = ptr->extension_->GetUsrConfig(); + try { + HILOGI("Do backup, start fwk timer begin."); + bool isFwkStart; + ptr->StartFwkTimer(isFwkStart); + if (!isFwkStart) { + HILOGE("Do backup, start fwk timer fail."); + return; + } + HILOGI("Do backup, start fwk timer end."); + BJsonCachedEntity cachedEntity(config); + auto cache = cachedEntity.Structuralize(); + auto ret = ptr->User0DoBackup(cache); + if (ret != ERR_OK) { + HILOGE("User0DoBackup, err = %{pubilc}d", ret); + ptr->AppIncrementalDone(BError::GetCodeByErrno(ret)); + } + } catch (const BError &e) { + HILOGE("extension: AsyncTaskBackup error, err code:%{public}d", e.GetCode()); + ptr->AppIncrementalDone(e.GetCode()); + } catch (const exception &e) { + HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); + ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); + } catch (...) { + HILOGE("Failed to restore the ext bundle"); + ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); + } + }; + + threadPool_.AddTask([task]() { + try { + task(); + } catch (...) { + HILOGE("Failed to add task to thread pool"); + } + }); +} + +void BackupExtExtension::DoUser0Backup(const BJsonEntityExtensionConfig &usrConfig) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + string path = string(BConstants::PATH_BUNDLE_BACKUP_HOME).append(BConstants::SA_BUNDLE_BACKUP_BACKUP); + if (mkdir(path.data(), S_IRWXU) && errno != EEXIST) { + throw BError(errno); + } + vector includes = usrConfig.GetIncludes(); + vector excludes = usrConfig.GetExcludes(); + auto task = [obj {wptr(this)}, includes, excludes]() { + auto ptr = obj.promote(); + BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released"); + try { + auto [bigFile, smallFile] = BDir::GetBackupList(includes, excludes); + vector allFiles; + vector smallFiles; + vector bigFiles; + BDir::GetUser0FileStat(move(bigFile), move(smallFile), allFiles, smallFiles, bigFiles); + auto ret = ptr->DoIncrementalBackup(allFiles, smallFiles, bigFiles); + ptr->AppIncrementalDone(ret); + HILOGI("User0 backup app done %{public}d", ret); + } catch (const BError &e) { + ptr->AppIncrementalDone(e.GetCode()); + } catch (const exception &e) { + ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); + } catch (...) { + HILOGE("Failed to restore the ext bundle"); + ptr->AppIncrementalDone(BError(BError::Codes::EXT_INVAL_ARG).GetCode()); + } + }; + + threadPool_.AddTask([task]() { + try { + task(); + } catch (...) { + HILOGE("Failed to add task to thread pool"); + } + }); +} + +int BackupExtExtension::User0DoBackup(const BJsonEntityExtensionConfig &usrConfig) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + HILOGI("Start Do User0Backup"); + if (extension_ == nullptr) { + HILOGE("Failed to do backup, extension is nullptr"); + return BError(BError::Codes::EXT_INVAL_ARG); + } + if (extension_->GetExtensionAction() != BConstants::ExtensionAction::BACKUP) { + return EPERM; + } + DoUser0Backup(usrConfig); + return ERR_OK; +} } // namespace OHOS::FileManagement::Backup diff --git a/utils/include/b_radar/b_radar.h b/utils/include/b_radar/b_radar.h index 87fd5ac4e38a892e6701e3fad0bcb7537955018a..226e0ece4186c82ab359383a63d6cc96e86ba3ac 100644 --- a/utils/include/b_radar/b_radar.h +++ b/utils/include/b_radar/b_radar.h @@ -76,11 +76,11 @@ public: struct DoRestoreInfo { uint32_t bigFileNum; uint64_t bigFileSize; - uint32_t bigFileSpendTime; + int64_t bigFileSpendTime; uint32_t tarFileNum; uint64_t tarFileSize; - uint32_t tarFileSpendTime; - uint32_t totalFileSpendTime; + int64_t tarFileSpendTime; + int64_t totalFileSpendTime; }; public: