]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#34] [#2006] Introduced file utilities
authorFrancis Dupont <fdupont@isc.org>
Wed, 8 Dec 2021 16:56:24 +0000 (17:56 +0100)
committerFrancis Dupont <fdupont@isc.org>
Thu, 6 Jan 2022 12:12:40 +0000 (13:12 +0100)
src/lib/util/Makefile.am
src/lib/util/file_utilities.cc [new file with mode: 0644]
src/lib/util/file_utilities.h [new file with mode: 0644]
src/lib/util/tests/Makefile.am
src/lib/util/tests/file_utilities_unittest.cc [new file with mode: 0644]

index 8004dbd6a34a848ed5f2efda95cc0a2366a30a23..983b91c62663339278db49693bb7ef1139f2afb8 100644 (file)
@@ -12,6 +12,7 @@ libkea_util_la_SOURCES += buffer.h io_utilities.h
 libkea_util_la_SOURCES += chrono_time_utils.h chrono_time_utils.cc
 libkea_util_la_SOURCES += csv_file.h csv_file.cc
 libkea_util_la_SOURCES += doubles.h
+libkea_util_la_SOURCES += file_utilities.h file_utilities.cc
 libkea_util_la_SOURCES += filename.h filename.cc
 libkea_util_la_SOURCES += hash.h
 libkea_util_la_SOURCES += labeled_value.h labeled_value.cc
@@ -58,6 +59,7 @@ libkea_util_include_HEADERS = \
        buffer.h \
        csv_file.h \
        doubles.h \
+       file_utilities.h \
        filename.h \
        hash.h \
        io_utilities.h \
diff --git a/src/lib/util/file_utilities.cc b/src/lib/util/file_utilities.cc
new file mode 100644 (file)
index 0000000..7ce544d
--- /dev/null
@@ -0,0 +1,58 @@
+// Copyright (C) 2021 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+
+#include <exceptions/exceptions.h>
+#include <util/filename.h>
+#include <cerrno>
+#include <cstring>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+using namespace std;
+
+namespace isc {
+namespace util {
+
+string
+getContent(const string& file_name) {
+    // Open the file.
+    int fd = ::open(file_name.c_str(), O_RDONLY);
+    if (fd < 0) {
+        isc_throw(BadValue, "can't open file '" << file_name << "': "
+                  << std::strerror(errno));
+    }
+    try {
+        struct stat stats;
+        if (fstat(fd, &stats) < 0) {
+            isc_throw(BadValue, "can't stat file '" << file_name << "': "
+                      << std::strerror(errno));
+        }
+        if ((stats.st_mode & S_IFMT) != S_IFREG) {
+            isc_throw(BadValue, "'" << file_name
+                      << "' must be a regular file");
+        }
+        string content(stats.st_size, ' ');
+        ssize_t got = ::read(fd, &content[0], stats.st_size);
+        if (got < 0) {
+            isc_throw(BadValue, "can't read file '" << file_name << "': "
+                      << std::strerror(errno));
+        }
+        if (got != stats.st_size) {
+            isc_throw(BadValue, "can't read whole file '" << file_name
+                      << "' (got " << got << " of " << stats.st_size << ")");
+        }
+        static_cast<void>(close(fd));
+        return (content);
+    } catch (const std::exception&) {
+        static_cast<void>(close(fd));
+        throw;
+    }
+}
+
+} // namespace log
+} // namespace isc
diff --git a/src/lib/util/file_utilities.h b/src/lib/util/file_utilities.h
new file mode 100644 (file)
index 0000000..75ecce4
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright (C) 2021 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#ifndef FILE_UTILITIES_H
+#define FILE_UTILITIES_H
+
+#include <string>
+
+namespace isc {
+namespace util {
+
+/// @brief Get the content of a regular file.
+///
+/// @param file_name The file name.
+/// @return The content of the file_name file.
+/// @throw BadValue when the file can't be opened or is not a regular one.
+std::string getContent(const std::string& file_name);
+
+} // namespace util
+} // namespace isc
+
+#endif // FILE_UTILITIES_H
index 4370a41b260d3bebc0df4e26227bfe4f290b5f79..df01b48cae977e224897f2862c3c2841753fda60 100644 (file)
@@ -33,6 +33,7 @@ run_unittests_SOURCES += csv_file_unittest.cc
 run_unittests_SOURCES += doubles_unittest.cc
 run_unittests_SOURCES += fd_share_tests.cc
 run_unittests_SOURCES += fd_tests.cc
+run_unittests_SOURCES += file_utilities_unittest.cc
 run_unittests_SOURCES += filename_unittest.cc
 run_unittests_SOURCES += hash_unittest.cc
 run_unittests_SOURCES += hex_unittest.cc
diff --git a/src/lib/util/tests/file_utilities_unittest.cc b/src/lib/util/tests/file_utilities_unittest.cc
new file mode 100644 (file)
index 0000000..6735b4b
--- /dev/null
@@ -0,0 +1,78 @@
+// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+
+#include <exceptions/exceptions.h>
+#include <util/file_utilities.h>
+#include <gtest/gtest.h>
+#include <fstream>
+
+using namespace isc;
+using namespace isc::util;
+using namespace std;
+
+namespace {
+
+/// @brief Test fixture class for testing operations on files.
+class FileUtilTest : public ::testing::Test {
+public:
+
+    /// @brief Destructor.
+    ///
+    /// Deletes the test file if any.
+    virtual ~FileUtilTest();
+};
+
+FileUtilTest::~FileUtilTest() {
+    string test_file_name = string(TEST_DATA_BUILDDIR) + "/fu.test";
+    static_cast<void>(remove(test_file_name.c_str()));
+}
+
+/// @brief Check an error is returned by getContent on not existent file.
+TEST_F(FileUtilTest, notExists) {
+    string file_name("/this/does/not/exists");
+    try {
+        string c = getContent(file_name);
+        FAIL() << "this test must throw before this line";
+    } catch (const BadValue& ex) {
+        string expected = "can't open file '" + file_name;
+        expected += "': No such file or directory";
+        EXPECT_EQ(string(ex.what()), expected);
+    } catch (const std::exception& ex) {
+        FAIL() << "unexpected exception: " << ex.what();
+    }
+}
+
+/// @note No easy can't stat.
+
+/// @brief Check an error is returned by getContent on not regular file.
+TEST_F(FileUtilTest, notRegular) {
+    string file_name("/");
+    try {
+        string c = getContent(file_name);
+        FAIL() << "this test must throw before this line";
+    } catch (const BadValue& ex) {
+        string expected = "'" + file_name + "' must be a regular file";
+        EXPECT_EQ(string(ex.what()), expected);
+    } catch (const std::exception& ex) {
+        FAIL() << "unexpected exception: " << ex.what();
+    }
+}
+
+/// @brief Check getContent works.
+TEST_F(FileUtilTest, basic) {
+    string file_name = string(TEST_DATA_BUILDDIR) + "/fu.test";
+    ofstream fs(file_name.c_str(), ofstream::out | ofstream::trunc);
+    ASSERT_TRUE(fs.is_open());
+    fs << "abdc";
+    fs.close();
+    string content;
+    EXPECT_NO_THROW(content = getContent(file_name));
+    EXPECT_EQ("abdc", content);
+}
+
+}