[Release] wrt-installer_0.1.55
[framework/web/wrt-installer.git] / src / jobs / widget_uninstall / task_remove_files.cpp
old mode 100644 (file)
new mode 100755 (executable)
index efde73b..709b4a5
  * @brief   Implementation file for uninstaller task for removing widget files
  */
 
+#include <unistd.h>
 #include <widget_uninstall/task_remove_files.h>
 #include <widget_uninstall/job_widget_uninstall.h>
+#include <widget_uninstall/uninstaller_context.h>
+#include <widget_uninstall/widget_uninstall_errors.h>
+#include <dpl/wrt-dao-rw/widget_dao.h>
 #include <dpl/wrt-dao-ro/widget_config.h>
-
-#include <errno.h>
+#include <dpl/wrt-dao-ro/vconf_config.h>
 #include <dpl/assert.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <dirent.h>
+#include <dpl/exception.h>
 #include <dpl/utils/wrt_utility.h>
+#include <ail.h>
+#include <pkgmgr/pkgmgr_parser.h>
+#include <errno.h>
+#include <string.h>
+#include <widget_install_to_external.h>
+#include <vconf.h>
 
 namespace Jobs {
 namespace WidgetUninstall {
-
 using namespace WrtDB;
 
-void TaskRemoveFiles::ReadDir(const std::string& path,
-        std::list<std::string>& filesList)
-{
-    LogInfo("Reading directory " << path);
-    DIR* dir = NULL;
-    struct dirent* ptr = NULL;
-    dir = opendir(path.c_str());
-    std::string delim = "";
-
-    // adding / for path to directory to build a proper path to file under directory
-    if (path[path.size() - 1] != '/') {
-        delim = "/";
-    }
-
-    if (dir) {
-        while ((ptr = readdir(dir)) != NULL) {
-            if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
-                LogPedantic("Omiting " << ptr->d_name);
-                continue;
-            }
-            std::string childPath = path + delim + ptr->d_name;
-
-            struct stat st;
-            if (0 != lstat(childPath.c_str(), &st)) {
-                switch (errno) {
-                case EACCES:
-                    LogWarning(
-                        "EACCESS Error occured during lstat with path: " <<
-                        childPath);
-                    continue;
-                case EBADF:
-                    LogWarning(
-                        "EBADF Error occured during lstat with path: " <<
-                        childPath);
-                    continue;
-                case ENOENT:
-                    LogWarning(
-                        "ENOENT Error occured during lstat with path: " <<
-                        childPath);
-                    continue;
-                case ENOTDIR:
-                    LogWarning(
-                        "ENOTDIR Error occured during lstat with path: " <<
-                        childPath);
-                    continue;
-                default:
-                    LogWarning(
-                        "Unknown Error occured during lstat with path: " <<
-                        childPath);
-                    continue;
-                }
-            } else {
-                if (S_ISDIR(st.st_mode)) {
-                    LogPedantic(
-                        "Calling ReadDir in recursive way " << childPath);
-                    ReadDir(childPath, filesList);
-                } else if (S_ISREG(st.st_mode) ||
-                           S_ISCHR(st.st_mode) ||
-                           S_ISBLK(st.st_mode) ||
-                           S_ISFIFO(st.st_mode) ||
-                           S_ISLNK(st.st_mode) ||
-                           S_ISSOCK(st.st_mode)) {
-                    LogPedantic("Adding to list  " << childPath);
-                    filesList.push_front(childPath);
-                } else {
-                    LogWarning("Uknown file type ??");
-                }
-            }
-        }
-        closedir(dir);
-    } else if (errno == ENOTDIR) {
-        LogDebug("Adding to list " << path);
-        filesList.push_front(path);
-    } else {
-        LogWarning("Unknown error");
-    }
+namespace {
+const char * const VCONF_KEY_PREFIX = "file/private/";
 }
 
 TaskRemoveFiles::TaskRemoveFiles(UninstallerContext& context) :
     DPL::TaskDecl<TaskRemoveFiles>(this),
     m_context(context)
 {
-    AddStep(&TaskRemoveFiles::StepPrepare);
-    AddStep(&TaskRemoveFiles::StepRemoveOneFile);
-    AddStep(&TaskRemoveFiles::StepRemoveDirectories);
-    AddStep(&TaskRemoveFiles::StepRemoveDesktop);
+    AddStep(&TaskRemoveFiles::StepRemoveInstallationDirectory);
+    AddStep(&TaskRemoveFiles::StepRemoveManifest);
+    AddStep(&TaskRemoveFiles::StepRemoveExternalLocations);
+    AddStep(&TaskRemoveFiles::StepRemoveVconf);
     AddStep(&TaskRemoveFiles::StepRemoveFinished);
 }
 
 TaskRemoveFiles::~TaskRemoveFiles()
-{
-}
+{}
 
-void TaskRemoveFiles::StepPrepare()
+void TaskRemoveFiles::StepRemoveInstallationDirectory()
 {
-    LogInfo("StepPrepare started");
-
-    std::ostringstream widgetDir;
-
-    DPL::OptionalString pkgname = WidgetDAO(m_context.widgetHandle).getPkgname();
-    widgetDir << GlobalConfig::GetUserInstalledWidgetPath() << "/";
-    widgetDir << pkgname << "/";
-
-    uninstRootDir = widgetDir.str();
-    ReadDir(uninstRootDir, filesList);
-
-    LogInfo("StepPrepare finished");
-
-    m_context.job->UpdateProgress(
-        UninstallerContext::UNINSTALL_REMOVE_PREPARE,
-        "Widget remove prepare Finished");
-    m_context.removeStarted = true;
-}
-
-void TaskRemoveFiles::StepRemoveOneFile()
-{
-    if (filesList.size() > 0) {
-        LogDebug("Removing " << filesList.front());
-        if (0 != unlink(filesList.front().c_str())) {
-            LogWarning("Failed to remove file" << filesList.front());
+    LogInfo("StepRemoveInstallationDirectory started");
+    Try {
+        if (APP2EXT_SD_CARD !=
+                app2ext_get_app_location(m_context.tzPkgid.c_str()))
+        {
+            LogDebug("Removing directory");
+            m_context.removeStarted = true;
+            if (!WrtUtilRemove(m_context.installedPath)) {
+                LogError("Removing widget installation directory failed : " <<
+                        m_context.installedPath);
+            }
+            std::string dataDir = m_context.locations->getUserDataRootDir();
+            if (!WrtUtilRemove(dataDir)) {
+                LogWarning(dataDir + " is already removed");
+            }
+        } else {
+            LogDebug("Removing sdcard directory");
+            Try {
+                WidgetInstallToExtSingleton::Instance().initialize(m_context.tzPkgid);
+                WidgetInstallToExtSingleton::Instance().uninstallation();
+                WidgetInstallToExtSingleton::Instance().deinitialize();
+            }
+            Catch(WidgetInstallToExt::Exception::ErrorInstallToExt)
+            {
+                Throw(Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
+                        RemoveFilesFailed);
+            }
         }
-        filesList.pop_front();
-        SwitchToStep(&TaskRemoveFiles::StepRemoveOneFile);
-    } else {
-        m_context.removeFinished = true;
-    }
-
-    m_context.job->UpdateProgress(
-        UninstallerContext::UNINSTALL_REMOVE_ONEFILE,
-        "Widget remove onefile Finished");
-}
+    } Catch(Exception::RemoveFilesFailed) {
+        ThrowMsg(Exceptions::RemoveFileFailure, "Cann't remove directory");
 
-void TaskRemoveFiles::StepRemoveDirectories()
-{
-    using namespace WrtDB;
-    LogInfo("StepRemoveDirectories started");
-
-    if (!_WrtUtilRemoveDir(uninstRootDir.c_str())) {
-        LogWarning("Failed to remove directory" << uninstRootDir.c_str());
     }
-    LogInfo("StepRemoveDirectories finished");
-
     m_context.job->UpdateProgress(
-        UninstallerContext::UNINSTALL_REMOVE_DIRECTORIES,
-        "Widget remove directories Finished");
+        UninstallerContext::UNINSTALL_REMOVE_WIDGETDIR,
+        "Widget INstallation Directory Removal Finished");
 }
 
 void TaskRemoveFiles::StepRemoveFinished()
@@ -193,19 +108,76 @@ void TaskRemoveFiles::StepRemoveFinished()
         "Widget remove steps Finished");
 }
 
-void TaskRemoveFiles::StepRemoveDesktop()
+void TaskRemoveFiles::StepRemoveManifest()
 {
-    std::ostringstream desktopFile;
-
-    DPL::OptionalString pkgname = WidgetDAO(m_context.widgetHandle).getPkgname();
-    desktopFile << GlobalConfig::GetUserWidgetDesktopPath() << "/";
-    desktopFile << pkgname << ".desktop";
+    std::ostringstream manifest_name;
+    manifest_name << m_context.tzPkgid << ".xml";
+    std::ostringstream destFile;
+    const std::string PRELOAD_INSTALLED_PATH = "/usr/apps";
+    if (0 == (m_context.installedPath).compare(0, PRELOAD_INSTALLED_PATH.length(),
+                PRELOAD_INSTALLED_PATH)) {
+        LogDebug("This widget is prealoded.");
+        destFile << "/usr/share/packages" << "/"; //TODO constant with path
+    } else {
+        destFile << "/opt/share/packages" << "/"; //TODO constant with path
+    }
+    destFile << manifest_name.str();
+    int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(
+            destFile.str().c_str(), NULL);
+    int ret2 = unlink(destFile.str().c_str());
+    if (ret1 != 0) {
+        LogWarning("Manifest file failed to parse for uninstallation");
+    }
+    if (ret2 != 0) {
+        LogWarning("No manifest file found: " << destFile.str());
+    } else {
+        LogDebug("Manifest file removed: " << destFile.str());
+    }
+}
 
-    unlink(desktopFile.str().c_str());
+void TaskRemoveFiles::StepRemoveExternalLocations()
+{
+    if (!m_context.removeAbnormal) {
+        WidgetDAO dao(DPL::FromUTF8String(m_context.tzAppid));
+        LogDebug("Removing external locations:");
+        WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
+        FOREACH(path, externalPaths)
+        {
+            if (WrtUtilFileExists(*path)) {
+                LogDebug("  -> " << *path);
+                int ret = remove(path->c_str());
+                if (ret != 0) {
+                    LogError(
+                            "Failed to remove the file: " << path->c_str() <<
+                            " with error: " << strerror(errno));
+                }
+            } else if (WrtUtilDirExists(*path)) {
+                LogDebug("  -> " << *path);
+                if (!WrtUtilRemove(*path)) {
+                    Throw(
+                            Jobs::WidgetUninstall::TaskRemoveFiles::Exception::
+                            RemoveFilesFailed);
+                }
+            } else {
+                LogWarning("  -> " << *path << "(no such a path)");
+            }
+        }
+        dao.unregisterAllExternalLocations();
+    }
+}
 
-    m_context.job->UpdateProgress(
-        UninstallerContext::UNINSTALL_REMOVE_DESKTOP,
-        "Widget remove desktop Finished");
+void TaskRemoveFiles::StepRemoveVconf()
+{
+    if (!m_context.removeAbnormal) {
+        std::string key =
+            WrtDB::VconfConfig::GetVconfKeyRootPath(DPL::FromUTF8String(m_context.
+                        tzAppid));
+        if (vconf_unset_recursive(key.c_str())) {
+            LogError("Fail to unset vconf file");
+        } else {
+            LogDebug("vconf file is removed");
+        }
+    }
 }
 } //namespace WidgetUninstall
 } //namespace Jobs