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) :
50 PathAndFilePair SplitFileAndPath(const std::string &filePath)
52 std::string::size_type position = filePath.rfind('/');
54 // Is this only a file without a path ?
55 if (position == std::string::npos) {
56 return PathAndFilePair(std::string(), filePath);
59 // This is full file-path pair
60 return PathAndFilePair(filePath.substr(0,
62 filePath.substr(position + 1));
67 namespace WidgetInstall {
68 TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
69 DPL::TaskDecl<TaskUnzip>(this),
70 m_installerContext(installerContext)
73 AddStep(&TaskUnzip::StepUnzipPrepare);
74 AddStep(&TaskUnzip::StepUnzipProgress);
75 AddStep(&TaskUnzip::StepUnzipFinished);
78 void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
79 const std::string &destFileName)
83 DPL::AbstractWaitableInputAdapter inputAdapter(input);
84 DPL::FileOutput output(destFileName);
86 DPL::Copy(&inputAdapter, &output);
88 Catch(DPL::FileOutput::Exception::OpenFailed)
90 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
92 Catch(DPL::CopyFailed)
94 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
98 void TaskUnzip::StepUnzipPrepare()
100 LogInfo("Prepare to unzip...");
104 m_zip.reset(new DPL::ZipInput(m_installerContext.locations->getWidgetSource()));
105 LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
107 // Widget package must not be empty
108 if (m_zip->empty()) {
109 ThrowMsg(Exceptions::ZipEmpty, m_installerContext.locations->getWidgetSource());
112 // Set iterator to first file
113 m_zipIterator = m_zip->begin();
115 Catch(DPL::ZipInput::Exception::OpenFailed)
117 ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.locations->getWidgetSource());
119 Catch(DPL::ZipInput::Exception::SeekFileFailed)
121 ThrowMsg(Exceptions::ExtractFileFailed,"m_installerContext.locations->getWidgetSource()");
125 void TaskUnzip::StepUnzipProgress()
128 LogInfo("Unzipping: '" << m_zipIterator->name <<
129 "', Comment: '" << m_zipIterator->comment <<
130 "', Compressed size: " << m_zipIterator->compressedSize <<
131 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
133 // Normalize file paths
134 // FIXME: Implement checking for invalid characters
136 // Extract file or path
137 std::string fileName = m_zipIterator->name;
139 if (fileName[fileName.size() - 1] == '/') {
141 std::string newPath = m_installerContext.locations->getTemporaryPackageDir() + "/" +
142 fileName.substr(0, fileName.size() - 1);
143 LogPedantic("Path to extract: " << newPath);
145 // Create path in case of it is empty
146 createTempPath(newPath);
148 // This is regular file
149 std::string fileExtractPath =
150 m_installerContext.locations->getTemporaryPackageDir() + "/" + fileName;
152 LogPedantic("File to extract: " << fileExtractPath);
154 // Split into pat & file pair
155 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
157 LogPedantic("Path and file: " <<
159 " : " << pathAndFile.file);
161 // First, ensure that path exists
162 createTempPath(pathAndFile.path);
167 std::unique_ptr<DPL::ZipInput::File> file(
168 m_zip->OpenFile(fileName));
170 // Extract single file
171 ExtractFile(file.get(), fileExtractPath);
173 Catch(DPL::ZipInput::Exception::OpenFileFailed)
175 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
179 // Check whether there are more files to extract
180 if (++m_zipIterator == m_zip->end()) {
181 LogInfo("Unzip progress finished successfuly");
183 SwitchToStep(&TaskUnzip::StepUnzipProgress);
186 m_installerContext.job->UpdateProgress(
187 InstallerContext::INSTALL_UNZIP_FILES,
188 "Unzip widget files to temporary directory");
191 void TaskUnzip::StepUnzipFinished()
193 // Unzip finished, close internal structures
197 LogInfo("Unzip finished");
200 } //namespace WidgetInstall