Duplicated code in plugins installer
authorPrzemyslaw Ciezkowski <p.ciezkowski@samsung.com>
Mon, 19 Nov 2012 13:42:13 +0000 (14:42 +0100)
committerGerrit Code Review <gerrit2@kim11>
Thu, 29 Nov 2012 13:52:22 +0000 (22:52 +0900)
[Issue#] N/A
[Bug] Duplicated code for retrieving plugins paths.
[Cause] N/A
[Solution] Moved duplicated code to PluginsUtils namespace.
[Verification] Install plugins using wrt-plugins-installer.

Change-Id: Id214f2e4cf75689188bda6c339b1c8065e933309

src/plugins-installer/wrt-installer/plugin_utils.cpp
src/plugins-installer/wrt-installer/plugin_utils.h
src/plugins-installer/wrt-installer/wrt_installer.cpp
src/plugins-installer/wrt-installer/wrt_installer.h
src/plugins-installer/wrt-installer/wrt_installer_api.cpp

index ae29f5e..1cf1120 100644 (file)
@@ -25,6 +25,8 @@
 #include <dpl/exception.h>
 #include <dpl/log/log.h>
 #include <dpl/wrt-dao-ro/global_config.h>
+#include <sys/stat.h>
+#include <dirent.h>
 
 using namespace WrtDB;
 
@@ -106,4 +108,49 @@ bool removeFile(const std::string& filename)
 
     return true;
 }
+
+PluginPathListPtr getPluginPaths()
+{
+    PluginPathListPtr pluginsPaths;
+    std::string PLUGIN_PATH = std::string(GlobalConfig::GetDevicePluginPath());
+    LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
+
+    DIR *dir;
+    dir = opendir(PLUGIN_PATH.c_str());
+    if (dir) {
+        struct dirent* libdir;
+
+        errno = 0;
+
+        pluginsPaths.reset(new PluginPathList());
+        while ((libdir = readdir(dir)) != 0) {
+            if (strcmp(libdir->d_name, ".") == 0 ||
+                strcmp(libdir->d_name, "..") == 0) {
+                continue;
+            }
+
+            std::string path = PLUGIN_PATH;
+            path += "/";
+            path += libdir->d_name;
+
+            struct stat tmp;
+
+            if (stat(path.c_str(), &tmp) == -1) {
+                LogError("Failed to open file" << path);
+                continue;
+            }
+
+            if (!S_ISDIR(tmp.st_mode)) {
+                LogError("Not a directory" << path);
+                continue;
+            }
+
+            pluginsPaths->push_back(path);
+        }
+        if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
+            LogError("Failed to close dir: " << dir);
+        }
+    }
+    return pluginsPaths;
+}
 } //namespace PluginUtils
index 659e627..3ba7303 100755 (executable)
@@ -23,7 +23,8 @@
 #define PLUGIN_UTILS_H
 
 #include <string>
-#include <sys/stat.h>
+#include <list>
+#include <memory>
 
 namespace PluginUtils {
 struct FileState
@@ -37,12 +38,16 @@ struct FileState
     };
 };
 
+typedef std::list<std::string> PluginPathList;
+typedef std::shared_ptr<PluginPathList> PluginPathListPtr;
+
 bool lockPluginInstallation();
 bool unlockPluginInstallation();
 bool checkPluginInstallationRequired();
 bool removeInstallationRequiredFlag();
 FileState::Type checkFile(const std::string& filename);
 bool removeFile(const std::string& filename);
+PluginPathListPtr getPluginPaths();
 
 }
 #endif // PLUGIN_UTILS_H
index 9af169b..11f5467 100644 (file)
@@ -19,7 +19,6 @@
  */
 
 #include "wrt_installer.h"
-#include "plugin_utils.h"
 
 #include <cstdlib>
 #include <string>
@@ -225,9 +224,9 @@ void WrtInstaller::OnEventReceived(
     PluginInstallerData* privateData = new PluginInstallerData;
     privateData->wrtInstaller = this;
 
-    if (!(*m_pluginsPaths).empty()) {
-        privateData->pluginPath = (*m_pluginsPaths).front();
-        (*m_pluginsPaths).pop_front();
+    if (m_pluginsPaths) {
+        privateData->pluginPath = m_pluginsPaths->front();
+        m_pluginsPaths->pop_front();
 
         wrt_install_plugin(privateData->pluginPath.c_str(),
                 static_cast<void*>(privateData),
@@ -271,61 +270,20 @@ void WrtInstaller::installPluginsStep()
         return;
     }
 
-    std::string PLUGIN_PATH = std::string(GlobalConfig::GetDevicePluginPath());
-
-    DIR *dir;
-    dir = opendir(PLUGIN_PATH.c_str());
-
-    if (!dir) {
+    PluginUtils::PluginPathListPtr pluginPaths = PluginUtils::getPluginPaths();
+    if (!pluginPaths) {
         return;
     }
 
-    LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
-    struct dirent* libdir;
-
-    errno = 0;
-
-    std::list<std::string> pluginsPaths;
-
-    while ((libdir = readdir(dir)) != 0) {
-        if (strcmp(libdir->d_name, ".") == 0 ||
-            strcmp(libdir->d_name, "..") == 0)
-        {
-            continue;
-        }
-
-        std::string path = PLUGIN_PATH;
-        path += "/";
-        path += libdir->d_name;
-
-        struct stat tmp;
-
-        if (stat(path.c_str(), &tmp) == -1) {
-            LogError("Failed to open file" << path);
-            continue;
-        }
-
-        if (!S_ISDIR(tmp.st_mode)) {
-            LogError("Not a directory" << path);
-            continue;
-        }
-
-        pluginsPaths.push_back(path);
-    }
-
     //set nb of plugins to install
     //this value indicate how many callbacks are expected
-    m_numPluginsToInstall = pluginsPaths.size();
+    m_numPluginsToInstall = pluginPaths->size();
     LogInfo("Plugins to install: " << m_numPluginsToInstall);
-    m_pluginsPaths = pluginsPaths;
+    m_pluginsPaths = pluginPaths;
 
     m_totalPlugins = m_numPluginsToInstall;
     DPL::Event::ControllerEventHandler<WRTInstallerNS::InstallPluginEvent>
         ::PostEvent(WRTInstallerNS::InstallPluginEvent());
-
-    if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
-        LogError("Failed to close dir: " << dir);
-    }
 }
 
 //void WrtInstaller::uninstallStep()
index 097a746..f81981d 100644 (file)
@@ -31,6 +31,7 @@
 #include <string>
 //#include <utilX.h>
 #include <wrt_installer_api.h>
+#include "plugin_utils.h"
 //#include <pkgmgr_installer.h>
 
 namespace WRTInstallerNS { //anonymous
@@ -150,6 +151,6 @@ class WrtInstaller :
     std::string m_webAppIcon;
 
     typedef std::list<std::string> PluginPathList;
-    DPL::Optional<PluginPathList> m_pluginsPaths;
+    PluginUtils::PluginPathListPtr m_pluginsPaths;
 };
 #endif // WRT_CLIENT_H
index 9c1da0e..3043acd 100755 (executable)
@@ -47,6 +47,7 @@
 #include <dpl/wrt-dao-ro/WrtDatabase.h>
 #include <vcore/VCore.h>
 #include <installer_main_thread.h>
+#include "plugin_utils.h"
 
 using namespace WrtDB;
 
@@ -442,52 +443,16 @@ extern "C"
                 LogWarning("Opening installation request file failed");
             }
 
-            std::string PLUGIN_PATH =
-                std::string(GlobalConfig::GetDevicePluginPath());
-
-            DIR *dir;
-            dir = opendir(PLUGIN_PATH.c_str());
-            if (!dir) {
+            PluginUtils::PluginPathListPtr pluginPaths =
+                    PluginUtils::getPluginPaths();
+            if (!pluginPaths) {
                 DPL::Semaphore::Remove(PLUGIN_INSTALL_SEMAPHORE);
                 return;
             }
 
-            LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
-            struct dirent* libdir;
-
-            errno = 0;
-
-            std::list<std::string> pluginsPaths;
-
-            while ((libdir = readdir(dir)) != 0) {
-                if (strcmp(libdir->d_name, ".") == 0 ||
-                    strcmp(libdir->d_name, "..") == 0)
-                {
-                    continue;
-                }
-
-                std::string path = PLUGIN_PATH;
-                path += "/";
-                path += libdir->d_name;
-
-                struct stat tmp;
-
-                if (stat(path.c_str(), &tmp) == -1) {
-                    LogError("Failed to open file" << path);
-                    continue;
-                }
-
-                if (!S_ISDIR(tmp.st_mode)) {
-                    LogError("Not a directory" << path);
-                    continue;
-                }
-
-                pluginsPaths.push_back(path);
-            }
-
-            wrt_count_plugin = pluginsPaths.size();
+            wrt_count_plugin = pluginPaths->size();
 
-            FOREACH(it, pluginsPaths) {
+            for (auto it = pluginPaths->begin(); it != pluginPaths->end(); ++it) {
                 wrt_plugin_data *plugin_data = new wrt_plugin_data;
 
                 plugin_data->plugin_installed_cb = installed_cb;
@@ -500,10 +465,6 @@ extern "C"
                     plugin_install_progress_cb);
             }
 
-            if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
-                LogError("Failed to close dir: " << PLUGIN_PATH);
-            }
-
             if (0 != unlink(installRequest.c_str())) {
                 LogError("Failed to remove file initializing plugin "
                          "installation");