+/*
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file widget_unzip.cpp
+ * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
+ * @version 1.0
+ * @brief Implementation file for installer task unzip
+ */
+#include <widget_install/widget_unzip.h>
+#include <widget_install/widget_install_errors.h>
+#include <widget_install/widget_install_context.h>
+#include <widget_install/job_widget_install.h>
+#include <dpl/log/log.h>
+#include <dpl/copy.h>
+#include <dpl/file_output.h>
+#include <dpl/abstract_waitable_input_adapter.h>
+#include <dpl/wrt-dao-ro/global_config.h>
+#include <task_commons.h>
+#include <sys/stat.h>
+#include <drm_trusted_client.h>
+
+using namespace WrtDB;
+
+namespace {
+struct PathAndFilePair
+{
+ std::string path;
+ std::string file;
+
+ PathAndFilePair(const std::string &p,
+ const std::string &f) :
+ path(p),
+ file(f)
+ {
+ }
+};
+
+PathAndFilePair SplitFileAndPath(const std::string &filePath)
+{
+ std::string::size_type position = filePath.rfind('/');
+
+ // Is this only a file without a path ?
+ if (position == std::string::npos) {
+ return PathAndFilePair(std::string(), filePath);
+ }
+
+ // This is full file-path pair
+ return PathAndFilePair(filePath.substr(0,
+ position),
+ filePath.substr(position + 1));
+}
+}
+
+namespace Jobs {
+namespace WidgetInstall {
+WidgetUnzip::WidgetUnzip(std::string source, std::string path) :
+ m_source(source),
+ m_destPath(path)
+{
+ LogDebug("WidgetUnzip");
+}
+
+void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
+ const std::string &destFileName)
+{
+ Try
+ {
+ DPL::AbstractWaitableInputAdapter inputAdapter(input);
+ DPL::FileOutput output(destFileName);
+
+ DPL::Copy(&inputAdapter, &output);
+ }
+ Catch(DPL::FileOutput::Exception::OpenFailed)
+ {
+ ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
+ }
+ Catch(DPL::CopyFailed)
+ {
+ ReThrowMsg(Exceptions::ExtractFileFailed, destFileName);
+ }
+}
+
+void WidgetUnzip::UnzipWidget()
+{
+ UnzipPrepare();
+ UnzipProgress();
+}
+
+void WidgetUnzip::UnzipPrepare()
+{
+ LogInfo("Prepare to unzip...");
+
+ Try
+ {
+ m_zip.reset(new DPL::ZipInput(m_source));
+ LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
+
+ // Widget package must not be empty
+ if (m_zip->empty()) {
+ ThrowMsg(Exceptions::ZipEmpty, m_source);
+ }
+
+ // Set iterator to first file
+ m_zipIterator = m_zip->begin();
+ }
+ Catch(DPL::ZipInput::Exception::OpenFailed)
+ {
+ ReThrowMsg(Exceptions::OpenZipFailed, m_source);
+ }
+}
+
+void WidgetUnzip::UnzipProgress()
+{
+
+ // Show file info
+ LogInfo("Unzipping: '" << m_zipIterator->name <<
+ "', Comment: '" << m_zipIterator->comment <<
+ "', Compressed size: " << m_zipIterator->compressedSize <<
+ ", Uncompressed size: " << m_zipIterator->uncompressedSize);
+
+
+ // Normalize file paths
+ // FIXME: Implement checking for invalid characters
+
+ // Extract file or path
+ std::string fileName = m_zipIterator->name;
+
+ if (fileName[fileName.size() - 1] == '/') {
+ // This is path
+ std::string newPath = m_destPath + "/" +
+ fileName.substr(0, fileName.size() - 1);
+ LogPedantic("Path to extract: " << newPath);
+
+ // Create path in case of it is empty
+ createTempPath(newPath);
+ } else {
+ // This is regular file
+ std::string fileExtractPath = m_destPath + "/" + fileName;
+
+ LogPedantic("File to extract: " << fileExtractPath);
+
+ // Split into pat & file pair
+ PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
+
+ LogPedantic("Path and file: " <<
+ pathAndFile.path <<
+ " : " << pathAndFile.file);
+
+ // First, ensure that path exists
+ createTempPath(pathAndFile.path);
+
+ Try
+ {
+ // Open file
+ std::unique_ptr<DPL::ZipInput::File> file(
+ m_zip->OpenFile(fileName));
+
+ // Extract single file
+ ExtractFile(file.get(), fileExtractPath);
+ }
+ Catch(DPL::ZipInput::Exception::OpenFileFailed)
+ {
+ ThrowMsg(Exceptions::ExtractFileFailed, fileName);
+ }
+ }
+
+ // Check whether there are more files to extract
+ if (++m_zipIterator == m_zip->end()) {
+ LogInfo("Unzip progress finished successfuly");
+ m_zip.reset();
+ } else {
+ UnzipProgress();
+ }
+}
+
+void WidgetUnzip::UnzipDRMWidget()
+{
+ LogDebug("Decrypt drm widget");
+ drm_trusted_sapps_decrypt_package_info_s package_info;
+
+ strncpy(package_info.sadcf_filepath, m_source.c_str(),
+ sizeof(package_info.sadcf_filepath));
+ strncpy(package_info.decrypt_filepath, m_destPath.c_str(),
+ sizeof(package_info.decrypt_filepath));
+
+ drm_trusted_request_type_e requestType =
+ DRM_TRUSTED_REQ_TYPE_SAPPS_DECRYPT_PACKAGE;
+
+ int ret = drm_trusted_handle_request(requestType,
+ (void *)&package_info, NULL);
+ if (DRM_TRUSTED_RETURN_SUCCESS != ret) {
+ LogDebug("Failed unzip drm widget");
+ ThrowMsg(Exceptions::ExtractFileFailed, m_destPath);
+ }
+}
+} //namespace WidgetInstall
+} //namespace Jobs