[Signal] Send 'error' code to pkgcmd 66/37766/2
authorTomasz Iwanek <t.iwanek@samsung.com>
Thu, 19 Mar 2015 10:41:56 +0000 (11:41 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Wed, 8 Apr 2015 10:20:44 +0000 (03:20 -0700)
All logic calling SignalPkgmgr class was moved to
AppInstaller class.

Change-Id: Ic99423abab4eaf5f239a27104b653e1dd12a8ad2
Signed-off-by: Pawel Sikorski <p.sikorski@samsung.com>
src/common/CMakeLists.txt
src/common/app_installer.cc
src/common/app_installer.h
src/common/context_installer.h
src/common/pkgmgr_signal.cc
src/common/pkgmgr_signal.h
src/common/step/step.h
src/common/step/step_signal.cc [deleted file]
src/common/step/step_signal.h [deleted file]
src/tpk/task.cc
src/wgt/wgt_backend.cc

index 566ce6c..cadd25c 100644 (file)
@@ -13,7 +13,6 @@ SET(SRCS
   step/step_remove_files.cc
   step/step_revoke_security.cc
   step/step_register_security.cc
-  step/step_signal.cc
   step/step_unregister_app.cc
 )
 # Target - definition
index c902afd..877c8ca 100644 (file)
@@ -14,7 +14,7 @@ namespace common_installer {
 AppInstaller::AppInstaller(pkgmgr_installer *pi, const char* package_type)
   : context_(new ContextInstaller()) {
   int request_type = pkgmgr_installer_get_request_type(pi);
-  context_->set_pi(std::unique_ptr<PkgmgrSignal>(new PkgmgrSignal(pi)));
+  pi_.reset(new PkgmgrSignal(pi));
   context_->request_type.set(request_type);
   context_->pkg_type.set(package_type);
   switch (request_type) {
@@ -32,23 +32,24 @@ AppInstaller::AppInstaller(pkgmgr_installer *pi, const char* package_type)
 AppInstaller::~AppInstaller() {
 }
 
-void AppInstaller::EnsureSignalSend() {
-  if (!context_->pi()->IsFinished()) {
-    // if signal was not sent during normal step execution
-    // then this will sent any signal about failure
-    (void) context_->pi()->sendStarted();
-    (void) context_->pi()->sendFinished(PkgmgrSignal::Result::FAILED);
-  }
-}
-
 int AppInstaller::Run() {
   std::list<std::unique_ptr<Step>>::iterator it(steps_.begin());
   std::list<std::unique_ptr<Step>>::iterator itStart(steps_.begin());
   std::list<std::unique_ptr<Step>>::iterator itEnd(steps_.end());
 
+  Step::Status process_status = Step::Status::OK;
   int ret = 0;
   for (; it != itEnd; ++it) {
-    if ((*it)->process() != Step::Status::OK) {
+    process_status = (*it)->process();
+
+    // send START signal as soon as possible
+    if (pi_->state() == PkgmgrSignal::State::NOT_SENT) {
+      if (!context_->pkgid.get().empty()) {
+        pi_->SendStarted(context_->pkg_type.get(), context_->pkgid.get());
+      }
+    }
+
+    if (process_status != Step::Status::OK) {
       LOG(ERROR) << "Error during processing";
       ret = -1;
       break;
@@ -72,8 +73,13 @@ int AppInstaller::Run() {
     }
   }
 
-  EnsureSignalSend();
-
+  // send START if pkgid was not parsed
+  if (pi_->state() == PkgmgrSignal::State::NOT_SENT) {
+    pi_->SendStarted(context_->pkg_type.get(), context_->pkgid.get());
+  }
+  pi_->SendFinished(process_status,
+                    context_->pkg_type.get(),
+                    context_->pkgid.get());
   return ret;
 }
 
index 80ec558..7ff1e6d 100644 (file)
@@ -7,6 +7,7 @@
 #include <list>
 #include <memory>
 
+#include "common/pkgmgr_signal.h"
 #include "common/step/step.h"
 #include "utils/logging.h"
 #include "utils/macros.h"
@@ -30,11 +31,12 @@ class AppInstaller {
   int Run();
 
  private:
-  void EnsureSignalSend();
-
   std::list<std::unique_ptr<Step>> steps_;
   std::unique_ptr<ContextInstaller> context_;
 
+  // data used to send signal
+  std::unique_ptr<PkgmgrSignal> pi_;
+
   SCOPE_LOG_TAG(AppInstaller)
 
   DISALLOW_COPY_AND_ASSIGN(AppInstaller);
index eeab6fd..e55913e 100644 (file)
@@ -11,8 +11,6 @@
 #include <memory>
 #include <string>
 
-#include "common/pkgmgr_signal.h"
-
 namespace common_installer {
 
 /** Template class for defining smart attributes.
@@ -78,25 +76,14 @@ class ContextInstaller {
   // uid of the user that request the operation
   Property<uid_t> uid;
 
-
   // data from config.xml
   Property<ConfigData> config_data;
 
-  // TODO(p.sikorski@samsung.com) change "pi" to Property
-  PkgmgrSignal* pi() const { return pi_.get(); }
-  void set_pi(std::unique_ptr<PkgmgrSignal> pi) {
-    pi_ = std::move(pi);
-  }
-
   // path for the applications directory
   Property<std::string> application_path;
 
   // path for the applications root directory
   Property<std::string> root_application_path;
-
- private :
-  // data used to send signal
-  std::unique_ptr<PkgmgrSignal> pi_;
 };
 
 }  // namespace common_installer
index 70d332e..7317ec0 100644 (file)
@@ -8,6 +8,11 @@
 
 #include "utils/logging.h"
 
+// Redefine this value as it is not exported by pkgmgr
+// this should probably be in public interface because of
+// otherwise there is no way to return errorcode
+#define PKGMGR_INSTALLER_ERROR_KEY_STR "error"
+
 namespace common_installer {
 
 PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi)
@@ -15,7 +20,7 @@ PkgmgrSignal::PkgmgrSignal(pkgmgr_installer* pi)
       state_(State::NOT_SENT) {
 }
 
-bool PkgmgrSignal::sendStarted(
+bool PkgmgrSignal::SendStarted(
     const std::string& type, const std::string& pkgid) {
   if (state_ != State::NOT_SENT) {
     return false;
@@ -32,13 +37,20 @@ bool PkgmgrSignal::sendStarted(
   return true;
 }
 
-bool PkgmgrSignal::sendFinished(
-    Result result, const std::string& type, const std::string& pkgid) {
+bool PkgmgrSignal::SendFinished(
+    Step::Status result, const std::string& type, const std::string& pkgid) {
   if (state_ != State::STARTED) {
     return false;
   }
+  if (result != Step::Status::OK) {
+    if (!SendSignal(
+        PKGMGR_INSTALLER_ERROR_KEY_STR,
+        std::to_string(static_cast<int>(result)).c_str(), type, pkgid)) {
+      return false;
+    }
+  }
   if (!SendSignal(
-        PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
+      PKGMGR_INSTALLER_END_KEY_STR, GetResultKey(result), type, pkgid)) {
     return false;
   }
   state_ = State::FINISHED;
@@ -72,14 +84,12 @@ bool PkgmgrSignal::SendSignal(
   return true;
 }
 
-const char* PkgmgrSignal::GetResultKey(Result result) const {
+const char* PkgmgrSignal::GetResultKey(Step::Status result) const {
   switch (result) {
-    case Result::SUCCESS:
+    case Step::Status::OK:
       return PKGMGR_INSTALLER_OK_EVENT_STR;
-    case Result::FAILED:
-      return PKGMGR_INSTALLER_FAIL_EVENT_STR;
     default:
-      assert(false && "Not Reached");
+      return PKGMGR_INSTALLER_FAIL_EVENT_STR;
   }
 }
 
index f32db2d..165e407 100644 (file)
@@ -10,6 +10,8 @@
 #include <memory>
 #include <string>
 
+#include "common/step/step.h"
+
 namespace common_installer {
 
 //
@@ -21,35 +23,32 @@ namespace common_installer {
 //
 class PkgmgrSignal {
  public:
-  enum class Result {
-    SUCCESS,
-    FAILED
+  enum class State {
+    NOT_SENT,
+    STARTED,
+    FINISHED
   };
 
   explicit PkgmgrSignal(pkgmgr_installer* pi);
 
-  bool sendStarted(
+  bool SendStarted(
       const std::string& type = std::string(),
       const std::string& pkgid = std::string());
-  bool sendFinished(
-      Result result,
+  bool SendFinished(
+      Step::Status result,
       const std::string& type = std::string(),
       const std::string& pkgid = std::string());
   bool IsFinished() const;
 
- private:
-  enum class State {
-    NOT_SENT,
-    STARTED,
-    FINISHED
-  };
+  State state() const { return state_; }
 
+ private:
   bool SendSignal(
       const char* key,
       const char* value,
       const std::string& type = std::string(),
       const std::string& pkgid = std::string()) const;
-  const char* GetResultKey(Result result) const;
+  const char* GetResultKey(Step::Status result) const;
 
   pkgmgr_installer* pi_;
   State state_;
index 73abb31..cff8ce4 100644 (file)
@@ -16,6 +16,9 @@
   The returned code of Step::Status::OK indicates a succeful execution.
   Otherwise, the returned code should be set to value different than
   Step::Status::OK.
+
+  Errornous result of processing is casted to interger value and sent to
+  client which sent request.
 */
 #ifndef COMMON_STEP_STEP_H_
 #define COMMON_STEP_STEP_H_
@@ -36,9 +39,9 @@ class Step {
   explicit Step(ContextInstaller* context) : context_(context) { }
   virtual ~Step() { }
 
-  virtual Step::Status process() = 0;
-  virtual Step::Status undo() = 0;
-  virtual Step::Status clean() = 0;
+  virtual Status process() = 0;
+  virtual Status undo() = 0;
+  virtual Status clean() = 0;
 
  protected:
   ContextInstaller* context_;
diff --git a/src/common/step/step_signal.cc b/src/common/step/step_signal.cc
deleted file mode 100644 (file)
index a5e8575..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/* 2014, Copyright © Eurogiciel Coporation, APACHE-2.0, see LICENSE file */
-
-#include "common/step/step_signal.h"
-
-#include <pkgmgr-info.h>
-#include <unistd.h>
-
-#include "utils/file_util.h"
-
-namespace common_installer {
-namespace signal {
-
-Step::Status StepSignal::process() {
-  if (!context_->pi()->sendStarted(
-      context_->manifest_data.get()->type, context_->pkgid.get())) {
-    return Status::ERROR;
-  }
-  LOG(DEBUG) << "Send Start";
-  return Status::OK;
-}
-
-Step::Status StepSignal::clean() {
-  if (!context_->pi()->sendFinished(
-        PkgmgrSignal::Result::SUCCESS,
-        context_->manifest_data.get()->type, context_->pkgid.get())) {
-    return Status::ERROR;
-  }
-  LOG(DEBUG) << "Send Success";
-  return Status::OK;
-}
-
-Step::Status StepSignal::undo() {
-  if (!context_->pi()->sendFinished(
-        PkgmgrSignal::Result::FAILED,
-        context_->manifest_data.get()->type, context_->pkgid.get())) {
-    return Status::ERROR;
-  }
-  LOG(ERROR) << "Send Error";
-  return Status::OK;
-}
-
-}  // namespace signal
-}  // namespace common_installer
diff --git a/src/common/step/step_signal.h b/src/common/step/step_signal.h
deleted file mode 100644 (file)
index 5adf1de..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/* 2014, Copyright © Eurogiciel Coporation, APACHE-2.0, see LICENSE file */
-
-#ifndef COMMON_STEP_STEP_SIGNAL_H_
-#define COMMON_STEP_STEP_SIGNAL_H_
-
-#include <string>
-
-#include "common/context_installer.h"
-
-#include "common/step/step.h"
-#include "utils/logging.h"
-
-namespace common_installer {
-namespace signal {
-
-class StepSignal : public Step {
- public:
-  using Step::Step;
-
-  Status process() override;
-  Status clean() override;
-  Status undo() override;
- private:
-  SCOPE_LOG_TAG(Signal)
-};
-
-}  // namespace signal
-}  // namespace common_installer
-
-#endif  // COMMON_STEP_STEP_SIGNAL_H_
index dec4e99..b616fde 100644 (file)
@@ -12,7 +12,6 @@
 #include "common/step/step_remove_files.h"
 #include "common/step/step_revoke_security.h"
 #include "common/step/step_register_security.h"
-#include "common/step/step_signal.h"
 #include "common/step/step_check_signature.h"
 #include "common/step/step_unregister_app.h"
 #include "common/step/step_unzip.h"
@@ -94,7 +93,6 @@ int Task::Install() {
   ai.AddStep<ci::unzip::StepUnzip>();
   ai.AddStep<ci::signature::StepCheckSignature>();
   ai.AddStep<tpk::step::StepParse>();
-  ai.AddStep<ci::signal::StepSignal>();
   ai.AddStep<ci::copy::StepCopy>();
   ai.AddStep<tpk::step::StepCreateSymbolicLink>();
   ai.AddStep<ci::security::StepRegisterSecurity>();
@@ -108,7 +106,6 @@ int Task::Uninstall() {
   ci::AppInstaller ai(pi_, kPkgType);
 
   ai.AddStep<ci::parse::StepParse>();
-  ai.AddStep<ci::signal::StepSignal>();
   ai.AddStep<ci::unregister_app::StepUnregisterApplication>();
   ai.AddStep<ci::remove::StepRemoveFiles>();
   ai.AddStep<ci::revoke_security::StepRevokeSecurity>();
index 804cbb0..bd4e2b1 100644 (file)
@@ -20,7 +20,6 @@
 #include "common/step/step_remove_files.h"
 #include "common/step/step_revoke_security.h"
 #include "common/step/step_register_security.h"
-#include "common/step/step_signal.h"
 #include "common/step/step_check_signature.h"
 #include "common/step/step_unregister_app.h"
 #include "common/step/step_unzip.h"
@@ -48,7 +47,6 @@ int main(int argc, char** argv) {
       installer.AddStep<ci::unzip::StepUnzip>();
       installer.AddStep<ci::signature::StepCheckSignature>();
       installer.AddStep<wgt::parse::StepParse>();
-      installer.AddStep<ci::signal::StepSignal>();
       installer.AddStep<ci::copy::StepCopy>();
       installer.AddStep<wgt::symbolic_link::StepCreateSymbolicLink>();
       installer.AddStep<ci::security::StepRegisterSecurity>();
@@ -58,7 +56,6 @@ int main(int argc, char** argv) {
     }
     case PKGMGR_REQ_UNINSTALL: {
       installer.AddStep<ci::parse::StepParse>();
-      installer.AddStep<ci::signal::StepSignal>();
       installer.AddStep<ci::unregister_app::StepUnregisterApplication>();
       installer.AddStep<ci::remove::StepRemoveFiles>();
       installer.AddStep<ci::revoke_security::StepRevokeSecurity>();