summaryrefslogtreecommitdiff
path: root/src/utils/mkdir.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2011-04-30 17:41:58 +0300
committerAndrei Karas <akaras@inbox.ru>2011-04-30 17:41:58 +0300
commit2032f0bab7b603f536996079d658c653b5e4e6c6 (patch)
tree3097fd9a2ace5981c8a608f13184c3c3a57e838f /src/utils/mkdir.cpp
parent3a8dd66c23e2c7b73a0a8b61b437392d37dad739 (diff)
downloadmv-2032f0bab7b603f536996079d658c653b5e4e6c6.tar.gz
mv-2032f0bab7b603f536996079d658c653b5e4e6c6.tar.bz2
mv-2032f0bab7b603f536996079d658c653b5e4e6c6.tar.xz
mv-2032f0bab7b603f536996079d658c653b5e4e6c6.zip
Fix windows mkdir issues.
Diffstat (limited to 'src/utils/mkdir.cpp')
-rw-r--r--src/utils/mkdir.cpp79
1 files changed, 65 insertions, 14 deletions
diff --git a/src/utils/mkdir.cpp b/src/utils/mkdir.cpp
index 4ab9f7d61..bc20cb196 100644
--- a/src/utils/mkdir.cpp
+++ b/src/utils/mkdir.cpp
@@ -37,6 +37,70 @@
#include "mkdir.h"
+#if defined WIN32
+int mkdir_r(const char *pathname)
+{
+ char tmp[PATH_MAX];
+ char *p;
+
+ if (strlen(pathname) >= PATH_MAX - 2)
+ return -1;
+
+ strncpy(tmp, pathname, sizeof(tmp) - 1);
+ tmp[PATH_MAX - 1] = '\0';
+
+ int len = static_cast<int>(strlen(tmp));
+
+ // terminate the pathname with '/'
+ if (tmp[len - 1] != '/')
+ {
+ tmp[len] = '/';
+ tmp[len + 1] = '\0';
+ }
+
+ for (p = tmp; *p; p++)
+ {
+ if (*p == '/' || *p == '\\')
+ {
+ *p = '\0';
+ // ignore a slash at the beginning of a path
+ if (strlen(tmp) == 0)
+ {
+ *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 (!CreateDirectory(tmp, 0))
+ {
+ // hack, hack. just assume that x: might be a drive
+ // letter, and try again
+ if (!(strlen(tmp) == 2 &&
+ !strcmp(tmp + 1, ":")))
+ return -1;
+ }
+
+#ifdef _MKDIR_TEST_
+ printf("%s\n", tmp);
+#endif
+ *p = '/';
+ }
+ }
+ return 0;
+}
+#else
/// Create a directory, making leading components first if necessary
int mkdir_r(const char *pathname)
{
@@ -55,11 +119,7 @@ int mkdir_r(const char *pathname)
for (p = tmp; *p; p++)
{
-#if defined WIN32
- if (*p == '/' || *p == '\\')
-#else
if (*p == '/')
-#endif
{
*p = '\0';
// ignore a slash at the beginning of a path
@@ -85,18 +145,8 @@ int mkdir_r(const char *pathname)
}
}
-#if defined WIN32
- if (!CreateDirectory(tmp, 0))
-#else
if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH))
-#endif
{
-#if defined WIN32
- // hack, hack. just assume that x: might be a drive
- // letter, and try again
- if (!(strlen(tmp) == 2 &&
- !strcmp(tmp + 1, ":")))
-#endif
delete []tmp;
return -1;
}
@@ -110,6 +160,7 @@ int mkdir_r(const char *pathname)
delete []tmp;
return 0;
}
+#endif
#ifdef _MKDIR_TEST_
int main(int argc, char** argv)