0e72ced3391e339b2b16384fed62deb25d671c6e
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_plugins_copy.cpp
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file    task_plugin_copy.cpp
18  * @author  Marcin Kaminski (marcin.ka@samsung.com)
19  * @version 1.0
20  * @brief   Copying plugins delivered in widget package.
21  */
22
23 #include "task_plugins_copy.h"
24 #include <dpl/log/log.h>
25 #include <dpl/string.h>
26 #include <dpl/utils/wrt_utility.h>
27 #include <dpl/errno_string.h>
28 #include <widget_install_context.h>
29 #include <widget_install/widget_install_errors.h>
30 #include <dpl/exception.h>
31 /* Headers needed for listing directories */
32 #include <sys/stat.h>
33 #include <dirent.h>
34 /* Installation expceptions support */
35 #include <dpl/exception.h>
36 #include <cerrno>
37
38 /* wrt-installer has to copy plugins for ARM architecture
39  * when running on target or for i586 architecture if launched
40  * on SDK emulator. */
41 #ifdef __arm__
42 const std::string plugins_dir = "arm";
43 #else
44 const std::string plugins_dir = "i586";
45 #endif
46
47 namespace {
48 const std::string PackagePluginsDir = "/plugins/";
49 const std::string InstallationPluginsDir = "/data/.netscape/plugins/";
50 const mode_t InstallationPluginsDirMode = 0755;
51 }
52
53 namespace Jobs {
54 namespace WidgetInstall {
55 TaskPluginsCopy::TaskPluginsCopy(InstallerContext& context) :
56     DPL::TaskDecl<TaskPluginsCopy>(this),
57     m_context(context)
58 {
59     LogDebug("Starting widget plugins copy task");
60     AddStep(&TaskPluginsCopy::StepFindPlugins);
61     AddStep(&TaskPluginsCopy::StepCopyPlugins);
62     AddStep(&TaskPluginsCopy::StepCopyingFinished);
63     LogDebug("Widget plugins copy task ended");
64     m_npsource = m_context.locations->getSourceDir() + PackagePluginsDir
65         + plugins_dir;
66     m_npdestination = m_context.locations->getPackageInstallationDir()
67         + InstallationPluginsDir;
68 }
69
70 void TaskPluginsCopy::StepFindPlugins()
71 {
72     LogDebug("Starting plugins finding step");
73     /* Check whether plugins directory for actual architecture exists
74      * (plugins for other architectures are omitted even they exists). */
75     if (!WrtUtilDirExists(m_npsource)) {
76         LogDebug(
77             "Plugins directory (" << m_npsource
78                                   <<
79             ") does not exists - skipping copy step");
80         SwitchToStep(&TaskPluginsCopy::StepCopyingFinished);
81         return;
82     }
83
84     /* Find all .so files and store their names in list */
85     DIR *dir;
86     struct dirent *entry;
87     struct stat st;
88     LogDebug("Opening plugins directory");
89     dir = opendir(m_npsource.c_str());
90     if (dir == NULL) {
91         LogError("Unable to open plugins directory");
92         ThrowMsg(Exceptions::InternalError, "Unable to read plugins directory");
93     }
94     std::string tempname;
95     const std::string ext(".so");
96     /* Listing directory and checking entries found inside */
97     while ((entry = readdir(dir)) != NULL) {
98         tempname = m_npsource + "/" + entry->d_name;
99         if (lstat(tempname.c_str(), &st) != 0) {
100             LogWarning(
101                 "Failed to call \"lstat\" (errno:" << errno
102                                                    <<
103                 ") on entry - skipping");
104             continue;
105         }
106         /* Directories other than "." and ".." should not be found*/
107         if (S_ISDIR(st.st_mode)) {
108             if (strncmp(entry->d_name, "..", 2) != 0
109                 && strncmp(entry->d_name, ".", 1) != 0)
110             {
111                 LogError("Directory detected instead of plugin file: "
112                          << entry->d_name);
113                 /* Subdirectories inside plugins/ARCH are not supported */
114                 if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
115                     LogError(
116                         "Failed to close dir: " << m_npsource
117                                                 << " with error: " <<
118                         DPL::GetErrnoString());
119                 }
120                 ThrowMsg(
121                     Exceptions::PluginsSubdirectory,
122                     "Subdirectories inside plugins directory are not supported");
123             } else {
124                 continue;
125             }
126         }
127
128         tempname = std::string(entry->d_name);
129         /* Check whether file extension is ".so" */
130         if (tempname.compare(tempname.size() - ext.size(), ext.size(),
131                              ext) == 0)
132         {
133             /* Plugin file found */
134             LogDebug("Plugin file found: " << tempname);
135             m_nplugins.push_back(tempname);
136         } else {
137             /* Non-.so file found in plugins directory- skipping */
138             LogWarning("Non-plugin file found: " << tempname);
139         }
140     }
141     if (-1 == TEMP_FAILURE_RETRY(closedir(dir))) {
142         LogError("Failed to close dir: " << m_npsource << " with error: "
143                                          << DPL::GetErrnoString());
144     }
145     /* If no .so files found (list is empty) abort taks*/
146     if (m_nplugins.empty()) {
147         LogError("No valid plugin files found");
148         ThrowMsg(Exceptions::EmptyPluginsDirectory, "No valid plugin found");
149     }
150     LogDebug("Number of detected plugins: " << m_nplugins.size());
151     LogDebug("Plugins finding step ended");
152 }
153
154 void TaskPluginsCopy::StepCopyPlugins()
155 {
156     LogDebug("Starting plugins copying step");
157     std::string source;
158     std::string destination;
159
160     /* Create new directory for plugins (data/.netscape/plugins/) */
161     LogDebug("Creating destination plugin directory");
162     if (!WrtUtilMakeDir(m_npdestination, InstallationPluginsDirMode)) {
163         LogError("Failed to create directory for plugins");
164         ThrowMsg(Exceptions::InternalError,
165                  "Failed to create directory for plugins");
166     }
167
168     LogDebug("Copying plugins to: " << m_npdestination);
169     /* Copy plugins from widget package into
170      * .netscape/plugins in widget's target directory */
171     for (std::list<std::string>::const_iterator it = m_nplugins.begin();
172          it != m_nplugins.end(); it++)
173     {
174         LogDebug("Copying plugin file: " << (*it));
175         source = m_npsource + "/" + (*it);
176         destination = m_npdestination + (*it);
177         if (rename(source.c_str(), destination.c_str()) != 0) {
178             LogError("Failed to move " << source << " to " << destination);
179             LogError("(errno: " << errno << ")");
180             ThrowMsg(Exceptions::InternalError, "Failed to copy plugin file");
181         }
182     }
183
184     /* Remove last part of path in source directory path
185      * (that is "arm" or "i586" depending on architecture) */
186     size_t position = m_npsource.find_last_of('/');
187     source = m_npsource.substr(0, position);
188     LogDebug("Removing unnecessary directory: " << source);
189     /* Remove source directory with plugins (possibly for multiple
190      * architectures). */
191     if (!WrtUtilRemove(source)) {
192         LogError("Failed to plugins source remove directory");
193         ThrowMsg(Exceptions::InternalError,
194                  "Failed to plugins source remove directory");
195     }
196     LogDebug("Plugins copying step ended");
197 }
198
199 void TaskPluginsCopy::StepCopyingFinished()
200 {
201     LogDebug("Plugins copy task finished");
202 }
203 } //namespace WidgetInstall
204 } //namespace Jobs