[Release] wrt-installer_0.0.89
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_unzip.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file    task_unzip.cpp
18  * @author  Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for installer task unzip
21  */
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>
27 #include <dpl/copy.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>
32 #include <sys/stat.h>
33
34 using namespace WrtDB;
35
36 namespace {
37 struct PathAndFilePair
38 {
39     std::string path;
40     std::string file;
41
42     PathAndFilePair(const std::string &p,
43             const std::string &f) :
44         path(p),
45         file(f)
46     {
47     }
48 };
49
50 PathAndFilePair SplitFileAndPath(const std::string &filePath)
51 {
52     std::string::size_type position = filePath.rfind('/');
53
54     // Is this only a file without a path ?
55     if (position == std::string::npos) {
56         return PathAndFilePair(std::string(), filePath);
57     }
58
59     // This is full file-path pair
60     return PathAndFilePair(filePath.substr(0,
61                                            position),
62                            filePath.substr(position + 1));
63 }
64 }
65
66 namespace Jobs {
67 namespace WidgetInstall {
68 TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
69     DPL::TaskDecl<TaskUnzip>(this),
70     m_installerContext(installerContext)
71 {
72     // Install steps
73     AddStep(&TaskUnzip::StepUnzipPrepare);
74     AddStep(&TaskUnzip::StepUnzipProgress);
75     AddStep(&TaskUnzip::StepUnzipFinished);
76 }
77
78 void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
79         const std::string &destFileName)
80 {
81     Try
82     {
83         DPL::AbstractWaitableInputAdapter inputAdapter(input);
84         DPL::FileOutput output(destFileName);
85
86         DPL::Copy(&inputAdapter, &output);
87     }
88     Catch(DPL::FileOutput::Exception::OpenFailed)
89     {
90         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
91     }
92     Catch(DPL::CopyFailed)
93     {
94         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
95     }
96 }
97
98 void TaskUnzip::StepUnzipPrepare()
99 {
100     LogInfo("Prepare to unzip...");
101
102     Try
103     {
104         m_zip.reset(new DPL::ZipInput(m_installerContext.locations->getWidgetSource()));
105         LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
106
107         // Widget package must not be empty
108         if (m_zip->empty()) {
109             ThrowMsg(Exceptions::ZipEmpty, m_installerContext.locations->getWidgetSource());
110         }
111
112         // Set iterator to first file
113         m_zipIterator = m_zip->begin();
114     }
115     Catch(DPL::ZipInput::Exception::OpenFailed)
116     {
117         ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.locations->getWidgetSource());
118     }
119     Catch(DPL::ZipInput::Exception::SeekFileFailed)
120     {
121         ThrowMsg(Exceptions::ExtractFileFailed,"m_installerContext.locations->getWidgetSource()");
122     }
123 }
124
125 void TaskUnzip::StepUnzipProgress()
126 {
127     // Show file info
128     LogInfo("Unzipping: '" << m_zipIterator->name <<
129             "', Comment: '" << m_zipIterator->comment <<
130             "', Compressed size: " << m_zipIterator->compressedSize <<
131             ", Uncompressed size: " << m_zipIterator->uncompressedSize);
132
133     // Normalize file paths
134     // FIXME: Implement checking for invalid characters
135
136     // Extract file or path
137     std::string fileName = m_zipIterator->name;
138
139     if (fileName[fileName.size() - 1] == '/') {
140         // This is path
141         std::string newPath = m_installerContext.locations->getTemporaryPackageDir() + "/" +
142             fileName.substr(0, fileName.size() - 1);
143         LogPedantic("Path to extract: " << newPath);
144
145         // Create path in case of it is empty
146         createTempPath(newPath);
147     } else {
148         // This is regular file
149         std::string fileExtractPath =
150             m_installerContext.locations->getTemporaryPackageDir() + "/" + fileName;
151
152         LogPedantic("File to extract: " << fileExtractPath);
153
154         // Split into pat & file pair
155         PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
156
157         LogPedantic("Path and file: " <<
158                     pathAndFile.path <<
159                     " : " << pathAndFile.file);
160
161         // First, ensure that path exists
162         createTempPath(pathAndFile.path);
163
164         Try
165         {
166             // Open file
167             std::unique_ptr<DPL::ZipInput::File> file(
168                 m_zip->OpenFile(fileName));
169
170             // Extract single file
171             ExtractFile(file.get(), fileExtractPath);
172         }
173         Catch(DPL::ZipInput::Exception::OpenFileFailed)
174         {
175             ThrowMsg(Exceptions::ExtractFileFailed, fileName);
176         }
177     }
178
179     // Check whether there are more files to extract
180     if (++m_zipIterator == m_zip->end()) {
181         LogInfo("Unzip progress finished successfuly");
182     } else {
183         SwitchToStep(&TaskUnzip::StepUnzipProgress);
184     }
185
186     m_installerContext.job->UpdateProgress(
187         InstallerContext::INSTALL_UNZIP_FILES,
188         "Unzip widget files to temporary directory");
189 }
190
191 void TaskUnzip::StepUnzipFinished()
192 {
193     // Unzip finished, close internal structures
194     m_zip.reset();
195
196     // Done
197     LogInfo("Unzip finished");
198 }
199
200 } //namespace WidgetInstall
201 } //namespace Jobs