upload tizen1.0 source
[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 <dpl/log/log.h>
26 #include <dpl/copy.h>
27 #include <dpl/file_output.h>
28 #include <dpl/abstract_waitable_input_adapter.h>
29 #include <task_commons.h>
30
31 namespace {
32
33 struct PathAndFilePair
34 {
35     std::string path;
36     std::string file;
37
38     PathAndFilePair(const std::string &p,
39             const std::string &f) :
40         path(p),
41         file(f)
42     {
43     }
44 };
45
46 PathAndFilePair SplitFileAndPath(const std::string &filePath)
47 {
48     std::string::size_type position = filePath.rfind('/');
49
50     // Is this only a file without a path ?
51     if (position == std::string::npos) {
52         return PathAndFilePair(std::string(), filePath);
53     }
54
55     // This is full file-path pair
56     return PathAndFilePair(filePath.substr(0,
57                                            position),
58                            filePath.substr(position + 1));
59 }
60
61 }
62
63 namespace Jobs {
64 namespace WidgetInstall {
65 TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
66     DPL::TaskDecl<TaskUnzip>(this),
67     m_installerContext(installerContext)
68 {
69     // Install steps
70     AddStep(&TaskUnzip::StepCreateTempPath);
71     AddStep(&TaskUnzip::StepUnzipPrepare);
72     AddStep(&TaskUnzip::StepUnzipProgress);
73     AddStep(&TaskUnzip::StepUnzipFinished);
74
75     AddAbortStep(&TaskUnzip::StepAbort);
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::StepCreateTempPath()
99 {
100     // Step succedded, save temporary widget path
101     m_installerContext.tempWidgetPath = createTempPath();
102 }
103
104 void TaskUnzip::StepUnzipPrepare()
105 {
106     LogInfo("Prepare to unzip...");
107
108     Try
109     {
110         m_zip.Reset(new DPL::ZipInput(m_installerContext.widgetSource));
111         LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
112
113         // Widget package must not be empty
114         if (m_zip->empty()) {
115             ThrowMsg(Exceptions::ZipEmpty, m_installerContext.widgetSource);
116         }
117
118         // Set iterator to first file
119         m_zipIterator = m_zip->begin();
120     }
121     Catch(DPL::ZipInput::Exception::OpenFailed)
122     {
123         ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.widgetSource);
124     }
125 }
126
127 void TaskUnzip::StepUnzipProgress()
128 {
129     // Show file info
130     LogInfo("Unzipping: '" << m_zipIterator->name <<
131             "', Comment: '" << m_zipIterator->comment <<
132             "', Compressed size: " << m_zipIterator->compressedSize <<
133             ", Uncompressed size: " << m_zipIterator->uncompressedSize);
134
135     // Normalize file paths
136     // FIXME: Implement checking for invalid characters
137
138     // Extract file or path
139     std::string fileName = m_zipIterator->name;
140
141     if (fileName[fileName.size() - 1] == '/') {
142         // This is path
143         std::string newPath = m_installerContext.tempWidgetPath + "/" +
144             fileName.substr(0, fileName.size() - 1);
145         LogPedantic("Path to extract: " << newPath);
146
147         // Create path in case of it is empty
148         createTempPath(newPath);
149     } else {
150         // This is regular file
151         std::string fileExtractPath =
152             m_installerContext.tempWidgetPath + "/" + fileName;
153
154         LogPedantic("File to extract: " << fileExtractPath);
155
156         // Split into pat & file pair
157         PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
158
159         LogPedantic("Path and file: " <<
160                     pathAndFile.path <<
161                     " : " << pathAndFile.file);
162
163         // First, ensure that path exists
164         createTempPath(pathAndFile.path);
165
166         Try
167         {
168             // Open file
169             DPL::ScopedPtr<DPL::ZipInput::File> file(
170                 m_zip->OpenFile(fileName));
171
172             // Extract single file
173             ExtractFile(file.Get(), fileExtractPath);
174         }
175         Catch(DPL::ZipInput::Exception::OpenFileFailed)
176         {
177             ThrowMsg(Exceptions::ExtractFileFailed, fileName);
178         }
179     }
180
181     // Check whether there are more files to extract
182     if (++m_zipIterator == m_zip->end()) {
183         LogInfo("Unzip progress finished successfuly");
184     } else {
185         SwitchToStep(&TaskUnzip::StepUnzipProgress);
186     }
187 }
188
189 void TaskUnzip::StepUnzipFinished()
190 {
191     // Unzip finished, close internal structures
192     m_zip.Reset();
193
194     // Done
195     LogInfo("Unzip finished");
196 }
197
198 void TaskUnzip::StepAbort()
199 {
200     removeTemporaryDir(m_installerContext.tempWidgetPath);
201 }
202 } //namespace WidgetInstall
203 } //namespace Jobs