[Release] wrt-installer_0.1.55
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_update_files.cpp
index 0ba0a07..12af068 100644 (file)
@@ -20,6 +20,7 @@
  * @brief   Implementation file for installer task update files
  */
 
+#include <unistd.h>
 #include <utility>
 #include <vector>
 #include <string>
@@ -36,8 +37,8 @@
 #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/exception.h>
 #include <dpl/errno_string.h>
@@ -45,7 +46,6 @@
 using namespace WrtDB;
 
 namespace {
-
 inline const char* GetWidgetBackupDirPath()
 {
     return "backup";
@@ -54,271 +54,56 @@ 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::StepUpdateExternalFiles);
+    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;
-
-    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)) {
+        WrtUtilRemove(backPath);
     }
-
-    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);
-        }
+    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_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);
-    symlink(clientPath.c_str(), execFile.c_str());
+    WrtUtilMakeDir(pkgPath);
 
     m_context.job->UpdateProgress(
-        InstallerContext::INSTALL_BACKUP_EXEC,
-        "Backup execution file for update");
+        InstallerContext::INSTALL_CREATE_BACKUP_DIR,
+        "Backup directory created for update");
 }
 
-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);
-
-    if(!WrtUtilRemove(srcPath)) {
-        LogError("Failed to remove " << srcPath);
-    }
 
-    if (rename(srcBuPath.c_str(), srcPath.c_str()) != 0) {
-        LogError("Failed to rename " << srcBuPath << " to " << srcPath);
-    }
-}
+    std::string backPath = m_context.locations->getBackupDir();
+    std::string pkgPath = m_context.locations->getPackageInstallationDir();
 
-void TaskUpdateFiles::StepAbortExecFileBackup()
-{
-    LogDebug("StepAbortExecFileBackup");
-    std::string binPath = m_context.locations->getBinaryDir();
+    LogDebug("Backup Folder " << backPath << " to " << pkgPath);
 
-    if(!WrtUtilRemove(binPath)) {
-        LogError("Failed to remove " << binPath);
+    if (!WrtUtilRemove(pkgPath)) {
+        LogError("Failed to remove " << pkgPath);
     }
 
-    std::string binBuPath = m_context.locations->getBackupBinaryDir();
-    if (rename(binBuPath.c_str(), binPath.c_str()) != 0) {
-        LogError("Failed to rename " << binBuPath << " to " << binPath);
+    if (rename(backPath.c_str(), pkgPath.c_str()) != 0) {
+        LogError("Failed to rename " << backPath << " to " << pkgPath);
     }
-    LogDebug("Backup Folder " << binBuPath << "move to " << binPath);
 }
-
-void TaskUpdateFiles::StepAbortCreateBackupFolder()
-{
-    LogDebug("StepAbortCreateBackupFolder");
-    std::ostringstream path;
-    path << m_context.locations->getBackupDir();
-    LogDebug("Remove backup directory : " << path.str());
-
-    if(!WrtUtilRemove(path.str())) {
-        LogError("Failed to remove " << path);
-    }
-}
-
-void TaskUpdateFiles::StepUpdateExternalFiles()
-{
-    WidgetDAO dao(m_context.locations->getPkgname());
-    WrtDB::ExternalLocationList externalLocationsUpdate = m_context.locations->listExternalLocations();
-    WrtDB::ExternalLocationList externalLocationsDB = dao.getWidgetExternalLocations();
-    LogDebug("Removing external files:");
-    FOREACH(file, externalLocationsDB)
-    {
-        if(std::find(externalLocationsUpdate.begin(), externalLocationsUpdate.end(), *file) == externalLocationsUpdate.end())
-        {
-            if(WrtUtilFileExists(*file))
-            {
-                LogDebug("  -> " << *file);
-                remove(file->c_str());
-            }
-            else if(WrtUtilDirExists(*file))
-            {
-                LogDebug("  -> " << *file);
-                if(!WrtUtilRemove(*file)){
-                    ThrowMsg(Exceptions::RemovingFolderFailure,
-                            "Failed to remove external directory");
-                }
-            }
-            else
-            {
-                LogWarning("  -> " << *file << "(no such a path)");
-            }
-        }
-    }
-    dao.unregisterAllExternalLocations();
-    LogDebug("Registering(update) external files:");
-    FOREACH(file, externalLocationsUpdate)
-    {
-        LogDebug("  -> " << *file);
-    }
-    dao.registerExternalLocations(externalLocationsUpdate);
-}
-
 } //namespace WidgetInstall
 } //namespace Jobs