From 0934d33a3159d4405055754e66b86809b8816a5d Mon Sep 17 00:00:00 2001 From: Ilho Kim Date: Tue, 7 Sep 2021 13:51:33 +0900 Subject: [PATCH] Handle path state in resource event signal Change-Id: I9c9c8de57bdf70ec71c9ab4e85e1e89f379f8087 Signed-off-by: Ilho Kim --- .../include/abstract_request_handler.hh | 4 +- src/res-copy/include/copy_request_handler.hh | 1 + src/res-copy/include/event_signal_sender.hh | 11 +++-- src/res-copy/include/res_path_info.hh | 11 ++++- src/res-copy/src/abstract_request_handler.cc | 6 ++- src/res-copy/src/copy_request_handler.cc | 23 ++++++++++ src/res-copy/src/createdir_request_handler.cc | 5 +- src/res-copy/src/event_signal_sender.cc | 46 ++++++++++++++----- src/res-copy/src/remove_request_handler.cc | 5 +- src/res-copy/src/request_handler_invoker.cc | 13 +++--- src/res-copy/src/res_handler.cc | 6 +-- src/res-copy/src/res_path_info.cc | 12 ++++- .../res-copy/src/test_event_signal_sender.cc | 12 ++--- 13 files changed, 117 insertions(+), 38 deletions(-) diff --git a/src/res-copy/include/abstract_request_handler.hh b/src/res-copy/include/abstract_request_handler.hh index aa61ab8..26c98d8 100644 --- a/src/res-copy/include/abstract_request_handler.hh +++ b/src/res-copy/include/abstract_request_handler.hh @@ -35,13 +35,14 @@ class AbstractRequestHandler { virtual ErrorType Execute() = 0; virtual const std::string GetRequestHandlerType() const = 0; + virtual std::list GetResultPathList(); protected: std::string GetRootPath(); std::string GetSrcRootPath(); std::string GetDstRootPath(); const std::string GetPkgID() const; - const std::list GetPathList() const; + std::list& GetPathList(); uid_t GetUID() const; private: @@ -49,6 +50,7 @@ class AbstractRequestHandler { uid_t uid_; std::string root_path_; std::list path_list_; + std::string result_path_; }; } // namespace res_handler diff --git a/src/res-copy/include/copy_request_handler.hh b/src/res-copy/include/copy_request_handler.hh index 9709fa5..37971f1 100644 --- a/src/res-copy/include/copy_request_handler.hh +++ b/src/res-copy/include/copy_request_handler.hh @@ -33,6 +33,7 @@ class CopyRequestHandler : public AbstractRequestHandler { ErrorType Execute() override; const std::string GetRequestHandlerType() const override; + std::list GetResultPathList() override; }; } // namespace res_handler diff --git a/src/res-copy/include/event_signal_sender.hh b/src/res-copy/include/event_signal_sender.hh index 495f059..f702208 100644 --- a/src/res-copy/include/event_signal_sender.hh +++ b/src/res-copy/include/event_signal_sender.hh @@ -17,12 +17,14 @@ #ifndef EVENT_SIGNAL_SENDER_HH_ #define EVENT_SIGNAL_SENDER_HH_ +#include #include #include #include #include "include/error_type.hh" +#include "include/res_path_info.hh" #include "include/request_type.hh" namespace res_handler { @@ -30,16 +32,17 @@ namespace res_handler { class EventSignalSender { public: EventSignalSender(pkgmgr_installer* installer); - bool SendStart(); - bool SendOK(); - bool SendFail(ErrorType error); + bool SendStart(const std::list& res_path_info); + bool SendOK(const std::list& res_path_info); + bool SendFail(ErrorType error, const std::list& res_path_info); void SetPkgID(std::string pkgid); void SetReqType(ReqType req_type); void SetUID(uid_t uid); void SetSessionID(std::string session_id); private: - bool SendSignal(const char* status, ErrorType error); + bool SendSignal(const char* status, ErrorType error, + const std::list& res_path_info); std::string pkgid_; ReqType req_type_; diff --git a/src/res-copy/include/res_path_info.hh b/src/res-copy/include/res_path_info.hh index d85fb57..54cc0f4 100644 --- a/src/res-copy/include/res_path_info.hh +++ b/src/res-copy/include/res_path_info.hh @@ -23,14 +23,23 @@ namespace res_handler { class ResPathInfo { public: - ResPathInfo(std::string src, std::string dst); + enum class State { + NONE, + OK, + FAILED + }; + + ResPathInfo(std::string src, std::string dst, State state = State::NONE); const std::string& GetSrcPath() const; const std::string& GetDstPath() const; + const State GetState() const; + void SetState(State state); private: std::string src_; std::string dst_; + State state_; }; } // namespace res_handler diff --git a/src/res-copy/src/abstract_request_handler.cc b/src/res-copy/src/abstract_request_handler.cc index 113f38e..b432e47 100644 --- a/src/res-copy/src/abstract_request_handler.cc +++ b/src/res-copy/src/abstract_request_handler.cc @@ -47,7 +47,7 @@ const std::string AbstractRequestHandler::GetPkgID() const { return pkgid_; } -const std::list AbstractRequestHandler::GetPathList() const { +std::list& AbstractRequestHandler::GetPathList() { return path_list_; } @@ -55,4 +55,8 @@ uid_t AbstractRequestHandler::GetUID() const { return uid_; } +std::list AbstractRequestHandler::GetResultPathList() { + return path_list_; +} + } // namespace res_handler diff --git a/src/res-copy/src/copy_request_handler.cc b/src/res-copy/src/copy_request_handler.cc index 4aca075..a1bc08d 100644 --- a/src/res-copy/src/copy_request_handler.cc +++ b/src/res-copy/src/copy_request_handler.cc @@ -55,6 +55,7 @@ ErrorType CopyRequestHandler::Execute() { bf::path src_path = src_root_path / path_info.GetSrcPath(); if (!bf::exists(src_path)) { LOG(ERROR) << "Path not exists :" << src_path; + path_info.SetState(ResPathInfo::State::FAILED); return ErrorType::ERROR_RES_NOT_FOUND; } @@ -63,6 +64,7 @@ ErrorType CopyRequestHandler::Execute() { if (bf::is_directory(src_path)) { if (!CopyDir(src_path, dst_path, FS_MERGE_OVERWRITE, true)) { LOG(ERROR) << "Failed to copy directory " << src_path; + path_info.SetState(ResPathInfo::State::FAILED); return ErrorType::ERROR_SYSTEM_ERROR; } } else { @@ -71,9 +73,11 @@ ErrorType CopyRequestHandler::Execute() { if (!CopyFile(src_path, dst_path)) { LOG(ERROR) << "Failed to copy directory " << src_path; + path_info.SetState(ResPathInfo::State::FAILED); return ErrorType::ERROR_SYSTEM_ERROR; } } + path_info.SetState(ResPathInfo::State::OK); } return ErrorType::ERROR_NONE; @@ -83,4 +87,23 @@ const std::string CopyRequestHandler::GetRequestHandlerType() const { return kCopyReqHandlerType; } +std::list CopyRequestHandler::GetResultPathList() { + bf::path src_root_path(GetSrcRootPath()); + bf::path dst_root_path(GetDstRootPath()); + std::list result; + + for (const auto& path_info : GetPathList()) { + bf::path src_path = src_root_path / path_info.GetSrcPath(); + bf::path dst_path = dst_root_path / path_info.GetDstPath(); + bf::path result_path = path_info.GetDstPath(); + + if (bf::is_directory(dst_path) && !bf::is_directory(src_path)) + result_path /= src_path.filename(); + + result.emplace_back(result_path.string(), "", path_info.GetState()); + } + + return result; +} + } // namespace res_handler diff --git a/src/res-copy/src/createdir_request_handler.cc b/src/res-copy/src/createdir_request_handler.cc index dbc053f..eaacee6 100644 --- a/src/res-copy/src/createdir_request_handler.cc +++ b/src/res-copy/src/createdir_request_handler.cc @@ -52,8 +52,11 @@ ErrorType CreateDirRequestHandler::Execute() { for (auto& path_info : GetPathList()) { bf::path dst_path = dst_root_path / path_info.GetSrcPath(); - if (!CreateDirs(GetPkgID(), dst_path)) + if (!CreateDirs(GetPkgID(), dst_path)) { + path_info.SetState(ResPathInfo::State::FAILED); return ErrorType::ERROR_SYSTEM_ERROR; + } + path_info.SetState(ResPathInfo::State::OK); } return ErrorType::ERROR_NONE; diff --git a/src/res-copy/src/event_signal_sender.cc b/src/res-copy/src/event_signal_sender.cc index c87a005..467f389 100644 --- a/src/res-copy/src/event_signal_sender.cc +++ b/src/res-copy/src/event_signal_sender.cc @@ -17,12 +17,11 @@ #include "include/event_signal_sender.hh" +#include + #include #include -#include -#include - #include "include/error_type.hh" #include "include/request_type.hh" #include "include/logging.hh" @@ -33,7 +32,8 @@ EventSignalSender::EventSignalSender(pkgmgr_installer* installer) : pkgid_(""), req_type_(ReqType::REQ_TYPE_UNKNOWN), uid_(0), session_id_(""), installer_(installer, pkgmgr_installer_free) {} -bool EventSignalSender::SendSignal(const char* status, ErrorType error) { +bool EventSignalSender::SendSignal(const char* status, ErrorType error, + const std::list& res_path_info) { if (!installer_) return false; @@ -44,8 +44,27 @@ bool EventSignalSender::SendSignal(const char* status, ErrorType error) { return false; } - pkgmgr_res_event_info_set_error_code(event_info.get(), - static_cast(error)); + if (pkgmgr_res_event_info_set_error_code(event_info.get(), + static_cast(error)) != PKGMGR_R_OK) { + LOG(ERROR) << "Fail to set error code"; + return false; + } + + for (const ResPathInfo& info : res_path_info) { + pkgmgr_res_event_path_state state = PM_RES_EVENT_PATH_STATE_NONE; + if (info.GetState() == ResPathInfo::State::NONE) + state = PM_RES_EVENT_PATH_STATE_NONE; + else if (info.GetState() == ResPathInfo::State::OK) + state = PM_RES_EVENT_PATH_STATE_OK; + else if (info.GetState() == ResPathInfo::State::FAILED) + state = PM_RES_EVENT_PATH_STATE_FAILED; + + if (pkgmgr_res_event_info_add_path_state(event_info.get(), + info.GetSrcPath().c_str(), state) != PKGMGR_R_OK) { + LOG(ERROR) << "Fail to add path state"; + return false; + } + } if (pkgmgr_installer_send_res_signal(installer_.get(), pkgid_.c_str(), status, event_info.get()) != 0) { @@ -62,8 +81,9 @@ bool EventSignalSender::SendSignal(const char* status, ErrorType error) { return true; } -bool EventSignalSender::SendStart() { - if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR, ErrorType::ERROR_NONE)) { +bool EventSignalSender::SendStart(const std::list& res_path_info) { + if (!SendSignal(PKGMGR_INSTALLER_START_KEY_STR, + ErrorType::ERROR_NONE, res_path_info)) { LOG(ERROR) << "Fail to send start signal"; return false; } @@ -71,8 +91,9 @@ bool EventSignalSender::SendStart() { return true; } -bool EventSignalSender::SendOK() { - if (!SendSignal(PKGMGR_INSTALLER_OK_EVENT_STR, ErrorType::ERROR_NONE)) { +bool EventSignalSender::SendOK(const std::list& res_path_info) { + if (!SendSignal(PKGMGR_INSTALLER_OK_EVENT_STR, + ErrorType::ERROR_NONE, res_path_info)) { LOG(ERROR) << "Fail to send ok signal"; return false; } @@ -80,8 +101,9 @@ bool EventSignalSender::SendOK() { return true; } -bool EventSignalSender::SendFail(ErrorType error) { - if (!SendSignal(PKGMGR_INSTALLER_FAIL_EVENT_STR, error)) { +bool EventSignalSender::SendFail(ErrorType error, + const std::list& res_path_info) { + if (!SendSignal(PKGMGR_INSTALLER_FAIL_EVENT_STR, error, res_path_info)) { LOG(ERROR) << "Fail to send fail signal"; return false; } diff --git a/src/res-copy/src/remove_request_handler.cc b/src/res-copy/src/remove_request_handler.cc index 9f752a3..ce97335 100644 --- a/src/res-copy/src/remove_request_handler.cc +++ b/src/res-copy/src/remove_request_handler.cc @@ -59,8 +59,11 @@ ErrorType RemoveRequestHandler::Execute() { continue; } - if (!RemoveAll(dst_path)) + if (!RemoveAll(dst_path)) { + path_info.SetState(ResPathInfo::State::FAILED); return ErrorType::ERROR_SYSTEM_ERROR; + } + path_info.SetState(ResPathInfo::State::OK); } return ErrorType::ERROR_NONE; diff --git a/src/res-copy/src/request_handler_invoker.cc b/src/res-copy/src/request_handler_invoker.cc index 52bf4e4..c6d3b9c 100644 --- a/src/res-copy/src/request_handler_invoker.cc +++ b/src/res-copy/src/request_handler_invoker.cc @@ -54,7 +54,7 @@ RequestHandlerInvoker::RequestHandlerInvoker( bool RequestHandlerInvoker::Validate() { if (handler_ == nullptr) { LOG(ERROR) << "Failed to initialize handler"; - signal_->SendFail(ErrorType::ERROR_SYSTEM_ERROR); + signal_->SendFail(ErrorType::ERROR_SYSTEM_ERROR, {}); return false; } @@ -63,7 +63,8 @@ bool RequestHandlerInvoker::Validate() { option_.GetRequestType(), option_.GetPathList()) != ErrorType::ERROR_NONE) { LOG(ERROR) << "Validation failed"; - signal_->SendFail(ErrorType::ERROR_SYSTEM_ERROR); + signal_->SendFail(ErrorType::ERROR_SYSTEM_ERROR, + handler_->GetResultPathList()); return false; } @@ -72,17 +73,17 @@ bool RequestHandlerInvoker::Validate() { bool RequestHandlerInvoker::Execute() { if (handler_ == nullptr) { - signal_->SendFail(ErrorType::ERROR_SYSTEM_ERROR); + signal_->SendFail(ErrorType::ERROR_SYSTEM_ERROR, {}); return false; } - signal_->SendStart(); + signal_->SendStart(handler_->GetResultPathList()); ErrorType ret = handler_->Execute(); if (ret != ErrorType::ERROR_NONE) { - signal_->SendFail(ret); + signal_->SendFail(ret, handler_->GetResultPathList()); return false; } - signal_->SendOK(); + signal_->SendOK(handler_->GetResultPathList()); return true; } diff --git a/src/res-copy/src/res_handler.cc b/src/res-copy/src/res_handler.cc index 8790639..a0012e1 100644 --- a/src/res-copy/src/res_handler.cc +++ b/src/res-copy/src/res_handler.cc @@ -16,10 +16,10 @@ #include "include/res_handler.hh" -#include - #include +#include + #include "include/event_signal_sender.hh" #include "include/logging.hh" #include "include/param_checker.hh" @@ -39,7 +39,7 @@ bool ResHandler::Init(int argc, char* argv[]) { res_handler::ParamChecker option(argc, argv); if (!option.Validate()) { LOG(ERROR) << "Invalid argument has given"; - signal->SendFail(ErrorType::ERROR_INVALID_PARAMETER); + signal->SendFail(ErrorType::ERROR_INVALID_PARAMETER, {}); return false; } signal->SetPkgID(option.GetPkgID()); diff --git a/src/res-copy/src/res_path_info.cc b/src/res-copy/src/res_path_info.cc index 56e07df..4c2fb76 100644 --- a/src/res-copy/src/res_path_info.cc +++ b/src/res-copy/src/res_path_info.cc @@ -21,8 +21,8 @@ namespace res_handler { -ResPathInfo::ResPathInfo(std::string src, std::string dst) : - src_(src), dst_(dst) {} +ResPathInfo::ResPathInfo(std::string src, std::string dst, State state) + : src_(src), dst_(dst), state_(state) {} const std::string& ResPathInfo::GetSrcPath() const { return src_; @@ -32,4 +32,12 @@ const std::string& ResPathInfo::GetDstPath() const { return dst_; } +const ResPathInfo::State ResPathInfo::GetState() const { + return state_; +} + +void ResPathInfo::SetState(ResPathInfo::State state) { + state_ = state; +} + } // namespace res_handler diff --git a/tests/unit_tests/res-copy/src/test_event_signal_sender.cc b/tests/unit_tests/res-copy/src/test_event_signal_sender.cc index 72ca3c9..50996c2 100644 --- a/tests/unit_tests/res-copy/src/test_event_signal_sender.cc +++ b/tests/unit_tests/res-copy/src/test_event_signal_sender.cc @@ -60,9 +60,9 @@ TEST_F(EventSignalSenderTest, SendSignals) { signal.SetUID(0); signal.SetSessionID("session_id"); - EXPECT_TRUE(signal.SendStart()); - EXPECT_TRUE(signal.SendOK()); - EXPECT_TRUE(signal.SendFail(res_handler::ErrorType::ERROR_NONE)); + EXPECT_TRUE(signal.SendStart({})); + EXPECT_TRUE(signal.SendOK({})); + EXPECT_TRUE(signal.SendFail(res_handler::ErrorType::ERROR_NONE, {})); } TEST_F(EventSignalSenderTest, SendSignalWithNullPkgMgrInstaller) { @@ -74,7 +74,7 @@ TEST_F(EventSignalSenderTest, SendSignalWithNullPkgMgrInstaller) { .Times(0); EventSignalSender signal(nullptr); - EXPECT_FALSE(signal.SendStart()); - EXPECT_FALSE(signal.SendOK()); - EXPECT_FALSE(signal.SendFail(res_handler::ErrorType::ERROR_NONE)); + EXPECT_FALSE(signal.SendStart({})); + EXPECT_FALSE(signal.SendOK({})); + EXPECT_FALSE(signal.SendFail(res_handler::ErrorType::ERROR_NONE, {})); } -- 2.34.1