dd9bdc8130313cec3452d56f5b1fcc1cbd224920
[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::StepCreateTempPath);
74     AddStep(&TaskUnzip::StepUnzipPrepare);
75     AddStep(&TaskUnzip::StepUnzipProgress);
76     AddStep(&TaskUnzip::StepUnzipFinished);
77
78     AddAbortStep(&TaskUnzip::StepAbort);
79 }
80
81 void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
82         const std::string &destFileName)
83 {
84     Try
85     {
86         DPL::AbstractWaitableInputAdapter inputAdapter(input);
87         DPL::FileOutput output(destFileName);
88
89         DPL::Copy(&inputAdapter, &output);
90     }
91     Catch(DPL::FileOutput::Exception::OpenFailed)
92     {
93         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
94     }
95     Catch(DPL::CopyFailed)
96     {
97         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
98     }
99 }
100
101 void TaskUnzip::StepCreateTempPath()
102 {
103     // Step succedded, save temporary widget path
104     m_installerContext.tempWidgetPath = createTempPath();
105     std::ostringstream path;
106
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();
112
113         path << GlobalConfig::GetUserInstalledWidgetPath() << "/";
114         path << m_installerContext.widgetConfig.pkgname;
115     } else {
116         m_installerContext.tempWidgetRoot = m_installerContext.tempWidgetPath;
117         path << GlobalConfig::GetUserInstalledWidgetPath() << "/";
118         path <<  m_installerContext.widgetConfig.pkgname << "/";
119         path << GlobalConfig::GetWidgetSrcPath();
120     }
121     m_installerContext.installPath = path.str();
122
123     m_installerContext.job->UpdateProgress(
124         InstallerContext::INSTALL_CREATE_TEMPDIR,
125         "Create temporary directory for unzip");
126 }
127
128 void TaskUnzip::StepUnzipPrepare()
129 {
130     LogInfo("Prepare to unzip...");
131
132     Try
133     {
134         m_zip.Reset(new DPL::ZipInput(m_installerContext.widgetSource));
135         LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
136
137         // Widget package must not be empty
138         if (m_zip->empty()) {
139             ThrowMsg(Exceptions::ZipEmpty, m_installerContext.widgetSource);
140         }
141
142         // Set iterator to first file
143         m_zipIterator = m_zip->begin();
144     }
145     Catch(DPL::ZipInput::Exception::OpenFailed)
146     {
147         ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.widgetSource);
148     }
149 }
150
151 void TaskUnzip::StepUnzipProgress()
152 {
153     // Show file info
154     LogInfo("Unzipping: '" << m_zipIterator->name <<
155             "', Comment: '" << m_zipIterator->comment <<
156             "', Compressed size: " << m_zipIterator->compressedSize <<
157             ", Uncompressed size: " << m_zipIterator->uncompressedSize);
158
159     // Normalize file paths
160     // FIXME: Implement checking for invalid characters
161
162     // Extract file or path
163     std::string fileName = m_zipIterator->name;
164
165     if (fileName[fileName.size() - 1] == '/') {
166         // This is path
167         std::string newPath = m_installerContext.tempWidgetPath + "/" +
168             fileName.substr(0, fileName.size() - 1);
169         LogPedantic("Path to extract: " << newPath);
170
171         // Create path in case of it is empty
172         createTempPath(newPath);
173     } else {
174         // This is regular file
175         std::string fileExtractPath =
176             m_installerContext.tempWidgetPath + "/" + fileName;
177
178         LogPedantic("File to extract: " << fileExtractPath);
179
180         // Split into pat & file pair
181         PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
182
183         LogPedantic("Path and file: " <<
184                     pathAndFile.path <<
185                     " : " << pathAndFile.file);
186
187         // First, ensure that path exists
188         createTempPath(pathAndFile.path);
189
190         Try
191         {
192             // Open file
193             DPL::ScopedPtr<DPL::ZipInput::File> file(
194                 m_zip->OpenFile(fileName));
195
196             // Extract single file
197             ExtractFile(file.Get(), fileExtractPath);
198         }
199         Catch(DPL::ZipInput::Exception::OpenFileFailed)
200         {
201             ThrowMsg(Exceptions::ExtractFileFailed, fileName);
202         }
203     }
204
205     // Check whether there are more files to extract
206     if (++m_zipIterator == m_zip->end()) {
207         LogInfo("Unzip progress finished successfuly");
208     } else {
209         SwitchToStep(&TaskUnzip::StepUnzipProgress);
210     }
211
212     m_installerContext.job->UpdateProgress(
213         InstallerContext::INSTALL_UNZIP_FILES,
214         "Unzip widget files to temporary directory");
215 }
216
217 void TaskUnzip::StepUnzipFinished()
218 {
219     // Unzip finished, close internal structures
220     m_zip.Reset();
221
222     // Done
223     LogInfo("Unzip finished");
224 }
225
226 void TaskUnzip::StepAbort()
227 {
228     removeTemporaryDir(m_installerContext.tempWidgetPath);
229 }
230 } //namespace WidgetInstall
231 } //namespace Jobs