remove drm code
[framework/web/wrt-installer.git] / src / jobs / widget_install / widget_unzip.cpp
1 /*
2  * Copyright (c) 2011 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    widget_unzip.cpp
18  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer widget unzip
21  */
22 #include <widget_install/widget_unzip.h>
23 #include <widget_install/widget_install_errors.h>
24 #include <widget_install/widget_install_context.h>
25 #include <widget_install/job_widget_install.h>
26 #include <dpl/log/log.h>
27 #include <dpl/copy.h>
28 #include <dpl/file_output.h>
29 #include <dpl/abstract_waitable_input_adapter.h>
30 #include <dpl/wrt-dao-ro/global_config.h>
31 #include <task_commons.h>
32 #include <sys/stat.h>
33
34 using namespace WrtDB;
35
36 namespace {
37 const char* const NPRUNTIME_PLUGINS_DIR = "plugins/";
38
39 struct PathAndFilePair
40 {
41     std::string path;
42     std::string file;
43
44     PathAndFilePair(const std::string &p,
45                     const std::string &f) :
46         path(p),
47         file(f)
48     {}
49 };
50
51 PathAndFilePair SplitFileAndPath(const std::string &filePath)
52 {
53     std::string::size_type position = filePath.rfind('/');
54
55     // Is this only a file without a path ?
56     if (position == std::string::npos) {
57         return PathAndFilePair(std::string(), filePath);
58     }
59
60     // This is full file-path pair
61     return PathAndFilePair(filePath.substr(0,
62                                            position),
63                            filePath.substr(position + 1));
64 }
65
66 inline bool isNPPlugin(const std::string& filePath)
67 {
68     std::string::size_type pos = filePath.find(NPRUNTIME_PLUGINS_DIR);
69     // Not specified if a plug-in name MUST end with a specific extension
70     // (e.g. .so)
71     return ((std::string::npos != pos) &&
72             ('/' != filePath[filePath.length() - 1]));
73 }
74
75 // precondition: isNPPlugin(filePath) == true
76 inline bool isValidNPPlugin(const std::string& filePath)
77 {
78     return (filePath.find(GlobalConfig::GetNPRuntimePluginsPath()) == 0);
79 }
80
81 bool shouldBeInstalled(const std::string& filePath)
82 {
83     if (isNPPlugin(filePath))
84     {
85         if (!isValidNPPlugin(filePath))
86         {
87             LogDebug("Not a valid NPRuntime plug-in: " << filePath);
88             return false;
89         }
90     }
91     return true;
92 }
93 }
94
95 namespace Jobs {
96 namespace WidgetInstall {
97 void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
98                             const std::string &destFileName)
99 {
100     Try
101     {
102         DPL::AbstractWaitableInputAdapter inputAdapter(input);
103         DPL::FileOutput output(destFileName);
104
105         DPL::Copy(&inputAdapter, &output);
106     }
107     Catch(DPL::FileOutput::Exception::OpenFailed)
108     {
109         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
110     }
111     Catch(DPL::CopyFailed)
112     {
113         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
114     }
115 }
116
117 void WidgetUnzip::unzipProgress(const std::string &destination)
118 {
119     // Show file info
120     LogInfo("Unzipping: '" << m_zipIterator->name <<
121             "', Comment: '" << m_zipIterator->comment <<
122             "', Compressed size: " << m_zipIterator->compressedSize <<
123             ", Uncompressed size: " << m_zipIterator->uncompressedSize);
124
125     // Normalize file paths
126     // FIXME: Implement checking for invalid characters
127
128     // Extract file or path
129     std::string fileName = m_zipIterator->name;
130
131     if (shouldBeInstalled(m_zipIterator->name))
132     {
133         if (fileName[fileName.size() - 1] == '/') {
134             // This is path
135             std::string newPath = destination + "/" +
136                 fileName.substr(0, fileName.size() - 1);
137             LogPedantic("Path to extract: " << newPath);
138
139             // Create path in case of it is empty
140             createTempPath(newPath);
141         } else {
142             // This is regular file
143             std::string fileExtractPath = destination + "/" + fileName;
144
145             LogPedantic("File to extract: " << fileExtractPath);
146
147             // Split into pat & file pair
148             PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
149
150             LogPedantic("Path and file: " <<
151                         pathAndFile.path <<
152                         " : " << pathAndFile.file);
153
154             // First, ensure that path exists
155             createTempPath(pathAndFile.path);
156
157             Try
158             {
159                 // Open file
160                 std::unique_ptr<DPL::ZipInput::File> file(
161                     m_zip->OpenFile(fileName));
162
163                 // Extract single file
164                 ExtractFile(file.get(), fileExtractPath);
165             }
166             Catch(DPL::ZipInput::Exception::OpenFileFailed)
167             {
168                 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
169             }
170         }
171     }
172     else
173     {
174         LogDebug("Skipping file: " << m_zipIterator->name);
175     }
176
177     // Check whether there are more files to extract
178     if (++m_zipIterator == m_zip->end()) {
179         LogInfo("Unzip progress finished successfuly");
180     } else {
181         unzipProgress(destination);
182     }
183 }
184
185 std::string WidgetUnzip::decryptDrmPackage(const std::string &source)
186 {
187     LogInfo("Check DRM...");
188     // TODO : check drm
189     return source;
190 }
191
192 void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &destination)
193 {
194     LogInfo("Prepare to unzip...");
195     std::string wgtFile;
196     Try
197     {
198         wgtFile = decryptDrmPackage(source);
199         LogDebug("wgtFile : " << wgtFile);
200
201         m_zip.reset(new DPL::ZipInput(wgtFile));
202         LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
203
204         // Widget package must not be empty
205         if (m_zip->empty()) {
206             ThrowMsg(Exceptions::ZipEmpty, wgtFile);
207         }
208
209         // Set iterator to first file
210         m_zipIterator = m_zip->begin();
211
212         unzipProgress(destination);
213
214         // Unzip finished, close internal structures
215         m_zip.reset();
216
217         // Done
218         LogInfo("Unzip finished");
219     }
220     Catch(DPL::ZipInput::Exception::OpenFailed)
221     {
222         ReThrowMsg(Exceptions::OpenZipFailed, wgtFile);
223     }
224     Catch(DPL::ZipInput::Exception::SeekFileFailed)
225     {
226         ThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
227     }
228     Catch(Exceptions::DrmDecryptFailed)
229     {
230         ReThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
231     }
232 }
233
234 } //namespace WidgetInstall
235 } //namespace Jobs