[Path usage Unification] Widget Uninstallation part unification
authorKamil Nowac <k.nowac@partner.samsung.com>
Wed, 29 May 2013 10:38:24 +0000 (12:38 +0200)
committerGerrit Code Review <gerrit2@kim11>
Wed, 5 Jun 2013 14:19:43 +0000 (23:19 +0900)
[Issue#] LINUXWRT-394
[Problem] A lot of paths used in wrt are constructed manually
[Cause] N/A
[Solution] Used module DPL::Utils::Path
[Verification] 1. Build wrt-installer repo with --define "WITH_TESTS ON"
2. Install and uninstall widgets on targets. They shoud uninstall without problems.

Change-Id: I90a680aad6d7292cb53a4dd236c7b353a5286187

src/jobs/widget_uninstall/job_widget_uninstall.cpp
src/jobs/widget_uninstall/task_remove_files.cpp
src/jobs/widget_uninstall/uninstaller_context.h

index 986ee29..70c6340 100644 (file)
 #include <dpl/wrt-dao-ro/global_config.h>
 #include <pkg-manager/pkgmgr_signal.h>
 #include <app2ext_interface.h>
+#include <dpl/utils/path.h>
 
 using namespace WrtDB;
 
 namespace { //anonymous
 const char* REG_TIZEN_PKGID_PATTERN = "^[a-zA-Z0-9]{10}$";
 const int PKGID_LENTH = 10;
-const std::string PRELOAD_INSTALLED_PATH = "/usr/apps";
+const DPL::Utils::Path PRELOAD_INSTALLED_PATH("/usr/apps");
 
 bool checkDirectoryExist(const std::string& pkgId)
 {
-    std::string installPath =
-        std::string(GlobalConfig::GetUserInstalledWidgetPath()) +
-        "/" + pkgId;
-
-    struct stat dirStat;
-    if ((stat(installPath.c_str(), &dirStat) == 0)) {
-        return true;
-    }
-    return false;
+    DPL::Utils::Path installPath(GlobalConfig::GetUserInstalledWidgetPath());
+    installPath /= pkgId;
+    return installPath.Exists();
 }
 }
 
@@ -106,7 +101,7 @@ JobWidgetUninstall::JobWidgetUninstall(
             m_context.locations = WidgetLocation(m_context.tzPkgid);
             m_context.locations->registerAppid(m_context.tzAppid);
             m_context.installedPath =
-                DPL::ToUTF8String(*dao.getWidgetInstalledPath());
+                DPL::Utils::Path(*dao.getWidgetInstalledPath());
 
             LogInfo("Widget model exists. App id : " << m_context.tzAppid);
 
@@ -150,7 +145,7 @@ WidgetStatus JobWidgetUninstall::getWidgetStatus(const std::string &id)
         LogDebug("Regcomp failed");
     }
     std::string pkgId;
-    std::string installPath;
+    DPL::Utils::Path installPath;
 
     Try {
         if ((regexec(&regx, id.c_str(),
@@ -163,16 +158,14 @@ WidgetStatus JobWidgetUninstall::getWidgetStatus(const std::string &id)
             LogDebug("Get appid from pkgid : " << appid);
             m_context.tzAppid = DPL::ToUTF8String(appid);
             WrtDB::WidgetDAOReadOnly dao(appid);
-            installPath = DPL::ToUTF8String(*dao.getWidgetInstalledPath());
+            installPath = DPL::Utils::Path(*dao.getWidgetInstalledPath());
         } else {
             pkgId = id.substr(0, PKGID_LENTH);
             WrtDB::WidgetDAOReadOnly dao(DPL::FromUTF8String(id));
             m_context.tzAppid = id;
-            installPath = DPL::ToUTF8String(*dao.getWidgetInstalledPath());
+            installPath = DPL::Utils::Path(*dao.getWidgetInstalledPath());
         }
-
-        if (0 == installPath.compare(0, PRELOAD_INSTALLED_PATH.length(),
-                    PRELOAD_INSTALLED_PATH)) {
+        if(installPath.isSubPath(PRELOAD_INSTALLED_PATH)){
             LogDebug("This widget is prealoded.");
         }
     } Catch(WidgetDAOReadOnly::Exception::WidgetNotExist) {
index 7404a74..4a5122f 100644 (file)
@@ -30,6 +30,7 @@
 #include <dpl/assert.h>
 #include <dpl/exception.h>
 #include <dpl/utils/wrt_utility.h>
+#include <dpl/utils/path.h>
 #include <ail.h>
 #include <pkgmgr/pkgmgr_parser.h>
 #include <errno.h>
@@ -62,13 +63,18 @@ void TaskRemoveFiles::StepRemoveInstallationDirectory()
         {
             LogDebug("Removing directory");
             m_context.removeStarted = true;
-            if (!WrtUtilRemove(m_context.installedPath)) {
+            DPL::Utils::Path widgetDir= m_context.installedPath;
+            Try{
+                DPL::Utils::Remove(widgetDir);
+            } Catch(DPL::Utils::Path::BaseException){
                 LogError("Removing widget installation directory failed : " <<
-                        m_context.installedPath);
+                        widgetDir.Fullpath());
             }
-            std::string dataDir = m_context.locations->getUserDataRootDir();
-            if (!WrtUtilRemove(dataDir)) {
-                LogWarning(dataDir + " is already removed");
+            DPL::Utils::Path dataDir(m_context.locations->getUserDataRootDir());
+            Try{
+                DPL::Utils::Remove(dataDir);
+            } Catch(DPL::Utils::Path::BaseException){
+                LogWarning(dataDir.Fullpath() << " is already removed");
             }
         } else {
             LogDebug("Removing sdcard directory");
@@ -105,32 +111,33 @@ void TaskRemoveFiles::StepRemoveManifest()
 {
     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
+    DPL::Utils::Path destFile;
+    const DPL::Utils::Path PRELOAD_INSTALLED_PATH("/usr/apps");
+    const DPL::Utils::Path USR_PACKAGES_PATH("/usr/share/packages");
+    const DPL::Utils::Path OPT_PACKAGES_PATH("/opt/share/packages");
+    if (0 == (m_context.installedPath.Fullpath()).compare(0,
+            PRELOAD_INSTALLED_PATH.Fullpath().length(),
+            PRELOAD_INSTALLED_PATH.Fullpath())) {
+        LogDebug("This widget is preloaded.");
+        destFile = USR_PACKAGES_PATH;
     } else {
-        destFile << "/opt/share/packages" << "/"; //TODO constant with path
+        destFile = OPT_PACKAGES_PATH;
     }
-    destFile << manifest_name.str();
-    std::string pre_manifest = "/usr/share/packages/" + manifest_name.str();
+    destFile /= manifest_name.str();
+    DPL::Utils::Path pre_manifest = USR_PACKAGES_PATH;
+    pre_manifest /= manifest_name.str();
 
-    if (!(access(destFile.str().c_str(), F_OK) == 0 &&
-            access(pre_manifest.c_str(), F_OK) == 0)) {
+    if (!(destFile.Exists() == 0 && pre_manifest.Exists())) {
         int ret1 = pkgmgr_parser_parse_manifest_for_uninstallation(
-                destFile.str().c_str(), NULL);
+                destFile.Fullpath().c_str(), NULL);
         if (ret1 != 0) {
             LogWarning("Manifest file failed to parse for uninstallation");
         }
     }
-
-    int ret2 = unlink(destFile.str().c_str());
-    if (ret2 != 0) {
-        LogWarning("No manifest file found: " << destFile.str());
+    if (!DPL::Utils::TryRemove(destFile)) {
+        LogWarning("No manifest file found: " << destFile.Fullpath());
     } else {
-        LogDebug("Manifest file removed: " << destFile.str());
+        LogDebug("Manifest file removed: " << destFile.Fullpath());
     }
 }
 
@@ -140,25 +147,28 @@ void TaskRemoveFiles::StepRemoveExternalLocations()
         WidgetDAO dao(DPL::FromUTF8String(m_context.tzAppid));
         LogDebug("Removing external locations:");
         WrtDB::ExternalLocationList externalPaths = dao.getWidgetExternalLocations();
-        FOREACH(path, externalPaths)
+        FOREACH(file, 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);
+            DPL::Utils::Path path(*file);
+            if(path.Exists()){
+                if(path.IsFile()){
+                    LogDebug("  -> " << path.Fullpath());
+                    Try{
+                        DPL::Utils::Remove(path);
+                    }Catch(DPL::Utils::Path::BaseException){
+                        LogError("Failed to remove the file: " << path.Fullpath());
+                    }
+                } else if (path.IsDir()){
+                    LogDebug("  -> " << path.Fullpath());
+                    Try{
+                        DPL::Utils::Remove(path);
+                    }Catch(DPL::Utils::Path::BaseException){
+                        Throw(Jobs::WidgetUninstall::TaskRemoveFiles::
+                                Exception::RemoveFilesFailed);
+                    }
                 }
-            } else {
-                LogWarning("  -> " << *path << "(no such a path)");
+            }else{
+                LogWarning("  -> " << path.Fullpath() << "(no such a path)");
             }
         }
         dao.unregisterAllExternalLocations();
index 3d9fe28..25c25e3 100644 (file)
@@ -26,6 +26,7 @@
 #include <string>
 #include <widget_uninstall/widget_uninstaller_struct.h>
 #include <widget_location.h>
+#include <dpl/utils/path.h>
 
 namespace Jobs {
 namespace WidgetUninstall {
@@ -62,7 +63,7 @@ struct UninstallerContext
     std::string tzAppid;
     std::string tzPkgid;
     bool removeAbnormal;
-    std::string installedPath;
+    DPL::Utils::Path installedPath;
 };
 
 #endif // WRT_SRC_INSTALLER_CORE_UNINSTALLER_TASKS_UNINSTALLER_CONTEXT_H_