Replace recursive implementation with a forward loop in Utility::MkDirP()

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
This commit is contained in:
Michael Friedrich 2014-06-13 09:10:35 +02:00
parent 2acf6a063e
commit 1eb77b0cd7
3 changed files with 25 additions and 19 deletions

View 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)
bool Utility::MkDir(const String& path, int flags)
{
#ifdef _WIN32
/* TODO */
return 0;
#else /* _WIN32 */
if (path.IsEmpty()) {
errno = EINVAL;
return 1;
#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;
}

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

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