[Release] wrt-installer_0.1.55
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_update_files.cpp
index 446c043..12af068 100644 (file)
  * @brief   Implementation file for installer task update files
  */
 
+#include <unistd.h>
 #include <utility>
 #include <vector>
 #include <string>
+#include <algorithm>
 #include <sys/stat.h>
 #include <dirent.h>
 
 #include <dpl/assert.h>
 #include <dpl/log/log.h>
 #include <dpl/foreach.h>
+#include <dpl/utils/wrt_utility.h>
 
 #include <widget_install/widget_install_context.h>
 #include <widget_install/widget_install_errors.h>
 #include <widget_install/job_widget_install.h>
+#include <widget_install/directory_api.h>
 
-#include <dpl/wrt-dao-rw/widget_dao.h>
 #include <dpl/wrt-dao-ro/global_config.h>
-#include <dpl/utils/wrt_utility.h>
 #include <dpl/exception.h>
+#include <dpl/errno_string.h>
 
 using namespace WrtDB;
 
 namespace {
-
 inline const char* GetWidgetBackupDirPath()
 {
     return "backup";
@@ -52,240 +54,55 @@ inline const char* GetWidgetBackupDirPath()
 
 namespace Jobs {
 namespace WidgetInstall {
-
 TaskUpdateFiles::TaskUpdateFiles(InstallerContext& context) :
     DPL::TaskDecl<TaskUpdateFiles>(this),
     m_context(context)
 {
-    AddStep(&TaskUpdateFiles::StepCreateBackupFolder);
-    AddStep(&TaskUpdateFiles::StepResourceFilesBackup);
-    AddStep(&TaskUpdateFiles::StepExecFileBackup);
+    AddStep(&TaskUpdateFiles::StepBackupDirectory);
 
-    AddAbortStep(&TaskUpdateFiles::StepAbortCreateBackupFolder);
-    AddAbortStep(&TaskUpdateFiles::StepAbortExecFileBackup);
-    AddAbortStep(&TaskUpdateFiles::StepAbortResourceFilesBackup);
+    AddAbortStep(&TaskUpdateFiles::StepAbortBackupDirectory);
 }
 
-void TaskUpdateFiles::StepCreateBackupFolder()
+void TaskUpdateFiles::StepBackupDirectory()
 {
     LogDebug("StepCreateBackupFolder");
-    std::ostringstream backDirPath;
 
-    backDirPath << GlobalConfig::GetUserInstalledWidgetPath() << "/";
-    DPL::OptionalString pkgname = m_context.widgetConfig.pkgname;
+    std::string backPath = m_context.locations->getBackupDir();
+    LogDebug("backup resource directory path : " << backPath);
+    std::string pkgPath = m_context.locations->getPackageInstallationDir();
 
-    backDirPath << pkgname;
-    m_pkgPath = backDirPath.str();
-
-    backDirPath << "/" << GetWidgetBackupDirPath() << "/";
-    m_srcBuPath = backDirPath.str() + GlobalConfig::GetWidgetSrcPath();
-    LogDebug("backup resource directory path : " << m_srcBuPath);
-    if(!_WrtMakeDir(m_srcBuPath.c_str(), 0755, WRT_FILEUTILS_RECUR)) {
-        ThrowMsg(Exceptions::BackupFailed, "Error occurs during create \
-                backup directory.");
+    if (0 == access(backPath.c_str(), F_OK)) {
+        WrtUtilRemove(backPath);
     }
-
-    m_binBuPath = backDirPath.str() + GlobalConfig::GetUserWidgetExecPath();
-    LogDebug("backup execution directory path : " << m_binBuPath);
-    if(!_WrtMakeDir(m_binBuPath.c_str(), 0755, WRT_FILEUTILS_RECUR)) {
-        ThrowMsg(Exceptions::BackupFailed, "Error occurs during create backup \
-                directory.");
+    LogDebug("copy : " << pkgPath << " to " << backPath);
+    if ((rename(pkgPath.c_str(), backPath.c_str())) != 0) {
+        LogError("Failed to rename " << pkgPath << " to " << backPath);
+        ThrowMsg(Exceptions::BackupFailed,
+                "Error occurs during rename file");
     }
 
+    WrtUtilMakeDir(pkgPath);
+
     m_context.job->UpdateProgress(
         InstallerContext::INSTALL_CREATE_BACKUP_DIR,
         "Backup directory created for update");
 }
 
-void TaskUpdateFiles::ReadDirList(std::string dirPath, ExistFileList &list,
-        size_t subLen)
-{
-    DIR* pkgDir = opendir(dirPath.c_str());
-    if (!pkgDir) {
-        LogDebug("Package directory doesn't exist");
-        ThrowMsg(Exceptions::InternalError, "Error occurs during read \
-                directory");
-    }
-
-    struct dirent* dirent;
-    struct stat statInfo;
-    do {
-        if ((dirent = readdir(pkgDir))) {
-            std::string dirName = dirent->d_name;
-            std::string absFileName = dirPath + "/" + dirName;
-            if (stat(absFileName.c_str(), &statInfo) != 0) {
-                ThrowMsg(Exceptions::InternalError, "Error occurs read file");
-            }
-
-            if (S_ISDIR(statInfo.st_mode)) {
-                if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name,
-                            "..") == 0) {
-                    continue;
-                }
-                ReadDirList(absFileName, list, subLen);
-            }
-
-            list.insert(absFileName.substr(subLen));
-        }
-    }
-    while(dirent);
-}
-
-void TaskUpdateFiles::StepResourceFilesBackup()
-{
-    LogDebug("StepCopyFiles");
-
-    ExistFileList resList;
-    ExistFileList tempList;
-
-    std::string pkgSrc = m_pkgPath + "/" + GlobalConfig::GetWidgetSrcPath();
-    ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str())+1);
-
-
-    std::string tempSrc = m_context.tempWidgetPath;
-    ReadDirList(tempSrc, tempList, strlen(m_context.tempWidgetPath.c_str())+1);
-
-    FOREACH(it, tempList) {
-        std::set<std::string>::iterator res;
-        res = resList.find(*it);
-        std::string resFile = pkgSrc + "/" + (*it);
-        std::string newFile = tempSrc + "/" +(*it);
-
-        if (res != resList.end()) {
-            std::string backupFile = m_srcBuPath + "/"+ (*it);
-
-            struct stat sInfo;
-            if (stat(resFile.c_str(), &sInfo) != 0) {
-                ThrowMsg(Exceptions::InternalError, "Error occurs read file");
-            }
-
-            if (S_ISDIR(sInfo.st_mode)) {
-                LogDebug(resFile << " is a directory. so create a folder : " <<
-                        backupFile);
-                _WrtMakeDir(backupFile.c_str(), 0755, WRT_FILEUTILS_RECUR);
-            } else {
-                if ((rename(resFile.c_str(), backupFile.c_str())) != 0) {
-                    LogError("Failed to rename " << resFile << " to " << backupFile);
-                    ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
-                            rename file");
-                }
-
-                LogDebug("backup : " << resFile << " to " << backupFile);
-
-                if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
-                    LogError("Failed to rename " << newFile << " to " << resFile);
-                    ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
-                            rename file");
-                }
-                LogDebug("copy : " << newFile << " to " << resFile);
-            }
-            resList.erase(res);
-        } else {
-            if ((rename(newFile.c_str(), resFile.c_str())) != 0) {
-                LogError("Failed to rename " << newFile << " to " << resFile);
-                ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
-                        rename file");
-            }
-            LogDebug("only copy : " << newFile << " to " << resFile);
-        }
-    }
-
-    if (resList.empty() != 0) {
-        FOREACH(remain, resList) {
-            std::string pkgFile = pkgSrc + "/" + (*remain);
-            std::string backFile = tempSrc + "/" + (*remain);
-            if ((rename(pkgFile.c_str(), backFile.c_str())) != 0) {
-                LogError("Failed to backup : " << pkgFile << " to " << backFile);
-                ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
-                        rename file");
-            }
-            LogDebug("only backup : " << pkgFile << " to " << backFile);
-        }
-    }
-
-    m_context.job->UpdateProgress(
-        InstallerContext::INSTALL_BACKUP_RES_FILES,
-        "Backup resource file for update");
-}
-
-void TaskUpdateFiles::StepExecFileBackup()
-{
-    std::ostringstream source;
-    source << m_pkgPath << "/" << GlobalConfig::GetUserWidgetExecPath();
-    source << "/" << m_context.existingWidgetInfo.existingHandle;
-
-    LogDebug(" source : " << source.str());
-
-    std::ostringstream tempSource;
-    tempSource << m_binBuPath << "/" <<
-        m_context.existingWidgetInfo.existingHandle;
-    LogDebug(" source : " << tempSource.str());
-
-    if ((rename(source.str().c_str(), tempSource.str().c_str())) != 0) {
-        LogError("Failed to rename " << source.str() << " to " <<
-                tempSource.str());
-        ThrowMsg(Exceptions::BackupFailed, "Error occurs during \
-                rename file");
-    }
-    LogDebug("Backup : " << source.str() << " to " << tempSource.str());
-
-    std::ostringstream newClient;
-    newClient << m_pkgPath << "/" << GlobalConfig::GetUserWidgetExecPath();
-    newClient << "/" << m_context.widgetHandle;
-
-    std::string clientPath = GlobalConfig::GetWrtClientExec();
-
-    LogInfo("link -s " << clientPath << " " << newClient.str());
-    symlink(clientPath.c_str(), newClient.str().c_str());
-
-    m_context.job->UpdateProgress(
-        InstallerContext::INSTALL_BACKUP_EXEC,
-        "Backup execution file for update");
-}
-
-void TaskUpdateFiles::StepAbortResourceFilesBackup()
+void TaskUpdateFiles::StepAbortBackupDirectory()
 {
     LogDebug("StepAbortCopyFiles");
-    std::string srcPath = m_pkgPath + "/" + GlobalConfig::GetWidgetSrcPath();
-
-    LogDebug("Backup Folder " << m_srcBuPath << " to " << srcPath);
-
-    if(!_WrtUtilRemoveDir(srcPath.c_str())) {
-        LogError("Failed to remove " << srcPath);
-    }
-
-    if (rename(m_srcBuPath.c_str(), srcPath.c_str()) != 0) {
-        LogError("Failed to rename " << m_srcBuPath << " to " << srcPath);
-    }
-}
 
-void TaskUpdateFiles::StepAbortExecFileBackup()
-{
-    LogDebug("StepAbortExecFileBackup");
-    std::string binPath = m_pkgPath + "/" +
-        GlobalConfig::GetUserWidgetExecPath();
+    std::string backPath = m_context.locations->getBackupDir();
+    std::string pkgPath = m_context.locations->getPackageInstallationDir();
 
-    if(!_WrtUtilRemoveDir(binPath.c_str())) {
-        LogError("Failed to remove " << binPath);
-    }
+    LogDebug("Backup Folder " << backPath << " to " << pkgPath);
 
-    if (rename(m_binBuPath.c_str(), binPath.c_str()) != 0) {
-        LogError("Failed to rename " << m_binBuPath << " to " << binPath);
+    if (!WrtUtilRemove(pkgPath)) {
+        LogError("Failed to remove " << pkgPath);
     }
-    LogDebug("Backup Folder " << m_binBuPath << "move to " << binPath);
-}
-
-void TaskUpdateFiles::StepAbortCreateBackupFolder()
-{
-    LogDebug("StepAbortCreateBackupFolder");
-    std::ostringstream path;
-    path << GlobalConfig::GetUserInstalledWidgetPath() << "/";
-    path << m_context.widgetConfig.pkgname << "/";
-    path << GetWidgetBackupDirPath();
-    LogDebug("Remove backup directory : " << path.str());
 
-    if(!_WrtUtilRemoveDir(path.str().c_str())) {
-        LogError("Failed to remove " << path);
+    if (rename(backPath.c_str(), pkgPath.c_str()) != 0) {
+        LogError("Failed to rename " << backPath << " to " << pkgPath);
     }
 }
 } //namespace WidgetInstall