df96243ea8c292c5a86d845e80e8439a84554d33
[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 struct PathAndFilePair
38 {
39     std::string path;
40     std::string file;
41
42     PathAndFilePair(const std::string &p,
43                     const std::string &f) :
44         path(p),
45         file(f)
46     {}
47 };
48
49 PathAndFilePair SplitFileAndPath(const std::string &filePath)
50 {
51     std::string::size_type position = filePath.rfind('/');
52
53     // Is this only a file without a path ?
54     if (position == std::string::npos) {
55         return PathAndFilePair(std::string(), filePath);
56     }
57
58     // This is full file-path pair
59     return PathAndFilePair(filePath.substr(0,
60                                            position),
61                            filePath.substr(position + 1));
62 }
63 }
64
65 namespace Jobs {
66 namespace WidgetInstall {
67 void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
68                             const std::string &destFileName)
69 {
70     Try
71     {
72         DPL::AbstractWaitableInputAdapter inputAdapter(input);
73         DPL::FileOutput output(destFileName);
74
75         DPL::Copy(&inputAdapter, &output);
76     }
77     Catch(DPL::FileOutput::Exception::OpenFailed)
78     {
79         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
80     }
81     Catch(DPL::CopyFailed)
82     {
83         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
84     }
85 }
86
87 void WidgetUnzip::unzipProgress(const std::string &destination)
88 {
89     // Show file info
90     LogInfo("Unzipping: '" << m_zipIterator->name <<
91             "', Comment: '" << m_zipIterator->comment <<
92             "', Compressed size: " << m_zipIterator->compressedSize <<
93             ", Uncompressed size: " << m_zipIterator->uncompressedSize);
94
95     // Normalize file paths
96     // FIXME: Implement checking for invalid characters
97
98     // Extract file or path
99     std::string fileName = m_zipIterator->name;
100
101     if (fileName[fileName.size() - 1] == '/') {
102         // This is path
103         std::string newPath = destination + "/" +
104             fileName.substr(0, fileName.size() - 1);
105         LogPedantic("Path to extract: " << newPath);
106
107         // Create path in case of it is empty
108         createTempPath(newPath);
109     } else {
110         // This is regular file
111         std::string fileExtractPath = destination + "/" + fileName;
112
113         LogPedantic("File to extract: " << fileExtractPath);
114
115         // Split into pat & file pair
116         PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
117
118         LogPedantic("Path and file: " <<
119                     pathAndFile.path <<
120                     " : " << pathAndFile.file);
121
122         // First, ensure that path exists
123         createTempPath(pathAndFile.path);
124
125         Try
126         {
127             // Open file
128             std::unique_ptr<DPL::ZipInput::File> file(
129                 m_zip->OpenFile(fileName));
130
131             // Extract single file
132             ExtractFile(file.get(), fileExtractPath);
133         }
134         Catch(DPL::ZipInput::Exception::OpenFileFailed)
135         {
136             ThrowMsg(Exceptions::ExtractFileFailed, fileName);
137         }
138     }
139
140     // Check whether there are more files to extract
141     if (++m_zipIterator == m_zip->end()) {
142         LogInfo("Unzip progress finished successfuly");
143     } else {
144         unzipProgress(destination);
145     }
146 }
147
148 std::string WidgetUnzip::decryptDrmPackage(const std::string &source)
149 {
150     LogInfo("Check DRM...");
151     // TODO : check drm
152     return source;
153 }
154
155 void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &destination)
156 {
157     LogInfo("Prepare to unzip...");
158     std::string wgtFile;
159     Try
160     {
161         wgtFile = decryptDrmPackage(source);
162         LogDebug("wgtFile : " << wgtFile);
163
164         m_zip.reset(new DPL::ZipInput(wgtFile));
165         LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
166
167         // Widget package must not be empty
168         if (m_zip->empty()) {
169             ThrowMsg(Exceptions::ZipEmpty, wgtFile);
170         }
171
172         // Set iterator to first file
173         m_zipIterator = m_zip->begin();
174
175         unzipProgress(destination);
176
177         // Unzip finished, close internal structures
178         m_zip.reset();
179
180         // Done
181         LogInfo("Unzip finished");
182     }
183     Catch(DPL::ZipInput::Exception::OpenFailed)
184     {
185         ReThrowMsg(Exceptions::OpenZipFailed, wgtFile);
186     }
187     Catch(DPL::ZipInput::Exception::SeekFileFailed)
188     {
189         ThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
190     }
191     Catch(Exceptions::DrmDecryptFailed)
192     {
193         ReThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
194     }
195 }
196
197 } //namespace WidgetInstall
198 } //namespace Jobs