[Release] wrt-installer_0.0.89
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_commons.cpp
index 983a68f..fea9ea1 100644 (file)
@@ -29,7 +29,8 @@
 #include <dpl/log/log.h>
 #include <dpl/exception.h>
 #include <dpl/errno_string.h>
-#include <dpl/utils/file_utils.h>
+#include <dpl/utils/wrt_utility.h>
+#include <widget_install/widget_install_errors.h>
 
 namespace Jobs {
 namespace WidgetInstall {
@@ -39,48 +40,22 @@ namespace {
 const char * const TEMPORARY_PATH_POSTFIX = "temp";
 const mode_t TEMPORARY_PATH_MODE = 0775;
 
-int lambdaDeleteFile(const char *fpath,
-                     const struct stat* /*sb*/,
-                     int tflag,
-                     struct FTW* /*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
 
-void removeTemporaryDir(const std::string& dir)
-{
-    LogError("[GenerateConfig Task] Aborting... (removing temporary dir: " <<
-             dir << " )");
-
-    static const int maxDepth = 1024;
-    struct stat fileInfo;
-    if (stat(dir.c_str(), &fileInfo) == 0) {
-        nftw(dir.c_str(), lambdaDeleteFile, maxDepth, FTW_DEPTH);
-    }
-}
 
-std::string createTempPath()
+std::string createTempPath(bool preload)
 {
     LogInfo("Step: Creating temporary path");
 
    // Temporary path
    std::ostringstream tempPathBuilder;
 
-   tempPathBuilder << WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
+   if (preload) {
+       tempPathBuilder << WrtDB::GlobalConfig::GetUserPreloadedWidgetPath();
+   } else {
+       tempPathBuilder << WrtDB::GlobalConfig::GetUserInstalledWidgetPath();
+   }
+   tempPathBuilder << WrtDB::GlobalConfig::GetTmpDirPath();
    tempPathBuilder << "/";
    tempPathBuilder << TEMPORARY_PATH_POSTFIX;
    tempPathBuilder << "_";
@@ -96,26 +71,25 @@ std::string createTempPath()
    // 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());
+       if(!WrtUtilRemove(tempPath)){
+           ThrowMsg(Exceptions::RemovingFolderFailure,
+                    "Failed to to remove temporary directory");
        }
    }
    // Create new path
-   FileUtils::MakePath(tempPath, TEMPORARY_PATH_MODE);
+   if(!WrtUtilMakeDir(tempPath, TEMPORARY_PATH_MODE)){
+       ThrowMsg(Exceptions::InternalError, "Failed to create temporary directory");
+   }
 
    return tempPath;
 }
 
 void createTempPath(const std::string& path)
 {
-    FileUtils::MakePath(path, TEMPORARY_PATH_MODE);
+   if(!WrtUtilMakeDir(path, TEMPORARY_PATH_MODE)){
+       ThrowMsg(Exceptions::InternalError, "Failed to create temporary directory");
+   }
 }
 
 } // WidgetInstall