merge with master
[platform/framework/web/wrt-plugins-common.git] / src / plugins-installer / plugin_installer.cpp
index a2c668d..929971b 100644 (file)
@@ -63,8 +63,8 @@ const std::string DIRECTORY_SEPARATOR = std::string("/");
 
 const int PluginsInstaller::INSTALLATION_ERROR = -1;
 
-PluginsInstaller::PluginsInstaller()
-    m_initialized(false)
+PluginsInstaller::PluginsInstaller() :
+    m_initialized(false)
 {
     LogInfo("PluginsInstaller created.");
 }
@@ -124,7 +124,7 @@ void PluginsInstaller::deinitialize()
 }
 
 PluginsInstaller::ReturnStatus PluginsInstaller::installPlugin(
-        const std::string& libPath)
+    const std::string& libPath)
 {
     if (!m_initialized) {
         LogError("Plugins installer not initialized.");
@@ -132,23 +132,25 @@ PluginsInstaller::ReturnStatus PluginsInstaller::installPlugin(
     }
     LogInfo("Plugin installation started. Checking path: " << libPath);
 
-    if (!PluginUtils::checkPath(libPath))
+    if (!PluginUtils::checkPath(libPath)) {
         return ReturnStatus::WrongPluginPath;
+    }
 
     LogInfo("Plugin path ok. Searching for config file...");
 
     std::string metaFileName = libPath + DIRECTORY_SEPARATOR +
         std::string(WrtDB::GlobalConfig::GetPluginMetafileName());
-    if (PluginUtils::checkFileExistance(metaFileName))
-    {
+    if (PluginUtils::checkFileExistance(metaFileName)) {
         return installPluginFromMetafile(libPath, metaFileName);
     }
 
     PluginMetafileData pluginInfo;
     pluginInfo.m_libraryName = getLibraryName(libPath);
 
-    LogInfo("Config file done. Lib name: " << pluginInfo.m_libraryName
-            << ". Searching for installed plugin...");
+    LogInfo(
+        "Config file done. Lib name: " << pluginInfo.m_libraryName
+                                       <<
+        ". Searching for installed plugin...");
 
     if (WrtDB::PluginDAO::isPluginInstalled(pluginInfo.m_libraryName)) {
         LogInfo("Plugin already installed.");
@@ -181,11 +183,11 @@ PluginsInstaller::ReturnStatus PluginsInstaller::installPlugin(
 
     FOREACH(o, *plugin->GetObjects()) {
         libraryObjects->addObjects(CAST(*o)->GetParentName(),
-            CAST(*o)->GetName());
+                                   CAST(*o)->GetName());
 
         LogDebug("[Parent << Object] " << CAST(*o)->GetParentName()
-            << " << "
-            << CAST(*o)->GetName());
+                                       << " << "
+                                       << CAST(*o)->GetName());
 
         registerObjects(libraryObjects, plugin->GetObjects());
     }
@@ -217,9 +219,10 @@ PluginsInstaller::ReturnStatus PluginsInstaller::installPlugin(
         LogInfo("Registration done. Resolving dependencies...");
 
         //TODO: can it be replaced with resolvePluginDependencies(handle)
-        if (!registerAndUpdateInstallation(pluginHandle, libraryObjects))
+        if (!registerAndUpdateInstallation(pluginHandle, libraryObjects)) {
             return ReturnStatus::InstallationWaiting;
-    } Catch (DPL::Exception) {
+        }
+    } Catch(DPL::Exception) {
         LogError("Failed to make database entry.");
         return ReturnStatus::DatabaseError;
     }
@@ -229,15 +232,15 @@ PluginsInstaller::ReturnStatus PluginsInstaller::installPlugin(
 }
 
 PluginObjectsPtr PluginsInstaller::loadLibraryFromMetafile(
-        const std::string& libName) const
+    const std::string& libName) const
 {
     LogInfo("Loading library: " << libName);
 
     void *dlHandle = dlopen(libName.c_str(), RTLD_NOW);
-    if (dlHandle == NULL ) {
+    if (dlHandle == NULL) {
         LogError(
-                "Failed to load plugin: " << libName <<
-                ". Reason: " << dlerror());
+            "Failed to load plugin: " << libName <<
+            ". Reason: " << dlerror());
         ThrowMsg(PluginInstall::Exceptions::LibraryException, "Library error");
     }
 
@@ -246,14 +249,14 @@ PluginObjectsPtr PluginsInstaller::loadLibraryFromMetafile(
 
     getWidgetEntityMapProcPtr =
         reinterpret_cast<get_widget_entity_map_proc *>(dlsym(dlHandle,
-                    PLUGIN_GET_CLASS_MAP_PROC_NAME));
+                                                             PLUGIN_GET_CLASS_MAP_PROC_NAME));
 
     if (getWidgetEntityMapProcPtr) {
         rawEntityList = (*getWidgetEntityMapProcPtr)();
     } else {
         rawEntityList =
             static_cast<const js_entity_definition_t *>(dlsym(dlHandle,
-                        PLUGIN_CLASS_MAP_NAME));
+                                                              PLUGIN_CLASS_MAP_NAME));
     }
 
     if (rawEntityList == NULL) {
@@ -270,14 +273,14 @@ PluginObjectsPtr PluginsInstaller::loadLibraryFromMetafile(
     LogInfo("#####");
 
     while (rawEntityListIterator->parent_name != NULL &&
-            rawEntityListIterator->object_name != NULL)
+           rawEntityListIterator->object_name != NULL)
     {
         LogInfo("#####     [" << rawEntityListIterator->object_name << "]: ");
         LogInfo("#####     Parent: " << rawEntityListIterator->parent_name);
         LogInfo("#####");
 
         libraryObjects->addObjects(rawEntityListIterator->parent_name,
-                rawEntityListIterator->object_name);
+                                   rawEntityListIterator->object_name);
 
         ++rawEntityListIterator;
     }
@@ -295,9 +298,8 @@ PluginObjectsPtr PluginsInstaller::loadLibraryFromMetafile(
     return libraryObjects;
 }
 
-
 PluginsInstaller::ReturnStatus PluginsInstaller::installPluginFromMetafile(
-        const std::string& path, const std::string& metaFilePath)
+    const std::string& path, const std::string& metaFilePath)
 {
     if (!m_initialized) {
         LogError("Plugins installer not initialized.");
@@ -308,7 +310,7 @@ PluginsInstaller::ReturnStatus PluginsInstaller::installPluginFromMetafile(
     {
         pluginData = parseMetafile(metaFilePath);
     }
-    Catch (PluginInstall::Exceptions::XMLFileParsingException)
+    Catch(PluginInstall::Exceptions::XMLFileParsingException)
     {
         LogError("Parsing metafile failed.");
         return ReturnStatus::MetafileError;
@@ -322,10 +324,11 @@ PluginsInstaller::ReturnStatus PluginsInstaller::installPluginFromMetafile(
         return ReturnStatus::AlreadyInstalled;
     }
     Try {
-
-        LogError("path is: " << path << ", libraryName: " << pluginData->m_libraryName);
+        LogError(
+            "path is: " << path << ", libraryName: " <<
+            pluginData->m_libraryName);
         PluginObjectsPtr objects = loadLibraryFromMetafile(
-            path + DIRECTORY_SEPARATOR + pluginData->m_libraryName);
+                path + DIRECTORY_SEPARATOR + pluginData->m_libraryName);
 
         PluginHandle pluginHandle =
             PluginDAO::registerPlugin(*pluginData, path);
@@ -344,9 +347,10 @@ PluginsInstaller::ReturnStatus PluginsInstaller::installPluginFromMetafile(
 
         LogInfo("Objects registered. Finishing...");
 
-        if (!registerAndUpdateInstallation(pluginHandle, objects))
+        if (!registerAndUpdateInstallation(pluginHandle, objects)) {
             return ReturnStatus::InstallationWaiting;
-    } Catch (DPL::Exception) {
+        }
+    } Catch(DPL::Exception) {
         LogError("Failed to make database entry.");
         return ReturnStatus::DatabaseError;
     }
@@ -373,21 +377,26 @@ int PluginsInstaller::installAllPlugins()
     }
 
     LogInfo("Plugin DIRECTORY IS" << PLUGIN_PATH);
-    struct dirent* libdir;
+    int return_code;
+    struct dirent libdir;
+    struct dirent* result;
 
     errno = 0;
 
     std::list<std::string> pluginsPaths;
 
-    while ((libdir = readdir(dir)) != 0) {
-        if (strcmp(libdir->d_name, ".") == 0 ||
-            strcmp(libdir->d_name, "..") == 0) {
+    for (return_code = readdir_r(dir, &libdir, &result);
+            result != NULL && return_code == 0;
+            return_code = readdir_r(dir, &libdir, &result)) {
+        if (strcmp(libdir.d_name, ".") == 0 ||
+            strcmp(libdir.d_name, "..") == 0)
+        {
             continue;
         }
 
         std::string path = PLUGIN_PATH;
         path += "/";
-        path += libdir->d_name;
+        path += libdir.d_name;
 
         struct stat tmp;
 
@@ -405,14 +414,18 @@ int PluginsInstaller::installAllPlugins()
         pluginsPaths.push_back(path);
     }
 
+    if (0 != return_code)
+        LogError("Error while reading directory.");
+
     if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
         LogError("Failed to close dir: " << PLUGIN_PATH);
     }
 
     LogDebug("Plugins to install: " << pluginsPaths.size());
 
-    for (int k = 0; k <= pluginsPaths.size(); ++k)
+    for (size_t k = 0; k <= pluginsPaths.size(); ++k) {
         printf(" ");
+    }
     printf("]\r[");
     int installedPluginsCount = 0;
     ReturnStatus ret = ReturnStatus::Unknown;
@@ -434,7 +447,7 @@ int PluginsInstaller::installAllPlugins()
     installedPluginsCount += installWaitingPlugins();
     m_registry.UnloadAll();
     LogInfo("Installed " << installedPluginsCount
-            << " plugins of total: " << pluginsPaths.size());
+                         << " plugins of total: " << pluginsPaths.size());
     return installedPluginsCount;
 }
 
@@ -448,8 +461,9 @@ int PluginsInstaller::installWaitingPlugins()
     int pluginsInstalled = 0;
     FOREACH(it, *waitingPlugins)
     {
-        if (resolvePluginDependencies(*it))
+        if (resolvePluginDependencies(*it)) {
             ++pluginsInstalled;
+        }
     }
     return pluginsInstalled;
 }
@@ -486,31 +500,29 @@ bool PluginsInstaller::resolvePluginDependencies(PluginHandle handle)
 void PluginsInstaller::registerObjects(const PluginObjectsPtr& libObj,
                                        const IObjectsListPtr& objects) const
 {
-   LogDebug("registerObjects invoked");
-
-   FOREACH(o, *objects)
-   {
-       auto children = CAST(*o)->GetChildren();
-
-       if(children)
-       {
-           FOREACH(c, *children)
-           {
-               libObj->addObjects(CAST(*o)->GetName(), CAST(*c)->GetName());
-
-               LogDebug("[Parent << Object] " << CAST(*c)->GetName()
-                        << " << "
-                        << CAST(*o)->GetName());
-           }
-
-           registerObjects(libObj, children);
-       }
-   }
-}
+    LogDebug("registerObjects invoked");
 
+    FOREACH(o, *objects)
+    {
+        auto children = CAST(*o)->GetChildren();
+
+        if (children) {
+            FOREACH(c, *children)
+            {
+                libObj->addObjects(CAST(*o)->GetName(), CAST(*c)->GetName());
+
+                LogDebug("[Parent << Object] " << CAST(*c)->GetName()
+                                               << " << "
+                                               << CAST(*o)->GetName());
+            }
+
+            registerObjects(libObj, children);
+        }
+    }
+}
 
 PluginsInstaller::OptionalPluginMetafileData PluginsInstaller::parseMetafile(
-        const std::string& path) const
+    const std::string& path) const
 {
     LogInfo("Plugin Config file::" << path);
     Try
@@ -532,7 +544,7 @@ PluginsInstaller::OptionalPluginMetafileData PluginsInstaller::parseMetafile(
     Catch(ValidationCore::ParserSchemaException::Base) {
         LogError("Error during file processing " << path);
         ThrowMsg(PluginInstall::Exceptions::XMLFileParsingException,
-            "Parsing metafile failed");
+                 "Parsing metafile failed");
     }
 }
 
@@ -541,12 +553,9 @@ std::string PluginsInstaller::getLibraryName(const std::string& dirPath) const
     std::string pluginPath = dirPath;
     size_t indexpos = pluginPath.find_last_of('/');
 
-    if (std::string::npos == indexpos)
-    {
+    if (std::string::npos == indexpos) {
         indexpos = 0;
-    }
-    else
-    {
+    } else {
         indexpos += 1;  // move after '/'
     }
 
@@ -558,8 +567,8 @@ std::string PluginsInstaller::getLibraryName(const std::string& dirPath) const
 }
 
 bool PluginsInstaller::registerAndUpdateInstallation(
-        const WrtDB::DbPluginHandle& pluginHandle,
-        const PluginObjectsPtr& libraries)
+    const WrtDB::DbPluginHandle& pluginHandle,
+    const PluginObjectsPtr& libraries)
 {
     PluginHandleSetPtr handles = PluginHandleSetPtr(new PluginHandleSet);
 
@@ -597,26 +606,30 @@ bool PluginsInstaller::fillMappingInterfaces(PluginMetafileData& pluginData,
 {
     void *dlHandle = dlopen(filename.c_str(), RTLD_NOW);
     if (dlHandle == NULL) {
-        LogError("Failed to load plugin: " << filename << ". Reason: " << dlerror());
+        LogError(
+            "Failed to load plugin: " << filename << ". Reason: " << dlerror());
         return false;
     }
     Try
     {
         ExportedApi* entryPoint =
-            static_cast<ExportedApi*> (dlsym(dlHandle, GetExportedSymbolName()));
+            static_cast<ExportedApi*>(dlsym(dlHandle, GetExportedSymbolName()));
         if (NULL == entryPoint) {
             LogError("Error: " << dlerror());
-            ThrowMsg(PluginInstall::Exceptions::LibraryException, "Library error");
+            ThrowMsg(PluginInstall::Exceptions::LibraryException,
+                     "Library error");
         }
 
         // obtain feature -> dev-cap mapping
-        feature_mapping_interface_t mappingInterface = {NULL, NULL, NULL};
+        feature_mapping_interface_t mappingInterface = { NULL, NULL, NULL };
         entryPoint->GetProvidedFeatures(&mappingInterface);
 
         if (!mappingInterface.featGetter || !mappingInterface.release ||
-            !mappingInterface.dcGetter) {
+            !mappingInterface.dcGetter)
+        {
             LogError("Failed to obtain mapping interface from .so");
-            ThrowMsg(PluginInstall::Exceptions::LibraryException, "Library error");
+            ThrowMsg(PluginInstall::Exceptions::LibraryException,
+                     "Library error");
         }
 
         feature_mapping_t* devcapMapping = mappingInterface.featGetter();
@@ -630,8 +643,10 @@ bool PluginsInstaller::fillMappingInterfaces(PluginMetafileData& pluginData,
             LogDebug("Feature: " << feature.m_name);
 
             const devcaps_t* dc =
-                mappingInterface.dcGetter(devcapMapping,
-                devcapMapping->features[i].feature_name);
+                mappingInterface.dcGetter(
+                    devcapMapping,
+                    devcapMapping->features[i].
+                        feature_name);
 
             LogDebug("device=cap: " << dc);
 
@@ -648,7 +663,7 @@ bool PluginsInstaller::fillMappingInterfaces(PluginMetafileData& pluginData,
         }
 
         mappingInterface.release(devcapMapping);
-    } Catch (PluginInstall::Exceptions::PluginsInstallerException)
+    } Catch(PluginInstall::Exceptions::PluginsInstallerException)
     {
         LogError("Exception while feature mapping");
         dlclose(dlHandle);
@@ -664,10 +679,12 @@ bool PluginsInstaller::fillMappingInterfaces(PluginMetafileData& pluginData,
     return true;
 }
 
-void PluginsInstaller::registerPluginObjects(const WrtDB::DbPluginHandle& handle,
-                                             const PluginObjectsPtr libObjects) const
+void PluginsInstaller::registerPluginObjects(
+    const WrtDB::DbPluginHandle& handle,
+    const PluginObjectsPtr libObjects)
+const
 {
-        //register implemented objects
+    //register implemented objects
     PluginObjects::ObjectsPtr objects =
         libObjects->getImplementedObject();