[Release] wrt-installer_0.1.114
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_update_files.cpp
index e3fabf0..75152ad 100644 (file)
@@ -37,6 +37,7 @@
 #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-ro/global_config.h>
 #include <dpl/exception.h>
@@ -57,252 +58,67 @@ TaskUpdateFiles::TaskUpdateFiles(InstallerContext& context) :
     DPL::TaskDecl<TaskUpdateFiles>(this),
     m_context(context)
 {
-    AddStep(&TaskUpdateFiles::StepCreateBackupFolder);
-    AddStep(&TaskUpdateFiles::StepResourceFilesBackup);
-    AddStep(&TaskUpdateFiles::StepExecFileBackup);
+    AddStep(&TaskUpdateFiles::StartStep);
+    AddStep(&TaskUpdateFiles::StepBackupDirectory);
+    AddStep(&TaskUpdateFiles::EndStep);
 
-    AddAbortStep(&TaskUpdateFiles::StepAbortCreateBackupFolder);
-    AddAbortStep(&TaskUpdateFiles::StepAbortExecFileBackup);
-    AddAbortStep(&TaskUpdateFiles::StepAbortResourceFilesBackup);
+    AddAbortStep(&TaskUpdateFiles::StepAbortBackupDirectory);
 }
 
-void TaskUpdateFiles::StepCreateBackupFolder()
+void TaskUpdateFiles::StepBackupDirectory()
 {
     LogDebug("StepCreateBackupFolder");
-    std::ostringstream backDirPath;
 
-    std::string srcBuPath = m_context.locations->getBackupSourceDir();
-    LogDebug("backup resource directory path : " << srcBuPath);
-    if (!WrtUtilMakeDir(srcBuPath)) {
-        ThrowMsg(
-            Exceptions::BackupFailed,
-            "Error occurs during create \
-                backup directory.");
-    }
-
-    std::string binBuPath = m_context.locations->getBackupBinaryDir();
-    LogDebug("backup execution directory path : " << binBuPath);
-    if (!WrtUtilMakeDir(binBuPath)) {
-        ThrowMsg(
-            Exceptions::BackupFailed,
-            "Error occurs during create backup \
-                directory.");
-    }
-
-    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) {
-        LogError("Package directory " << dirPath << " 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);
-    //closing the directory
-    if (-1 == TEMP_FAILURE_RETRY(closedir(pkgDir))) {
-        LogError("Failed to close dir: " << dirPath << " with error: "
-                                         << DPL::GetErrnoString());
-    }
-}
-
-void TaskUpdateFiles::StepResourceFilesBackup()
-{
-    LogDebug("StepCopyFiles");
-
-    ExistFileList resList;
-    ExistFileList tempList;
-
-    std::string pkgSrc = m_context.locations->getSourceDir();
-    ReadDirList(pkgSrc, resList, strlen(pkgSrc.c_str()) + 1);
-
-    std::string tempSrc = m_context.locations->getTemporaryPackageDir();
-    ReadDirList(tempSrc, tempList, strlen(tempSrc.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_context.locations->getBackupSourceDir() +
-                "/" + (*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);
-                WrtUtilMakeDir(backupFile);
-            } 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);
+    std::string backPath = m_context.locations->getBackupDir();
+    LogDebug("Backup resource directory path : " << backPath);
+    std::string pkgPath = m_context.locations->getPackageInstallationDir();
 
-                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 (0 == access(backPath.c_str(), F_OK)) {
+        if (!WrtUtilRemove(backPath)) {
+            ThrowMsg(Exceptions::RemovingFolderFailure,
+                    "Error occurs during removing backup resource directory");
         }
     }
-
-    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::string execFile = m_context.locations->getExecFile();
-
-    LogDebug(" source : " << execFile);
-
-    std::string tempSource = m_context.locations->getBackupExecFile();
-    LogDebug(" source : " << tempSource);
-
-    if ((rename(execFile.c_str(), tempSource.c_str())) != 0) {
-        LogError("Failed to rename " << execFile << " to " <<
-                 tempSource);
-        ThrowMsg(Exceptions::BackupFailed,
-                 "Error occurs during \
-                rename file");
-    }
-    LogDebug("Backup : " << execFile << " to " << tempSource);
-
-    std::string clientPath = GlobalConfig::GetWrtClientExec();
-
-    LogInfo("link -s " << clientPath << " " << execFile);
-
-    errno = 0;
-    if( symlink(clientPath.c_str(), execFile.c_str()) != 0 )
-    {
-        int error = errno;
-        if(error)
-            LogPedantic("Failed to make a symbolic name for a file "
-                    << "[" <<  DPL::GetErrnoString(error) << "]");
+    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");
     }
-    m_context.job->UpdateProgress(
-        InstallerContext::INSTALL_BACKUP_EXEC,
-        "Backup execution file for update");
+
+    WrtUtilMakeDir(pkgPath);
 }
 
-void TaskUpdateFiles::StepAbortResourceFilesBackup()
+void TaskUpdateFiles::StepAbortBackupDirectory()
 {
     LogDebug("StepAbortCopyFiles");
-    std::string srcPath = m_context.locations->getSourceDir();
-    std::string srcBuPath = m_context.locations->getBackupSourceDir();
 
-    LogDebug("Backup Folder " << srcBuPath << " to " << srcPath);
+    std::string backPath = m_context.locations->getBackupDir();
+    std::string pkgPath = m_context.locations->getPackageInstallationDir();
 
-    if (!WrtUtilRemove(srcPath)) {
-        LogError("Failed to remove " << srcPath);
+    LogDebug("Backup Folder " << backPath << " to " << pkgPath);
+
+    if (!WrtUtilRemove(pkgPath)) {
+        LogError("Failed to remove " << pkgPath);
     }
 
-    if (rename(srcBuPath.c_str(), srcPath.c_str()) != 0) {
-        LogError("Failed to rename " << srcBuPath << " to " << srcPath);
+    if (rename(backPath.c_str(), pkgPath.c_str()) != 0) {
+        LogError("Failed to rename " << backPath << " to " << pkgPath);
     }
 }
 
-void TaskUpdateFiles::StepAbortExecFileBackup()
+void TaskUpdateFiles::StartStep()
 {
-    LogDebug("StepAbortExecFileBackup");
-    std::string binPath = m_context.locations->getBinaryDir();
-
-    if (!WrtUtilRemove(binPath)) {
-        LogError("Failed to remove " << binPath);
-    }
-
-    std::string binBuPath = m_context.locations->getBackupBinaryDir();
-    if (rename(binBuPath.c_str(), binPath.c_str()) != 0) {
-        LogError("Failed to rename " << binBuPath << " to " << binPath);
-    }
-    LogDebug("Backup Folder " << binBuPath << "move to " << binPath);
+    LogDebug("--------- <TaskUpdateFiles> : START ----------");
 }
 
-void TaskUpdateFiles::StepAbortCreateBackupFolder()
+void TaskUpdateFiles::EndStep()
 {
-    LogDebug("StepAbortCreateBackupFolder");
-    std::ostringstream path;
-    path << m_context.locations->getBackupDir();
-    LogDebug("Remove backup directory : " << path.str());
+    m_context.job->UpdateProgress(
+        InstallerContext::INSTALL_CREATE_BACKUP_DIR,
+        "Backup directory created for update");
 
-    if (!WrtUtilRemove(path.str())) {
-        LogError("Failed to remove " << path);
-    }
+    LogDebug("--------- <TaskUpdateFiles> : END ----------");
 }
 } //namespace WidgetInstall
 } //namespace Jobs