2 * Copyright (c) 2011 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 widget_unzip.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief Implementation file for installer task unzip
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>
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>
33 #include <drm_trusted_client.h>
35 using namespace WrtDB;
38 struct PathAndFilePair
43 PathAndFilePair(const std::string &p,
44 const std::string &f) :
51 PathAndFilePair SplitFileAndPath(const std::string &filePath)
53 std::string::size_type position = filePath.rfind('/');
55 // Is this only a file without a path ?
56 if (position == std::string::npos) {
57 return PathAndFilePair(std::string(), filePath);
60 // This is full file-path pair
61 return PathAndFilePair(filePath.substr(0,
63 filePath.substr(position + 1));
68 namespace WidgetInstall {
69 WidgetUnzip::WidgetUnzip(std::string source, std::string path) :
73 LogDebug("WidgetUnzip");
76 void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
77 const std::string &destFileName)
81 DPL::AbstractWaitableInputAdapter inputAdapter(input);
82 DPL::FileOutput output(destFileName);
84 DPL::Copy(&inputAdapter, &output);
86 Catch(DPL::FileOutput::Exception::OpenFailed)
88 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
90 Catch(DPL::CopyFailed)
92 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
96 void WidgetUnzip::UnzipWidget()
102 void WidgetUnzip::UnzipPrepare()
104 LogInfo("Prepare to unzip...");
108 m_zip.reset(new DPL::ZipInput(m_source));
109 LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
111 // Widget package must not be empty
112 if (m_zip->empty()) {
113 ThrowMsg(Exceptions::ZipEmpty, m_source);
116 // Set iterator to first file
117 m_zipIterator = m_zip->begin();
119 Catch(DPL::ZipInput::Exception::OpenFailed)
121 ReThrowMsg(Exceptions::OpenZipFailed, m_source);
125 void WidgetUnzip::UnzipProgress()
129 LogInfo("Unzipping: '" << m_zipIterator->name <<
130 "', Comment: '" << m_zipIterator->comment <<
131 "', Compressed size: " << m_zipIterator->compressedSize <<
132 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
135 // Normalize file paths
136 // FIXME: Implement checking for invalid characters
138 // Extract file or path
139 std::string fileName = m_zipIterator->name;
141 if (fileName[fileName.size() - 1] == '/') {
143 std::string newPath = m_destPath + "/" +
144 fileName.substr(0, fileName.size() - 1);
145 LogPedantic("Path to extract: " << newPath);
147 // Create path in case of it is empty
148 createTempPath(newPath);
150 // This is regular file
151 std::string fileExtractPath = m_destPath + "/" + fileName;
153 LogPedantic("File to extract: " << fileExtractPath);
155 // Split into pat & file pair
156 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
158 LogPedantic("Path and file: " <<
160 " : " << pathAndFile.file);
162 // First, ensure that path exists
163 createTempPath(pathAndFile.path);
168 std::unique_ptr<DPL::ZipInput::File> file(
169 m_zip->OpenFile(fileName));
171 // Extract single file
172 ExtractFile(file.get(), fileExtractPath);
174 Catch(DPL::ZipInput::Exception::OpenFileFailed)
176 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
180 // Check whether there are more files to extract
181 if (++m_zipIterator == m_zip->end()) {
182 LogInfo("Unzip progress finished successfuly");
189 void WidgetUnzip::UnzipDRMWidget()
191 LogDebug("Decrypt drm widget");
192 drm_trusted_sapps_decrypt_package_info_s package_info;
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));
199 drm_trusted_request_type_e requestType =
200 DRM_TRUSTED_REQ_TYPE_SAPPS_DECRYPT_PACKAGE;
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);
209 } //namespace WidgetInstall