From b16e3ebe6c2113b24bf170064b70f4d529a74a82 Mon Sep 17 00:00:00 2001 From: psychon Date: Thu, 8 May 2008 17:47:55 +0000 Subject: [PATCH] Replace CDir::MakeDir() with a version that doesn't do chdir() This means that ZNC now doesn't break with relative data dirs (for me) any more! git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1045 726aef4b-f618-498e-8847-2d620e286838 --- FileUtils.cpp | 72 +++++++++++++++++++-------------------------------- FileUtils.h | 2 +- 2 files changed, 27 insertions(+), 47 deletions(-) diff --git a/FileUtils.cpp b/FileUtils.cpp index 364458e5..905a2443 100644 --- a/FileUtils.cpp +++ b/FileUtils.cpp @@ -397,62 +397,42 @@ CString CDir::ChangeDir(const CString& sPath, const CString& sAdd, const CString return (sRet.empty()) ? "/" : sRet; } -int CDir::MakeDir(const CString& sPath, mode_t iMode) { - CString sDir = sPath; - CString::size_type iFind = sDir.find("/"); +bool CDir::MakeDir(const CString& sPath, mode_t iMode) { + CString sDir; + VCString dirs; + VCString::iterator it; - if (iFind == CString::npos) { - return mkdir(sDir.c_str(), iMode); - } - iFind++; + // Just in case someone tries this... + if (sPath.empty()) + return false; - while ((iFind < sDir.length()) && (sDir[iFind] == '/')) { - iFind++; // eat up extra /'s - } + // If this is an absolute path, we need to handle this now! + if (sPath.Left(1) == "/") + sDir = "/"; - if (iFind >= sDir.length()) { - return mkdir(sDir.c_str(), iMode); - } + // For every single subpath, do... + sPath.Split("/", dirs, false); + for (it = dirs.begin(); it != dirs.end(); it++) { + // Add this to the path we already created + sDir += *it; - CString sWorkDir = sDir.substr(0, iFind); // include the trailing slash - CString sNewDir = sDir.erase(0, iFind); + int i = mkdir(sDir.c_str(), iMode); - struct stat st; + if (i != 0) { + // All errors except EEXIST are fatal + if (errno != EEXIST) + return false; - if (sWorkDir.length() > 1) { - sWorkDir = sWorkDir.erase(sWorkDir.length() - 1, 1); // trim off the trailing slash - } - - if (stat(sWorkDir.c_str(), &st) == 0) { - int iChdir = chdir(sWorkDir.c_str()); - if (iChdir != 0) { - return iChdir; // could not change to dir + // If it's EEXIST we have to make sure it's a dir + if (!CFile::IsDir(sDir)) + return false; } - // go ahead and call the next step - return MakeDir(sNewDir.c_str(), iMode); + sDir += "/"; } - switch(errno) { - case ENOENT: { - // ok, file doesn't exists, lets create it and cd into it - int iMkdir = mkdir(sWorkDir.c_str(), iMode); - if (iMkdir != 0) { - return iMkdir; // could not create dir - } - - int iChdir = chdir(sWorkDir.c_str()); - if (iChdir != 0) { - return iChdir; // could not change to dir - } - - return MakeDir(sNewDir.c_str(), iMode); - } - default: - break; - } - - return -1; + // All went well + return true; } int CExecSock::popen2(int & iReadFD, int & iWriteFD, const CString & sCommand) { diff --git a/FileUtils.h b/FileUtils.h index 0f3f60b6..f90427d1 100644 --- a/FileUtils.h +++ b/FileUtils.h @@ -267,7 +267,7 @@ public: bool IsDescending() { return m_bDesc; } static CString ChangeDir(const CString& sPath, const CString& sAdd, const CString& sHomeDir); - static int MakeDir(const CString& sPath, mode_t iMode = 0700); + static bool MakeDir(const CString& sPath, mode_t iMode = 0700); static CString GetCWD() { CString sRet;