*/
#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
{
}
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)
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)
}
}
+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();
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.");
}