From: Maciej Piotrowski Date: Fri, 9 Aug 2013 06:59:43 +0000 (+0200) Subject: Dir operation duplications clean up. X-Git-Tag: submit/tizen_2.2/20130927.091100^2~6 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=9808ee9155d5dc692a810a13a0e418c09a747fc4;p=platform%2Fframework%2Fweb%2Fwrt-commons.git Dir operation duplications clean up. [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 --- diff --git a/modules/utils/include/dpl/utils/path.h b/modules/utils/include/dpl/utils/path.h index 67ffd43..83290d0 100644 --- a/modules/utils/include/dpl/utils/path.h +++ b/modules/utils/include/dpl/utils/path.h @@ -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); } } diff --git a/modules/utils/src/path.cpp b/modules/utils/src/path.cpp index ea1653f..9008fad 100644 --- a/modules/utils/src/path.cpp +++ b/modules/utils/src/path.cpp @@ -20,9 +20,7 @@ */ #include "dpl/utils/path.h" - #include - #include #include #include @@ -30,13 +28,21 @@ #include #include #include +#include + #include -#include +#include +#include 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(tv.tv_sec) * 1000000ULL + static_cast(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(); diff --git a/tests/utils/path_tests.cpp b/tests/utils/path_tests.cpp index 0d29057..eac35fd 100644 --- a/tests/utils/path_tests.cpp +++ b/tests/utils/path_tests.cpp @@ -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."); }