File encryption method reading whole files into a dynamic stack buffer.
[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 PathAndFilePair SplitFileAndPath(const std::string &filePath)
50 {
51     std::string::size_type position = filePath.rfind('/');
52
53     // Is this only a file without a path ?
54     if (position == std::string::npos) {
55         return PathAndFilePair(std::string(), filePath);
56     }
57
58     // This is full file-path pair
59     return PathAndFilePair(filePath.substr(0,
60                                            position),
61                            filePath.substr(position + 1));
62 }
63 }
64
65 namespace Jobs {
66 namespace WidgetInstall {
67 TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
68     DPL::TaskDecl<TaskUnzip>(this),
69     m_installerContext(installerContext)
70 {
71     // Install steps
72     AddStep(&TaskUnzip::StepUnzipPrepare);
73     AddStep(&TaskUnzip::StepUnzipProgress);
74     AddStep(&TaskUnzip::StepUnzipFinished);
75 }
76
77 void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
78                             const std::string &destFileName)
79 {
80     Try
81     {
82         DPL::AbstractWaitableInputAdapter inputAdapter(input);
83         DPL::FileOutput output(destFileName);
84
85         DPL::Copy(&inputAdapter, &output);
86     }
87     Catch(DPL::FileOutput::Exception::OpenFailed)
88     {
89         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
90     }
91     Catch(DPL::CopyFailed)
92     {
93         ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
94     }
95 }
96
97 void TaskUnzip::StepUnzipPrepare()
98 {
99     LogInfo("Prepare to unzip...");
100
101     Try
102     {
103         m_zip.reset(new DPL::ZipInput(m_installerContext.locations->
104                                           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,
110                      m_installerContext.locations->getWidgetSource());
111         }
112
113         // Set iterator to first file
114         m_zipIterator = m_zip->begin();
115     }
116     Catch(DPL::ZipInput::Exception::OpenFailed)
117     {
118         ReThrowMsg(Exceptions::OpenZipFailed,
119                    m_installerContext.locations->getWidgetSource());
120     }
121     Catch(DPL::ZipInput::Exception::SeekFileFailed)
122     {
123         ThrowMsg(Exceptions::ExtractFileFailed,
124                  "m_installerContext.locations->getWidgetSource()");
125     }
126 }
127
128 void TaskUnzip::StepUnzipProgress()
129 {
130     // Show file info
131     LogInfo("Unzipping: '" << m_zipIterator->name <<
132             "', Comment: '" << m_zipIterator->comment <<
133             "', Compressed size: " << m_zipIterator->compressedSize <<
134             ", Uncompressed size: " << m_zipIterator->uncompressedSize);
135
136     // Normalize file paths
137     // FIXME: Implement checking for invalid characters
138
139     // Extract file or path
140     std::string fileName = m_zipIterator->name;
141
142     if (fileName[fileName.size() - 1] == '/') {
143         // This is path
144         std::string newPath =
145             m_installerContext.locations->getTemporaryPackageDir() + "/" +
146             fileName.substr(0, fileName.size() - 1);
147         LogPedantic("Path to extract: " << newPath);
148
149         // Create path in case of it is empty
150         createTempPath(newPath);
151     } else {
152         // This is regular file
153         std::string fileExtractPath =
154             m_installerContext.locations->getTemporaryPackageDir() + "/" +
155             fileName;
156
157         LogPedantic("File to extract: " << fileExtractPath);
158
159         // Split into pat & file pair
160         PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
161
162         LogPedantic("Path and file: " <<
163                     pathAndFile.path <<
164                     " : " << pathAndFile.file);
165
166         // First, ensure that path exists
167         createTempPath(pathAndFile.path);
168
169         Try
170         {
171             // Open file
172             std::unique_ptr<DPL::ZipInput::File> file(
173                 m_zip->OpenFile(fileName));
174
175             // Extract single file
176             ExtractFile(file.get(), fileExtractPath);
177         }
178         Catch(DPL::ZipInput::Exception::OpenFileFailed)
179         {
180             ThrowMsg(Exceptions::ExtractFileFailed, fileName);
181         }
182     }
183
184     // Check whether there are more files to extract
185     if (++m_zipIterator == m_zip->end()) {
186         LogInfo("Unzip progress finished successfuly");
187     } else {
188         SwitchToStep(&TaskUnzip::StepUnzipProgress);
189     }
190
191     m_installerContext.job->UpdateProgress(
192         InstallerContext::INSTALL_UNZIP_FILES,
193         "Unzip widget files to temporary directory");
194 }
195
196 void TaskUnzip::StepUnzipFinished()
197 {
198     // Unzip finished, close internal structures
199     m_zip.reset();
200
201     // Done
202     LogInfo("Unzip finished");
203 }
204 } //namespace WidgetInstall
205 } //namespace Jobs