From 56e9e78a8775be791bf1acf203251aa28f0fa0cb Mon Sep 17 00:00:00 2001 From: Bernd Wachter Date: Mon, 1 Mar 2010 01:05:00 +0100 Subject: Handle cases properly where the path already exists, but not as directory --- src/utils/mkdir.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/utils/mkdir.cpp b/src/utils/mkdir.cpp index 223abe71..43f5264e 100644 --- a/src/utils/mkdir.cpp +++ b/src/utils/mkdir.cpp @@ -24,10 +24,10 @@ #if defined WIN32 #include -#else -#include #endif +#include + #ifdef _MKDIR_TEST_ // compile with -D_MKDIR_TEST_ to get a standalone binary #include @@ -67,12 +67,24 @@ int mkdir_r(const char *pathname) { *p = '/'; continue; } + + // check if the name already exists, but not as directory + struct stat statbuf; + if (!stat(tmp, &statbuf)) + { + if (S_ISDIR(statbuf.st_mode)) + { + *p = '/'; + continue; + } + else + return -1; + } + #if defined WIN32 - if (!CreateDirectory(tmp, 0) && - GetLastError() != ERROR_ALREADY_EXISTS) + if (!CreateDirectory(tmp, 0)) #else - if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && - errno != EEXIST) + if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) #endif { #if defined WIN32 -- cgit v1.2.3-70-g09d2