Merge remote-tracking branch 'upstream/master'

This commit is contained in:
yaoruozi1@huawei.com 2024-08-18 18:17:26 +08:00
commit 75e5725ce9
7 changed files with 203 additions and 130 deletions

View File

@ -245,13 +245,15 @@ private:
std::function<void(ErrCode, const std::string)> ExecOnProcessCallback(wptr<BackupExtExtension> obj,
BackupRestoreScenario scenario);
// std::function<void(ErrCode, const std::string)> ExecOnProcessTimeoutCallback(wptr<BackupExtExtension> obj);
bool CreateExecOnProcessTask(BackupRestoreScenario scenario);
bool ShutDownExecOnProcessTask();
bool ExecOnProcessTimeoutTask(wptr<BackupExtExtension> obj);
bool ShutDownOnProcessTimeoutTask();
void StartOnProcessTaskThread(BackupRestoreScenario scenario);
void FinishOnProcessTask();
void ExecCallOnProcessTask(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario);
void AsyncCallJsOnProcessTask(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario);
void StartOnProcessTimeOutTimer(wptr<BackupExtExtension> obj);
void CloseOnProcessTimeOutTimer();
private:
@ -274,33 +276,14 @@ private:
bool isRpValid_ {false};
Utils::Timer execOnProcessTaskTimer_ {"execOnProcessTaskTimer_"};
uint32_t execOnProcessTaskTimerId_;
Utils::Timer execOnProcessTimeoutTimer_ {"execOnProcessTimeoutTimer_"};
uint32_t execOnProcessTimeoutTimerId_;
int execOnProcessTimeoutTimes_;
bool isSameClock_ {false};
bool appExecFinished_ {false};
std::thread callJsOnProcessThread_;
Utils::Timer onProcessTimeoutTimer_ {"onProcessTimeoutTimer_"};
uint32_t onProcessTimeoutTimerId_;
std::atomic<int> onProcessTimeoutCnt_;
std::atomic<bool> stopCallJsOnProcess_ {false};
std::condition_variable execOnProcessCon_;
std::mutex onProcessLock_;
std::atomic<bool> onProcessTimeout_ {false};
};
} // namespace OHOS::FileManagement::Backup

View File

@ -1259,6 +1259,7 @@ void BackupExtExtension::AsyncTaskIncreRestoreSpecialVersion()
void BackupExtExtension::AsyncTaskRestoreForUpgrade()
{
HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
StartOnProcessTaskThread(BackupRestoreScenario::FULL_RESTORE);
auto task = [obj {wptr<BackupExtExtension>(this)}]() {
auto ptr = obj.promote();
BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released");
@ -1302,6 +1303,7 @@ void BackupExtExtension::ExtClear()
void BackupExtExtension::AsyncTaskIncrementalRestoreForUpgrade()
{
StartOnProcessTaskThread(BackupRestoreScenario::INCREMENTAL_RESTORE);
auto task = [obj {wptr<BackupExtExtension>(this)}]() {
auto ptr = obj.promote();
BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released");
@ -1402,6 +1404,7 @@ void BackupExtExtension::AppResultReport(const std::string restoreRetInfo,
void BackupExtExtension::AsyncTaskOnBackup()
{
HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
StartOnProcessTaskThread(BackupRestoreScenario::FULL_BACKUP);
auto task = [obj {wptr<BackupExtExtension>(this)}]() {
auto ptr = obj.promote();
BExcepUltils::BAssert(ptr, BError::Codes::EXT_BROKEN_FRAMEWORK, "Ext extension handle have been released");
@ -1409,7 +1412,6 @@ void BackupExtExtension::AsyncTaskOnBackup()
try {
auto callBackup = ptr->HandleFullBackupCallback(obj);
auto callBackupEx = ptr->HandleFullBackupCallbackEx(obj);
ptr->CreateExecOnProcessTask(BackupRestoreScenario::FULL_BACKUP);
ErrCode err = ptr->extension_->OnBackup(callBackup, callBackupEx);
if (err != ERR_OK) {
HILOGE("OnBackup done, err = %{pubilc}d", err);
@ -1780,6 +1782,7 @@ void BackupExtExtension::AsyncTaskDoIncrementalBackup(UniqueFd incrementalFd, Un
void BackupExtExtension::AsyncTaskOnIncrementalBackup()
{
StartOnProcessTaskThread(BackupRestoreScenario::INCREMENTAL_BACKUP);
auto task = [obj {wptr<BackupExtExtension>(this)}]() {
auto callBackup = [obj](ErrCode errCode, std::string errMsg) {
HILOGI("App onbackup end");
@ -1792,9 +1795,8 @@ void BackupExtExtension::AsyncTaskOnIncrementalBackup()
HILOGE("Current extension execute call backup error, extPtr is empty");
return;
}
extPtr->appExecFinished_ = true;
extPtr->ShutDownExecOnProcessTask();
HILOGI("Start GetAppLocalListAndDoIncrementalBackup");
extPtr->FinishOnProcessTask();
proxy->GetAppLocalListAndDoIncrementalBackup();
};
auto ptr = obj.promote();
@ -1802,7 +1804,6 @@ void BackupExtExtension::AsyncTaskOnIncrementalBackup()
BExcepUltils::BAssert(ptr->extension_, BError::Codes::EXT_INVAL_ARG, "Extension handle have been released");
try {
auto callBackupEx = ptr->HandleBackupEx(obj);
ptr->CreateExecOnProcessTask(BackupRestoreScenario::INCREMENTAL_BACKUP);
ErrCode err = ptr->extension_->OnBackup(callBackup, callBackupEx);
if (err != ERR_OK) {
HILOGE("OnBackup done, err = %{pubilc}d", err);
@ -2054,16 +2055,13 @@ std::function<void(ErrCode, std::string)> BackupExtExtension::IncRestoreResultCa
return;
}
extensionPtr->extension_->InvokeAppExtMethod(errCode, restoreRetInfo);
extensionPtr->FinishOnProcessTask();
if (errCode == ERR_OK) {
if (restoreRetInfo.size()) {
extensionPtr->appExecFinished_ = true;
extensionPtr->ShutDownExecOnProcessTask();
extensionPtr->AppResultReport(restoreRetInfo, BackupRestoreScenario::INCREMENTAL_RESTORE);
}
return;
}
extensionPtr->appExecFinished_ = true;
extensionPtr->ShutDownExecOnProcessTask();
if (restoreRetInfo.empty()) {
extensionPtr->AppIncrementalDone(errCode);
extensionPtr->DoClear();
@ -2096,8 +2094,7 @@ std::function<void(ErrCode, const std::string)> BackupExtExtension::HandleBackup
extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo);
if (backupExRetInfo.size()) {
HILOGI("Start GetAppLocalListAndDoIncrementalBackup");
extensionPtr->appExecFinished_ = true;
extensionPtr->ShutDownExecOnProcessTask();
extensionPtr->FinishOnProcessTask();
proxy->GetAppLocalListAndDoIncrementalBackup();
HILOGI("Will notify backup result report");
extensionPtr->AppResultReport(backupExRetInfo, BackupRestoreScenario::INCREMENTAL_BACKUP);

View File

@ -425,8 +425,7 @@ std::function<void(ErrCode, const std::string)> BackupExtExtension::HandleFullBa
extensionPtr->extension_->InvokeAppExtMethod(errCode, backupExRetInfo);
if (backupExRetInfo.size()) {
HILOGI("Will notify backup result report");
extensionPtr->appExecFinished_ = true;
extensionPtr->ShutDownExecOnProcessTask();
extensionPtr->FinishOnProcessTask();
extensionPtr->AsyncTaskBackup(extensionPtr->extension_->GetUsrConfig());
extensionPtr->AppResultReport(backupExRetInfo, BackupRestoreScenario::FULL_BACKUP);
}
@ -445,12 +444,11 @@ std::function<void(ErrCode, const std::string)> BackupExtExtension::HandleFullBa
HILOGE("Ext extension handle have been released");
return;
}
extensionPtr->appExecFinished_ = true;
extensionPtr->ShutDownExecOnProcessTask();
if (extensionPtr->extension_ == nullptr) {
HILOGE("Extension handle have been released");
return;
}
extensionPtr->FinishOnProcessTask();
extensionPtr->AsyncTaskBackup(extensionPtr->extension_->GetUsrConfig());
};
}
@ -468,6 +466,7 @@ std::function<void(ErrCode, std::string)> BackupExtExtension::FullRestoreCallbac
HILOGE("Extension handle have been released");
return;
}
extensionPtr->FinishOnProcessTask();
extensionPtr->extension_->InvokeAppExtMethod(errCode, restoreRetInfo);
if (errCode == ERR_OK) {
if (restoreRetInfo.size()) {
@ -496,9 +495,8 @@ std::function<void(ErrCode, std::string)> BackupExtExtension::FullRestoreCallbac
HILOGE("Ext extension handle have been released");
return;
}
extensionPtr->appExecFinished_ = true;
extensionPtr->ShutDownExecOnProcessTask();
HILOGI("Current bundle will execute app done");
extensionPtr->FinishOnProcessTask();
if (errMsg.empty()) {
extensionPtr->AppDone(errCode);
} else {
@ -519,8 +517,7 @@ std::function<void(ErrCode, std::string)> BackupExtExtension::IncRestoreResultCa
HILOGE("Ext extension handle have been released");
return;
}
extensionPtr->appExecFinished_ = true;
extensionPtr->ShutDownExecOnProcessTask();
extensionPtr->FinishOnProcessTask();
HILOGI("Current bundle will execute app done");
if (errMsg.empty()) {
extensionPtr->AppIncrementalDone(errCode);
@ -547,105 +544,142 @@ void BackupExtExtension::ReportAppProcessInfo(const std::string processInfo, Bac
}
}
std::function<void(ErrCode, const std::string)> BackupExtExtension::ExecOnProcessCallback(wptr<BackupExtExtension> obj,
BackupRestoreScenario scenario)
// 2024-08-18
void BackupExtExtension::StartOnProcessTaskThread(BackupRestoreScenario scenario)
{
return [obj, scenario](ErrCode errCode, const std::string processInfo) {
HILOGI("Begin Create onProcess Task Thread");
callJsOnProcessThread_ = std::thread[obj { wptr<BackupExtExtension>(this) }, scenario]() {
auto extPtr = obj.promote();
if (extPtr == nullptr) {
HILOGE("Execute onProcess callback error, extPtr is empty");
HILOGE("Create onProcess Task thread failed, extPtr is empty");
return;
}
extPtr->ShutDownOnProcessTimeoutTask();
if (extPtr->isSameClock_ && extPtr->execOnProcessTimeoutTimes_ > 0) {
HILOGE("Current time call onProcess no result, timeOut count:%{public}d",
extPtr->execOnProcessTimeoutTimes_);
return;
}
extPtr->execOnProcessTimeoutTimes_ = 0; // 正常场景重置超时次数
if (processInfo.size()) {
extPtr->ReportAppProcessInfo(processInfo, scenario);
} else {
extPtr->ShutDownExecOnProcessTask(); // 认为没实现onProcess,停掉定时器
return;
}
};
extPtr->ExecCallOnProcessTask(obj, scenario);
}
HILOGI("End Create onProcess Task End");
}
// std::function<void(ErrCode, const std::string)> BackupExtExtension::ExecOnProcessTimeoutCallback(
// wptr<BackupExtExtension> obj)
// {
// }
bool BackupExtExtension::CreateExecOnProcessTask(BackupRestoreScenario scenario)
void BackupExtExtension::ExecCallOnProcessTask(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario)
{
auto taskCallBack = [obj{ wptr<BackupExtExtension>(this) }, scenario]() {
HILOGI("1111");
auto extPtr = obj.promote();
if (extPtr == nullptr) {
HILOGE("CreateExecOnProcessTask error, extPtr is empty");
HILOGI("Begin");
onProcessTimeoutTimer_.Setup();
AsyncCallJsOnProcessTask(obj, scenario);
StartOnProcessTimeOutTimer(obj);
while (!stopCallJsOnProcess_.load()) {
std::unique_lock<std::mutex> lock(onProcessLock_);
execOnProcessCon_.wait_for(lock, std::chrono::seconds(BConstants::CALL_APP_ON_PROCESS_TIME_INTERVAL),
[this] { return this->stopCallJsOnProcess_.load(); });
if (stopCallJsOnProcess_.load()) {
HILOGE("Current extension js onProcess finished");
return;
}
extPtr->isSameClock_ = true;
auto onProcessCallback = extPtr->ExecOnProcessCallback(obj, scenario);
if (!extPtr->appExecFinished_) { // app没有结束就继续执行
extPtr->ExecOnProcessTimeoutTask(obj);
ErrCode ret = extPtr->extension_->OnProcess(onProcessCallback);
if (ret != ERR_OK) {
HILOGE("Execute js method onProcess error, ret:%{public}d", ret);
if (onProcessTimeout_.load()) {
HILOGE("Current extension js onProcess status is timeout");
StartOnProcessTimeOutTimer(obj);
continue;
}
AsyncCallJsOnProcessTask(obj, scenario);
StartOnProcessTimeOutTimer(obj);
}
HILOGI("End");
}
void BackupExtExtension::AsyncCallJsOnProcessTask(wptr<BackupExtExtension> obj, BackupRestoreScenario scenario)
{
HILOGI("Begin");
auto task = [obj, scenario]() {
auto callBack = [obj, scenario](ErrCode errCode, const std::string processInfo) {
// 进入此方法说明onProcess正常1s内响应,则关闭超时的定时器,超时计数减一
auto extPtr = obj.promote();
if (extPtr == nullptr) {
HILOGE("Async call js onPreocess callback failed, exPtr is empty");
return;
}
extPtr->CloseOnProcessTimeOutTimer();
extPtr->onProcessTimeout_.store(false);
if (extPtr->onProcessTimeoutCnt_.load() != 0) {
extPtr->onProcessTimeoutCnt_--;
}
if (processInfo.size == 0) {
extPtr->stopCallJsOnProcess_.store(true);
extPtr->execOnProcessCon_.notify_one();
return;
}
std::string processInfoJsonStr;
BJsonUtil::BuildOnProcessRetInfo(processInfoJsonStr, processInfo);
extPtr->ReportAppProcessInfo(processInfoJsonStr, scenario);
};
auto extenionPtr = obj.promote();
if (extenionPtr == nullptr) {
HILOGE("Async call js onPreocess failed, extenionPtr is empty");
return;
}
ErrCode ret = extenionPtr->extension_->OnProcess(callBack);
if (ret != ERR_OK) {
HILOGE("Call OnProcess Failed, ret:%{public}d", ret);
return;
}
extPtr->isSameClock_ = false;
};
execOnProcessTaskTimer_.Setup();
uint32_t onProcessTimerId = execOnProcessTaskTimer_.Register(taskCallBack,
BConstants::CALL_APP_ON_PROCESS_TIME_INTERVAL, false);
execOnProcessTaskTimerId_ = onProcessTimerId;
return true;
threadPool_.AddTask([task]() {
task();
});
HILOGI("End");
}
bool BackupExtExtension::ShutDownExecOnProcessTask()
void BackupExtExtension::FinishOnProcessTask()
{
HILOGI("Start Shutdown onProcess task timer");
execOnProcessTaskTimer_.Shutdown();
execOnProcessTaskTimer_.Unregister(execOnProcessTaskTimerId_);
HILOGI("End Shutdown onProcess task timer, execOnProcessTaskTimerId: %{public}d", execOnProcessTaskTimerId_);
return true;
HILOGI("Begin");
stopCallJsOnProcess_.store(true);
execOnProcessCon_.notify_one();
if (callJsOnProcessThread_.joinable()) {
callJsOnProcessThread_.join();
}
HILOGI("End");
}
bool BackupExtExtension::ExecOnProcessTimeoutTask(wptr<BackupExtExtension> obj)
void BackupExtExtension::StartOnProcessTimeOutTimer(wptr<BackupExtExtension> obj)
{
HILOGI("Begin");
if (stopCallJsOnProcess_.load()) {
HILOGE("Current extension execute finished");
return;
}
auto timeoutCallback = [obj]() {
auto extPtr = obj.promote();
if (extPtr == nullptr) {
HILOGE("CreateExecOnProcessTask error, extPtr is empty");
HILOGE("Start Create timeout callback failed, extPtr is empty");
return;
}
if (extPtr->execOnProcessTimeoutTimes_ >= BConstants::APP_ON_PROCESS_TIMEOUT_MAX_COUNT) {
HILOGE("Current App timeout more than three times, Will Report");
extPtr->execOnProcessTimeoutTimes_ = 0; // 已经满足三次超时条件
extPtr->ShutDownOnProcessTimeoutTask();
extPtr->ShutDownExecOnProcessTask();
extPtr->AppDone(BError(BError::Codes::EXT_ABILITY_DIED));
} else {
extPtr->execOnProcessTimeoutTimes_++;
if (extPtr->onProcessTimeoutCnt_.load() >= BConstants::APP_ON_PROCESS_TIMEOUT_MAX_COUNT) {
HILOGE("Current extension onProcess timeout more than three times");
extPtr->stopCallJsOnProcess_.store(true);
extPtr->onProcessTimeoutCnt_ = 0;
extPtr->execOnProcessCon_.notify_one();
extPtr->AppDone(BError(BError::Codes::EXT_ABILITY_TIMEOUT));
return;
}
extPtr->onProcessTimeoutCnt_++;
extPtr->onProcessTimeout_.store(true);
HILOGE("Current extension onProcess status is timeout,
onProcessTimeoutCnt:%{public}d", extPtr->onProcessTimeoutCnt_);
};
execOnProcessTimeoutTimer_.Setup();
uint32_t timeoutTimerId = execOnProcessTimeoutTimer_.Register(timeoutCallback,
BConstants::APP_ON_PROCESS_MAX_TIMEOUT, true);
execOnProcessTimeoutTimerId_ = timeoutTimerId;
return true;
uint32_t timerId = onProcessTimeoutTimer_.Register(timeoutCallback, BConstants::APP_ON_PROCESS_MAX_TIMEOUT, true);
onProcessTimeoutTimerId_ = timerId;
HILOGI("End");
}
bool BackupExtExtension::ShutDownOnProcessTimeoutTask()
void BackupExtExtension::CloseOnProcessTimeOutTimer()
{
HILOGI("Start Shutdown onProcess Timeout timer");
execOnProcessTimeoutTimer_.Shutdown();
execOnProcessTimeoutTimer_.Unregister(execOnProcessTimeoutTimerId_);
HILOGI("End Shutdown onProcess Timeout timer, timeoutTimerId: %{public}d", execOnProcessTimeoutTimerId_);
return true;
HILOGI("Begin");
onProcessTimeoutTimer_.Unregister(onProcessTimeoutTimerId_);
HILOGI("End");
}
} // namespace OHOS::FileManagement::Backup

View File

@ -130,7 +130,7 @@ std::tuple<int, EndFileInfo, ErrFileInfo> UntarFile::UnPacket(
fclose(tarFilePtr_);
tarFilePtr_ = nullptr;
return {ret, fileInfos, errInfos};
return {0, fileInfos, errInfos};
}
std::tuple<int, EndFileInfo, ErrFileInfo> UntarFile::IncrementalUnPacket(
@ -151,7 +151,7 @@ std::tuple<int, EndFileInfo, ErrFileInfo> UntarFile::IncrementalUnPacket(
fclose(tarFilePtr_);
tarFilePtr_ = nullptr;
return {ret, fileInfos, errFileInfos};
return {0, fileInfos, errFileInfos};
}
off_t UntarFile::HandleTarBuffer(const string &buff, const string &name, FileStatInfo &info)
@ -225,8 +225,8 @@ bool UntarFile::CheckIfTarBlockValid(char *buff, size_t buffLen, TarHeader *head
if (tailRead == BLOCK_SIZE && IsEmptyBlock(tailBuff)) {
HILOGE("Parsing tar file completed, tailBuff is empty.");
ret = 0;
return false;
}
return false;
}
// check header
if (!IsValidTarBlock(*header)) {
@ -284,9 +284,9 @@ int UntarFile::DealIncreParseTarFileResult(const std::tuple<int, bool, ErrFileIn
}
if (!isFilter) {
fileInfos[fileName] = fileSize;
if (!subErrInfo.empty()) {
errInfos.merge(subErrInfo);
}
}
if (!subErrInfo.empty()) {
errInfos.merge(subErrInfo);
}
return 0;
}

View File

@ -115,6 +115,22 @@ public:
*
*/
static bool BuildRestoreErrInfo(std::string &jsonStr, std::map<std::string, std::vector<int>> errFileInfo);
/**
* @brief App进度返回的信息
*
* @param jsonStr
* @param onProcessRet onProcess接口返回值
*
* @return
*
*/
static bool BuildOnProcessRetInfo(std::string &jsonStr, std::string onProcessRet);
/**
* @brief
*/
static std::string GetCurrentTimeInfo();
};
} // namespace OHOS::FileManagement::Backup

View File

@ -78,7 +78,7 @@ constexpr int MIN_FD_SEND_RATE = 0; // 允许应用申请的最小FD数量
constexpr int DEFAULT_FD_SEND_RATE = 60; // 框架默认的FD数量
constexpr int32_t PARAM_STARING_MAX_MEMORY = 2 * 1024 * 1024;
constexpr int CALL_APP_ON_PROCESS_TIME_INTERVAL = 5000; // 框架每隔5s去调用应用的onProcess
constexpr int CALL_APP_ON_PROCESS_TIME_INTERVAL = 5; // 框架每隔5s去调用应用的onProcess
constexpr int APP_ON_PROCESS_MAX_TIMEOUT = 1000; // 应用的onProcess接口最大超时时间为1秒
constexpr int APP_ON_PROCESS_TIMEOUT_MAX_COUNT = 3; // 应用的onProcess接口超时的上限次数

View File

@ -16,10 +16,14 @@
#include "b_jsonutil/b_jsonutil.h"
#include <cstring>
#include <chrono>
#include <iomanip>
#include <sstream>
#include "cJSON.h"
#include "b_error/b_error.h"
#include "filemgmt_libhilog.h"
#include "b_jsonutil.h"
namespace OHOS::FileManagement::Backup {
using namespace std;
@ -238,4 +242,43 @@ bool BJsonUtil::BuildRestoreErrInfo(std::string &jsonStr, std::map<std::string,
cJSON_free(data);
return true;
}
}
}
bool OHOS::FileManagement::Backup::BJsonUtil::BuildOnProcessRetInfo(std::string &jsonStr, std::string onProcessRet)
{
cJSON *info = cJSON_CreateObject();
if (info == nullptr) {
return false;
}
cJSON *processInfo = cJSON_CreateObject();
if (processInfo == nullptr) {
cJSON_Delete(info);
return false;
}
std::string timeInfo = GetCurrentTimeInfo();
cJSON_AddStringToObject(processInfo, "timeInfo", timeInfo.c_str());
cJSON_AddStringToObject(processInfo, "resultInfo", onProcessRet.c_str());
cJSON_AddItemToObject(info, "processResult", processInfo);
char *data = cJSON_Print(info);
if (data == nullptr) {
cJSON_Delete(info);
return false;
}
jsonStr = std::string(data);
cJSON_Delete(info);
cJSON_free(data);
return true;
}
std::string OHOS::FileManagement::Backup::BJsonUtil::GetCurrentTimeInfo()
{
auto now = std::chrono::system_clock::now();
auto timePoint = std::chrono::system_clock::to_time_t(now);
std::stringstream timestream;
timestream << std::put_time(std::localtime(&timePoint), "%Y-%m-%d %H:%M:%S");
std::string result = timestream.str();
return result;
}