External file removal fix
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_database.cpp
index 759112b..450de41 100644 (file)
 #include <widget_install/job_widget_install.h>
 #include <widget_install/widget_install_errors.h>
 #include <widget_install/widget_install_context.h>
-//#include <dpl/wrt-dao-ro/config_parser_data.h>
 #include <dpl/wrt-dao-rw/widget_dao.h>
 #include <dpl/foreach.h>
+#include <dpl/utils/wrt_utility.h>
 #include <dpl/log/log.h>
 #include <dpl/assert.h>
-//#include <dpl/wrt-dao-ro/global_config.h>
 #include <string>
 #include <sstream>
+#include <ace_api_install.h>
+#include <ace_registration.h>
 
 using namespace WrtDB;
 
@@ -41,38 +42,53 @@ namespace Jobs {
 namespace WidgetInstall {
 TaskDatabase::TaskDatabase(InstallerContext& context) :
     DPL::TaskDecl<TaskDatabase>(this),
-    m_context(context)
+    m_context(context),
+    m_handleToRemove(INVALID_WIDGET_HANDLE),
+    m_handle(INVALID_WIDGET_HANDLE)
 {
-    AddStep(&TaskDatabase::StepDBInsert);
+    AddStep(&TaskDatabase::StepRegisterExternalFiles);
+    AddStep(&TaskDatabase::StepWrtDBInsert);
+    AddStep(&TaskDatabase::StepAceDBInsert);
+    AddStep(&TaskDatabase::StepRemoveExternalFiles);
 
     AddAbortStep(&TaskDatabase::StepAbortDBInsert);
 }
 
-void TaskDatabase::StepDBInsert()
+void TaskDatabase::StepWrtDBInsert()
 {
     Try
     {
         /* Set install Time */
         time(&m_context.widgetConfig.installedTime);
 
-        LogInfo("Registering widget...");
-
-        WidgetDAO::registerWidget(
-                *(m_context.widgetHandle),
-                m_context.widgetConfig,
-                m_context.wacSecurity);
+        if (m_context.existingWidgetInfo.isExist) //update
+        {
+            m_handleToRemove = WidgetDAOReadOnly::getHandle(
+                m_context.locations->getPkgname());
+            LogInfo("Registering widget... (update)");
+            WidgetDAO::registerOrUpdateWidget(
+                    m_context.locations->getPkgname(),
+                    m_context.widgetConfig,
+                    m_context.wacSecurity);
+            m_handle = WidgetDAOReadOnly::getHandle(
+                m_context.locations->getPkgname());
+        }
+        else //new installation
+        {
+            LogInfo("Registering widget...");
+            WidgetDAO::registerWidget(
+                    m_context.locations->getPkgname(),
+                    m_context.widgetConfig,
+                    m_context.wacSecurity);
+            m_handle = WidgetDAOReadOnly::getHandle(
+                m_context.locations->getPkgname());
+        }
 
         FOREACH (cap, m_context.staticPermittedDevCaps) {
             LogInfo("staticPermittedDevCaps : " << cap->first
                     << " smack status: " << cap->second);
         }
 
-        Assert(!!m_context.widgetConfig.pkgname
-               && "pkgName should be initialized");
-
-        WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
-        widgetDao.setPkgName(m_context.widgetConfig.pkgname);
-
         LogInfo("Widget registered");
     }
     Catch(WidgetDAO::Exception::DatabaseError)
@@ -85,26 +101,105 @@ void TaskDatabase::StepDBInsert()
         LogError("Database failure!");
         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
     }
+}
+
+void TaskDatabase::StepAceDBInsert()
+{
+    LogDebug("Inserting Ace database entry. New handle: " << m_handle);
+    if (INVALID_WIDGET_HANDLE != m_handleToRemove) {
+        LogDebug("Removing old insallation. Handle: " << m_handleToRemove);
+        if (ACE_OK != ace_unregister_widget(
+                static_cast<ace_widget_handle_t>(m_handleToRemove)))
+        {
+            LogWarning("Error while removing ace entry for previous insallation");
+        };
+    }
+
+    if(!AceApi::registerAceWidget(m_handle, m_context.widgetConfig,
+                                  m_context.wacSecurity.getCertificateList()))
+    {
+        LogError("ace database insert failed");
+        ThrowMsg(Exceptions::NotAllowed, "Update failure. ace_register_widget failed");
+    }
+    LogDebug("Ace data inserted");
 
     m_context.job->UpdateProgress(
         InstallerContext::INSTALL_NEW_DB_INSERT,
         "New Widget DB UPDATE Finished");
 }
 
+void TaskDatabase::StepRegisterExternalFiles()
+{
+    WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
+    if (m_context.existingWidgetInfo.isExist) //update
+    {
+        WidgetDAO dao(m_context.locations->getPkgname());
+        WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
+        FOREACH(file, externalLocationsDB)
+        {
+            if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
+            {
+                m_externalLocationsToRemove.push_back(*file);
+            }
+        }
+    }
+    LogDebug("Registering external files:");
+    FOREACH(file, externalLocationsUpdate)
+    {
+        LogDebug("  -> " << *file);
+    }
+
+    //set external locations to be registered
+    m_context.widgetConfig.externalLocations = externalLocationsUpdate;
+}
+
+void TaskDatabase::StepRemoveExternalFiles()
+{
+    if(!m_externalLocationsToRemove.empty())
+    {
+        LogDebug("Removing external files:");
+    }
+
+    FOREACH(file, m_externalLocationsToRemove)
+    {
+        if(WrtUtilFileExists(*file))
+        {
+            LogDebug("  -> " << *file);
+            remove(file->c_str());
+        }
+        else if(WrtUtilDirExists(*file))
+        {
+            LogDebug("  -> " << *file);
+            if(!WrtUtilRemove(*file)){
+                ThrowMsg(Exceptions::RemovingFolderFailure,
+                        "Failed to remove external directory");
+            }
+        }
+        else
+        {
+            LogWarning("  -> " << *file << "(no such a path)");
+        }
+    }
+}
+
 void TaskDatabase::StepAbortDBInsert()
 {
     LogWarning("[DB Update Task] Aborting... (DB Clean)");
-    Assert(!!m_context.widgetHandle);
     Try
     {
-        WidgetDAO::unregisterWidget(*m_context.widgetHandle); //TODO: unregister by pkgname (ace_widget_handle_t not int needed)
-
+        WidgetDAO::unregisterWidget(m_context.locations->getPkgname());
         LogDebug("Cleaning DB successful!");
     }
     Catch(DPL::DB::SqlConnection::Exception::Base)
     {
         LogError("Failed to handle StepAbortDBClean!");
     }
+
+    ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
+    // Remove also old one. If it was already updated nothing wrong will happen,
+    // but if not old widget will be removed.
+    if (INVALID_WIDGET_HANDLE != m_handleToRemove)
+        ace_unregister_widget(static_cast<ace_widget_handle_t>(m_handle));
 }
 
 } //namespace WidgetInstall