]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3025] add isFile
authorAndrei Pavel <andrei@isc.org>
Tue, 20 Feb 2024 15:36:10 +0000 (17:36 +0200)
committerAndrei Pavel <andrei@isc.org>
Thu, 22 Feb 2024 08:06:32 +0000 (10:06 +0200)
src/lib/util/file_utilities.cc
src/lib/util/file_utilities.h
src/lib/util/tests/Makefile.am
src/lib/util/tests/file_utilities_unittest.cc

index dfe5e89324360ae7f8aab94391cc192394fc10c2..e59e8f43cac773ce22cf5ef894e24ee106905c33 100644 (file)
@@ -64,6 +64,16 @@ isDir(const string& name) {
     return ((stats.st_mode & S_IFMT) == S_IFDIR);
 }
 
+bool
+isFile(const string& name) {
+    struct stat stats;
+    if (::stat(name.c_str(), &stats) < 0) {
+        return (false);
+    }
+    return ((stats.st_mode & S_IFMT) == S_IFREG);
+}
+
+
 } // namespace file
 } // namespace log
 } // namespace isc
index 8c34822de6fb8ef1c7c152289c80e84c64509052..4999b2b7eb5c539cb00a5b8e3fbafbdd7dcb56b7 100644 (file)
@@ -20,13 +20,20 @@ namespace file {
 /// @throw BadValue when the file can't be opened or is not a regular one.
 std::string getContent(const std::string& file_name);
 
-/// @brief Is a directory predicate.
+/// @brief Check if there is a directory at the given path.
 ///
-/// @param name The file or directory name.
+/// @param name the path being checked.
 /// @return True if the name points to a directory, false otherwise including
 /// if the pointed location does not exist.
 bool isDir(const std::string& name);
 
+/// @brief Check if there is a file at the given path.
+///
+/// @param name the path being checked.
+/// @return True if the name points to a file, false otherwise including
+/// if the pointed location does not exist.
+bool isFile(const std::string& name);
+
 } // namespace file
 } // namespace util
 } // namespace isc
index de05ec62e97c595ebf194a1c56c2281eedf55451..9a59b4c8caadd9a267c68f62be55bc73cd16b4b9 100644 (file)
@@ -3,10 +3,6 @@ SUBDIRS = .
 AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
 AM_CPPFLAGS += $(BOOST_INCLUDES)
 AM_CPPFLAGS += -DTEST_DATA_BUILDDIR=\"$(abs_builddir)\"
-# XXX: we'll pollute the top builddir for creating a temporary test file
-# used to bind a UNIX domain socket so we can minimize the risk of exceeding
-# the limit of file name path size.
-AM_CPPFLAGS += -DTEST_DATA_TOPBUILDDIR=\"$(abs_top_builddir)\"
 AM_CXXFLAGS = $(KEA_CXXFLAGS)
 
 if USE_STATIC_LINK
index 4ee9093c2843a53b145d4b1efcb2c76b2740333f..478d4764ecb2bb4786d888989044d9f8f05277b2 100644 (file)
@@ -83,4 +83,10 @@ TEST_F(FileUtilTest, isDir) {
     EXPECT_FALSE(isDir("/etc/hosts"));
 }
 
+/// @brief Check isFile.
+TEST_F(FileUtilTest, isFile) {
+    EXPECT_TRUE(isFile("file_utilities_unittest.cc"));
+    EXPECT_FALSE(isFile(TEST_DATA_BUILDDIR));
 }
+
+}  // namespace