InitBackSession receives the Cap file and verifies the space

Change-Id: Iecc84da0f2121b100f51e6d9779800f9b1de49a9
Signed-off-by: huaqingsimeng <qiukaiqing@huawei.com>
This commit is contained in:
maokelong95 2022-05-05 15:20:16 +08:00
parent 8cdea7eef1
commit cd7f440a1b
17 changed files with 360 additions and 5 deletions

View File

@ -0,0 +1,36 @@
/*
* (c) 2022
*/
#include "b_session_backup.h"
#include "filemgmt_libhilog.h"
#include "service_proxy.h"
namespace OHOS {
namespace FileManagement {
namespace Backup {
using namespace std;
unique_ptr<BSessionBackup> BSessionBackup::Init(UniqueFd remoteCap, std::vector<AppId> appsToBackup)
{
try {
auto backup = make_unique<BSessionBackup>();
auto proxy = ServiceProxy::GetInstance();
if (proxy == nullptr) {
return nullptr;
}
int32_t res = proxy->InitBackupSession(move(remoteCap), appsToBackup);
if (res != 0) {
HILOGE("Failed to Backup because of %{public}d", res);
return nullptr;
}
return backup;
} catch (const exception e) {
HILOGE("Failed to Backup because of %{public}s", e.what());
}
return nullptr;
}
} // namespace Backup
} // namespace FileManagement
} // namespace OHOS

View File

@ -0,0 +1,45 @@
/*
* (c) 2022
*/
#include "b_session_restore.h"
#include "filemgmt_libhilog.h"
#include "service_proxy.h"
namespace OHOS {
namespace FileManagement {
namespace Backup {
using namespace std;
unique_ptr<BSessionRestore> BSessionRestore::Init(std::vector<AppId> appsToRestore)
{
HILOGI("Begin");
try {
auto restore = make_unique<BSessionRestore>();
auto proxy = ServiceProxy::GetInstance();
if (proxy == nullptr) {
return nullptr;
}
int32_t res = proxy->InitRestoreSession(appsToRestore);
if (res != 0) {
HILOGE("Failed to Restore because of %{public}d", res);
return nullptr;
}
return restore;
} catch (const exception e) {
HILOGE("Failed to Restore because of %{public}s", e.what());
}
return nullptr;
}
UniqueFd BSessionRestore::GetLocalCapabilities()
{
auto proxy = ServiceProxy::GetInstance();
if (proxy == nullptr) {
return UniqueFd(-1);
}
return UniqueFd(proxy->GetLocalCapabilities());
}
} // namespace Backup
} // namespace FileManagement
} // namespace OHOS

View File

@ -49,7 +49,37 @@ int32_t ServiceProxy::InitRestoreSession(std::vector<AppId> apps)
return -EPIPE;
}
Remote()->SendRequest(IService::SERVICE_CMD_INIT_RESTORE_SESSION, data, reply, option);
int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_INIT_RESTORE_SESSION, data, reply, option);
if (ret != NO_ERROR) {
HILOGE("Received error %{public}d when doing IPC", ret);
return ret;
}
HILOGI("Successful");
return reply.ReadInt32();
}
int32_t ServiceProxy::InitBackupSession(UniqueFd fd, std::vector<AppId> appIDs)
{
HILOGI("Start");
MessageParcel data;
data.WriteInterfaceToken(GetDescriptor());
MessageParcel reply;
MessageOption option;
if (!data.WriteFileDescriptor(fd)) {
HILOGI("Failed to send the fd");
return -EPIPE;
}
if (!data.WriteStringVector(appIDs)) {
HILOGE("Failed to send appIDs");
return -EPIPE;
}
int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_INIT_BACKUP_SESSION, data, reply, option);
if (ret != NO_ERROR) {
HILOGE("Received error %{public}d when doing IPC", ret);
return ret;
}
HILOGI("Successful");
return reply.ReadInt32();
}
@ -62,7 +92,7 @@ int32_t ServiceProxy::GetLocalCapabilities()
MessageParcel reply;
MessageOption option;
auto ret = Remote()->SendRequest(IService::SERVICE_CMD_GET_LOCAL_CAPABILITIES, data, reply, option);
int32_t ret = Remote()->SendRequest(IService::SERVICE_CMD_GET_LOCAL_CAPABILITIES, data, reply, option);
if (ret != NO_ERROR) {
HILOGE("Received error %{public}d when doing IPC", ret);
return ret;

View File

@ -16,6 +16,8 @@ config("private_config") {
ohos_shared_library("backup_api") {
sources = [
"//foundation/filemanagement/backup/frameworks/native/src/b_session_backup.cpp",
"//foundation/filemanagement/backup/frameworks/native/src/b_session_restore.cpp",
"//foundation/filemanagement/backup/frameworks/native/src/complex_object.cpp",
"//foundation/filemanagement/backup/frameworks/native/src/service_proxy.cpp",
]
@ -40,9 +42,11 @@ ohos_shared_library("backup_api") {
public_deps = [
"//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core",
"//foundation/filemanagement/backup/utils:backup_utils",
"//utils/native/base:utils",
]
use_exceptions = true
part_name = "backup"
subsystem_name = "filemanagement"
}

View File

@ -0,0 +1,34 @@
/*
* (c) 2022
*/
#ifndef OHOS_FILEMGMT_BACKUP_B_SESSION_BACKUP_H
#define OHOS_FILEMGMT_BACKUP_B_SESSION_BACKUP_H
#include <functional>
#include <memory>
#include <vector>
#include "b_file_info.h"
#include "errors.h"
#include "unique_fd.h"
namespace OHOS {
namespace FileManagement {
namespace Backup {
class BSessionBackup {
public:
/**
* @brief
*
* @param remoteCap Json文件使BRestoreSession的GetLocalCaablities方法获取
* @param appsToBackup
* @return std::unique_ptr<BSessionBackup>
*/
static std::unique_ptr<BSessionBackup> Init(UniqueFd remoteCap, std::vector<AppId> appsToBackup);
};
} // namespace Backup
} // namespace FileManagement
} // namespace OHOS
#endif // OHOS_FILEMGMT_BACKUP_B_SESSION_BACKUP_H

View File

@ -5,6 +5,7 @@
#ifndef OHOS_FILEMGMT_BACKUP_BACKUP_KIT_H
#define OHOS_FILEMGMT_BACKUP_BACKUP_KIT_H
#include "b_session_backup.h"
#include "b_session_restore.h"
#endif // OHOS_FILEMGMT_BACKUP_BACKUP_KIT_H

View File

@ -21,12 +21,14 @@ public:
SERVICE_CMD_ECHO,
SERVICE_CMD_DUMPOBJ,
SERVICE_CMD_INIT_RESTORE_SESSION,
SERVICE_CMD_INIT_BACKUP_SESSION,
SERVICE_CMD_GET_LOCAL_CAPABILITIES,
};
virtual int32_t EchoServer(const std::string &echoStr) = 0;
virtual void DumpObj(const ComplexObject &obj) = 0;
virtual int32_t InitRestoreSession(std::vector<AppId> apps) = 0;
virtual int32_t InitBackupSession(UniqueFd fd, std::vector<AppId> apps) = 0;
virtual int32_t GetLocalCapabilities() = 0;
DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Filemanagement.Backup.IService")

View File

@ -16,6 +16,7 @@ public:
int32_t EchoServer(const std::string &echoStr) override;
void DumpObj(const ComplexObject &obj) override;
int32_t InitRestoreSession(std::vector<AppId> apps) override;
int32_t InitBackupSession(UniqueFd fd, std::vector<AppId> appIds) override;
int32_t GetLocalCapabilities() override;
public:

View File

@ -19,7 +19,7 @@ ohos_shared_library("backup_sa") {
deps = [
"//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libhilog",
"//foundation/filemanagement/backup/interfaces/inner_api/native:backup_api",
"//foundation/filemanagement/backup/utils/:backup_utils",
"//foundation/filemanagement/backup/utils:backup_utils",
]
external_deps = [

View File

@ -6,6 +6,7 @@
#define OHOS_FILEMGMT_BACKUP_B_JSON_ENTITY_CAPS_H
#include "b_json/b_json_cached_entity.h"
#include "filemgmt_libhilog.h"
namespace OHOS {
namespace FileManagement {
@ -19,9 +20,11 @@ public:
uint64_t GetFreeDiskSpace()
{
if (!obj_ || obj_.isMember("freeDiskSpace")) {
if (!obj_ || !obj_.isMember("freeDiskSpace") || !obj_["freeDiskSpace"].isUInt64()) {
HILOGE("Failed to init field FreeDiskSpace");
return 0;
}
return obj_["freeDiskSpace"].asUInt64();
};

View File

@ -27,6 +27,7 @@ public:
int32_t EchoServer(const std::string &echoStr) override;
void DumpObj(const ComplexObject &obj) override;
int32_t InitRestoreSession(std::vector<AppId> apps) override;
int32_t InitBackupSession(UniqueFd fd, std::vector<AppId> apps) override;
int32_t GetLocalCapabilities() override;
private:

View File

@ -26,6 +26,7 @@ private:
int32_t CmdEchoServer(MessageParcel &data, MessageParcel &reply);
int32_t CmdDumpObj(MessageParcel &data, MessageParcel &reply);
int32_t CmdInitRestoreSession(MessageParcel &data, MessageParcel &reply);
int32_t CmdInitBackupSession(MessageParcel &data, MessageParcel &reply);
int32_t CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply);
};
} // namespace Backup

View File

@ -84,6 +84,27 @@ int32_t Service::InitRestoreSession(std::vector<AppId> apps)
return e.GetCode();
}
}
int32_t Service::InitBackupSession(UniqueFd fd, std::vector<AppId> apps)
{
try {
if (apps.empty()) {
throw BError(BError::Codes::SA_INVAL_ARG, "No app was selected");
}
BJsonCachedEntity<BJsonEntityCaps> cachedEntity(move(fd));
auto cache = cachedEntity.Structuralize();
uint64_t size = cache.GetFreeDiskSpace();
if (size == 0) {
throw BError(BError::Codes::SA_INVAL_ARG,
"Field FreeDiskSpace is invalid or there's no enough space left on disk");
}
HILOGI("Check field FreeDiskSpace size %{public}llu", size);
return BError(BError::Codes::OK);
} catch (const BError &e) {
return e.GetCode();
}
}
} // namespace Backup
} // namespace FileManagement
} // namespace OHOS

View File

@ -22,6 +22,7 @@ ServiceStub::ServiceStub()
opToInterfaceMap_[SERVICE_CMD_ECHO] = &ServiceStub::CmdEchoServer;
opToInterfaceMap_[SERVICE_CMD_DUMPOBJ] = &ServiceStub::CmdDumpObj;
opToInterfaceMap_[SERVICE_CMD_INIT_RESTORE_SESSION] = &ServiceStub::CmdInitRestoreSession;
opToInterfaceMap_[SERVICE_CMD_INIT_BACKUP_SESSION] = &ServiceStub::CmdInitBackupSession;
opToInterfaceMap_[SERVICE_CMD_GET_LOCAL_CAPABILITIES] = &ServiceStub::CmdGetLocalCapabilities;
}
@ -80,6 +81,28 @@ int32_t ServiceStub::CmdInitRestoreSession(MessageParcel &data, MessageParcel &r
return BError(BError::Codes::OK);
}
int32_t ServiceStub::CmdInitBackupSession(MessageParcel &data, MessageParcel &reply)
{
HILOGE("Begin");
UniqueFd fd(data.ReadFileDescriptor());
if (fd < 0) {
return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive fd");
}
std::vector<string> appIds;
if (!data.ReadStringVector(&appIds)) {
return BError(BError::Codes::SA_INVAL_ARG, "Failed to receive appIds");
}
int res = InitBackupSession(move(fd), appIds);
if (!reply.WriteInt32(res)) {
stringstream ss;
ss << "Failed to send the result " << res;
return BError(BError::Codes::SA_BROKEN_IPC, ss.str());
}
return BError(BError::Codes::OK);
}
int32_t ServiceStub::CmdGetLocalCapabilities(MessageParcel &data, MessageParcel &reply)
{
HILOGE("Begin");

View File

@ -6,9 +6,11 @@ ohos_executable("backup_tool") {
sources = [
"src/main.cpp",
"src/tools_op.cpp",
"src/tools_op_backup.cpp",
"src/tools_op_check_sa.cpp",
"src/tools_op_help.cpp",
"src/tools_op_mock.cpp",
"src/tools_op_restore.cpp",
]
defines = [

View File

@ -0,0 +1,71 @@
/*
* (c) 2022
*/
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include "b_filesystem/b_file.h"
#include "backup_kit.h"
#include "service_proxy.h"
#include "tools_op.h"
namespace OHOS {
namespace FileManagement {
namespace Backup {
using namespace std;
static string GenHelpMsg()
{
return "the functionality of the backup api. Arg list:\n"
"path_cap_file app_id1 app_id2...";
}
static int32_t InitPathCapFile(string_view pathCapFile, ToolsOp::CRefVStrView args)
{
std::vector<AppId> appIds;
for (auto &&id : args) {
appIds.push_back(id.data());
}
UniqueFd fd(open(pathCapFile.data(), O_RDONLY));
if (fd < 0) {
fprintf(stderr, "Failed to open file error: %d %s\n", errno, strerror(errno));
return -errno;
}
auto backup = BSessionBackup::Init(move(fd), appIds);
if (backup == nullptr) {
printf("Failed to init backup");
return -EPERM;
}
return 0;
}
static int Exec(ToolsOp::CRefVStrView args)
{
if (args.empty()) {
fprintf(stderr, "Please input the name of API to backup\n");
return -EINVAL;
}
std::vector<string_view> argsWithoutHead(args.begin() + 1, args.end());
return InitPathCapFile(args.front(), argsWithoutHead);
}
/**
* @brief The hack behind is that "variable with static storage duration has initialization or a destructor with side
* effects; it shall not be eliminated even if it appears to be unused" -- point 2.[basic.stc.static].c++ draft
*
*/
static bool g_autoRegHack = ToolsOp::Register(ToolsOp::Descriptor {
.opName = {"backup"},
.funcGenHelpMsg = GenHelpMsg,
.funcExec = Exec,
});
} // namespace Backup
} // namespace FileManagement
} // namespace OHOS

View File

@ -0,0 +1,80 @@
/*
* (c) 2022
*/
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include "b_filesystem/b_file.h"
#include "backup_kit.h"
#include "service_proxy.h"
#include "tools_op.h"
namespace OHOS {
namespace FileManagement {
namespace Backup {
using namespace std;
static string GenHelpMsg()
{
return "the functionality of the restore api. Arg list:\n"
"path_cap_file app_id1 app_id2...";
}
static int32_t GetPathCapFile(string_view pathCapFile, ToolsOp::CRefVStrView args)
{
std::vector<AppId> appIds;
for (auto &&id : args) {
appIds.push_back(id.data());
}
auto restore = BSessionRestore::Init(appIds);
if (restore == nullptr) {
printf("Failed to init restore");
return -EPERM;
}
UniqueFd fd(restore->GetLocalCapabilities());
if (fd < 0) {
printf("Failed to receive fd");
return fd;
}
auto buf = BFile::ReadFile(fd);
UniqueFd fdfile(open(pathCapFile.data(), O_WRONLY | O_CREAT, S_IRWXU));
if (fdfile < 0) {
fprintf(stderr, "Failed to open file. error: %d %s\n", errno, strerror(errno));
return -errno;
}
if (write(fdfile, buf.get(), strlen(buf.get())) <= 0) {
fprintf(stderr, "Failed to write file. error: %d %s\n", errno, strerror(errno));
return -errno;
}
return 0;
}
static int Exec(ToolsOp::CRefVStrView args)
{
if (args.empty()) {
fprintf(stderr, "Please input the name of API to restore\n");
return -EINVAL;
}
std::vector<string_view> argsWithoutHead(args.begin() + 1, args.end());
return GetPathCapFile(args.front(), argsWithoutHead);
}
/**
* @brief The hack behind is that "variable with static storage duration has initialization or a destructor with side
* effects; it shall not be eliminated even if it appears to be unused" -- point 2.[basic.stc.static].c++ draft
*
*/
static bool g_autoRegHack = ToolsOp::Register(ToolsOp::Descriptor {
.opName = {"restore"},
.funcGenHelpMsg = GenHelpMsg,
.funcExec = Exec,
});
} // namespace Backup
} // namespace FileManagement
} // namespace OHOS