From 2acf6a063e98d1685aae90baf046f1cf3bbbdc02 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 12 Jun 2014 22:51:48 +0200 Subject: [PATCH] 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 b59a1f81e..9f10f5578 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 #include #include @@ -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 f6e418928..5e5a36999 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& callback, int type = GlobFile | GlobDirectory); static bool GlobRecursive(const String& path, const String& pattern, const boost::function& callback, int type = GlobFile | GlobDirectory); + static int MkDirP(const String& path, int flags); static void QueueAsyncCallback(const boost::function& callback); diff --git a/lib/remote/apilistener-sync.cpp b/lib/remote/apilistener-sync.cpp index 9c561a956..dc98fed30 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(); - 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 cac4f93b9..a8a8609d9 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); };