]> granicus.if.org Git - pdns/commitdiff
gatherIncludes method for gathering include files from include directory
authorAki Tuomi <cmouse@desteem.org>
Tue, 4 Feb 2014 17:11:47 +0000 (19:11 +0200)
committerAki Tuomi <cmouse@desteem.org>
Fri, 7 Feb 2014 10:56:22 +0000 (12:56 +0200)
pdns/arguments.cc
pdns/arguments.hh

index e526d6f63ae156e3b2db44d47116dca4a43e8312..57b1539faaf081d7a33111a9e49bc507841ec292 100644 (file)
@@ -465,53 +465,57 @@ bool ArgvMap::file(const char *fname, bool lax, bool included)
 
   // handle include here (avoid re-include)
   if (!included && !params["include-dir"].empty()) {
-      // rerun parser for all files
-      struct stat st;
-      DIR *dir;
-      struct dirent *ent;
-
-      // stat
-      if (stat(params["include-dir"].c_str(), &st)) {
-         L << Logger::Error << params["include-dir"] << " does not exist!" << std::endl;
-         throw ArgException(params["include-dir"] + " does not exist!");
+    std::vector<std::string> extraConfigs;
+    gatherIncludes(extraConfigs); 
+    BOOST_FOREACH(const std::string& fn, extraConfigs) {
+      if (!file(fn.c_str(), lax, true)) {
+        L << Logger::Error << fn << " could not be parsed" << std::endl;
+        throw ArgException(fn + " could not be parsed");
       }
+    }
+    closedir(dir);
+  }
 
-      // wonder if it's accessible directory
-      if (!S_ISDIR(st.st_mode)) {
-         L << Logger::Error << params["include-dir"] << " is not a directory" << std::endl;
-         throw ArgException(params["include-dir"] + " is not a directory");
-      }
+  return true;
+}
 
-      if (!(dir = opendir(params["include-dir"].c_str()))) {
-         L << Logger::Error << params["include-dir"] << " is not accessible" << std::endl;
-         throw ArgException(params["include-dir"] + " is not accessible");
-      }
-      
-      std::vector<std::string> extraConfigs;
-      while((ent = readdir(dir)) != NULL) {
-         if (ent->d_name[0] == '.') continue; // skip any dots
-         if (boost::ends_with(ent->d_name, ".conf")) {
-            // build name
-            std::ostringstream namebuf;
-            namebuf << params["include-dir"].c_str() << "/" << ent->d_name; // FIXME: Use some path separator
-            // ensure it's readable file
-            if (stat(namebuf.str().c_str(), &st) || !S_ISREG(st.st_mode)) {
-                L << Logger::Error << namebuf.str() << " is not a file" << std::endl;
-                throw ArgException(namebuf.str() + " does not exist!");
-            }
-            extraConfigs.push_back(namebuf.str());
-         }
-      }
-      std::sort(extraConfigs.begin(), extraConfigs.end(), CIStringComparePOSIX());
-      BOOST_FOREACH(const std::string& fn, extraConfigs) {
-            if (!file(fn.c_str(), lax, true)) {
-                L << Logger::Error << fn << " could not be parsed" << std::endl;
-                throw ArgException(fn + " could not be parsed");
-            }
-      }
+void ArgvMap::gatherIncludes(std::vector<std::string> &extraConfigs) {
+  extraConfigs.clear();
+  if (params["include-dir"].empty()) return; // nothing to do
+    struct stat st;
+    DIR *dir;
+    struct dirent *ent;
+
+    // stat
+    if (stat(params["include-dir"].c_str(), &st)) {
+       L << Logger::Error << params["include-dir"] << " does not exist!" << std::endl;
+       throw ArgException(params["include-dir"] + " does not exist!");
+    }
 
-      closedir(dir);
-  }
+    // wonder if it's accessible directory
+    if (!S_ISDIR(st.st_mode)) {
+       L << Logger::Error << params["include-dir"] << " is not a directory" << std::endl;
+       throw ArgException(params["include-dir"] + " is not a directory");
+    }
 
-  return true;
+    if (!(dir = opendir(params["include-dir"].c_str()))) {
+       L << Logger::Error << params["include-dir"] << " is not accessible" << std::endl;
+       throw ArgException(params["include-dir"] + " is not accessible");
+    }
+
+    while((ent = readdir(dir)) != NULL) {
+      if (ent->d_name[0] == '.') continue; // skip any dots
+      if (boost::ends_with(ent->d_name, ".conf")) {
+        // build name
+        std::ostringstream namebuf;
+        namebuf << params["include-dir"].c_str() << "/" << ent->d_name; // FIXME: Use some path separator
+        // ensure it's readable file
+        if (stat(namebuf.str().c_str(), &st) || !S_ISREG(st.st_mode)) {
+          L << Logger::Error << namebuf.str() << " is not a file" << std::endl;
+          throw ArgException(namebuf.str() + " does not exist!");
+        }
+        extraConfigs.push_back(namebuf.str());
+      }
+    }
+    std::sort(extraConfigs.begin(), extraConfigs.end(), CIStringComparePOSIX()); 
 }
index 15773070605e4aa9d571bdcb10c542aa84ba36e7..afd0ede35ee51ff0f27b222dc05497bb681ecd77 100644 (file)
@@ -116,6 +116,7 @@ public:
   const param_t::const_iterator end(); //!< iterator semantics
   const string &operator[](const string &); //!< iterator semantics
   const vector<string>&getCommands();
+  void gatherIncludes(std::vector<std::string> &extraConfigs);
 private:
   void parseOne(const string &unparsed, const string &parseOnly="", bool lax=false);
   typedef map<string,string> params_t;