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 task_unzip.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief Implementation file for installer task unzip
22 #include <widget_install/task_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 TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
68 DPL::TaskDecl<TaskUnzip>(this),
69 m_installerContext(installerContext)
72 AddStep(&TaskUnzip::StepUnzipPrepare);
73 AddStep(&TaskUnzip::StepUnzipProgress);
74 AddStep(&TaskUnzip::StepUnzipFinished);
77 void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
78 const std::string &destFileName)
82 DPL::AbstractWaitableInputAdapter inputAdapter(input);
83 DPL::FileOutput output(destFileName);
85 DPL::Copy(&inputAdapter, &output);
87 Catch(DPL::FileOutput::Exception::OpenFailed)
89 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
91 Catch(DPL::CopyFailed)
93 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
97 void TaskUnzip::StepUnzipPrepare()
99 LogInfo("Prepare to unzip...");
103 m_zip.reset(new DPL::ZipInput(m_installerContext.locations->
105 LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
107 // Widget package must not be empty
108 if (m_zip->empty()) {
109 ThrowMsg(Exceptions::ZipEmpty,
110 m_installerContext.locations->getWidgetSource());
113 // Set iterator to first file
114 m_zipIterator = m_zip->begin();
116 Catch(DPL::ZipInput::Exception::OpenFailed)
118 ReThrowMsg(Exceptions::OpenZipFailed,
119 m_installerContext.locations->getWidgetSource());
121 Catch(DPL::ZipInput::Exception::SeekFileFailed)
123 ThrowMsg(Exceptions::ExtractFileFailed,
124 "m_installerContext.locations->getWidgetSource()");
128 void TaskUnzip::StepUnzipProgress()
131 LogInfo("Unzipping: '" << m_zipIterator->name <<
132 "', Comment: '" << m_zipIterator->comment <<
133 "', Compressed size: " << m_zipIterator->compressedSize <<
134 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
136 // Normalize file paths
137 // FIXME: Implement checking for invalid characters
139 // Extract file or path
140 std::string fileName = m_zipIterator->name;
142 if (fileName[fileName.size() - 1] == '/') {
144 std::string newPath =
145 m_installerContext.locations->getTemporaryPackageDir() + "/" +
146 fileName.substr(0, fileName.size() - 1);
147 LogPedantic("Path to extract: " << newPath);
149 // Create path in case of it is empty
150 createTempPath(newPath);
152 // This is regular file
153 std::string fileExtractPath =
154 m_installerContext.locations->getTemporaryPackageDir() + "/" +
157 LogPedantic("File to extract: " << fileExtractPath);
159 // Split into pat & file pair
160 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
162 LogPedantic("Path and file: " <<
164 " : " << pathAndFile.file);
166 // First, ensure that path exists
167 createTempPath(pathAndFile.path);
172 std::unique_ptr<DPL::ZipInput::File> file(
173 m_zip->OpenFile(fileName));
175 // Extract single file
176 ExtractFile(file.get(), fileExtractPath);
178 Catch(DPL::ZipInput::Exception::OpenFileFailed)
180 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
184 // Check whether there are more files to extract
185 if (++m_zipIterator == m_zip->end()) {
186 LogInfo("Unzip progress finished successfuly");
188 SwitchToStep(&TaskUnzip::StepUnzipProgress);
191 m_installerContext.job->UpdateProgress(
192 InstallerContext::INSTALL_UNZIP_FILES,
193 "Unzip widget files to temporary directory");
196 void TaskUnzip::StepUnzipFinished()
198 // Unzip finished, close internal structures
202 LogInfo("Unzip finished");
204 } //namespace WidgetInstall