Revert "Add send signal if there is not enough memory during web app installation"
[framework/web/wrt-installer.git] / src / jobs / widget_install / widget_unzip.cpp
index 56d1dff..d41b890 100644 (file)
 #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/utils/path.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 <dlfcn.h>
+#include <installer_log.h>
 
 using namespace WrtDB;
 
 namespace {
-const char* const NPRUNTIME_PLUGINS_DIR = "plugins/";
+const char *const DRM_LIB_PATH = "/usr/lib/libdrm-service-core-tizen.so";
 
 struct PathAndFilePair
 {
@@ -62,34 +63,6 @@ PathAndFilePair SplitFileAndPath(const std::string &filePath)
                                            position),
                            filePath.substr(position + 1));
 }
-
-inline bool isNPPlugin(const std::string& filePath)
-{
-    std::string::size_type pos = filePath.find(NPRUNTIME_PLUGINS_DIR);
-    // Not specified if a plug-in name MUST end with a specific extension
-    // (e.g. .so)
-    return ((std::string::npos != pos) &&
-            ('/' != filePath[filePath.length() - 1]));
-}
-
-// precondition: isNPPlugin(filePath) == true
-inline bool isValidNPPlugin(const std::string& filePath)
-{
-    return (filePath.find(GlobalConfig::GetNPRuntimePluginsPath()) == 0);
-}
-
-bool shouldBeInstalled(const std::string& filePath)
-{
-    if (isNPPlugin(filePath))
-    {
-        if (!isValidNPPlugin(filePath))
-        {
-            LogDebug("Not a valid NPRuntime plug-in: " << filePath);
-            return false;
-        }
-    }
-    return true;
-}
 }
 
 namespace Jobs {
@@ -117,10 +90,8 @@ void WidgetUnzip::ExtractFile(DPL::ZipInput::File *input,
 void WidgetUnzip::unzipProgress(const std::string &destination)
 {
     // Show file info
-    LogInfo("Unzipping: '" << m_zipIterator->name <<
-            "', Comment: '" << m_zipIterator->comment <<
-            "', Compressed size: " << m_zipIterator->compressedSize <<
-            ", Uncompressed size: " << m_zipIterator->uncompressedSize);
+    _D("Unzipping: '%s', Comment: '%s', Compressed size: %lld, Uncompressed size: %lld",
+            m_zipIterator->name.c_str(), m_zipIterator->comment.c_str(), m_zipIterator->compressedSize, m_zipIterator->uncompressedSize);
 
     // Normalize file paths
     // FIXME: Implement checking for invalid characters
@@ -128,64 +99,156 @@ void WidgetUnzip::unzipProgress(const std::string &destination)
     // Extract file or path
     std::string fileName = m_zipIterator->name;
 
-    if (shouldBeInstalled(m_zipIterator->name))
-    {
-        if (fileName[fileName.size() - 1] == '/') {
-            // This is path
-            std::string newPath = destination + "/" +
-                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 = destination + "/" + 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);
-            }
+    if (fileName[fileName.size() - 1] == '/') {
+        // This is path
+        std::string newPath = destination + "/" +
+            fileName.substr(0, fileName.size() - 1);
+        _D("Path to extract: %s", newPath.c_str());
+
+        // Create path in case of it is empty
+        Try
+        {
+            DPL::Utils::MakeDir(DPL::Utils::Path(newPath));
+        }
+        Catch(DPL::Utils::Path::BaseException)
+        {
+            ThrowMsg(Exceptions::FileOperationFailed, "Failed to create temporary directory");
+        }
+    } else {
+        // This is regular file
+        std::string fileExtractPath = destination + "/" + fileName;
+
+        _D("File to extract: %s", fileExtractPath.c_str());
+
+        // Split into pat & file pair
+        PathAndFilePair pathAndFile = SplitFileAndPath(fileExtractPath);
+
+        _D("Path and file: %s : %s", pathAndFile.path.c_str(), pathAndFile.file.c_str());
+
+        // First, ensure that path exists
+        Try
+        {
+            DPL::Utils::MakeDir(DPL::Utils::Path(pathAndFile.path));
+        }
+        Catch(DPL::Utils::Path::BaseException)
+        {
+            ThrowMsg(Exceptions::FileOperationFailed, "Failed to create temporary directory");
+        }
+
+        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);
         }
-    }
-    else
-    {
-        LogDebug("Skipping file: " << m_zipIterator->name);
     }
 
     // Check whether there are more files to extract
     if (++m_zipIterator == m_zip->end()) {
-        LogInfo("Unzip progress finished successfuly");
+        _D("Unzip progress finished successfuly");
     } else {
         unzipProgress(destination);
     }
 }
 
-std::string WidgetUnzip::decryptDrmPackage(const std::string &source)
+bool WidgetUnzip::isDRMPackage(const std::string &source)
+{
+    _D("Enter : isDRMPackage()");
+    int ret = 0;
+    void* pHandle = NULL;
+    char* pErrorMsg = NULL;
+    int (*drm_oem_sapps_is_drm_file)(const char* pDcfPath, int dcfPathLen);
+
+    pHandle = dlopen(DRM_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
+    if (!pHandle) {
+        _E("dlopen failed : %s [%s]", source.c_str(), dlerror());
+        return false;
+    }
+
+    // clear existing error
+    dlerror();
+
+    drm_oem_sapps_is_drm_file = reinterpret_cast <int (*)(const char*, int)>
+        (dlsym(pHandle, "drm_oem_sapps_is_drm_file"));
+
+    if ((pErrorMsg = dlerror()) != NULL) {
+        _E("dlsym failed : %s [%s]", source.c_str(), pErrorMsg);
+        dlclose(pHandle);
+        return false;
+    }
+
+    if (drm_oem_sapps_is_drm_file == NULL) {
+        _E("drm_oem_sapps_is_drm_file is NULL : %s", source.c_str());
+        dlclose(pHandle);
+        return false;
+    }
+
+    ret = drm_oem_sapps_is_drm_file(source.c_str(), source.length());
+    dlclose(pHandle);
+    if (1 == ret) {
+        _D("%s is DRM file", source.c_str());
+        return true;
+    }
+    _D("%s isn't DRM file", source.c_str());
+    return false;
+}
+
+bool WidgetUnzip::decryptDRMPackage(const std::string &source, const std::string
+        &decryptedSource)
 {
-    LogInfo("Check DRM...");
-    if (1 == drm_oem_sapps_is_drm_file(source.c_str(), source.length())) {
+    _D("Enter : decryptDRMPackage()");
+    int ret = 0;
+    void* pHandle = NULL;
+    char* pErrorMsg = NULL;
+    int (*drm_oem_sapps_decrypt_package)(const char* pDcfPath, int dcfPathLen,
+            const char* pDecryptedFile, int decryptedFileLen);
+
+    pHandle = dlopen(DRM_LIB_PATH, RTLD_LAZY | RTLD_GLOBAL);
+    if (!pHandle) {
+        _E("dlopen failed : %s [%s]", source.c_str(), dlerror());
+        return false;
+    }
+
+    // clear existing error
+    dlerror();
+
+    drm_oem_sapps_decrypt_package = reinterpret_cast <int (*)(const char*, int,
+            const char*, int)>
+        (dlsym(pHandle, "drm_oem_sapps_decrypt_package"));
+
+    if ((pErrorMsg = dlerror()) != NULL) {
+        _E("dlsym failed : %s [%s]", source.c_str(), pErrorMsg);
+        dlclose(pHandle);
+        return false;
+    }
+
+    if (drm_oem_sapps_decrypt_package == NULL) {
+        _E("drm_oem_sapps_decrypt_package is NULL : %s", source.c_str());
+        dlclose(pHandle);
+        return false;
+    }
+
+    ret = drm_oem_sapps_decrypt_package(source.c_str(), source.length(),
+            decryptedSource.c_str(), decryptedSource.length());
+    dlclose(pHandle);
+    if (1 == ret) {
+        _D("%s is decrypted : %s", source.c_str(), decryptedSource.c_str());
+        return true;
+    }
+    return false;
+}
+
+std::string WidgetUnzip::getDecryptedPackage(const std::string &source)
+{
+    _D("Check DRM...");
+    if (isDRMPackage(source)) {
         std::string decryptedFile;
         size_t found = source.find_last_of(".wgt");
         if (found == std::string::npos) {
@@ -195,10 +258,9 @@ std::string WidgetUnzip::decryptDrmPackage(const std::string &source)
                     1) + "_tmp.wgt";
         }
 
-        LogDebug("decrypted file name : " << decryptedFile);
-        if (1 != drm_oem_sapps_decrypt_package(source.c_str(), source.length(),
-                    decryptedFile.c_str(), decryptedFile.length())) {
-            LogError("Failed decrypt drm file");
+        _D("decrypted file name : %s", decryptedFile.c_str());
+        if (!decryptDRMPackage(source, decryptedFile)) {
+            _E("Failed decrypt drm file");
             ThrowMsg(Exceptions::DrmDecryptFailed, source);
         }
         return decryptedFile;
@@ -208,15 +270,16 @@ std::string WidgetUnzip::decryptDrmPackage(const std::string &source)
 
 void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &destination)
 {
-    LogInfo("Prepare to unzip...");
+    _D("Prepare to unzip...");
     std::string wgtFile;
     Try
     {
-        wgtFile = decryptDrmPackage(source);
-        LogDebug("wgtFile : " << wgtFile);
+        wgtFile = getDecryptedPackage(source);
+        _D("wgtFile : %s", wgtFile.c_str());
 
         m_zip.reset(new DPL::ZipInput(wgtFile));
-        LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
+        _D("Widget package comment: %s", m_zip->GetGlobalComment().c_str());
+
 
         // Widget package must not be empty
         if (m_zip->empty()) {
@@ -232,7 +295,7 @@ void WidgetUnzip::unzipWgtFile(const std::string &source, const std::string &des
         m_zip.reset();
 
         // Done
-        LogInfo("Unzip finished");
+        _D("Unzip finished");
     }
     Catch(DPL::ZipInput::Exception::OpenFailed)
     {