Implement file type flags for Utility::Glob.

Fixes #5123
This commit is contained in:
Gunnar Beutner 2013-11-22 09:03:52 +01:00
parent 5bc3b02758
commit 0978e46d5a
7 changed files with 43 additions and 11 deletions

View 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;

View 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)

View 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)

View 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)

View 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);
}

View 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);

View 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()));