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 widget 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>
34 using namespace WrtDB;
37 struct PathAndFilePair
42 PathAndFilePair(const std::string &p,
43 const std::string &f) :
49 PathAndFilePair SplitFileAndPath(const std::string &filePath)
51 std::string::size_type position = filePath.rfind('/');
53 // Is this only a file without a path ?
54 if (position == std::string::npos) {
55 return PathAndFilePair(std::string(), filePath);
58 // This is full file-path pair
59 return PathAndFilePair(filePath.substr(0,
61 filePath.substr(position + 1));
66 namespace WidgetInstall {
67 void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
68 const std::string &destFileName)
72 DPL::AbstractWaitableInputAdapter inputAdapter(input);
73 DPL::FileOutput output(destFileName);
75 DPL::Copy(&inputAdapter, &output);
77 Catch(DPL::FileOutput::Exception::OpenFailed)
79 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
81 Catch(DPL::CopyFailed)
83 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
87 void WidgetUnzip::unzipProgress(const std::string &destination)
90 LogInfo("Unzipping: '" << m_zipIterator->name <<
91 "', Comment: '" << m_zipIterator->comment <<
92 "', Compressed size: " << m_zipIterator->compressedSize <<
93 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
95 // Normalize file paths
96 // FIXME: Implement checking for invalid characters
98 // Extract file or path
99 std::string fileName = m_zipIterator->name;
101 if (fileName[fileName.size() - 1] == '/') {
103 std::string newPath = destination + "/" +
104 fileName.substr(0, fileName.size() - 1);
105 LogPedantic("Path to extract: " << newPath);
107 // Create path in case of it is empty
108 createTempPath(newPath);
110 // This is regular file
111 std::string fileExtractPath = destination + "/" + fileName;
113 LogPedantic("File to extract: " << fileExtractPath);
115 // Split into pat & file pair
116 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
118 LogPedantic("Path and file: " <<
120 " : " << pathAndFile.file);
122 // First, ensure that path exists
123 createTempPath(pathAndFile.path);
128 std::unique_ptr<DPL::ZipInput::File> file(
129 m_zip->OpenFile(fileName));
131 // Extract single file
132 ExtractFile(file.get(), fileExtractPath);
134 Catch(DPL::ZipInput::Exception::OpenFileFailed)
136 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
140 // Check whether there are more files to extract
141 if (++m_zipIterator == m_zip->end()) {
142 LogInfo("Unzip progress finished successfuly");
144 unzipProgress(destination);
148 std::string WidgetUnzip::decryptDrmPackage(const std::string &source)
150 LogInfo("Check DRM...");
155 void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &destination)
157 LogInfo("Prepare to unzip...");
161 wgtFile = decryptDrmPackage(source);
162 LogDebug("wgtFile : " << wgtFile);
164 m_zip.reset(new DPL::ZipInput(wgtFile));
165 LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
167 // Widget package must not be empty
168 if (m_zip->empty()) {
169 ThrowMsg(Exceptions::ZipEmpty, wgtFile);
172 // Set iterator to first file
173 m_zipIterator = m_zip->begin();
175 unzipProgress(destination);
177 // Unzip finished, close internal structures
181 LogInfo("Unzip finished");
183 Catch(DPL::ZipInput::Exception::OpenFailed)
185 ReThrowMsg(Exceptions::OpenZipFailed, wgtFile);
187 Catch(DPL::ZipInput::Exception::SeekFileFailed)
189 ThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
191 Catch(Exceptions::DrmDecryptFailed)
193 ReThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
197 } //namespace WidgetInstall