]> granicus.if.org Git - icinga2/commitdiff
Implement file type flags for Utility::Glob.
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 22 Nov 2013 08:03:52 +0000 (09:03 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 22 Nov 2013 08:03:52 +0000 (09:03 +0100)
Fixes #5123

components/cluster/clusterlistener.cpp
components/livestatus/logtable.cpp
components/livestatus/statehisttable.cpp
lib/base/unix.h
lib/base/utility.cpp
lib/base/utility.h
lib/config/configcompiler.cpp

index 85a2e0003526e5ad67b29d99ab543a14459d20d1..137dd44589a03f211ce5ed825c0d9eb87e7c98e4 100644 (file)
@@ -383,7 +383,7 @@ void ClusterListener::ReplayLog(const Endpoint::Ptr& endpoint, const Stream::Ptr
                count = 0;
 
                std::vector<int> files;
-               Utility::Glob(GetClusterDir() + "log/*", boost::bind(&ClusterListener::LogGlobHandler, boost::ref(files), _1));
+               Utility::Glob(GetClusterDir() + "log/*", boost::bind(&ClusterListener::LogGlobHandler, boost::ref(files), _1), GlobFile);
                std::sort(files.begin(), files.end());
 
                BOOST_FOREACH(int ts, files) {
@@ -479,6 +479,8 @@ void ClusterListener::ReplayLog(const Endpoint::Ptr& endpoint, const Stream::Ptr
 
 void ClusterListener::ConfigGlobHandler(const Dictionary::Ptr& config, const String& file, bool basename)
 {
+       CONTEXT("Creating config update for file '" + file + "'");
+
        Dictionary::Ptr elem = make_shared<Dictionary>();
 
        std::ifstream fp(file.CStr());
@@ -532,7 +534,7 @@ void ClusterListener::NewClientHandler(const Socket::Ptr& client, TlsRole role)
        if (configFiles) {
                ObjectLock olock(configFiles);
                BOOST_FOREACH(const String& pattern, configFiles) {
-                       Utility::Glob(pattern, boost::bind(&ClusterListener::ConfigGlobHandler, boost::cref(config), _1, false));
+                       Utility::Glob(pattern, boost::bind(&ClusterListener::ConfigGlobHandler, boost::cref(config), _1, false), GlobFile);
                }
        }
 
@@ -591,7 +593,7 @@ void ClusterListener::ClusterTimerHandler(void)
        }
 
        std::vector<int> files;
-       Utility::Glob(GetClusterDir() + "log/*", boost::bind(&ClusterListener::LogGlobHandler, boost::ref(files), _1));
+       Utility::Glob(GetClusterDir() + "log/*", boost::bind(&ClusterListener::LogGlobHandler, boost::ref(files), _1), GlobFile);
        std::sort(files.begin(), files.end());
 
        BOOST_FOREACH(int ts, files) {
@@ -1391,7 +1393,7 @@ void ClusterListener::MessageHandler(const Endpoint::Ptr& sender, const Dictiona
                }
 
                Dictionary::Ptr localConfig = make_shared<Dictionary>();
-               Utility::Glob(dir + "/*", boost::bind(&ClusterListener::ConfigGlobHandler, boost::cref(localConfig), _1, true));
+               Utility::Glob(dir + "/*", boost::bind(&ClusterListener::ConfigGlobHandler, boost::cref(localConfig), _1, true), GlobFile);
 
                bool configChange = false;
 
index 30f3e79c6a2dfde083acf0cb680dedcb008312f1..1493ec3ab2d988826aeb728f8581b388fdb498d4 100644 (file)
@@ -291,8 +291,8 @@ Value LogTable::CommandNameAccessor(const Value& row)
 
 void LogTable::CreateLogIndex(const String& path)
 {
-       Utility::Glob(path + "/icinga.log", boost::bind(&LogTable::CreateLogIndexFileHandler, _1, boost::ref(m_LogFileIndex)));
-       Utility::Glob(path + "/archives/*.log", boost::bind(&LogTable::CreateLogIndexFileHandler, _1, boost::ref(m_LogFileIndex)));
+       Utility::Glob(path + "/icinga.log", boost::bind(&LogTable::CreateLogIndexFileHandler, _1, boost::ref(m_LogFileIndex)), GlobFile);
+       Utility::Glob(path + "/archives/*.log", boost::bind(&LogTable::CreateLogIndexFileHandler, _1, boost::ref(m_LogFileIndex)), GlobFile);
 }
 
 void LogTable::CreateLogIndexFileHandler(const String& path, std::map<unsigned long, String>& index)
index 1d1fa3def4ada11a7c622827d979fd498644506a..908f810b9610baba96df57d51b9fb32d6562d1cb 100644 (file)
@@ -518,8 +518,8 @@ Value StateHistTable::DurationPartUnmonitoredAccessor(const Value& row)
 
 void StateHistTable::CreateLogIndex(const String& path)
 {
-       Utility::Glob(path + "/icinga.log", boost::bind(&StateHistTable::CreateLogIndexFileHandler, _1, boost::ref(m_LogFileIndex)));
-       Utility::Glob(path + "/archives/*.log", boost::bind(&StateHistTable::CreateLogIndexFileHandler, _1, boost::ref(m_LogFileIndex)));
+       Utility::Glob(path + "/icinga.log", boost::bind(&StateHistTable::CreateLogIndexFileHandler, _1, boost::ref(m_LogFileIndex)), GlobFile);
+       Utility::Glob(path + "/archives/*.log", boost::bind(&StateHistTable::CreateLogIndexFileHandler, _1, boost::ref(m_LogFileIndex)), GlobFile);
 }
 
 void StateHistTable::CreateLogIndexFileHandler(const String& path, std::map<unsigned long, String>& index)
index 76e8c1d2fc9998f8cdafc595cd2b726e2e5b94a2..ee6022839f7fd2e06a6a8d801c46c0029c10f77e 100644 (file)
@@ -35,6 +35,8 @@
 #include <sys/wait.h>
 #include <glob.h>
 #include <dlfcn.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 typedef int SOCKET;
 #define INVALID_SOCKET (-1)
index bb32eef419c34592d4f87e1c8da2a7c0f95a06da..d43e12b90af78492ae0e7a4b11a636031c91794a 100644 (file)
@@ -320,8 +320,10 @@ String Utility::NewUniqueID(void)
  * Calls the specified callback for each file matching the path specification.
  *
  * @param pathSpec The path specification.
+ * @param callback The callback which is invoked for each matching file.
+ * @param type The file type (a combination of GlobFile and GlobDirectory)
  */
-bool Utility::Glob(const String& pathSpec, const boost::function<void (const String&)>& callback)
+bool Utility::Glob(const String& pathSpec, const boost::function<void (const String&)>& callback, int type)
 {
 #ifdef _WIN32
        HANDLE handle;
@@ -342,6 +344,12 @@ bool Utility::Glob(const String& pathSpec, const boost::function<void (const Str
        }
 
        do {
+               if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(type & GlobDirectory))
+                       continue;
+
+               if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(type & GlobFile))
+                       continue;
+
                callback(DirName(pathSpec) + "/" + wfd.cFileName);
        } while (FindNextFile(handle, &wfd));
 
@@ -375,6 +383,20 @@ bool Utility::Glob(const String& pathSpec, const boost::function<void (const Str
        size_t left;
        char **gp;
        for (gp = gr.gl_pathv, left = gr.gl_pathc; left > 0; gp++, left--) {
+               struct stat statbuf;
+
+               if (lstat(*gp, &statbuf) < 0)
+                       continue;
+
+               if (!S_ISDIR(statbuf.st_mode) && !S_ISREG(statbuf.st_mode))
+                       continue;
+
+               if (S_ISDIR(statbuf.st_mode) && !(type & GlobDirectory))
+                       continue;
+
+               if (!S_ISDIR(statbuf.st_mode) && !(type & GlobFile))
+                       continue;
+
                callback(*gp);
        }
 
index ddffa4b448cc4ef2cfb3698de7bb8b541782aa44..269b9af96dfdcf9aea6fa83863f282217933a54a 100644 (file)
@@ -43,6 +43,12 @@ struct THREADNAME_INFO
 #      pragma pack(pop)
 #endif
 
+enum GlobType
+{
+       GlobFile = 1,
+       GlobDirectory = 2
+};
+
 /**
  * Helper functions.
  *
@@ -69,7 +75,7 @@ public:
 
        static String NewUniqueID(void);
 
-       static bool Glob(const String& pathSpec, const boost::function<void (const String&)>& callback);
+       static bool Glob(const String& pathSpec, const boost::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
 
        static void QueueAsyncCallback(const boost::function<void (void)>& callback);
 
index cf798480e16043574e976e8538f03f16326f05eb..ea42c78b98f4b3c10a02d0a20d36a4649b0498d4 100644 (file)
@@ -203,7 +203,7 @@ void ConfigCompiler::HandleFileInclude(const String& include, bool search,
 
        std::vector<ConfigItem::Ptr> items;
 
-       if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CompileFile, _1)) && includePath.FindFirstOf("*?") == String::NPos) {
+       if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CompileFile, _1), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
                std::ostringstream msgbuf;
                msgbuf << "Include file '" + include + "' does not exist: " << debuginfo;
                BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));