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 <dpl/log/log.h>
27 #include <dpl/file_output.h>
28 #include <dpl/abstract_waitable_input_adapter.h>
29 #include <task_commons.h>
33 struct PathAndFilePair
38 PathAndFilePair(const std::string &p,
39 const std::string &f) :
46 PathAndFilePair SplitFileAndPath(const std::string &filePath)
48 std::string::size_type position = filePath.rfind('/');
50 // Is this only a file without a path ?
51 if (position == std::string::npos) {
52 return PathAndFilePair(std::string(), filePath);
55 // This is full file-path pair
56 return PathAndFilePair(filePath.substr(0,
58 filePath.substr(position + 1));
64 namespace WidgetInstall {
65 TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
66 DPL::TaskDecl<TaskUnzip>(this),
67 m_installerContext(installerContext)
70 AddStep(&TaskUnzip::StepCreateTempPath);
71 AddStep(&TaskUnzip::StepUnzipPrepare);
72 AddStep(&TaskUnzip::StepUnzipProgress);
73 AddStep(&TaskUnzip::StepUnzipFinished);
75 AddAbortStep(&TaskUnzip::StepAbort);
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::StepCreateTempPath()
100 // Step succedded, save temporary widget path
101 m_installerContext.tempWidgetPath = createTempPath();
104 void TaskUnzip::StepUnzipPrepare()
106 LogInfo("Prepare to unzip...");
110 m_zip.Reset(new DPL::ZipInput(m_installerContext.widgetSource));
111 LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
113 // Widget package must not be empty
114 if (m_zip->empty()) {
115 ThrowMsg(Exceptions::ZipEmpty, m_installerContext.widgetSource);
118 // Set iterator to first file
119 m_zipIterator = m_zip->begin();
121 Catch(DPL::ZipInput::Exception::OpenFailed)
123 ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.widgetSource);
127 void TaskUnzip::StepUnzipProgress()
130 LogInfo("Unzipping: '" << m_zipIterator->name <<
131 "', Comment: '" << m_zipIterator->comment <<
132 "', Compressed size: " << m_zipIterator->compressedSize <<
133 ", 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_installerContext.tempWidgetPath + "/" +
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 =
152 m_installerContext.tempWidgetPath + "/" + fileName;
154 LogPedantic("File to extract: " << fileExtractPath);
156 // Split into pat & file pair
157 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
159 LogPedantic("Path and file: " <<
161 " : " << pathAndFile.file);
163 // First, ensure that path exists
164 createTempPath(pathAndFile.path);
169 DPL::ScopedPtr<DPL::ZipInput::File> file(
170 m_zip->OpenFile(fileName));
172 // Extract single file
173 ExtractFile(file.Get(), fileExtractPath);
175 Catch(DPL::ZipInput::Exception::OpenFileFailed)
177 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
181 // Check whether there are more files to extract
182 if (++m_zipIterator == m_zip->end()) {
183 LogInfo("Unzip progress finished successfuly");
185 SwitchToStep(&TaskUnzip::StepUnzipProgress);
189 void TaskUnzip::StepUnzipFinished()
191 // Unzip finished, close internal structures
195 LogInfo("Unzip finished");
198 void TaskUnzip::StepAbort()
200 removeTemporaryDir(m_installerContext.tempWidgetPath);
202 } //namespace WidgetInstall