Reduce the number of signal to send
[platform/core/appfw/app-installers.git] / src / common / installer / app_installer.cc
index cf6e88f..56ef9e4 100644 (file)
@@ -5,10 +5,14 @@
 
 #include <unistd.h>
 
+#include <algorithm>
+#include <cmath>
 #include <cstdio>
 #include <fstream>
 #include <TTraceWrapper.h>
 
+#include <pkgmgr_parser_db.h>
+
 #include "common/installer/app_installer.h"
 #include "common/installer_context.h"
 #include "common/pkgmgr_interface.h"
@@ -33,6 +37,7 @@
 #include "common/step/filesystem/step_copy_tep.h"
 #include "common/step/filesystem/step_create_globalapp_symlinks.h"
 #include "common/step/filesystem/step_create_icons.h"
+#include "common/step/filesystem/step_create_res_control_directories.h"
 #include "common/step/filesystem/step_create_storage_directories.h"
 #include "common/step/filesystem/step_delta_patch.h"
 #include "common/step/filesystem/step_disable_external_mount.h"
@@ -40,7 +45,6 @@
 #include "common/step/filesystem/step_migrate_legacy_external_image.h"
 #include "common/step/filesystem/step_move_installed_storage.h"
 #include "common/step/filesystem/step_optional_acquire_external_storage.h"
-#include "common/step/filesystem/step_recover_change_owner.h"
 #include "common/step/filesystem/step_recover_external_storage.h"
 #include "common/step/filesystem/step_recover_files.h"
 #include "common/step/filesystem/step_recover_globalapp_symlinks.h"
@@ -73,7 +77,9 @@
 #include "common/step/pkgmgr/step_recover_application.h"
 #include "common/step/pkgmgr/step_recover_parser_plugins.h"
 #include "common/step/pkgmgr/step_register_app.h"
+#include "common/step/pkgmgr/step_recover_priv_sharedres.h"
 #include "common/step/pkgmgr/step_remove_manifest.h"
+#include "common/step/pkgmgr/step_remove_priv_sharedres.h"
 #include "common/step/pkgmgr/step_run_parser_plugins.h"
 #include "common/step/pkgmgr/step_unregister_app.h"
 #include "common/step/pkgmgr/step_update_app.h"
@@ -105,6 +111,7 @@ const char kLogFileName[] = LOGDIR"/app-installers.log";
 const char kHistoryFileName[] = LOGDIR"/installation-history.log";
 const int kLogRotationSize = 1024 * 256;  // 256KB
 const int kLogMaximumRotation = 3;
+const int kSignalCount = 10;
 
 }
 
@@ -119,7 +126,7 @@ AppInstaller::AppInstaller(const char* package_type, PkgMgrPtr pkgmgr)
   // pkgmgr signal should work only for online mode
   // there is no one to receive it in offline mode
   if (context_->installation_mode.get() == InstallationMode::ONLINE) {
-    pi_ = pkgmgr->CreatePkgmgrSignal();
+    ps_ = pkgmgr->CreatePkgmgrSignal();
   }
 }
 
@@ -219,30 +226,6 @@ AppInstaller::Result AppInstaller::Run() {
   return result_;
 }
 
-void AppInstaller::LogHistoryStart() {
-  std::shared_ptr<utils::FileLogBackend> history_logger =
-      std::shared_ptr<utils::FileLogBackend>(new utils::FileLogBackend(
-          kHistoryFileName, kLogRotationSize, kLogMaximumRotation));
-  std::string history = context_->pkgid.get() + "|" +
-      GetRequestTypeString(context_->request_type.get()) + "|START";
-  history_logger->WriteLog(::utils::LogLevel::LOG_INFO, "", history);
-  history_logger->WriteLogToFile();
-}
-
-void AppInstaller::LogHistoryEnd(bool success) {
-  std::shared_ptr<utils::FileLogBackend> history_logger =
-      std::shared_ptr<utils::FileLogBackend>(new utils::FileLogBackend(
-          kHistoryFileName, kLogRotationSize, kLogMaximumRotation));
-  std::string history = context_->pkgid.get() + "|" +
-      GetRequestTypeString(context_->request_type.get());
-  if (success)
-    history += "|SUCCESS";
-  else
-    history += "|FAIL";
-  history_logger->WriteLog(::utils::LogLevel::LOG_INFO, "", history);
-  history_logger->WriteLogToFile();
-}
-
 AppInstaller::Result AppInstaller::Process() {
   Init();
 
@@ -253,6 +236,8 @@ AppInstaller::Result AppInstaller::Process() {
 
   unsigned total_steps = steps_.size();
   unsigned current_step = 1;
+  double div = total_steps / static_cast<double>(kSignalCount);
+  int interval = std::max(1, static_cast<int>(std::round(div)));
 
   for (it_ = steps_.begin(); it_ != steps_.end(); ++it_, ++current_step) {
     status_ = SafeExecute(*it_, &Step::precheck, "precheck");
@@ -261,12 +246,17 @@ AppInstaller::Result AppInstaller::Process() {
 
     if (status_ != Step::Status::OK) {
       if (status_ != Step::Status::RECOVERY_DONE) {
-        LOG(ERROR) << "Error during processing";
+        LOG(ERROR) << "Error during processing(" << (*it_)->name() << ")";
         result_ = Result::ERROR;
       }
       break;
     }
-    SendProgress(current_step * kProgressRange / total_steps);
+    if (!context_->pkgid.get().empty())
+      history_logger_.LogHistoryStart(context_->pkgid.get(),
+          GetPackageVersion(), context_->request_type.get());
+
+    if (current_step == total_steps || current_step % interval == 0)
+      SendProgress(current_step * kProgressRange / total_steps);
   }
 
   return result_;
@@ -293,7 +283,8 @@ AppInstaller::Result AppInstaller::Undo() {
     status_ = Step::Status::ERROR;
   SendFinished(status_);
 
-  LogHistoryEnd(false);
+  history_logger_.LogHistoryEnd(context_->pkgid.get(),
+      GetPackageVersion(), context_->request_type.get(), false);
 
   return result_;
 }
@@ -305,7 +296,6 @@ AppInstaller::Result AppInstaller::Clean() {
     recovery_file->set_cleanup(true);
     recovery_file->WriteAndCommitFileContent();
   }
-  SendFinished(status_);
 
   do {
     if (it_ == steps_.end())
@@ -317,8 +307,10 @@ AppInstaller::Result AppInstaller::Clean() {
     }
   } while (it_-- != steps_.begin());
   sync();
+  SendFinished(status_);
 
-  LogHistoryEnd(true);
+  history_logger_.LogHistoryEnd(context_->pkgid.get(),
+      GetPackageVersion(), context_->request_type.get(), true);
 
   return result_;
 }
@@ -342,12 +334,13 @@ void AppInstaller::InstallSteps() {
   AddStep<ci::filesystem::StepCopy>();
   AddStep<ci::filesystem::StepCopyTep>();
   AddStep<ci::filesystem::StepCreateIcons>();
-  AddStep<ci::pkgmgr::StepRegisterApplication>();
+  AddStep<ci::filesystem::StepCreateResControlDirectories>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::INSTALL);
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Install);
   AddStep<ci::security::StepRegisterSecurity>();
+  AddStep<ci::pkgmgr::StepRegisterApplication>();
   AddStep<ci::pkgmgr::StepRunParserPlugin>(ci::Plugin::ActionType::Install);
   AddStep<ci::filesystem::StepCreateStorageDirectories>();
   AddStep<ci::filesystem::StepChangeOwnershipAndPermission>();
@@ -381,12 +374,13 @@ void AppInstaller::UpdateSteps() {
   AddStep<ci::filesystem::StepUpdateTep>();
   AddStep<ci::filesystem::StepCopyStorageDirectories>();
   AddStep<ci::filesystem::StepCreateIcons>();
-  AddStep<ci::pkgmgr::StepUpdateApplication>();
+  AddStep<ci::filesystem::StepCreateResControlDirectories>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::UPDATE);
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Update);
   AddStep<ci::security::StepUpdateSecurity>();
+  AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::pkgmgr::StepRunParserPlugin>(ci::Plugin::ActionType::Upgrade);
   AddStep<ci::filesystem::StepUpdateStorageDirectories>();
   AddStep<ci::filesystem::StepChangeOwnershipAndPermission>();
@@ -413,12 +407,12 @@ void AppInstaller::UninstallSteps() {
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Uninstall);
   AddStep<ci::filesystem::StepRemoveFiles>();
+  AddStep<ci::pkgmgr::StepRemovePrivSharedres>();
   AddStep<ci::filesystem::StepRemoveZipImage>();
   AddStep<ci::filesystem::StepRemoveIcons>();
   AddStep<ci::filesystem::StepRemoveTep>();
   AddStep<ci::security::StepRevokeSecurity>();
   AddStep<ci::pkgmgr::StepRemoveManifest>();
-  AddStep<ci::filesystem::StepCreateGlobalAppSymlinks>();
 }
 
 void AppInstaller::ReinstallSteps() {
@@ -445,12 +439,13 @@ void AppInstaller::ReinstallSteps() {
   AddStep<ci::rds::StepRDSModify>();
   AddStep<ci::filesystem::StepUpdateTep>();
   AddStep<ci::filesystem::StepCreateIcons>();
-  AddStep<ci::pkgmgr::StepUpdateApplication>();
+  AddStep<ci::filesystem::StepCreateResControlDirectories>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::UPDATE);
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Update);
   AddStep<ci::security::StepUpdateSecurity>();
+  AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::pkgmgr::StepRunParserPlugin>(ci::Plugin::ActionType::Upgrade);
   AddStep<ci::filesystem::StepUpdateStorageDirectories>();
   AddStep<ci::filesystem::StepChangeOwnershipAndPermission>();
@@ -487,12 +482,13 @@ void AppInstaller::DeltaSteps() {
   AddStep<ci::filesystem::StepUpdateTep>();
   AddStep<ci::filesystem::StepCopyStorageDirectories>();
   AddStep<ci::filesystem::StepCreateIcons>();
-  AddStep<ci::pkgmgr::StepUpdateApplication>();
+  AddStep<ci::filesystem::StepCreateResControlDirectories>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::UPDATE);
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Update);
   AddStep<ci::security::StepUpdateSecurity>();
+  AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::pkgmgr::StepRunParserPlugin>(ci::Plugin::ActionType::Upgrade);
   AddStep<ci::filesystem::StepUpdateStorageDirectories>();
   AddStep<ci::filesystem::StepChangeOwnershipAndPermission>();
@@ -522,14 +518,14 @@ void AppInstaller::RecoverySteps() {
   AddStep<ci::pkgmgr::StepRecoverParserPlugin>();
   AddStep<ci::filesystem::StepRecoverManifest>();
   AddStep<ci::filesystem::StepRecoverExternalStorage>();
-  AddStep<ci::filesystem::StepRecoverStorageDirectories>();
   AddStep<ci::filesystem::StepRecoverGlobalAppSymlinks>();
   AddStep<ci::filesystem::StepRecoverFiles>();
+  AddStep<ci::pkgmgr::StepRecoverPrivSharedres>();
   AddStep<ci::mount::StepMountRecover>();
-  AddStep<ci::filesystem::StepRecoverChangeOwner>();
   AddStep<ci::pkgmgr::StepRecoverApplication>();
   AddStep<ci::security::StepRecoverTrustAnchor>();
   AddStep<ci::security::StepRecoverSecurity>();
+  AddStep<ci::filesystem::StepRecoverStorageDirectories>();
 }
 
 void AppInstaller::MountInstallSteps() {
@@ -550,12 +546,12 @@ void AppInstaller::MountInstallSteps() {
   AddStep<ci::mount::StepMountInstall>();
   AddStep<ci::filesystem::StepCopyTep>();
   AddStep<ci::filesystem::StepCreateIcons>();
-  AddStep<ci::pkgmgr::StepRegisterApplication>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::INSTALL);
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Install);
   AddStep<ci::security::StepRegisterSecurity>();
+  AddStep<ci::pkgmgr::StepRegisterApplication>();
   AddStep<ci::pkgmgr::StepRunParserPlugin>(ci::Plugin::ActionType::Install);
   AddStep<ci::mount::StepUnmount>();
   AddStep<ci::filesystem::StepCreateStorageDirectories>();
@@ -588,12 +584,12 @@ void AppInstaller::MountUpdateSteps() {
   AddStep<ci::mount::StepMountUpdate>();
   AddStep<ci::filesystem::StepUpdateTep>();
   AddStep<ci::filesystem::StepCreateIcons>();
-  AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::UPDATE);
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Update);
   AddStep<ci::security::StepUpdateSecurity>();
+  AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::pkgmgr::StepRunParserPlugin>(ci::Plugin::ActionType::Upgrade);
   AddStep<ci::mount::StepUnmount>();
   AddStep<ci::filesystem::StepUpdateStorageDirectories>();
@@ -613,6 +609,7 @@ void AppInstaller::ManifestDirectInstallSteps() {
   AddStep<ci::security::StepSignature>(true);
   AddStep<ci::security::StepRollbackInstallationSecurity>();
   AddStep<ci::filesystem::StepRemoveGlobalAppSymlinks>();
+  AddStep<ci::filesystem::StepCreateResControlDirectories>();
   AddStep<ci::pkgmgr::StepRegisterApplication>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::UPDATE);
@@ -638,6 +635,7 @@ void AppInstaller::ManifestDirectUpdateSteps() {
   AddStep<ci::security::StepCheckOldCertificate>();
   AddStep<ci::pkgmgr::StepKillApps>();
   AddStep<ci::filesystem::StepRemoveGlobalAppSymlinks>();
+  AddStep<ci::filesystem::StepCreateResControlDirectories>();
   AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::UPDATE);
@@ -701,6 +699,7 @@ void AppInstaller::PartialUninstallSteps() {
   AddStep<ci::filesystem::StepRemoveGlobalAppSymlinks>();
   AddStep<ci::filesystem::StepOptionalAcquireExternalStorage>();
   AddStep<ci::filesystem::StepRemovePerUserStorageDirectories>();
+  AddStep<ci::pkgmgr::StepRemovePrivSharedres>();
   AddStep<ci::pkgmgr::StepUnregisterApplication>();
   AddStep<ci::security::StepUnregisterTrustAnchor>();
   AddStep<ci::security::StepPrivacyPrivilege>(
@@ -734,12 +733,13 @@ void AppInstaller::ReadonlyUpdateInstallSteps() {
   AddStep<ci::filesystem::StepCopy>();
   AddStep<ci::filesystem::StepCopyTep>();
   AddStep<ci::filesystem::StepCreateIcons>();
-  AddStep<ci::pkgmgr::StepUpdateApplication>();
+  AddStep<ci::filesystem::StepCreateResControlDirectories>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::UPDATE);
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Update);
   AddStep<ci::security::StepUpdateSecurity>();
+  AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::pkgmgr::StepRunParserPlugin>(ci::Plugin::ActionType::Upgrade);
   AddStep<ci::filesystem::StepUpdateStorageDirectories>();
   AddStep<ci::filesystem::StepChangeOwnershipAndPermission>();
@@ -748,6 +748,7 @@ void AppInstaller::ReadonlyUpdateInstallSteps() {
 
 void AppInstaller::ReadonlyUpdateUninstallSteps() {
   AddStep<ci::configuration::StepConfigure>(pkgmgr_);
+  AddStep<ci::recovery::StepCreateRecoveryFile>();
   AddStep<ci::configuration::StepParseManifest>(
       ci::configuration::StepParseManifest::ManifestLocation::INSTALLED,
       ci::configuration::StepParseManifest::StoreLocation::BACKUP);
@@ -762,18 +763,19 @@ void AppInstaller::ReadonlyUpdateUninstallSteps() {
   AddStep<ci::pkgmgr::StepKillApps>();
   AddStep<ci::filesystem::StepRemoveGlobalAppSymlinks>();
   AddStep<ci::security::StepRevokeTrustAnchor>();
-  AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::security::StepRegisterTrustAnchor>(
       ci::security::StepRegisterTrustAnchor::RegisterType::UPDATE);
   AddStep<ci::security::StepPrivacyPrivilege>(
       ci::security::StepPrivacyPrivilege::ActionType::Update);
   AddStep<ci::filesystem::StepRemoveFiles>();
+  AddStep<ci::pkgmgr::StepRemovePrivSharedres>();
   AddStep<ci::filesystem::StepRemoveZipImage>();
   AddStep<ci::filesystem::StepRemoveIcons>();
   AddStep<ci::filesystem::StepRemoveTep>();
   AddStep<ci::filesystem::StepRemoveUserData>();
   AddStep<ci::configuration::StepSwitchReadonlyMode>();
   AddStep<ci::security::StepUpdateSecurity>();
+  AddStep<ci::pkgmgr::StepUpdateApplication>();
   AddStep<ci::pkgmgr::StepRemoveManifest>();
   AddStep<ci::pkgmgr::StepRunParserPlugin>(ci::Plugin::ActionType::Upgrade);
   AddStep<ci::filesystem::StepUpdateStorageDirectories>();
@@ -785,6 +787,7 @@ void AppInstaller::DisablePkgSteps() {
       ci::configuration::StepParseManifest::ManifestLocation::INSTALLED,
       ci::configuration::StepParseManifest::StoreLocation::NORMAL);
   AddStep<ci::pkgmgr::StepKillApps>();
+  AddStep<ci::pkgmgr::StepRemovePrivSharedres>();
   AddStep<ci::pkgmgr::StepUpdatePkgDisableInfo>(
     ci::pkgmgr::StepUpdatePkgDisableInfo::ActionType::Disable);
   AddStep<ci::pkgmgr::StepRunParserPlugin>(
@@ -826,26 +829,38 @@ void AppInstaller::UnknownSteps() {
 
 void AppInstaller::HandleStepError(Step::Status result,
                                    const std::string& error) {
-  if (!pi_)
+  if (!ps_)
     return;
 
-  pi_->SendError(result, error, context_->pkg_type.get(),
+  ps_->SendError(result, error, context_->pkg_type.get(),
                  context_->pkgid.get());
 }
 
+std::string AppInstaller::GetPackageVersion() {
+  std::string version;
+  if (context_->manifest_data.get()) {
+    version = context_->manifest_data.get()->version ?
+        context_->manifest_data.get()->version : "";
+  } else {
+    PkgQueryInterface pkg_query(context_->pkgid.get(), context_->uid.get());
+    version = pkg_query.Version();
+  }
+  return version;
+}
+
 bool AppInstaller::SendStartIfNotSent(bool is_skippable) {
-  if (!pi_)
+  if (!ps_)
     return false;
 
-  if ((pi_->state() != PkgmgrSignal::State::NOT_SENT ||
-      context_->pkgid.get().empty()) && is_skippable)
+  if (ps_->state() != PkgmgrSignal::State::NOT_SENT)
     return true;
 
-  // set request type before sending start signal
-  pi_->SetRequestType(context_->request_type.get());
-  pi_->SendStarted(context_->pkg_type.get(), context_->pkgid.get());
+  if (context_->pkgid.get().empty() && is_skippable)
+    return true;
 
-  LogHistoryStart();
+  // set request type before sending start signal
+  ps_->SetRequestType(context_->request_type.get());
+  ps_->SendStarted(context_->pkg_type.get(), context_->pkgid.get());
 
   return true;
 }
@@ -856,15 +871,17 @@ void AppInstaller::SendProgress(int progress) {
     return;
 
   // send installation progress
-  pi_->SendProgress(progress,
+  ps_->SendProgress(progress,
       context_->pkg_type.get(), context_->pkgid.get());
 }
 
 void AppInstaller::SendFinished(Step::Status process_status) {
+  pkgmgr_parser_clear_cache_memory_db();
+
   if (!SendStartIfNotSent(false))
     return;
 
-  pi_->SendFinished(process_status,
+  ps_->SendFinished(process_status,
                     context_->pkg_type.get(),
                     context_->pkgid.get());
 }
@@ -875,6 +892,7 @@ Step::Status AppInstaller::SafeExecute(std::unique_ptr<Step> const& step_ptr,
   Step::Status process_status = Step::Status::OK;
   try {
     TTRACE(TTRACE_TAG_APP, "%s_%s", step_ptr->name(), name.c_str());
+    LOG(DEBUG) << step_ptr->name() << "_" << name.c_str();
     process_status = ((*step_ptr).*method)();
   } catch (const std::exception& err) {
     LOG(ERROR) << "Exception occurred in " << name.c_str() << "(): "