]> granicus.if.org Git - icinga2/commitdiff
Replace recursive implementation with a forward loop in Utility::MkDirP()
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 13 Jun 2014 07:10:35 +0000 (09:10 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Fri, 13 Jun 2014 07:10:35 +0000 (09:10 +0200)
That way we always move into the tree, but not start in the deepest
level and may limit the tree level too in the future, if required.

Solves the Win32 implementation by moving the general mkdir() call into
Utility::MkDir().

refs #6328

lib/base/utility.cpp
lib/base/utility.hpp
lib/remote/apilistener-sync.cpp

index 9f10f5578f4aedc7c9aa829c0472885ec02d69ab..5eac40d35c992c93fd86904f0540eff02647ec93 100644 (file)
@@ -671,28 +671,33 @@ 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;
+bool Utility::MkDir(const String& path, int flags)
+{
+#ifndef _WIN32
+       if (mkdir(path.CStr(), flags) < 0 && errno != EEXIST) {
+#else /*_ WIN32 */
+       if (mkdir(path.CStr()) < 0 && errno != EEXIST) {
+#endif /* _WIN32 */
+               //TODO handle missing dirs properly
+               return false;
        }
 
-       if (path.GetLength() == 1 && path[0] == '.')
-               return 0;
+       return true;
+}
 
-       String dir = DirName(path);
+bool Utility::MkDirP(const String& path, int flags)
+{
+       size_t pos = 0;
 
-       MkDirP(dir, flags);
+       bool ret = true;
 
-       return mkdir(dir.CStr(), flags);
-#endif /* _WIN32 */
+       while (ret && pos != String::NPos) {
+               pos = path.Find("/", pos + 1);
+               ret = MkDir(path.SubStr(0, pos), flags);
+       }
+
+       return ret;
 }
 
 
index 5e5a36999c97dcb2843d2cce3b84873cfa32be05..f3e931850b173d82e86e8e301300792031a05284 100644 (file)
@@ -81,7 +81,8 @@ 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 bool MkDir(const String& path, int flags);
+       static bool MkDirP(const String& path, int flags);
 
        static void QueueAsyncCallback(const boost::function<void (void)>& callback);
 
index dc98fed30e1300b704d320d91309692bad2218d5..fab2d92f9df71ff57401de885dfb741f11caccdf 100644 (file)
@@ -76,8 +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);
+                       //pass the directory and generate a dir tree, if not existing already
+                       Utility::MkDirP(Utility::DirName(path), 0755);
                        std::ofstream fp(path.CStr(), std::ofstream::out | std::ostream::trunc);
                        fp << kv.second;
                        fp.close();