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>
27 #include <dpl/file_output.h>
28 #include <dpl/abstract_waitable_input_adapter.h>
29 #include <dpl/wrt-dao-ro/global_config.h>
30 #include <task_commons.h>
33 #include <installer_log.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 _D("Unzipping: '%s', Comment: '%s', Compressed size: %lld, Uncompressed size: %lld",
94 m_zipIterator->name.c_str(), m_zipIterator->comment.c_str(), m_zipIterator->compressedSize, m_zipIterator->uncompressedSize);
96 // Normalize file paths
97 // FIXME: Implement checking for invalid characters
99 // Extract file or path
100 std::string fileName = m_zipIterator->name;
102 if (fileName[fileName.size() - 1] == '/') {
104 std::string newPath = destination + "/" +
105 fileName.substr(0, fileName.size() - 1);
106 _D("Path to extract: %s", newPath.c_str());
108 // Create path in case of it is empty
109 createTempPath(newPath);
111 // This is regular file
112 std::string fileExtractPath = destination + "/" + fileName;
114 _D("File to extract: %s", fileExtractPath.c_str());
116 // Split into pat & file pair
117 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
119 _D("Path and file: %s : %s", pathAndFile.path.c_str(), pathAndFile.file.c_str());
121 // First, ensure that path exists
122 createTempPath(pathAndFile.path);
127 std::unique_ptr<DPL::ZipInput::File> file(
128 m_zip->OpenFile(fileName));
130 // Extract single file
131 ExtractFile(file.get(), fileExtractPath);
133 Catch(DPL::ZipInput::Exception::OpenFileFailed)
135 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
139 // Check whether there are more files to extract
140 if (++m_zipIterator == m_zip->end()) {
141 _D("Unzip progress finished successfuly");
143 unzipProgress(destination);
147 bool WidgetUnzip::isDRMPackage(const std::string &source)
149 _D("Enter : isDRMPackage()");
151 void* pHandle = NULL;
152 char* pErrorMsg = NULL;
153 int (*drm_oem_sapps_is_drm_file)(const char* pDcfPath, int dcfPathLen);
155 pHandle = dlopen(DRM_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
157 _E("dlopen failed : %s [%s]", source.c_str(), dlerror());
161 drm_oem_sapps_is_drm_file = reinterpret_cast <int (*)(const char*, int)>
162 (dlsym(pHandle, "drm_oem_sapps_is_drm_file"));
163 pErrorMsg = dlerror();
164 if ((pErrorMsg != NULL) || (drm_oem_sapps_is_drm_file == NULL)) {
165 _E("dlopen failed : %s [%s]", source.c_str(), pErrorMsg);
170 ret = drm_oem_sapps_is_drm_file(source.c_str(), source.length());
173 _D("%s is DRM file", source.c_str());
176 _D("%s isn't DRM file", source.c_str());
180 bool WidgetUnzip::decryptDRMPackage(const std::string &source, const std::string
183 _D("Enter : decryptDRMPackage()");
185 void* pHandle = NULL;
186 char* pErrorMsg = NULL;
187 int (*drm_oem_sapps_decrypt_package)(const char* pDcfPath, int dcfPathLen,
188 const char* pDecryptedFile, int decryptedFileLen);
190 pHandle = dlopen(DRM_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
192 _E("dlopen failed : %s [%s]", source.c_str(), dlerror());
196 drm_oem_sapps_decrypt_package = reinterpret_cast <int (*)(const char*, int,
198 (dlsym(pHandle, "drm_oem_sapps_decrypt_package"));
199 pErrorMsg = dlerror();
200 if ((pErrorMsg != NULL) || (drm_oem_sapps_decrypt_package == NULL)) {
201 _E("dlopen failed : %s [%s]", source.c_str(), pErrorMsg);
206 ret = drm_oem_sapps_decrypt_package(source.c_str(), source.length(),
207 decryptedSource.c_str(), decryptedSource.length());
210 _D("%s is decrypted : %s", source.c_str(), decryptedSource.c_str());
216 std::string WidgetUnzip::getDecryptedPackage(const std::string &source)
219 if (isDRMPackage(source)) {
220 std::string decryptedFile;
221 size_t found = source.find_last_of(".wgt");
222 if (found == std::string::npos) {
223 decryptedFile += source + "_tmp.wgt";
225 decryptedFile += source.substr(0, source.find_last_not_of(".wgt") +
229 _D("decrypted file name : %s", decryptedFile.c_str());
230 if (!decryptDRMPackage(source, decryptedFile)) {
231 _E("Failed decrypt drm file");
232 ThrowMsg(Exceptions::DrmDecryptFailed, source);
234 return decryptedFile;
239 void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &destination)
241 _D("Prepare to unzip...");
245 wgtFile = getDecryptedPackage(source);
246 _D("wgtFile : %s", wgtFile.c_str());
248 m_zip.reset(new DPL::ZipInput(wgtFile));
249 _D("Widget package comment: %s", m_zip->GetGlobalComment().c_str());
252 // Widget package must not be empty
253 if (m_zip->empty()) {
254 ThrowMsg(Exceptions::ZipEmpty, wgtFile);
257 // Set iterator to first file
258 m_zipIterator = m_zip->begin();
260 unzipProgress(destination);
262 // Unzip finished, close internal structures
266 _D("Unzip finished");
268 Catch(DPL::ZipInput::Exception::OpenFailed)
270 ReThrowMsg(Exceptions::OpenZipFailed, wgtFile);
272 Catch(DPL::ZipInput::Exception::SeekFileFailed)
274 ThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
276 Catch(Exceptions::DrmDecryptFailed)
278 ReThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
282 } //namespace WidgetInstall