From ae411fb45879b9cdfb58955bfab93c7609b4b942 Mon Sep 17 00:00:00 2001
From: Przemyslaw Ciezkowski
Date: Mon, 19 Nov 2012 14:42:13 +0100
Subject: [PATCH] Duplicated code in plugins installer
[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
---
.../wrt-installer/plugin_utils.cpp | 47 ++++++++++++++++++
src/plugins-installer/wrt-installer/plugin_utils.h | 7 ++-
.../wrt-installer/wrt_installer.cpp | 56 +++-------------------
.../wrt-installer/wrt_installer.h | 3 +-
.../wrt-installer/wrt_installer_api.cpp | 51 +++-----------------
5 files changed, 68 insertions(+), 96 deletions(-)
diff --git a/src/plugins-installer/wrt-installer/plugin_utils.cpp b/src/plugins-installer/wrt-installer/plugin_utils.cpp
index ae29f5e..1cf1120 100644
--- a/src/plugins-installer/wrt-installer/plugin_utils.cpp
+++ b/src/plugins-installer/wrt-installer/plugin_utils.cpp
@@ -25,6 +25,8 @@
#include
#include
#include
+#include
+#include
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
diff --git a/src/plugins-installer/wrt-installer/plugin_utils.h b/src/plugins-installer/wrt-installer/plugin_utils.h
index 659e627..3ba7303 100755
--- a/src/plugins-installer/wrt-installer/plugin_utils.h
+++ b/src/plugins-installer/wrt-installer/plugin_utils.h
@@ -23,7 +23,8 @@
#define PLUGIN_UTILS_H
#include
-#include
+#include
+#include
namespace PluginUtils {
struct FileState
@@ -37,12 +38,16 @@ struct FileState
};
};
+typedef std::list PluginPathList;
+typedef std::shared_ptr 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
diff --git a/src/plugins-installer/wrt-installer/wrt_installer.cpp b/src/plugins-installer/wrt-installer/wrt_installer.cpp
index 9af169b..11f5467 100644
--- a/src/plugins-installer/wrt-installer/wrt_installer.cpp
+++ b/src/plugins-installer/wrt-installer/wrt_installer.cpp
@@ -19,7 +19,6 @@
*/
#include "wrt_installer.h"
-#include "plugin_utils.h"
#include
#include
@@ -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(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 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
::PostEvent(WRTInstallerNS::InstallPluginEvent());
-
- if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
- LogError("Failed to close dir: " << dir);
- }
}
//void WrtInstaller::uninstallStep()
diff --git a/src/plugins-installer/wrt-installer/wrt_installer.h b/src/plugins-installer/wrt-installer/wrt_installer.h
index 097a746..f81981d 100644
--- a/src/plugins-installer/wrt-installer/wrt_installer.h
+++ b/src/plugins-installer/wrt-installer/wrt_installer.h
@@ -31,6 +31,7 @@
#include
//#include
#include
+#include "plugin_utils.h"
//#include
namespace WRTInstallerNS { //anonymous
@@ -150,6 +151,6 @@ class WrtInstaller :
std::string m_webAppIcon;
typedef std::list PluginPathList;
- DPL::Optional m_pluginsPaths;
+ PluginUtils::PluginPathListPtr m_pluginsPaths;
};
#endif // WRT_CLIENT_H
diff --git a/src/plugins-installer/wrt-installer/wrt_installer_api.cpp b/src/plugins-installer/wrt-installer/wrt_installer_api.cpp
index 9c1da0e..3043acd 100755
--- a/src/plugins-installer/wrt-installer/wrt_installer_api.cpp
+++ b/src/plugins-installer/wrt-installer/wrt_installer_api.cpp
@@ -47,6 +47,7 @@
#include
#include
#include
+#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 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");
--
2.7.4