[Release] wrt-installer_0.1.55
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_file_manipulation.cpp
index dcb4884..b365d9a 100644 (file)
@@ -26,6 +26,7 @@
 #include <widget_install/job_widget_install.h>
 #include <widget_install/widget_install_errors.h>
 #include <widget_install/widget_install_context.h>
+#include <widget_install/directory_api.h>
 #include <dpl/utils/wrt_utility.h>
 #include <dpl/foreach.h>
 #include <dpl/log/log.h>
@@ -149,19 +150,21 @@ TaskFileManipulation::TaskFileManipulation(InstallerContext& context) :
             m_context.locationType)
     {
         AddStep(&TaskFileManipulation::StepCreateDirs);
-        AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
         if (m_context.widgetConfig.packagingType !=
             WrtDB::PKG_TYPE_DIRECTORY_WEB_APP)
         {
             AddStep(&TaskFileManipulation::StepRenamePath);
             AddAbortStep(&TaskFileManipulation::StepAbortRenamePath);
         }
+        AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
+        AddStep(&TaskFileManipulation::StepCreateSharedFolder);
         AddStep(&TaskFileManipulation::StepLinkForPreload);
 
     } else {
         AddStep(&TaskFileManipulation::StepPrepareExternalDir);
         AddStep(&TaskFileManipulation::StepInstallToExternal);
         AddStep(&TaskFileManipulation::StepCreatePrivateStorageDir);
+        AddStep(&TaskFileManipulation::StepCreateSharedFolder);
 
         AddAbortStep(&TaskFileManipulation::StepAbortCreateExternalDir);
     }
@@ -186,6 +189,10 @@ void TaskFileManipulation::StepCreateDirs()
         LogDebug("Create resource directory");
         WrtUtilMakeDir(widgetBinPath);
         WrtUtilMakeDir(widgetSrcPath);
+        if (m_context.mode.installTime == InstallMode::InstallTime::PRELOAD) {
+            std::string userWidgetDir = m_context.locations->getUserDataRootDir();
+            WrtUtilMakeDir(userWidgetDir);
+        }
     }
 
     m_context.job->UpdateProgress(
@@ -198,6 +205,17 @@ void TaskFileManipulation::StepCreatePrivateStorageDir()
     std::string storagePath = m_context.locations->getPrivateStorageDir();
     LogDebug("Create private storage directory : " <<
             m_context.locations->getPrivateStorageDir());
+
+    if (m_context.isUpdateMode) { //update
+        std::string backData = m_context.locations->getBackupPrivateDir();
+        LogDebug("copy private storage " << backData << " to " << storagePath);
+        WrtUtilMakeDir(storagePath);
+        if (!DirectoryApi::DirectoryCopy(backData, storagePath)) {
+            LogError("Failed to rename " << backData << " to " << storagePath);
+            ThrowMsg(Exceptions::BackupFailed,
+                    "Error occurs copy private strage files");
+        }
+    }
     changeOwnerForDirectory(storagePath);
 }
 
@@ -230,15 +248,13 @@ void TaskFileManipulation::StepRenamePath()
 
 void TaskFileManipulation::StepLinkForPreload()
 {
-    if (m_context.job->getInstallerStruct().m_installMode
-            == InstallMode::INSTALL_MODE_PRELOAD)
-    {
+    if (m_context.mode.rootPath == InstallMode::RootPath::RO) {
         std::string srcDir = m_context.locations->getUserDataRootDir() +
             WrtDB::GlobalConfig::GetWidgetSrcPath();
 
         if (0 != access(srcDir.c_str(), F_OK)) {
-            LogDebug("Make symbolic name for preaload app" << " to " << srcDir <<
-                    "from " << m_context.locations->getSourceDir());
+            LogDebug("Make symbolic name for preaload app" <<
+                    m_context.locations->getSourceDir() << " to " << srcDir);
             std::string resDir = m_context.locations->getUserDataRootDir() +
                 "/res";
 
@@ -258,17 +274,38 @@ void TaskFileManipulation::StepLinkForPreload()
         std::string storagePath = m_context.locations->getPrivateStorageDir();
         std::string dataDir = m_context.locations->getPackageInstallationDir() +
             "/" + WrtDB::GlobalConfig::GetWidgetPrivateStoragePath();
+        if (0 != access(dataDir.c_str(), F_OK)) {
+            LogDebug("Make symbolic name for preaload app " <<
+                    storagePath << " to " << dataDir);
+
+            if (symlink(storagePath.c_str(), dataDir.c_str()) != 0)
+            {
+                int error = errno;
+                if (error)
+                    LogPedantic("Failed to make a symbolic name for a file "
+                            << "[" <<  DPL::GetErrnoString(error) << "]");
+                ThrowMsg(Exceptions::FileOperationFailed,
+                        "Symbolic link creating is not done.");
+            }
+            changeOwnerForDirectory(dataDir);
+        }
+
+        if (m_context.widgetConfig.packagingType != PKG_TYPE_HYBRID_WEB_APP) {
+            std::string widgetBinPath = m_context.locations->getBinaryDir();
+            std::string userBinPath = m_context.locations->getUserBinaryDir();
+            LogDebug("Make symbolic link for preload app " << widgetBinPath <<
+                    " to " << userBinPath);
+            if (symlink(widgetBinPath.c_str(), userBinPath.c_str()) != 0)
+            {
+                int error = errno;
+                if (error)
+                    LogPedantic("Failed to make a symbolic name for a file "
+                            << "[" <<  DPL::GetErrnoString(error) << "]");
+                ThrowMsg(Exceptions::FileOperationFailed,
+                        "Symbolic link creating is not done.");
+            }
 
-        if (symlink(storagePath.c_str(), dataDir.c_str()) != 0)
-        {
-            int error = errno;
-            if (error)
-                LogPedantic("Failed to make a symbolic name for a file "
-                        << "[" <<  DPL::GetErrnoString(error) << "]");
-            ThrowMsg(Exceptions::FileOperationFailed,
-                    "Symbolic link creating is not done.");
         }
-        changeOwnerForDirectory(dataDir);
     }
 }
 
@@ -282,6 +319,15 @@ void TaskFileManipulation::StepAbortRenamePath()
             ThrowMsg(Exceptions::RemovingFolderFailure,
                      "Error occurs during removing existing folder");
         }
+        // Remove user data directory if preload web app.
+        std::string userData = m_context.locations->getUserDataRootDir();
+        if (0 == access(userData.c_str(), F_OK)) {
+            if (!WrtUtilRemove(userData)) {
+                ThrowMsg(Exceptions::RemovingFolderFailure,
+                         "Error occurs during removing user data directory");
+            }
+        }
+
     }
     LogDebug("Rename widget path sucessful!");
 }
@@ -365,5 +411,29 @@ void TaskFileManipulation::StepAbortCreateExternalDir()
     }
     WidgetInstallToExtSingleton::Instance().deinitialize();
 }
+
+void TaskFileManipulation::StepCreateSharedFolder()
+{
+    LogDebug("StepCreateSharedFolder");
+    std::string sharedPath = m_context.locations->getSharedRootDir();
+    LogDebug("Create shared directory : " <<
+            m_context.locations->getSharedRootDir());
+
+    WrtUtilMakeDir(sharedPath);
+
+    if (m_context.isUpdateMode) { //update
+        std::string backData = m_context.locations->getBackupSharedDir();
+        LogDebug("copy shared storage " << backData << " to " << sharedPath);
+        if (!DirectoryApi::DirectoryCopy(backData, sharedPath)) {
+            LogError("Failed to rename " << backData << " to " << sharedPath);
+            ThrowMsg(Exceptions::BackupFailed,
+                    "Error occurs copy shared strage files");
+        }
+    } else {
+        WrtUtilMakeDir(m_context.locations->getSharedResourceDir());
+        WrtUtilMakeDir(m_context.locations->getSharedDataDir());
+        WrtUtilMakeDir(m_context.locations->getSharedTrustedDir());
+    }
+}
 } //namespace WidgetInstall
 } //namespace Jobs