b8d0b9877ac4852e44e1863691515f2461594f3f
[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 task 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 #include <drm_trusted_client.h>
34
35 using namespace WrtDB;
36
37 namespace {
38 struct PathAndFilePair
39 {
40     std::string path;
41     std::string file;
42
43     PathAndFilePair(const std::string &p,
44             const std::string &f) :
45         path(p),
46         file(f)
47     {
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
67 namespace Jobs {
68 namespace WidgetInstall {
69 WidgetUnzip::WidgetUnzip(std::string source, std::string path) :
70     m_source(source),
71     m_destPath(path)
72 {
73     LogDebug("WidgetUnzip");
74 }
75
76 void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
77         const std::string &destFileName)
78 {
79     Try
80     {
81         DPL::AbstractWaitableInputAdapter inputAdapter(input);
82         DPL::FileOutput output(destFileName);
83
84         DPL::Copy(&inputAdapter, &output);
85     }
86     Catch(DPL::FileOutput::Exception::OpenFailed)
87     {
88         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
89     }
90     Catch(DPL::CopyFailed)
91     {
92         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
93     }
94 }
95
96 void WidgetUnzip::UnzipWidget()
97 {
98     UnzipPrepare();
99     UnzipProgress();
100 }
101
102 void WidgetUnzip::UnzipPrepare()
103 {
104     LogInfo("Prepare to unzip...");
105
106     Try
107     {
108         m_zip.reset(new DPL::ZipInput(m_source));
109         LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
110
111         // Widget package must not be empty
112         if (m_zip->empty()) {
113             ThrowMsg(Exceptions::ZipEmpty, m_source);
114         }
115
116         // Set iterator to first file
117         m_zipIterator = m_zip->begin();
118     }
119     Catch(DPL::ZipInput::Exception::OpenFailed)
120     {
121         ReThrowMsg(Exceptions::OpenZipFailed, m_source);
122     }
123 }
124
125 void WidgetUnzip::UnzipProgress()
126 {
127
128     // Show file info
129     LogInfo("Unzipping: '" << m_zipIterator->name <<
130             "', Comment: '" << m_zipIterator->comment <<
131             "', Compressed size: " << m_zipIterator->compressedSize <<
132             ", Uncompressed size: " << m_zipIterator->uncompressedSize);
133
134
135     // Normalize file paths
136     // FIXME: Implement checking for invalid characters
137
138     // Extract file or path
139     std::string fileName = m_zipIterator->name;
140
141     if (fileName[fileName.size() - 1] == '/') {
142         // This is path
143         std::string newPath = m_destPath + "/" +
144             fileName.substr(0, fileName.size() - 1);
145         LogPedantic("Path to extract: " << newPath);
146
147         // Create path in case of it is empty
148         createTempPath(newPath);
149     } else {
150         // This is regular file
151         std::string fileExtractPath = m_destPath + "/" + fileName;
152
153         LogPedantic("File to extract: " << fileExtractPath);
154
155         // Split into pat & file pair
156         PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
157
158         LogPedantic("Path and file: " <<
159                     pathAndFile.path <<
160                     " : " << pathAndFile.file);
161
162         // First, ensure that path exists
163         createTempPath(pathAndFile.path);
164
165         Try
166         {
167             // Open file
168             std::unique_ptr<DPL::ZipInput::File> file(
169                 m_zip->OpenFile(fileName));
170
171             // Extract single file
172             ExtractFile(file.get(), fileExtractPath);
173         }
174         Catch(DPL::ZipInput::Exception::OpenFileFailed)
175         {
176             ThrowMsg(Exceptions::ExtractFileFailed, fileName);
177         }
178     }
179
180     // Check whether there are more files to extract
181     if (++m_zipIterator == m_zip->end()) {
182         LogInfo("Unzip progress finished successfuly");
183         m_zip.reset();
184     } else {
185         UnzipProgress();
186     }
187 }
188
189 void WidgetUnzip::UnzipDRMWidget()
190 {
191     LogDebug("Decrypt drm widget");
192     drm_trusted_sapps_decrypt_package_info_s package_info;
193
194     strncpy(package_info.sadcf_filepath, m_source.c_str(),
195             sizeof(package_info.sadcf_filepath));
196     strncpy(package_info.decrypt_filepath, m_destPath.c_str(),
197             sizeof(package_info.decrypt_filepath));
198
199     drm_trusted_request_type_e requestType =
200         DRM_TRUSTED_REQ_TYPE_SAPPS_DECRYPT_PACKAGE;
201
202     int ret = drm_trusted_handle_request(requestType,
203                                          (void *)&package_info, NULL);
204     if (DRM_TRUSTED_RETURN_SUCCESS != ret) {
205         LogDebug("Failed unzip drm widget");
206         ThrowMsg(Exceptions::ExtractFileFailed, m_destPath);
207     }
208 }
209 } //namespace WidgetInstall
210 } //namespace Jobs