Handle path state in resource event signal 15/263615/4
authorIlho Kim <ilho159.kim@samsung.com>
Tue, 7 Sep 2021 04:51:33 +0000 (13:51 +0900)
committerIlho Kim <ilho159.kim@samsung.com>
Wed, 8 Sep 2021 03:51:01 +0000 (12:51 +0900)
Change-Id: I9c9c8de57bdf70ec71c9ab4e85e1e89f379f8087
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
13 files changed:
src/res-copy/include/abstract_request_handler.hh
src/res-copy/include/copy_request_handler.hh
src/res-copy/include/event_signal_sender.hh
src/res-copy/include/res_path_info.hh
src/res-copy/src/abstract_request_handler.cc
src/res-copy/src/copy_request_handler.cc
src/res-copy/src/createdir_request_handler.cc
src/res-copy/src/event_signal_sender.cc
src/res-copy/src/remove_request_handler.cc
src/res-copy/src/request_handler_invoker.cc
src/res-copy/src/res_handler.cc
src/res-copy/src/res_path_info.cc
tests/unit_tests/res-copy/src/test_event_signal_sender.cc

index aa61ab8..26c98d8 100644 (file)
@@ -35,13 +35,14 @@ class AbstractRequestHandler {
 
   virtual ErrorType Execute() = 0;
   virtual const std::string GetRequestHandlerType() const = 0;
+  virtual std::list<ResPathInfo> GetResultPathList();
 
  protected:
   std::string GetRootPath();
   std::string GetSrcRootPath();
   std::string GetDstRootPath();
   const std::string GetPkgID() const;
-  const std::list<ResPathInfo> GetPathList() const;
+  std::list<ResPathInfo>& GetPathList();
   uid_t GetUID() const;
 
  private:
@@ -49,6 +50,7 @@ class AbstractRequestHandler {
   uid_t uid_;
   std::string root_path_;
   std::list<ResPathInfo> path_list_;
+  std::string result_path_;
 };
 
 }  // namespace res_handler
index 9709fa5..37971f1 100644 (file)
@@ -33,6 +33,7 @@ class CopyRequestHandler : public AbstractRequestHandler {
 
   ErrorType Execute() override;
   const std::string GetRequestHandlerType() const override;
+  std::list<ResPathInfo> GetResultPathList() override;
 };
 
 }  // namespace res_handler
index 495f059..f702208 100644 (file)
 #ifndef EVENT_SIGNAL_SENDER_HH_
 #define EVENT_SIGNAL_SENDER_HH_
 
+#include <list>
 #include <memory>
 #include <string>
 
 #include <pkgmgr_installer.h>
 
 #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<ResPathInfo>& res_path_info);
+  bool SendOK(const std::list<ResPathInfo>& res_path_info);
+  bool SendFail(ErrorType error, const std::list<ResPathInfo>& 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<ResPathInfo>& res_path_info);
 
   std::string pkgid_;
   ReqType req_type_;
index d85fb57..54cc0f4 100644 (file)
@@ -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
index 113f38e..b432e47 100644 (file)
@@ -47,7 +47,7 @@ const std::string AbstractRequestHandler::GetPkgID() const {
   return pkgid_;
 }
 
-const std::list<ResPathInfo> AbstractRequestHandler::GetPathList() const {
+std::list<ResPathInfo>& AbstractRequestHandler::GetPathList() {
   return path_list_;
 }
 
@@ -55,4 +55,8 @@ uid_t AbstractRequestHandler::GetUID() const {
   return uid_;
 }
 
+std::list<ResPathInfo> AbstractRequestHandler::GetResultPathList() {
+  return path_list_;
+}
+
 }  // namespace res_handler
index 4aca075..a1bc08d 100644 (file)
@@ -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<ResPathInfo> CopyRequestHandler::GetResultPathList() {
+  bf::path src_root_path(GetSrcRootPath());
+  bf::path dst_root_path(GetDstRootPath());
+  std::list<ResPathInfo> 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
index dbc053f..eaacee6 100644 (file)
@@ -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;
index c87a005..467f389 100644 (file)
 
 #include "include/event_signal_sender.hh"
 
+#include <package-manager.h>
+
 #include <iostream>
 #include <string>
 
-#include <package-manager.h>
-#include <package-manager-types.h>
-
 #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<ResPathInfo>& 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<int>(error));
+  if (pkgmgr_res_event_info_set_error_code(event_info.get(),
+      static_cast<int>(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<ResPathInfo>& 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<ResPathInfo>& 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<ResPathInfo>& res_path_info) {
+  if (!SendSignal(PKGMGR_INSTALLER_FAIL_EVENT_STR, error, res_path_info)) {
     LOG(ERROR) << "Fail to send fail signal";
     return false;
   }
index 9f752a3..ce97335 100644 (file)
@@ -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;
index 52bf4e4..c6d3b9c 100644 (file)
@@ -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;
 }
index 8790639..a0012e1 100644 (file)
 
 #include "include/res_handler.hh"
 
-#include <iostream>
-
 #include <pkgmgr_installer.h>
 
+#include <iostream>
+
 #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());
index 56e07df..4c2fb76 100644 (file)
@@ -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
index 72ca3c9..50996c2 100644 (file)
@@ -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, {}));
 }