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/utils/path.h"
29 #include <dpl/abstract_waitable_input_adapter.h>
30 #include <dpl/wrt-dao-ro/global_config.h>
32 #include <sys/statvfs.h>
34 #include <installer_log.h>
36 using namespace WrtDB;
39 const char *const DRM_LIB_PATH = "/usr/lib/libdrm-service-core-tizen.so";
40 const size_t SPACE_SIZE = 1024 * 1024;
42 struct PathAndFilePair
47 PathAndFilePair(const std::string &p,
48 const std::string &f) :
54 PathAndFilePair SplitFileAndPath(const std::string &filePath)
56 std::string::size_type position = filePath.rfind('/');
58 // Is this only a file without a path ?
59 if (position == std::string::npos) {
60 return PathAndFilePair(std::string(), filePath);
63 // This is full file-path pair
64 return PathAndFilePair(filePath.substr(0,
66 filePath.substr(position + 1));
71 namespace WidgetInstall {
72 void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
73 const std::string &destFileName)
77 DPL::AbstractWaitableInputAdapter inputAdapter(input);
78 DPL::FileOutput output(destFileName);
80 DPL::Copy(&inputAdapter, &output);
82 Catch(DPL::FileOutput::Exception::OpenFailed)
84 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
86 Catch(DPL::CopyFailed)
88 ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
92 void WidgetUnzip::unzipProgress(const std::string &destination)
95 _D("Unzipping: '%s', Comment: '%s', Compressed size: %lld, Uncompressed size: %lld",
96 m_zipIterator->name.c_str(), m_zipIterator->comment.c_str(), m_zipIterator->compressedSize, 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 _D("Path to extract: %s", newPath.c_str());
110 // Create path in case of it is empty
113 DPL::Utils::MakeDir(DPL::Utils::Path(newPath));
115 Catch(DPL::Utils::Path::BaseException)
117 ThrowMsg(Exceptions::FileOperationFailed, "Failed to create temporary directory");
120 // This is regular file
121 std::string fileExtractPath = destination + "/" + fileName;
123 _D("File to extract: %s", fileExtractPath.c_str());
125 // Split into pat & file pair
126 PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
128 _D("Path and file: %s : %s", pathAndFile.path.c_str(), pathAndFile.file.c_str());
130 // First, ensure that path exists
133 DPL::Utils::MakeDir(DPL::Utils::Path(pathAndFile.path));
135 Catch(DPL::Utils::Path::BaseException)
137 ThrowMsg(Exceptions::FileOperationFailed, "Failed to create temporary directory");
143 std::unique_ptr<DPL::ZipInput::File> file(
144 m_zip->OpenFile(fileName));
146 // Extract single file
147 ExtractFile(file.get(), fileExtractPath);
149 Catch(DPL::ZipInput::Exception::OpenFileFailed)
151 ThrowMsg(Exceptions::ExtractFileFailed, fileName);
155 // Check whether there are more files to extract
156 if (++m_zipIterator == m_zip->end()) {
157 _D("Unzip progress finished successfuly");
159 unzipProgress(destination);
163 bool WidgetUnzip::isDRMPackage(const std::string &source)
165 _D("Enter : isDRMPackage()");
167 void* pHandle = NULL;
168 char* pErrorMsg = NULL;
169 int (*drm_oem_sapps_is_drm_file)(const char* pDcfPath, int dcfPathLen);
171 pHandle = dlopen(DRM_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
173 _E("dlopen failed : %s [%s]", source.c_str(), dlerror());
177 // clear existing error
180 drm_oem_sapps_is_drm_file = reinterpret_cast <int (*)(const char*, int)>
181 (dlsym(pHandle, "drm_oem_sapps_is_drm_file"));
183 if ((pErrorMsg = dlerror()) != NULL) {
184 _E("dlsym failed : %s [%s]", source.c_str(), pErrorMsg);
189 if (drm_oem_sapps_is_drm_file == NULL) {
190 _E("drm_oem_sapps_is_drm_file is NULL : %s", source.c_str());
195 ret = drm_oem_sapps_is_drm_file(source.c_str(), source.length());
198 _D("%s is DRM file", source.c_str());
201 _D("%s isn't DRM file", source.c_str());
205 bool WidgetUnzip::decryptDRMPackage(const std::string &source, const std::string
208 _D("Enter : decryptDRMPackage()");
210 void* pHandle = NULL;
211 char* pErrorMsg = NULL;
212 int (*drm_oem_sapps_decrypt_package)(const char* pDcfPath, int dcfPathLen,
213 const char* pDecryptedFile, int decryptedFileLen);
215 pHandle = dlopen(DRM_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
217 _E("dlopen failed : %s [%s]", source.c_str(), dlerror());
221 // clear existing error
224 drm_oem_sapps_decrypt_package = reinterpret_cast <int (*)(const char*, int,
226 (dlsym(pHandle, "drm_oem_sapps_decrypt_package"));
228 if ((pErrorMsg = dlerror()) != NULL) {
229 _E("dlsym failed : %s [%s]", source.c_str(), pErrorMsg);
234 if (drm_oem_sapps_decrypt_package == NULL) {
235 _E("drm_oem_sapps_decrypt_package is NULL : %s", source.c_str());
240 ret = drm_oem_sapps_decrypt_package(source.c_str(), source.length(),
241 decryptedSource.c_str(), decryptedSource.length());
244 _D("%s is decrypted : %s", source.c_str(), decryptedSource.c_str());
250 std::string WidgetUnzip::getDecryptedPackage(const std::string &source)
253 if (isDRMPackage(source)) {
254 std::string decryptedFile;
255 size_t found = source.find_last_of(".wgt");
256 if (found == std::string::npos) {
257 decryptedFile += source + "_tmp.wgt";
259 decryptedFile += source.substr(0, source.find_last_not_of(".wgt") +
263 _D("decrypted file name : %s", decryptedFile.c_str());
264 if (!decryptDRMPackage(source, decryptedFile)) {
265 _E("Failed decrypt drm file");
266 ThrowMsg(Exceptions::DrmDecryptFailed, source);
268 return decryptedFile;
273 void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &destination)
275 _D("Prepare to unzip...");
279 wgtFile = getDecryptedPackage(source);
280 _D("wgtFile : %s", wgtFile.c_str());
282 m_zip.reset(new DPL::ZipInput(wgtFile));
283 _D("Widget package comment: %s", m_zip->GetGlobalComment().c_str());
286 // Widget package must not be empty
287 if (m_zip->empty()) {
288 ThrowMsg(Exceptions::ZipEmpty, wgtFile);
291 checkAvailableSpace(destination);
293 // Set iterator to first file
294 m_zipIterator = m_zip->begin();
296 unzipProgress(destination);
298 // Unzip finished, close internal structures
302 _D("Unzip finished");
304 Catch(DPL::ZipInput::Exception::OpenFailed)
306 ReThrowMsg(Exceptions::OpenZipFailed, wgtFile);
308 Catch(DPL::ZipInput::Exception::SeekFileFailed)
310 ThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
312 Catch(Exceptions::DrmDecryptFailed)
314 ReThrowMsg(Exceptions::ExtractFileFailed, wgtFile);
318 void WidgetUnzip::checkAvailableSpace(const std::string &destination)
320 _D("checkAvailableSpace ... ");
322 double unCompressedSize = m_zip->GetTotalUncompressedSize();
323 _D("unCompressedSize : %ld", unCompressedSize);
326 if (-1 == statvfs(destination.c_str(), &vfs)) {
327 ThrowMsg(Exceptions::OutOfStorageFailed, "There is no space for installation");
330 double freeSize = (double)vfs.f_bsize * vfs.f_bavail;
331 _D("Space Size : %ld", freeSize);
333 if (unCompressedSize + SPACE_SIZE >= freeSize) {
334 ThrowMsg(Exceptions::OutOfStorageFailed, "There is no space for installation");
338 } //namespace WidgetInstall