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