2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @file task_plugin_copy.cpp
18 * @author Marcin Kaminski (marcin.ka@samsung.com)
20 * @brief Copying plugins delivered in widget package.
24 #include "task_plugins_copy.h"
25 #include <dpl/log/log.h>
26 #include <dpl/string.h>
27 #include <dpl/utils/wrt_utility.h>
28 #include <dpl/errno_string.h>
29 #include <widget_install/job_widget_install.h>
30 #include <widget_install_context.h>
31 #include <widget_install/widget_install_errors.h>
32 #include <dpl/exception.h>
33 /* Headers needed for listing directories */
36 /* Installation expceptions support */
37 #include <dpl/exception.h>
40 /* wrt-installer has to copy plugins for ARM architecture
41 * when running on target or for i586 architecture if launched
44 const std::string plugins_dir = "arm";
46 const std::string plugins_dir = "i586";
50 const std::string PackagePluginsDir = "/plugins/";
51 const std::string InstallationPluginsDir = "/data/.netscape/plugins/";
52 const mode_t InstallationPluginsDirMode = 0755;
56 namespace WidgetInstall {
57 TaskPluginsCopy::TaskPluginsCopy(InstallerContext& context) :
58 DPL::TaskDecl<TaskPluginsCopy>(this),
61 LogDebug("Starting widget plugins copy task");
62 AddStep(&TaskPluginsCopy::StepFindPlugins);
63 AddStep(&TaskPluginsCopy::StepCopyPlugins);
64 AddStep(&TaskPluginsCopy::StepCopyingFinished);
65 LogDebug("Widget plugins copy task ended");
66 m_npsource = m_context.locations->getSourceDir() + PackagePluginsDir
68 m_npdestination = m_context.locations->getPackageInstallationDir()
69 + InstallationPluginsDir;
72 void TaskPluginsCopy::StepFindPlugins()
74 LogDebug("Starting plugins finding step");
75 /* Check whether plugins directory for actual architecture exists
76 * (plugins for other architectures are omitted even they exists). */
77 if (!WrtUtilDirExists(m_npsource)) {
79 "Plugins directory (" << m_npsource
81 ") does not exists - skipping copy step");
82 SwitchToStep(&TaskPluginsCopy::StepCopyingFinished);
86 /* Find all .so files and store their names in list */
90 LogDebug("Opening plugins directory");
91 dir = opendir(m_npsource.c_str());
93 LogError("Unable to open plugins directory");
94 ThrowMsg(Exceptions::InternalError, "Unable to read plugins directory");
97 const std::string ext(".so");
98 /* Listing directory and checking entries found inside */
99 while ((entry = readdir(dir)) != NULL) {
100 tempname = m_npsource + "/" + entry->d_name;
101 if (lstat(tempname.c_str(), &st) != 0) {
103 "Failed to call \"lstat\" (errno:" << errno
105 ") on entry - skipping");
108 /* Directories other than "." and ".." should not be found*/
109 if (S_ISDIR(st.st_mode)) {
110 if (strncmp(entry->d_name, "..", 2) != 0
111 && strncmp(entry->d_name, ".", 1) != 0)
113 LogError("Directory detected instead of plugin file: "
115 /* Subdirectories inside plugins/ARCH are not supported */
116 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
118 "Failed to close dir: " << m_npsource
119 << " with error: " <<
120 DPL::GetErrnoString());
123 Exceptions::PluginsSubdirectory,
124 "Subdirectories inside plugins directory are not supported");
130 tempname = std::string(entry->d_name);
131 /* Check whether file extension is ".so" */
132 if (tempname.compare(tempname.size() - ext.size(), ext.size(),
135 /* Plugin file found */
136 LogDebug("Plugin file found: " << tempname);
137 m_nplugins.push_back(tempname);
139 /* Non-.so file found in plugins directory- skipping */
140 LogWarning("Non-plugin file found: " << tempname);
143 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
144 LogError("Failed to close dir: " << m_npsource << " with error: "
145 << DPL::GetErrnoString());
147 /* If no .so files found (list is empty) abort taks*/
148 if (m_nplugins.empty()) {
149 LogError("No valid plugin files found");
150 ThrowMsg(Exceptions::EmptyPluginsDirectory, "No valid plugin found");
152 LogDebug("Number of detected plugins: " << m_nplugins.size());
153 LogDebug("Plugins finding step ended");
156 void TaskPluginsCopy::StepCopyPlugins()
158 LogDebug("Starting plugins copying step");
160 std::string destination;
162 /* Create new directory for plugins (data/.netscape/plugins/) */
163 LogDebug("Creating destination plugin directory");
164 if (!WrtUtilMakeDir(m_npdestination, InstallationPluginsDirMode)) {
165 LogError("Failed to create directory for plugins");
166 ThrowMsg(Exceptions::InternalError,
167 "Failed to create directory for plugins");
170 LogDebug("Copying plugins to: " << m_npdestination);
171 /* Copy plugins from widget package into
172 * .netscape/plugins in widget's target directory */
173 for (std::list<std::string>::const_iterator it = m_nplugins.begin();
174 it != m_nplugins.end(); it++)
176 LogDebug("Copying plugin file: " << (*it));
177 source = m_npsource + "/" + (*it);
178 destination = m_npdestination + (*it);
179 if (rename(source.c_str(), destination.c_str()) != 0) {
180 LogError("Failed to move " << source << " to " << destination);
181 LogError("(errno: " << errno << ")");
182 ThrowMsg(Exceptions::InternalError, "Failed to copy plugin file");
186 /* Remove last part of path in source directory path
187 * (that is "arm" or "i586" depending on architecture) */
188 size_t position = m_npsource.find_last_of('/');
189 source = m_npsource.substr(0, position);
190 LogDebug("Removing unnecessary directory: " << source);
191 /* Remove source directory with plugins (possibly for multiple
193 if (!WrtUtilRemove(source)) {
194 LogError("Failed to plugins source remove directory");
195 ThrowMsg(Exceptions::InternalError,
196 "Failed to plugins source remove directory");
198 LogDebug("Plugins copying step ended");
200 m_context.job->UpdateProgress(
201 InstallerContext::INSTALL_PLUGINS_COPY,
205 void TaskPluginsCopy::StepCopyingFinished()
207 LogDebug("Plugins copy task finished");
209 } //namespace WidgetInstall