From: Miod Vallat Date: Mon, 16 Jun 2025 13:18:08 +0000 (+0200) Subject: Factor out code responsible for gathering files in a directory. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=89b933b2fc8a70ccdc57801305c8a61d93c843e7;p=thirdparty%2Fpdns.git Factor out code responsible for gathering files in a directory. Signed-off-by: Miod Vallat --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 934d1f400b..6b25f03553 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -1691,6 +1691,8 @@ fuzz_target_packetcache_SOURCES = \ dnsname.cc dnsname.hh \ ednsoptions.cc ednsoptions.hh \ fuzz_packetcache.cc \ + logger.cc logger.hh \ + logging.cc logging.hh \ misc.cc misc.hh \ packetcache.hh \ qtype.cc qtype.hh \ diff --git a/pdns/arguments.cc b/pdns/arguments.cc index 857ecf1607..0ef2c11e03 100644 --- a/pdns/arguments.cc +++ b/pdns/arguments.cc @@ -564,36 +564,7 @@ void ArgvMap::gatherIncludes(const std::string& directory, const std::string& su return; // nothing to do } - std::vector vec; - auto directoryError = pdns::visit_directory(directory, [this, &directory, &suffix, &vec]([[maybe_unused]] ino_t inodeNumber, const std::string_view& name) { - (void)this; - if (boost::starts_with(name, ".")) { - return true; // skip any dots - } - if (boost::ends_with(name, suffix)) { - // build name - string fullName = directory + "/" + std::string(name); - // ensure it's readable file - struct stat statInfo{}; - if (stat(fullName.c_str(), &statInfo) != 0 || !S_ISREG(statInfo.st_mode)) { - string msg = fullName + " is not a regular file"; - SLOG(g_log << Logger::Error << msg << std::endl, - d_log->info(Logr::Error, "Unable to open non-regular file", "name", Logging::Loggable(fullName))); - throw ArgException(std::move(msg)); - } - vec.emplace_back(fullName); - } - return true; - }); - - if (directoryError) { - int err = errno; - string msg = directory + " is not accessible: " + stringerror(err); - SLOG(g_log << Logger::Error << msg << std::endl, - d_log->error(Logr::Error, err, "Directory is not accessible", "name", Logging::Loggable(directory))); - throw ArgException(std::move(msg)); - } - + std::vector vec = pdns::list_directory(directory, suffix, d_log); std::sort(vec.begin(), vec.end(), CIStringComparePOSIX()); extraConfigs.insert(extraConfigs.end(), vec.begin(), vec.end()); } diff --git a/pdns/dnsdemog.cc b/pdns/dnsdemog.cc index 7c152d1b58..2567ae1d92 100644 --- a/pdns/dnsdemog.cc +++ b/pdns/dnsdemog.cc @@ -34,6 +34,8 @@ #include "namespaces.hh" +bool g_slogStructured{false}; + StatBag S; struct Entry diff --git a/pdns/dnsdistdist/logger.hh b/pdns/dnsdistdist/logger.hh new file mode 120000 index 0000000000..1ab94e917a --- /dev/null +++ b/pdns/dnsdistdist/logger.hh @@ -0,0 +1 @@ +../logger.hh \ No newline at end of file diff --git a/pdns/kvresp.cc b/pdns/kvresp.cc index 8e32b3b094..8451cec098 100644 --- a/pdns/kvresp.cc +++ b/pdns/kvresp.cc @@ -30,6 +30,8 @@ What it does is provide answers to queries from the Lua generic UDP Question/Answer stuff in kv-example-script.lua */ +bool g_slogStructured{false}; + StatBag S; int main(int argc, char** argv) diff --git a/pdns/lua-base4.cc b/pdns/lua-base4.cc index 4d2603f0c7..b39bae6491 100644 --- a/pdns/lua-base4.cc +++ b/pdns/lua-base4.cc @@ -36,41 +36,9 @@ void BaseLua4::loadString(const std::string &script) { }; void BaseLua4::includePath(const std::string& directory) { - std::vector vec; const std::string& suffix = "lua"; - auto directoryError = pdns::visit_directory(directory, [this, &directory, &suffix, &vec]([[maybe_unused]] ino_t inodeNumber, const std::string_view& name) { - (void)this; - if (boost::starts_with(name, ".")) { - return true; // skip any dots - } - if (boost::ends_with(name, suffix)) { - // build name - string fullName = directory + "/" + std::string(name); - // ensure it's readable file - struct stat statInfo - { - }; - if (stat(fullName.c_str(), &statInfo) != 0 || !S_ISREG(statInfo.st_mode)) { - string msg = fullName + " is not a regular file"; - SLOG(g_log << Logger::Error << msg << std::endl, - g_slog->withName("lua")->info(Logr::Error, "include file is not a regular file", "file", Logging::Loggable(fullName))); - throw PDNSException(std::move(msg)); - } - vec.emplace_back(fullName); - } - return true; - }); - - if (directoryError) { - int err = errno; - string msg = directory + " is not accessible: " + stringerror(err); - SLOG(g_log << Logger::Error << msg << std::endl, - g_slog->withName("lua")->error(Logr::Error, err, "Error trying to walk directory", "directory", Logging::Loggable(directory))); - throw PDNSException(std::move(msg)); - } - + std::vector vec = pdns::list_directory(directory, suffix, g_slog->withName("lua")); std::sort(vec.begin(), vec.end(), CIStringComparePOSIX()); - for(const auto& file: vec) { loadFile(file, false); } diff --git a/pdns/misc.cc b/pdns/misc.cc index c476b07ef5..23c3024071 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -57,6 +58,8 @@ #include "iputils.hh" #include "dnsparser.hh" #include "dns_random.hh" +#include "logger.hh" +#include "logging.hh" #include #include #include @@ -1789,6 +1792,43 @@ std::optional visit_directory(const std::string& directory, const s return std::nullopt; } +std::vector list_directory(const std::string& directory, const std::string& suffix, Logr::log_t d_log) +{ + std::vector results; + + auto directoryError = pdns::visit_directory(directory, + [&directory, &suffix, &results, d_log] + ([[maybe_unused]] ino_t inodeNumber, const std::string_view& name) { + if (boost::starts_with(name, ".")) { + return true; // skip any dots + } + if (boost::ends_with(name, suffix)) { + // build name + string fullName = directory + "/" + std::string(name); + // ensure it's a readable file + struct stat statInfo{}; + if (stat(fullName.c_str(), &statInfo) != 0 || !S_ISREG(statInfo.st_mode)) { + string msg = fullName + " is not a regular file"; + SLOG(g_log << Logger::Error << msg << std::endl, + d_log->info(Logr::Error, "Unable to open non-regular file", "name", Logging::Loggable(fullName))); + throw PDNSException(std::move(msg)); + } + results.emplace_back(fullName); + } + return true; + }); + + if (directoryError) { + int err = errno; + string msg = directory + " is not accessible: " + stringerror(err); + SLOG(g_log << Logger::Error << msg << std::endl, + d_log->error(Logr::Error, err, "Directory is not accessible", "name", Logging::Loggable(directory))); + throw PDNSException(std::move(msg)); + } + + return results; +} + UniqueFilePtr openFileForWriting(const std::string& filePath, mode_t permissions, bool mustNotExist, bool appendIfExists) { int flags = O_WRONLY | O_CREAT; diff --git a/pdns/misc.hh b/pdns/misc.hh index 0165b1418d..3eca587573 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -45,6 +45,7 @@ #include #include "namespaces.hh" +#include "logr.hh" class DNSName; #if defined(PDNS_AUTH) @@ -976,6 +977,8 @@ namespace pdns { [[nodiscard]] std::optional visit_directory(const std::string& directory, const std::function& visitor); +std::vector list_directory(const std::string& directory, const std::string& suffix, Logr::log_t d_log); + struct FilePtrDeleter { /* using a deleter instead of decltype(&fclose) has two big advantages: