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
buffer.h \
csv_file.h \
doubles.h \
+ file_utilities.h \
filename.h \
hash.h \
io_utilities.h \
--- /dev/null
+// 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
--- /dev/null
+// 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
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
--- /dev/null
+// 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);
+}
+
+}