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::StepCreateTempPath);
74 AddStep(&TaskUnzip::StepUnzipPrepare);
75 AddStep(&TaskUnzip::StepUnzipProgress);
76 AddStep(&TaskUnzip::StepUnzipFinished);
78 AddAbortStep(&TaskUnzip::StepAbort);
81 void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
82 const std::string &destFileName)
86 DPL::AbstractWaitableInputAdapter inputAdapter(input);
87 DPL::FileOutput output(destFileName);
89 DPL::Copy(&inputAdapter, &output);
91 Catch(DPL::FileOutput::Exception::OpenFailed)
93 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
95 Catch(DPL::CopyFailed)
97 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
101 void TaskUnzip::StepCreateTempPath()
103 // Step succedded, save temporary widget path
104 m_installerContext.tempWidgetPath = createTempPath();
105 std::ostringstream path;
107 if (m_installerContext.widgetConfig.pType == PKG_TYPE_TIZEN_WITHSVCAPP) {
108 std::ostringstream tempRoot;
109 tempRoot << m_installerContext.tempWidgetPath;
110 tempRoot << "/" << GlobalConfig::GetWidgetSrcPath();
111 m_installerContext.tempWidgetRoot = tempRoot.str();
113 path << GlobalConfig::GetUserInstalledWidgetPath() << "/";
114 path << m_installerContext.widgetConfig.pkgname;
116 m_installerContext.tempWidgetRoot = m_installerContext.tempWidgetPath;
117 path << GlobalConfig::GetUserInstalledWidgetPath() << "/";
118 path << m_installerContext.widgetConfig.pkgname << "/";
119 path << GlobalConfig::GetWidgetSrcPath();
121 m_installerContext.installPath = path.str();
123 m_installerContext.job->UpdateProgress(
124 InstallerContext::INSTALL_CREATE_TEMPDIR,
125 "Create temporary directory for unzip");
128 void TaskUnzip::StepUnzipPrepare()
130 LogInfo("Prepare to unzip...");
134 m_zip.Reset(new DPL::ZipInput(m_installerContext.widgetSource));
135 LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
137 // Widget package must not be empty
138 if (m_zip->empty()) {
139 ThrowMsg(Exceptions::ZipEmpty, m_installerContext.widgetSource);
142 // Set iterator to first file
143 m_zipIterator = m_zip->begin();
145 Catch(DPL::ZipInput::Exception::OpenFailed)
147 ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.widgetSource);
151 void TaskUnzip::StepUnzipProgress()
154 LogInfo("Unzipping: '" << m_zipIterator->name <<
155 "', Comment: '" << m_zipIterator->comment <<
156 "', Compressed size: " << m_zipIterator->compressedSize <<
157 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
159 // Normalize file paths
160 // FIXME: Implement checking for invalid characters
162 // Extract file or path
163 std::string fileName = m_zipIterator->name;
165 if (fileName[fileName.size() - 1] == '/') {
167 std::string newPath = m_installerContext.tempWidgetPath + "/" +
168 fileName.substr(0, fileName.size() - 1);
169 LogPedantic("Path to extract: " << newPath);
171 // Create path in case of it is empty
172 createTempPath(newPath);
174 // This is regular file
175 std::string fileExtractPath =
176 m_installerContext.tempWidgetPath + "/" + fileName;
178 LogPedantic("File to extract: " << fileExtractPath);
180 // Split into pat & file pair
181 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
183 LogPedantic("Path and file: " <<
185 " : " << pathAndFile.file);
187 // First, ensure that path exists
188 createTempPath(pathAndFile.path);
193 DPL::ScopedPtr<DPL::ZipInput::File> file(
194 m_zip->OpenFile(fileName));
196 // Extract single file
197 ExtractFile(file.Get(), fileExtractPath);
199 Catch(DPL::ZipInput::Exception::OpenFileFailed)
201 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
205 // Check whether there are more files to extract
206 if (++m_zipIterator == m_zip->end()) {
207 LogInfo("Unzip progress finished successfuly");
209 SwitchToStep(&TaskUnzip::StepUnzipProgress);
212 m_installerContext.job->UpdateProgress(
213 InstallerContext::INSTALL_UNZIP_FILES,
214 "Unzip widget files to temporary directory");
217 void TaskUnzip::StepUnzipFinished()
219 // Unzip finished, close internal structures
223 LogInfo("Unzip finished");
226 void TaskUnzip::StepAbort()
228 removeTemporaryDir(m_installerContext.tempWidgetPath);
230 } //namespace WidgetInstall