2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file widget_unzip.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief Implementation file for installer widget unzip
22 #include <widget_install/widget_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>
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>
34 using namespace WrtDB;
37 const char* const NPRUNTIME_PLUGINS_DIR = "plugins/";
39 struct PathAndFilePair
44 PathAndFilePair(const std::string &p,
45 const std::string &f) :
51 PathAndFilePair SplitFileAndPath(const std::string &filePath)
53 std::string::size_type position = filePath.rfind('/');
55 // Is this only a file without a path ?
56 if (position == std::string::npos) {
57 return PathAndFilePair(std::string(), filePath);
60 // This is full file-path pair
61 return PathAndFilePair(filePath.substr(0,
63 filePath.substr(position + 1));
66 inline bool isNPPlugin(const std::string& filePath)
68 std::string::size_type pos = filePath.find(NPRUNTIME_PLUGINS_DIR);
69 // Not specified if a plug-in name MUST end with a specific extension
71 return ((std::string::npos != pos) &&
72 ('/' != filePath[filePath.length() - 1]));
75 // precondition: isNPPlugin(filePath) == true
76 inline bool isValidNPPlugin(const std::string& filePath)
78 return (filePath.find(GlobalConfig::GetNPRuntimePluginsPath()) == 0);
81 bool shouldBeInstalled(const std::string& filePath)
83 if (isNPPlugin(filePath))
85 if (!isValidNPPlugin(filePath))
87 LogDebug("Not a valid NPRuntime plug-in: " << filePath);
96 namespace WidgetInstall {
97 void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
98 const std::string &destFileName)
102 DPL::AbstractWaitableInputAdapter inputAdapter(input);
103 DPL::FileOutput output(destFileName);
105 DPL::Copy(&inputAdapter, &output);
107 Catch(DPL::FileOutput::Exception::OpenFailed)
109 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
111 Catch(DPL::CopyFailed)
113 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
117 void WidgetUnzip::unzipProgress(const std::string &destination)
120 LogInfo("Unzipping: '" << m_zipIterator->name <<
121 "', Comment: '" << m_zipIterator->comment <<
122 "', Compressed size: " << m_zipIterator->compressedSize <<
123 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
125 // Normalize file paths
126 // FIXME: Implement checking for invalid characters
128 // Extract file or path
129 std::string fileName = m_zipIterator->name;
131 if (shouldBeInstalled(m_zipIterator->name))
133 if (fileName[fileName.size() - 1] == '/') {
135 std::string newPath = destination + "/" +
136 fileName.substr(0, fileName.size() - 1);
137 LogPedantic("Path to extract: " << newPath);
139 // Create path in case of it is empty
140 createTempPath(newPath);
142 // This is regular file
143 std::string fileExtractPath = destination + "/" + fileName;
145 LogPedantic("File to extract: " << fileExtractPath);
147 // Split into pat & file pair
148 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
150 LogPedantic("Path and file: " <<
152 " : " << pathAndFile.file);
154 // First, ensure that path exists
155 createTempPath(pathAndFile.path);
160 std::unique_ptr<DPL::ZipInput::File> file(
161 m_zip->OpenFile(fileName));
163 // Extract single file
164 ExtractFile(file.get(), fileExtractPath);
166 Catch(DPL::ZipInput::Exception::OpenFileFailed)
168 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
174 LogDebug("Skipping file: " << m_zipIterator->name);
177 // Check whether there are more files to extract
178 if (++m_zipIterator == m_zip->end()) {
179 LogInfo("Unzip progress finished successfuly");
181 unzipProgress(destination);
185 std::string WidgetUnzip::decryptDrmPackage(const std::string &source)
187 LogInfo("Check DRM...");
192 void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &destination)
194 LogInfo("Prepare to unzip...");
198 wgtFile = decryptDrmPackage(source);
199 LogDebug("wgtFile : " << wgtFile);
201 m_zip.reset(new DPL::ZipInput(wgtFile));
202 LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
204 // Widget package must not be empty
205 if (m_zip->empty()) {
206 ThrowMsg(Exceptions::ZipEmpty, wgtFile);
209 // Set iterator to first file
210 m_zipIterator = m_zip->begin();
212 unzipProgress(destination);
214 // Unzip finished, close internal structures
218 LogInfo("Unzip finished");
220 Catch(DPL::ZipInput::Exception::OpenFailed)
222 ReThrowMsg(Exceptions::OpenZipFailed, wgtFile);
224 Catch(DPL::ZipInput::Exception::SeekFileFailed)
226 ThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
228 Catch(Exceptions::DrmDecryptFailed)
230 ReThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
234 } //namespace WidgetInstall