diff options
author | Bernd Wachter <bwachter-tmw@lart.info> | 2010-03-01 01:05:00 +0100 |
---|---|---|
committer | Bernd Wachter <bwachter-tmw@lart.info> | 2010-03-01 01:05:00 +0100 |
commit | 56e9e78a8775be791bf1acf203251aa28f0fa0cb (patch) | |
tree | 47a5a8d27424e9ee897987d6ea4dd6e043c6bde1 /src/utils | |
parent | 23070ec12d1c0ed92c98cc1b424de535f6539abe (diff) | |
download | mana-56e9e78a8775be791bf1acf203251aa28f0fa0cb.tar.gz mana-56e9e78a8775be791bf1acf203251aa28f0fa0cb.tar.bz2 mana-56e9e78a8775be791bf1acf203251aa28f0fa0cb.tar.xz mana-56e9e78a8775be791bf1acf203251aa28f0fa0cb.zip |
Handle cases properly where the path already exists, but not as directory
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/mkdir.cpp | 24 |
1 files changed, 18 insertions, 6 deletions
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 <windows.h> -#else -#include <sys/stat.h> #endif +#include <sys/stat.h> + #ifdef _MKDIR_TEST_ // compile with -D_MKDIR_TEST_ to get a standalone binary #include <cstdio> @@ -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 |