Add ExistsAndIsFile() and ExistsAndIsDir() in DPL::Utils::Path
authorKamil Nowac <k.nowac@partner.samsung.com>
Thu, 22 Aug 2013 08:18:30 +0000 (10:18 +0200)
committerSoo-Hyun Choi <sh9.choi@samsung.com>
Tue, 27 Aug 2013 13:43:50 +0000 (22:43 +0900)
[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
modules/utils/src/path.cpp
tests/utils/path_tests.cpp

index ff7b544fee2173447c38e4a2610a06322e1f2979..3883d3d12c50579dfa16c66a1fe25e62e9442912 100644 (file)
@@ -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;
     /**
index 51b54aaa3bcb45ce105c1348902ad1db61e0e820..213f59b05f46f2940c4983fdb69f7a53a1eddcb1 100644 (file)
@@ -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;
index 7ad96d2f817451c6062b39c7c7e9aab391c8d2c6..3f97fe30e2fd33e78aef79c7586f904199300688 100644 (file)
@@ -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