changeset 534:a864aa8c6331

Fix mkdir -p to accept paths that already exist, and detect path ending in a file.
author Rob Landley <rob@landley.net>
date Wed, 07 Mar 2012 20:05:36 -0600
parents 31215cc6c9f2
children d51be130fda2
files toys/mkdir.c
diffstat 1 files changed, 26 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/toys/mkdir.c	Wed Mar 07 19:04:50 2012 -0600
+++ b/toys/mkdir.c	Wed Mar 07 20:05:36 2012 -0600
@@ -30,21 +30,35 @@
 
 static int do_mkdir(char *dir)
 {
-	unsigned int i;
+	struct stat buf;
+	char *s;
 
-	if (toys.optflags && *dir) {
-		// Skip first char (it can be /)
-		for (i = 1; dir[i]; i++) {
-			int ret;
+	// 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;
+	}
 
-			if (dir[i] != '/') continue;
-			dir[i] = 0;
-			ret = mkdir(dir, TT.mode);
-			if (ret < 0 && errno != EEXIST) return ret;
-			dir[i] = '/';
-		}
+	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 mkdir(dir, TT.mode);
+
+	return 0;
 }
 
 void mkdir_main(void)