From 9318c9e1ce1882a71baa87366e777a43f81b5d31 Mon Sep 17 00:00:00 2001 From: Kamil Nowac Date: Thu, 22 Aug 2013 10:18:30 +0200 Subject: [PATCH] Add ExistsAndIsFile() and ExistsAndIsDir() in DPL::Utils::Path [Issue#] N/A [Problem] Not provided functions for Exists _AND_ IsFile/Dir check. [Cause] Because of throws in IsFile() and IsDir() functions, there are difficulties to use them in conditional statements. Sometimes unhandled exception occurs and there is no way to handle it. [Solution] Added functions: ExistsAndIsFile() and ExistsAndIsDir() [Verification] 1. Build with --define "WITH_TESTS ON" 2. Run wrt-commons-tests-utils --output=text --regexp="path_" [SCMRequest] Needed by: http://tizendev.org/gerrit/#/c/72690/ Change-Id: If74a9d565c03d52d57ea8f5ebeef9113b3536b97 --- modules/utils/include/dpl/utils/path.h | 2 ++ modules/utils/src/path.cpp | 24 ++++++++++++++++++++++++ tests/utils/path_tests.cpp | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/modules/utils/include/dpl/utils/path.h b/modules/utils/include/dpl/utils/path.h index ff7b544..3883d3d 100644 --- a/modules/utils/include/dpl/utils/path.h +++ b/modules/utils/include/dpl/utils/path.h @@ -120,6 +120,8 @@ public: bool Exists() const; bool IsDir() const; bool IsFile() const; + bool ExistsAndIsFile() const; + bool ExistsAndIsDir() const; bool IsSymlink() const; std::size_t Size() const; /** diff --git a/modules/utils/src/path.cpp b/modules/utils/src/path.cpp index 51b54aa..213f59b 100644 --- a/modules/utils/src/path.cpp +++ b/modules/utils/src/path.cpp @@ -215,6 +215,30 @@ bool Path::IsFile() const return S_ISREG(tmp.st_mode); } +bool Path::ExistsAndIsFile() const +{ + bool flag = false; + Try + { + flag = this->IsFile(); + } Catch (Path::NotExists) { + LogPedantic(*this << "is not a file."); + } + return flag; +} + +bool Path::ExistsAndIsDir() const +{ + bool flag = false; + Try + { + flag = this->IsDir(); + } Catch (Path::NotExists) { + LogPedantic(*this << "is not a directory."); + } + return flag; +} + bool Path::IsSymlink() const { struct stat tmp; diff --git a/tests/utils/path_tests.cpp b/tests/utils/path_tests.cpp index 7ad96d2..3f97fe3 100644 --- a/tests/utils/path_tests.cpp +++ b/tests/utils/path_tests.cpp @@ -101,6 +101,26 @@ RUNNER_TEST(path_mkfile_exists) RUNNER_ASSERT_MSG(cannotCreate2ndTime, "File created should not be able to be created second time"); } +/* +Name: path_exists_and_is_file_or_dir +Description: test for checking for existence of directory or file +Expected: success +*/ +RUNNER_TEST(path_exists_and_is_file_or_dir) +{ + DPL::ScopedDir sd(rootTest); + + Path file = Path(rootTest) / "testfile.txt"; + MakeEmptyFile(file); + RUNNER_ASSERT_MSG(file.ExistsAndIsFile(), "File should exist"); + RUNNER_ASSERT_MSG(!file.ExistsAndIsDir(), "It should not be a directory"); + + Path dir = Path(rootTest) / "testdir"; + MakeDir(dir); + RUNNER_ASSERT_MSG(dir.ExistsAndIsDir(), "Directory should exist"); + RUNNER_ASSERT_MSG(!dir.ExistsAndIsFile(), "Is should not be a file"); +} + /* Name: path_mkfile_invalid_path Description: tries to create file in not exisitng directory -- 2.34.1