]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Factor out code responsible for gathering files in a directory.
authorMiod Vallat <miod.vallat@powerdns.com>
Mon, 16 Jun 2025 13:18:08 +0000 (15:18 +0200)
committerMiod Vallat <miod.vallat@powerdns.com>
Wed, 1 Jul 2026 14:57:35 +0000 (16:57 +0200)
Signed-off-by: Miod Vallat <miod.vallat@powerdns.com>
pdns/Makefile.am
pdns/arguments.cc
pdns/dnsdemog.cc
pdns/dnsdistdist/logger.hh [new symlink]
pdns/kvresp.cc
pdns/lua-base4.cc
pdns/misc.cc
pdns/misc.hh

index 934d1f400bd3d162301c52898e4f22a3313eb1b2..6b25f03553f0d825aaa1b2135888ccaa0849d1ee 100644 (file)
@@ -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 \
index 857ecf1607c705b2d0e4dd468f3cad2a5c3b1d02..0ef2c11e03df89ca35b93d1dca43830b6e89d7ec 100644 (file)
@@ -564,36 +564,7 @@ void ArgvMap::gatherIncludes(const std::string& directory, const std::string& su
     return; // nothing to do
   }
 
-  std::vector<std::string> 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<std::string> vec = pdns::list_directory(directory, suffix, d_log);
   std::sort(vec.begin(), vec.end(), CIStringComparePOSIX());
   extraConfigs.insert(extraConfigs.end(), vec.begin(), vec.end());
 }
index 7c152d1b5811a4aaad32e16206d143a9be2da7d4..2567ae1d92856d4b7f68e70fd812a3f3cfdd4a70 100644 (file)
@@ -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 (symlink)
index 0000000..1ab94e9
--- /dev/null
@@ -0,0 +1 @@
+../logger.hh
\ No newline at end of file
index 8e32b3b094653c4cf38c9a5a855685150bcd33aa..8451cec098e93541b1b2e9bea2956f8f827e76eb 100644 (file)
@@ -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)
index 4d2603f0c757a077dbae54dc9912970faca2a187..b39bae6491c4914490dbabb55e3f89c38aa1b6b3 100644 (file)
@@ -36,41 +36,9 @@ void BaseLua4::loadString(const std::string &script) {
 };
 
 void BaseLua4::includePath(const std::string& directory) {
-  std::vector<std::string> 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<std::string> 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);
   }
index c476b07ef5eba4a65ad42e7f7f00941c2fe54081..23c3024071742101b6ef4666e4106edbadb048ca 100644 (file)
@@ -26,6 +26,7 @@
 
 #include <sys/param.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <fcntl.h>
 #include <netdb.h>
 #include <sys/time.h>
@@ -57,6 +58,8 @@
 #include "iputils.hh"
 #include "dnsparser.hh"
 #include "dns_random.hh"
+#include "logger.hh"
+#include "logging.hh"
 #include <pwd.h>
 #include <grp.h>
 #include <climits>
@@ -1789,6 +1792,43 @@ std::optional<std::string> visit_directory(const std::string& directory, const s
   return std::nullopt;
 }
 
+std::vector<std::string> list_directory(const std::string& directory, const std::string& suffix, Logr::log_t d_log)
+{
+  std::vector<std::string> 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;
index 0165b1418d0c587c46e0a9d085322ccd281598d2..3eca5875736783f81a2c52f001f1dbd541abfa0f 100644 (file)
@@ -45,6 +45,7 @@
 #include <vector>
 
 #include "namespaces.hh"
+#include "logr.hh"
 
 class DNSName;
 #if defined(PDNS_AUTH)
@@ -976,6 +977,8 @@ namespace pdns
 {
 [[nodiscard]] std::optional<std::string> visit_directory(const std::string& directory, const std::function<bool(ino_t inodeNumber, const std::string_view& name)>& visitor);
 
+std::vector<std::string> 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: