Dir operation duplications clean up.
authorMaciej Piotrowski <m.piotrowski@samsung.com>
Fri, 9 Aug 2013 06:59:43 +0000 (08:59 +0200)
committerGerrit Code Review <gerrit@gerrit.vlan144.tizendev.org>
Thu, 12 Sep 2013 01:49:16 +0000 (01:49 +0000)
[Issue#]   LINUXWRT-717
[Problem]  Dir operation duplications.
[Cause]    N/A
[Solution] Functionality from
            - wrt-installer/src/jobs/widget_install/directory_api.*
            - wrt-installer/src/jobs/widget_install/task_common.*
            was transferred to wrt-commons/modules/utils/src/path.cpp

[Verification] Build repository, install/uninstall widgets,
create test case for added method DPL::Utils::CreateTempPath, run tests.

Change-Id: Ia1a18a1c301ead904674197bc51705e53546ed55

modules/utils/include/dpl/utils/path.h
modules/utils/src/path.cpp
tests/utils/path_tests.cpp

index 67ffd437094312b12b561e7a53af6451b5a505b6..83290d09b5b823bb82f85bc48e7aacf3651d9f26 100644 (file)
@@ -226,6 +226,7 @@ void CopyFile(const Path & from, const Path & to);
  */
 void CopyDir(const Path & from, const Path & to);
 
+Path CreateTempPath(const Path & path);
 }
 
 }
index ea1653fdfdf8668cdd2275145b50ab6fc39afa24..9008fadd6b5a0fb64f74e5170d702869bf569640 100644 (file)
@@ -20,9 +20,7 @@
  */
 
 #include "dpl/utils/path.h"
-
 #include <dpl/utils/wrt_utility.h>
-
 #include <dpl/scoped_free.h>
 #include <dpl/errno_string.h>
 #include <dpl/file_input.h>
 #include <dpl/copy.h>
 #include <dpl/log/log.h>
 #include <dpl/foreach.h>
+#include <dpl/wrt-dao-ro/global_config.h>
+
 #include <unistd.h>
-#include <sys/stat.h>
+#include <ftw.h>
+#include <sys/time.h>
 
 namespace DPL {
 
 namespace Utils {
 
+namespace {
+const char * const TEMPORARY_PATH_POSTFIX = "temp";
+const mode_t TEMPORARY_PATH_MODE = 0775;
+} // namespace
+
 Path::Iterator::Iterator() //end iterator by default
 {
 }
@@ -372,7 +378,8 @@ bool Path::hasExtension(const std::string& extension) const
 void MakeDir(const Path & path, mode_t mode)
 {
     path.RootGuard();
-    if(!WrtUtilMakeDir(path.Fullpath(), mode)) ThrowMsg(Path::OperationFailed, "Cannot make directory");
+    if(!WrtUtilMakeDir(path.Fullpath(), mode))
+        ThrowMsg(Path::OperationFailed, "Cannot make directory");
 }
 
 void MakeEmptyFile(const Path & path)
@@ -405,8 +412,7 @@ void Remove(const Path & path)
 bool TryRemove(const Path & path)
 {
     path.RootGuard();
-    if(!WrtUtilRemove(path.Fullpath())) return false;
-    return true;
+    return WrtUtilRemove(path.Fullpath());
 }
 
 void Rename(const Path & from, const Path & to)
@@ -496,6 +502,25 @@ void CopyDir(const Path & from, const Path & to)
     }
 }
 
+Path CreateTempPath(const Path & basePath)
+{
+    LogDebug("Step: Creating temporary path");
+
+    // Temporary path
+    Path tempPath = basePath;
+    tempPath /= WrtDB::GlobalConfig::GetTmpDirPath();
+
+    timeval tv;
+    gettimeofday(&tv, NULL);
+    unsigned long long nr = (static_cast<unsigned long long>(tv.tv_sec) * 1000000ULL + static_cast<unsigned long long>(tv.tv_usec));
+    std::stringstream relPath;
+    relPath << TEMPORARY_PATH_POSTFIX << "_" << nr;
+    tempPath /= relPath.str();
+
+    MakeDir(tempPath, TEMPORARY_PATH_MODE);
+    return tempPath;
+}
+
 bool Exists(const Path & path)
 {
     return path.Exists();
index 0d29057be2dc5d9f289b9f22616ce5420f8ec5a2..eac35fdb1569fd310e9b6d7926f2f9fdcd41a14e 100644 (file)
@@ -923,6 +923,24 @@ RUNNER_TEST(path_has_extension_test)
     RUNNER_ASSERT_MSG(!path6.hasExtension(".JS"),
             "Wrong argument in hasExtension() function");
 
-    RUNNER_ASSERT_MSG(path3.hasExtension(""), "Extension length should be 0");
-    RUNNER_ASSERT_MSG(path4.hasExtension(""), "There should be no extension");
+    RUNNER_ASSERT_MSG(!path3.hasExtension(""), "Extension length is 0");
+
+    RUNNER_ASSERT_MSG(!path4.hasExtension(""), "Not a directory");
+}
+
+/*
+Name: path_create_temp_dir
+Description: tests if temp dir was created
+Expected: temp dir exists
+*/
+RUNNER_TEST(path_create_temp_dir)
+{
+    Path p1 = CreateTempPath(Path("/usr/tmp/"));
+    Path p2 = CreateTempPath(Path("/opt/usr/apps/tmp/"));
+    Path p3 = CreateTempPath(Path("/opt/usr/apps/tmp/"));
+
+    RUNNER_ASSERT_MSG(p1.Exists(), "Temp dir doesn't exists");
+    RUNNER_ASSERT_MSG(p2.Exists(), "Temp dir doesn't exists");
+    RUNNER_ASSERT_MSG(p3.Exists(), "Temp dir doesn't exists");
+    RUNNER_ASSERT_MSG(p2.Fullpath() != p3.Fullpath(), "Each temp path should be unique due to having tv_usec in dir name.");
 }