Tizen 2.1 base
[platform/framework/web/wrt-installer.git] / src / jobs / widget_install / task_unzip.cpp
index 1da1176..fb084f6 100644 (file)
 #include <widget_install/task_unzip.h>
 #include <widget_install/widget_install_errors.h>
 #include <widget_install/widget_install_context.h>
-#include <dpl/wrt-dao-ro/global_config.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/errno_string.h>
-#include <unistd.h>
+#include <dpl/wrt-dao-ro/global_config.h>
+#include <task_commons.h>
 #include <sys/stat.h>
-#include <sys/time.h>
-#include <errno.h>
-#include <ftw.h>
-#include <dpl/utils/file_utils.h>
 
 using namespace WrtDB;
 
-namespace // anonymous
-{
-const char * const TEMPORARY_PATH_POSTFIX = "temp";
-const mode_t TEMPORARY_PATH_MODE = 0775;
-
+namespace {
 struct PathAndFilePair
 {
     std::string path;
@@ -69,31 +61,7 @@ PathAndFilePair SplitFileAndPath(const std::string &filePath)
                                            position),
                            filePath.substr(position + 1));
 }
-
-static int lambdaDeleteFile(const char *fpath,
-        const struct stat *sb,
-        int tflag,
-        struct FTW *ftwbuf)
-{
-    (void)sb;
-    (void)ftwbuf;
-
-    switch (tflag) {
-    case FTW_D:
-    case FTW_DNR:
-    case FTW_DP:
-        LogInfo("Removing old temporary directory" << fpath);
-        return rmdir(fpath);
-        break;
-    default:
-        LogInfo("Unlinking old temporary file" << fpath);
-        return unlink(fpath);
-        break;
-    }
-
-    return 0;
 }
-} // namespace anonymous
 
 namespace Jobs {
 namespace WidgetInstall {
@@ -102,12 +70,9 @@ TaskUnzip::TaskUnzip(InstallerContext &installerContext) :
     m_installerContext(installerContext)
 {
     // Install steps
-    AddStep(&TaskUnzip::StepCreateTempPath);
     AddStep(&TaskUnzip::StepUnzipPrepare);
     AddStep(&TaskUnzip::StepUnzipProgress);
     AddStep(&TaskUnzip::StepUnzipFinished);
-
-    AddAbortStep(&TaskUnzip::StepAbort);
 }
 
 void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
@@ -130,62 +95,18 @@ void TaskUnzip::ExtractFile(DPL::ZipInput::File *input,
     }
 }
 
-void TaskUnzip::StepCreateTempPath()
-{
-    LogInfo("Step: Creating temporary path");
-
-    // Temporary path
-    std::ostringstream tempPathBuilder;
-
-    tempPathBuilder << GlobalConfig::GetUserInstalledWidgetPath();
-    tempPathBuilder << "/";
-    tempPathBuilder << "widget";
-    tempPathBuilder << "/";
-    tempPathBuilder << TEMPORARY_PATH_POSTFIX;
-    tempPathBuilder << "_";
-
-    timeval tv;
-    gettimeofday(&tv, NULL);
-    tempPathBuilder <<
-    (static_cast<unsigned long long>(tv.tv_sec) * 1000000ULL +
-     static_cast<unsigned long long>(tv.tv_usec));
-
-    std::string tempPath = tempPathBuilder.str();
-
-    // Remove old path if any
-    struct stat fileInfo;
-
-    // FIXME: what if there are more then maxDepth recursive directories
-    static const int maxDepth = 1024;
-    if (stat(tempPath.c_str(), &fileInfo) == 0) {
-        int error = nftw(
-                tempPath.c_str(), lambdaDeleteFile, maxDepth, FTW_DEPTH);
-
-        if (error == -1) {
-            ThrowMsg(DPL::CommonException::InternalError,
-                     DPL::GetErrnoString());
-        }
-    }
-    // Create new path
-    FileUtils::MakePath(tempPath, TEMPORARY_PATH_MODE);
-
-    // Step succedded, save temporary widget path
-    m_installerContext.tempWidgetPath = tempPath;
-    m_installerContext.unzipStarted = true;
-}
-
 void TaskUnzip::StepUnzipPrepare()
 {
     LogInfo("Prepare to unzip...");
 
     Try
     {
-        m_zip.Reset(new DPL::ZipInput(m_installerContext.widgetFilePath));
+        m_zip.reset(new DPL::ZipInput(m_installerContext.locations->getWidgetSource()));
         LogInfo("Widget package comment: " << m_zip->GetGlobalComment());
 
         // Widget package must not be empty
         if (m_zip->empty()) {
-            ThrowMsg(Exceptions::ZipEmpty, m_installerContext.widgetFilePath);
+            ThrowMsg(Exceptions::ZipEmpty, m_installerContext.locations->getWidgetSource());
         }
 
         // Set iterator to first file
@@ -193,7 +114,11 @@ void TaskUnzip::StepUnzipPrepare()
     }
     Catch(DPL::ZipInput::Exception::OpenFailed)
     {
-        ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.widgetFilePath);
+        ReThrowMsg(Exceptions::OpenZipFailed, m_installerContext.locations->getWidgetSource());
+    }
+    Catch(DPL::ZipInput::Exception::SeekFileFailed)
+    {
+        ThrowMsg(Exceptions::ExtractFileFailed,"m_installerContext.locations->getWidgetSource()");
     }
 }
 
@@ -213,16 +138,16 @@ void TaskUnzip::StepUnzipProgress()
 
     if (fileName[fileName.size() - 1] == '/') {
         // This is path
-        std::string newPath = m_installerContext.tempWidgetPath + "/" +
+        std::string newPath = m_installerContext.locations->getTemporaryPackageDir() + "/" +
             fileName.substr(0, fileName.size() - 1);
         LogPedantic("Path to extract: " << newPath);
 
         // Create path in case of it is empty
-        FileUtils::MakePath(newPath, TEMPORARY_PATH_MODE);
+        createTempPath(newPath);
     } else {
         // This is regular file
         std::string fileExtractPath =
-            m_installerContext.tempWidgetPath + "/" + fileName;
+            m_installerContext.locations->getTemporaryPackageDir() + "/" + fileName;
 
         LogPedantic("File to extract: " << fileExtractPath);
 
@@ -234,16 +159,16 @@ void TaskUnzip::StepUnzipProgress()
                     " : " << pathAndFile.file);
 
         // First, ensure that path exists
-        FileUtils::MakePath(pathAndFile.path, TEMPORARY_PATH_MODE);
+        createTempPath(pathAndFile.path);
 
         Try
         {
             // Open file
-            DPL::ScopedPtr<DPL::ZipInput::File> file(
+            std::unique_ptr<DPL::ZipInput::File> file(
                 m_zip->OpenFile(fileName));
 
             // Extract single file
-            ExtractFile(file.Get(), fileExtractPath);
+            ExtractFile(file.get(), fileExtractPath);
         }
         Catch(DPL::ZipInput::Exception::OpenFileFailed)
         {
@@ -257,30 +182,20 @@ void TaskUnzip::StepUnzipProgress()
     } else {
         SwitchToStep(&TaskUnzip::StepUnzipProgress);
     }
+
+    m_installerContext.job->UpdateProgress(
+        InstallerContext::INSTALL_UNZIP_FILES,
+        "Unzip widget files to temporary directory");
 }
 
 void TaskUnzip::StepUnzipFinished()
 {
     // Unzip finished, close internal structures
-    m_zip.Reset();
-
-    m_installerContext.unzipFinished = true;
+    m_zip.reset();
 
     // Done
     LogInfo("Unzip finished");
 }
 
-void TaskUnzip::StepAbort()
-{
-    LogError("[Unzip Task] Aborting... (removing temporary dir: " <<
-             m_installerContext.tempWidgetPath << " )");
-
-    static const int maxDepth = 1024;
-    struct stat fileInfo;
-    if (stat(m_installerContext.tempWidgetPath.c_str(), &fileInfo) == 0) {
-        nftw(m_installerContext.tempWidgetPath.c_str(),
-             lambdaDeleteFile, maxDepth, FTW_DEPTH);
-    }
-}
 } //namespace WidgetInstall
 } //namespace Jobs