aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-03-07 20:05:36 -0600
committerRob Landley <rob@landley.net>2012-03-07 20:05:36 -0600
commit13cacbd09de09e38e4d1227f7122fda8f1d6fc63 (patch)
treeb2305cbd819e6325bc9d7a43abdc3b71a1be2dbe
parentf05f660d9c4f18aa4702d723ed7b35f7053ce08c (diff)
downloadtoybox-13cacbd09de09e38e4d1227f7122fda8f1d6fc63.tar.gz
Fix mkdir -p to accept paths that already exist, and detect path ending in a file.
-rw-r--r--toys/mkdir.c42
1 files changed, 28 insertions, 14 deletions
diff --git a/toys/mkdir.c b/toys/mkdir.c
index d875f95d..90329019 100644
--- a/toys/mkdir.c
+++ b/toys/mkdir.c
@@ -30,21 +30,35 @@ DEFINE_GLOBALS(
static int do_mkdir(char *dir)
{
- unsigned int i;
-
- if (toys.optflags && *dir) {
- // Skip first char (it can be /)
- for (i = 1; dir[i]; i++) {
- int ret;
-
- if (dir[i] != '/') continue;
- dir[i] = 0;
- ret = mkdir(dir, TT.mode);
- if (ret < 0 && errno != EEXIST) return ret;
- dir[i] = '/';
- }
+ struct stat buf;
+ char *s;
+
+ // mkdir -p one/two/three is not an error if the path already exists,
+ // but is if "three" is a file. The others we dereference and catch
+ // not-a-directory along the way, but the last one we must explicitly
+ // test for. Might as well do it up front.
+
+ if (!stat(dir, &buf) && !S_ISDIR(buf.st_mode)) {
+ errno = EEXIST;
+ return 1;
}
- return mkdir(dir, TT.mode);
+
+ for (s=dir; ; s++) {
+ char save=0;
+
+ // Skip leading / of absolute paths.
+ if (s!=dir && *s == '/' && toys.optflags) {
+ save = *s;
+ *s = 0;
+ } else if (*s) continue;
+
+ if (mkdir(dir, TT.mode)<0 && (!toys.optflags || errno != EEXIST))
+ return 1;
+
+ if (!(*s = save)) break;
+ }
+
+ return 0;
}
void mkdir_main(void)