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>
35 using namespace WrtDB;
38 const char *const DRM_LIB_PATH = "/usr/lib/libdrm-service-core-tizen.so";
40 struct PathAndFilePair
45 PathAndFilePair(const std::string &p,
46 const std::string &f) :
52 PathAndFilePair SplitFileAndPath(const std::string &filePath)
54 std::string::size_type position = filePath.rfind('/');
56 // Is this only a file without a path ?
57 if (position == std::string::npos) {
58 return PathAndFilePair(std::string(), filePath);
61 // This is full file-path pair
62 return PathAndFilePair(filePath.substr(0,
64 filePath.substr(position + 1));
69 namespace WidgetInstall {
70 void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
71 const std::string &destFileName)
75 DPL::AbstractWaitableInputAdapter inputAdapter(input);
76 DPL::FileOutput output(destFileName);
78 DPL::Copy(&inputAdapter, &output);
80 Catch(DPL::FileOutput::Exception::OpenFailed)
82 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
84 Catch(DPL::CopyFailed)
86 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
90 void WidgetUnzip::unzipProgress(const std::string &destination)
93 LogDebug("Unzipping: '" << m_zipIterator->name <<
94 "', Comment: '" << m_zipIterator->comment <<
95 "', Compressed size: " << m_zipIterator->compressedSize <<
96 ", Uncompressed size: " << m_zipIterator->uncompressedSize);
98 // Normalize file paths
99 // FIXME: Implement checking for invalid characters
101 // Extract file or path
102 std::string fileName = m_zipIterator->name;
104 if (fileName[fileName.size() - 1] == '/') {
106 std::string newPath = destination + "/" +
107 fileName.substr(0, fileName.size() - 1);
108 LogPedantic("Path to extract: " << newPath);
110 // Create path in case of it is empty
111 createTempPath(newPath);
113 // This is regular file
114 std::string fileExtractPath = destination + "/" + fileName;
116 LogPedantic("File to extract: " << fileExtractPath);
118 // Split into pat & file pair
119 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
121 LogPedantic("Path and file: " <<
123 " : " << pathAndFile.file);
125 // First, ensure that path exists
126 createTempPath(pathAndFile.path);
131 std::unique_ptr<DPL::ZipInput::File> file(
132 m_zip->OpenFile(fileName));
134 // Extract single file
135 ExtractFile(file.get(), fileExtractPath);
137 Catch(DPL::ZipInput::Exception::OpenFileFailed)
139 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
143 // Check whether there are more files to extract
144 if (++m_zipIterator == m_zip->end()) {
145 LogDebug("Unzip progress finished successfuly");
147 unzipProgress(destination);
151 bool WidgetUnzip::isDRMPackage(const std::string &source)
153 LogDebug("Enter : isDRMPackage()");
155 void* pHandle = NULL;
156 char* pErrorMsg = NULL;
157 int (*drm_oem_sapps_is_drm_file)(const char* pDcfPath, int dcfPathLen);
159 pHandle = dlopen(DRM_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
161 LogError("dlopen failed : " << source << " [" << dlerror() << "]");
165 drm_oem_sapps_is_drm_file = reinterpret_cast <int (*)(const char*, int)>
166 (dlsym(pHandle, "drm_oem_sapps_is_drm_file"));
167 pErrorMsg = dlerror();
168 if ((pErrorMsg != NULL) || (drm_oem_sapps_is_drm_file == NULL)) {
169 LogError("dlopen failed : " << source << " [" << dlerror() << "]");
174 ret = drm_oem_sapps_is_drm_file(source.c_str(), source.length());
177 LogDebug(source << " is DRM file");
180 LogDebug(source << " isn't DRM file");
184 bool WidgetUnzip::decryptDRMPackage(const std::string &source, const std::string
187 LogDebug("Enter : decryptDRMPackage()");
189 void* pHandle = NULL;
190 char* pErrorMsg = NULL;
191 int (*drm_oem_sapps_decrypt_package)(const char* pDcfPath, int dcfPathLen,
192 const char* pDecryptedFile, int decryptedFileLen);
194 pHandle = dlopen(DRM_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
196 LogError("dlopen failed : " << source << " [" << dlerror() << "]");
200 drm_oem_sapps_decrypt_package = reinterpret_cast <int (*)(const char*, int,
202 (dlsym(pHandle, "drm_oem_sapps_decrypt_package"));
203 pErrorMsg = dlerror();
204 if ((pErrorMsg != NULL) || (drm_oem_sapps_decrypt_package == NULL)) {
205 LogError("dlopen failed : " << source << " [" << dlerror() << "]");
210 ret = drm_oem_sapps_decrypt_package(source.c_str(), source.length(),
211 decryptedSource.c_str(), decryptedSource.length());
214 LogDebug(source << " is decrypted : " << decryptedSource);
220 std::string WidgetUnzip::getDecryptedPackage(const std::string &source)
222 LogDebug("Check DRM...");
223 if (isDRMPackage(source)) {
224 std::string decryptedFile;
225 size_t found = source.find_last_of(".wgt");
226 if (found == std::string::npos) {
227 decryptedFile += source + "_tmp.wgt";
229 decryptedFile += source.substr(0, source.find_last_not_of(".wgt") +
233 LogDebug("decrypted file name : " << decryptedFile);
234 if (!decryptDRMPackage(source, decryptedFile)) {
235 LogError("Failed decrypt drm file");
236 ThrowMsg(Exceptions::DrmDecryptFailed, source);
238 return decryptedFile;
243 void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &destination)
245 LogDebug("Prepare to unzip...");
249 wgtFile = getDecryptedPackage(source);
250 LogDebug("wgtFile : " << wgtFile);
252 m_zip.reset(new DPL::ZipInput(wgtFile));
253 LogDebug("Widget package comment: " << m_zip->GetGlobalComment());
255 // Widget package must not be empty
256 if (m_zip->empty()) {
257 ThrowMsg(Exceptions::ZipEmpty, wgtFile);
260 // Set iterator to first file
261 m_zipIterator = m_zip->begin();
263 unzipProgress(destination);
265 // Unzip finished, close internal structures
269 LogDebug("Unzip finished");
271 Catch(DPL::ZipInput::Exception::OpenFailed)
273 ReThrowMsg(Exceptions::OpenZipFailed, wgtFile);
275 Catch(DPL::ZipInput::Exception::SeekFileFailed)
277 ThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
279 Catch(Exceptions::DrmDecryptFailed)
281 ReThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
285 } //namespace WidgetInstall