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/wrt-dao-ro/global_config.h>
26 #include <dpl/log/log.h>
28 #include <dpl/file_output.h>
29 #include <dpl/abstract_waitable_input_adapter.h>
30 #include <dpl/errno_string.h>
36 #include <dpl/utils/file_utils.h>
38 using namespace WrtDB;
40 namespace // anonymous
42 const char * const TEMPORARY_PATH_POSTFIX = "temp";
43 const mode_t TEMPORARY_PATH_MODE = 0775;
45 struct PathAndFilePair
50 PathAndFilePair(const std::string &p,
51 const std::string &f) :
58 PathAndFilePair SplitFileAndPath(const std::string &filePath)
60 std::string::size_type position = filePath.rfind('/');
62 // Is this only a file without a path ?
63 if (position == std::string::npos) {
64 return PathAndFilePair(std::string(), filePath);
67 // This is full file-path pair
68 return PathAndFilePair(filePath.substr(0,
70 filePath.substr(position + 1));
73 static int lambdaDeleteFile(const char *fpath,
74 const struct stat *sb,
85 LogInfo("Removing old temporary directory" << fpath);
89 LogInfo("Unlinking old temporary file" << fpath);
96 } // namespace anonymous
99 namespace WidgetInstall {
100 TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
101 DPL::TaskDecl<TaskUnzip>(this),
102 m_installerContext(installerContext)
105 AddStep(&TaskUnzip::StepCreateTempPath);
106 AddStep(&TaskUnzip::StepUnzipPrepare);
107 AddStep(&TaskUnzip::StepUnzipProgress);
108 AddStep(&TaskUnzip::StepUnzipFinished);
110 AddAbortStep(&TaskUnzip::StepAbort);
113 void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
114 const std::string &destFileName)
118 DPL::AbstractWaitableInputAdapter inputAdapter(input);
119 DPL::FileOutput output(destFileName);
121 DPL::Copy(&inputAdapter, &output);
123 Catch(DPL::FileOutput::Exception::OpenFailed)
125 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
127 Catch(DPL::CopyFailed)
129 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
133 void TaskUnzip::StepCreateTempPath()
135 LogInfo("Step: Creating temporary path");
138 std::ostringstream tempPathBuilder;
140 tempPathBuilder << GlobalConfig::GetUserInstalledWidgetPath();
141 tempPathBuilder << "/";
142 tempPathBuilder << "widget";
143 tempPathBuilder << "/";
144 tempPathBuilder << TEMPORARY_PATH_POSTFIX;
145 tempPathBuilder << "_";
148 gettimeofday(&tv, NULL);
150 (static_cast<unsigned long long>(tv.tv_sec) * 1000000ULL +
151 static_cast<unsigned long long>(tv.tv_usec));
153 std::string tempPath = tempPathBuilder.str();
155 // Remove old path if any
156 struct stat fileInfo;
158 // FIXME: what if there are more then maxDepth recursive directories
159 static const int maxDepth = 1024;
160 if (stat(tempPath.c_str(), &fileInfo) == 0) {
162 tempPath.c_str(), lambdaDeleteFile, maxDepth, FTW_DEPTH);
165 ThrowMsg(DPL::CommonException::InternalError,
166 DPL::GetErrnoString());
170 FileUtils::MakePath(tempPath, TEMPORARY_PATH_MODE);
172 // Step succedded, save temporary widget path
173 m_installerContext.tempWidgetPath = tempPath;
174 m_installerContext.unzipStarted = true;
177 void TaskUnzip::StepUnzipPrepare()
179 LogInfo("Prepare to unzip...");
183 m_zip.Reset(new DPL::ZipInput(m_installerContext.widgetFilePath));
184 LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
186 // Widget package must not be empty
187 if (m_zip->empty()) {
188 ThrowMsg(Exceptions::ZipEmpty, m_installerContext.widgetFilePath);
191 // Set iterator to first file
192 m_zipIterator = m_zip->begin();
194 Catch(DPL::ZipInput::Exception::OpenFailed)
196 ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.widgetFilePath);
200 void TaskUnzip::StepUnzipProgress()
203 LogInfo("Unzipping: '" << m_zipIterator->name <<
204 "', Comment: '" << m_zipIterator->comment <<
205 "', Compressed size: " << m_zipIterator->compressedSize <<
206 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
208 // Normalize file paths
209 // FIXME: Implement checking for invalid characters
211 // Extract file or path
212 std::string fileName = m_zipIterator->name;
214 if (fileName[fileName.size() - 1] == '/') {
216 std::string newPath = m_installerContext.tempWidgetPath + "/" +
217 fileName.substr(0, fileName.size() - 1);
218 LogPedantic("Path to extract: " << newPath);
220 // Create path in case of it is empty
221 FileUtils::MakePath(newPath, TEMPORARY_PATH_MODE);
223 // This is regular file
224 std::string fileExtractPath =
225 m_installerContext.tempWidgetPath + "/" + fileName;
227 LogPedantic("File to extract: " << fileExtractPath);
229 // Split into pat & file pair
230 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
232 LogPedantic("Path and file: " <<
234 " : " << pathAndFile.file);
236 // First, ensure that path exists
237 FileUtils::MakePath(pathAndFile.path, TEMPORARY_PATH_MODE);
242 DPL::ScopedPtr<DPL::ZipInput::File> file(
243 m_zip->OpenFile(fileName));
245 // Extract single file
246 ExtractFile(file.Get(), fileExtractPath);
248 Catch(DPL::ZipInput::Exception::OpenFileFailed)
250 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
254 // Check whether there are more files to extract
255 if (++m_zipIterator == m_zip->end()) {
256 LogInfo("Unzip progress finished successfuly");
258 SwitchToStep(&TaskUnzip::StepUnzipProgress);
262 void TaskUnzip::StepUnzipFinished()
264 // Unzip finished, close internal structures
267 m_installerContext.unzipFinished = true;
270 LogInfo("Unzip finished");
273 void TaskUnzip::StepAbort()
275 LogError("[Unzip Task] Aborting... (removing temporary dir: " <<
276 m_installerContext.tempWidgetPath << " )");
278 static const int maxDepth = 1024;
279 struct stat fileInfo;
280 if (stat(m_installerContext.tempWidgetPath.c_str(), &fileInfo) == 0) {
281 nftw(m_installerContext.tempWidgetPath.c_str(),
282 lambdaDeleteFile, maxDepth, FTW_DEPTH);
285 } //namespace WidgetInstall