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());
121 void TaskUnzip::StepUnzipProgress()
124 LogInfo("Unzipping: '" << m_zipIterator->name <<
125 "', Comment: '" << m_zipIterator->comment <<
126 "', Compressed size: " << m_zipIterator->compressedSize <<
127 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
129 // Normalize file paths
130 // FIXME: Implement checking for invalid characters
132 // Extract file or path
133 std::string fileName = m_zipIterator->name;
135 if (fileName[fileName.size() - 1] == '/') {
137 std::string newPath = m_installerContext.locations->getTemporaryPackageDir() + "/" +
138 fileName.substr(0, fileName.size() - 1);
139 LogPedantic("Path to extract: " << newPath);
141 // Create path in case of it is empty
142 createTempPath(newPath);
144 // This is regular file
145 std::string fileExtractPath =
146 m_installerContext.locations->getTemporaryPackageDir() + "/" + fileName;
148 LogPedantic("File to extract: " << fileExtractPath);
150 // Split into pat & file pair
151 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
153 LogPedantic("Path and file: " <<
155 " : " << pathAndFile.file);
157 // First, ensure that path exists
158 createTempPath(pathAndFile.path);
163 std::unique_ptr<DPL::ZipInput::File> file(
164 m_zip->OpenFile(fileName));
166 // Extract single file
167 ExtractFile(file.get(), fileExtractPath);
169 Catch(DPL::ZipInput::Exception::OpenFileFailed)
171 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
175 // Check whether there are more files to extract
176 if (++m_zipIterator == m_zip->end()) {
177 LogInfo("Unzip progress finished successfuly");
179 SwitchToStep(&TaskUnzip::StepUnzipProgress);
182 m_installerContext.job->UpdateProgress(
183 InstallerContext::INSTALL_UNZIP_FILES,
184 "Unzip widget files to temporary directory");
187 void TaskUnzip::StepUnzipFinished()
189 // Unzip finished, close internal structures
193 LogInfo("Unzip finished");
196 } //namespace WidgetInstall