[icinga-checkins] icinga.org: icinga2/fix/sync-zones.d-recursively-6328: Fix syncing zones.d from etc/ to var/lib in a recursive manner

git at icinga.org git at icinga.org
Thu Jun 12 23:16:47 CEST 2014


Module: icinga2
Branch: fix/sync-zones.d-recursively-6328
Commit: 535932604e23ab677f8579d4d7915465180cc633
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=535932604e23ab677f8579d4d7915465180cc633

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Thu Jun 12 22:51:48 2014 +0200

Fix syncing zones.d from etc/ to var/lib in a recursive manner

Missing mkdir -p basically. Introducing Utility::MkDirP() which still
requires Win32 implementation.

refs #6328

---

 lib/base/utility.cpp            |   26 ++++++++++++++++++++++++++
 lib/base/utility.hpp            |    1 +
 lib/remote/apilistener-sync.cpp |    8 ++++++--
 lib/remote/apilistener.hpp      |    2 +-
 4 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp
index b59a1f8..9f10f55 100644
--- a/lib/base/utility.cpp
+++ b/lib/base/utility.cpp
@@ -23,6 +23,7 @@
 #include "base/logger_fwd.hpp"
 #include "base/exception.hpp"
 #include "base/socket.hpp"
+#include "base/utility.hpp"
 #include <mmatch.h>
 #include <boost/lexical_cast.hpp>
 #include <boost/foreach.hpp>
@@ -670,6 +671,31 @@ bool Utility::GlobRecursive(const String& path, const String& pattern, const boo
 	return true;
 }
 
+int Utility::MkDirP(const String& path, int flags)
+{
+
+#ifdef _WIN32
+	/* TODO */
+	return 0;
+#else /* _WIN32 */
+
+	if (path.IsEmpty()) {
+		errno = EINVAL;
+		return 1;
+	}
+
+	if (path.GetLength() == 1 && path[0] == '.')
+		return 0;
+
+	String dir = DirName(path);
+
+	MkDirP(dir, flags);
+
+	return mkdir(dir.CStr(), flags);
+#endif /* _WIN32 */
+}
+
+
 #ifndef _WIN32
 void Utility::SetNonBlocking(int fd)
 {
diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp
index f6e4189..5e5a369 100644
--- a/lib/base/utility.hpp
+++ b/lib/base/utility.hpp
@@ -81,6 +81,7 @@ public:
 
 	static bool Glob(const String& pathSpec, const boost::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
 	static bool GlobRecursive(const String& path, const String& pattern, const boost::function<void (const String&)>& callback, int type = GlobFile | GlobDirectory);
+	static int MkDirP(const String& path, int flags);
 
 	static void QueueAsyncCallback(const boost::function<void (void)>& callback);
 
diff --git a/lib/remote/apilistener-sync.cpp b/lib/remote/apilistener-sync.cpp
index 9c561a9..dc98fed 100644
--- a/lib/remote/apilistener-sync.cpp
+++ b/lib/remote/apilistener-sync.cpp
@@ -35,10 +35,12 @@ bool ApiListener::IsConfigMaster(const Zone::Ptr& zone) const
 	return Utility::PathExists(path);
 }
 
-void ApiListener::ConfigGlobHandler(const Dictionary::Ptr& config, const String& path, const String& file)
+void ApiListener::ConfigGlobHandler(Dictionary::Ptr& config, const String& path, const String& file)
 {
 	CONTEXT("Creating config update for file '" + file + "'");
 
+	Log(LogNotice, "ApiListener", "Creating config update for file '" + file + "'");
+
 	std::ifstream fp(file.CStr());
 	if (!fp)
 		return;
@@ -50,7 +52,7 @@ void ApiListener::ConfigGlobHandler(const Dictionary::Ptr& config, const String&
 Dictionary::Ptr ApiListener::LoadConfigDir(const String& dir)
 {
 	Dictionary::Ptr config = make_shared<Dictionary>();
-	Utility::GlobRecursive(dir, "*.conf", boost::bind(&ApiListener::ConfigGlobHandler, config, dir, _1), GlobFile);
+	Utility::GlobRecursive(dir, "*.conf", boost::bind(&ApiListener::ConfigGlobHandler, boost::ref(config), dir, _1), GlobFile);
 	return config;
 }
 
@@ -74,6 +76,8 @@ bool ApiListener::UpdateConfigDir(const Dictionary::Ptr& oldConfig, const Dictio
 			String path = configDir + "/" + kv.first;
 			Log(LogInformation, "ApiListener", "Updating configuration file: " + path);
 
+			//TODO mkdir -p?
+			Utility::MkDirP(path, 0755);
 			std::ofstream fp(path.CStr(), std::ofstream::out | std::ostream::trunc);
 			fp << kv.second;
 			fp.close();
diff --git a/lib/remote/apilistener.hpp b/lib/remote/apilistener.hpp
index cac4f93..a8a8609 100644
--- a/lib/remote/apilistener.hpp
+++ b/lib/remote/apilistener.hpp
@@ -110,7 +110,7 @@ private:
 	void SyncZoneDir(const Zone::Ptr& zone) const;
 
 	bool IsConfigMaster(const Zone::Ptr& zone) const;
-	static void ConfigGlobHandler(const Dictionary::Ptr& config, const String& path, const String& file);
+	static void ConfigGlobHandler(Dictionary::Ptr& config, const String& path, const String& file);
 	void SendConfigUpdate(const ApiClient::Ptr& aclient);
 };
 



More information about the icinga-checkins mailing list