Update wrt-installer_0.0.54
[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 }
120
121 void TaskUnzip::StepUnzipProgress()
122 {
123     // Show file info
124     LogInfo("Unzipping: '" << m_zipIterator->name <<
125             "', Comment: '" << m_zipIterator->comment <<
126             "', Compressed size: " << m_zipIterator->compressedSize <<
127             ", Uncompressed size: " << m_zipIterator->uncompressedSize);
128
129     // Normalize file paths
130     // FIXME: Implement checking for invalid characters
131
132     // Extract file or path
133     std::string fileName = m_zipIterator->name;
134
135     if (fileName[fileName.size() - 1] == '/') {
136         // This is path
137         std::string newPath = m_installerContext.locations->getTemporaryPackageDir() + "/" +
138             fileName.substr(0, fileName.size() - 1);
139         LogPedantic("Path to extract: " << newPath);
140
141         // Create path in case of it is empty
142         createTempPath(newPath);
143     } else {
144         // This is regular file
145         std::string fileExtractPath =
146             m_installerContext.locations->getTemporaryPackageDir() + "/" + fileName;
147
148         LogPedantic("File to extract: " << fileExtractPath);
149
150         // Split into pat & file pair
151         PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
152
153         LogPedantic("Path and file: " <<
154                     pathAndFile.path <<
155                     " : " << pathAndFile.file);
156
157         // First, ensure that path exists
158         createTempPath(pathAndFile.path);
159
160         Try
161         {
162             // Open file
163             DPL::ScopedPtr<DPL::ZipInput::File> file(
164                 m_zip->OpenFile(fileName));
165
166             // Extract single file
167             ExtractFile(file.Get(), fileExtractPath);
168         }
169         Catch(DPL::ZipInput::Exception::OpenFileFailed)
170         {
171             ThrowMsg(Exceptions::ExtractFileFailed, fileName);
172         }
173     }
174
175     // Check whether there are more files to extract
176     if (++m_zipIterator == m_zip->end()) {
177         LogInfo("Unzip progress finished successfuly");
178     } else {
179         SwitchToStep(&TaskUnzip::StepUnzipProgress);
180     }
181
182     m_installerContext.job->UpdateProgress(
183         InstallerContext::INSTALL_UNZIP_FILES,
184         "Unzip widget files to temporary directory");
185 }
186
187 void TaskUnzip::StepUnzipFinished()
188 {
189     // Unzip finished, close internal structures
190     m_zip.Reset();
191
192     // Done
193     LogInfo("Unzip finished");
194 }
195
196 } //namespace WidgetInstall
197 } //namespace Jobs