Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 10 Oct 2006 19:24:33 +0400
From:      Ruslan Ermilov <ru@freebsd.org>
To:        Alex Unleashed <unledev@gmail.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: mkdir -m option POSIX compliance
Message-ID:  <20061010152433.GA35377@rambler-co.ru>
In-Reply-To: <5e4707340610100624j38ce9f8du7741bd027eb8cb1b@mail.gmail.com>
References:  <5e4707340610100624j38ce9f8du7741bd027eb8cb1b@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
On Tue, Oct 10, 2006 at 03:24:14PM +0200, Alex Unleashed wrote:
> Hi all,
> 
> "mkdir -m mode -p /some/directory" calls chmod() on /some/directory even if
> it already exists, effectively changing the mode. POSIX specifies that this
> mode may only be applied to newly created directories. Patch attached.
> Please look at the URLs referenced in the patch for further information.
> 
I've modified your patch slightly, to save a variable.
How does this look to you?

%%%
Index: mkdir.c
===================================================================
RCS file: /home/ncvs/src/bin/mkdir/mkdir.c,v
retrieving revision 1.32
diff -u -p -r1.32 mkdir.c
--- mkdir.c	9 Feb 2005 17:37:38 -0000	1.32
+++ mkdir.c	10 Oct 2006 15:22:05 -0000
@@ -99,19 +99,19 @@ main(int argc, char *argv[])
 	}
 
 	for (exitval = 0; *argv != NULL; ++argv) {
-		success = 1;
 		if (pflag) {
-			if (build(*argv, omode))
-				success = 0;
+			success = build(*argv, omode);
 		} else if (mkdir(*argv, omode) < 0) {
 			if (errno == ENOTDIR || errno == ENOENT)
 				warn("%s", dirname(*argv));
 			else
 				warn("%s", *argv);
 			success = 0;
-		} else if (vflag)
-			(void)printf("%s\n", *argv);
-		
+		} else {
+			success = 1;
+			if (vflag)
+				(void)printf("%s\n", *argv);
+		}
 		if (!success)
 			exitval = 1;
 		/*
@@ -119,9 +119,10 @@ main(int argc, char *argv[])
 		 * nine bits, so if you try to set a mode including the
 		 * sticky, setuid, setgid bits you lose them.  Don't do
 		 * this unless the user has specifically requested a mode,
-		 * as chmod will (obviously) ignore the umask.
+		 * as chmod will (obviously) ignore the umask.  Do this
+		 * on newly created directories only.
 		 */
-		if (success && mode != NULL && chmod(*argv, omode) == -1) {
+		if (success == 1 && mode != NULL && chmod(*argv, omode) == -1) {
 			warn("%s", *argv);
 			exitval = 1;
 		}
@@ -129,6 +130,11 @@ main(int argc, char *argv[])
 	exit(exitval);
 }
 
+
+/*
+ * Returns 1 if a directory has been created,
+ * 2 if it already existed, and 0 on failure.
+ */
 int
 build(char *path, mode_t omode)
 {
@@ -139,7 +145,7 @@ build(char *path, mode_t omode)
 
 	p = path;
 	oumask = 0;
-	retval = 0;
+	retval = 1;
 	if (p[0] == '/')		/* Skip leading '/'. */
 		++p;
 	for (first = 1, last = 0; !last ; ++p) {
@@ -174,7 +180,7 @@ build(char *path, mode_t omode)
 			if (errno == EEXIST || errno == EISDIR) {
 				if (stat(path, &sb) < 0) {
 					warn("%s", path);
-					retval = 1;
+					retval = 0;
 					break;
 				} else if (!S_ISDIR(sb.st_mode)) {
 					if (last)
@@ -182,12 +188,14 @@ build(char *path, mode_t omode)
 					else
 						errno = ENOTDIR;
 					warn("%s", path);
-					retval = 1;
+					retval = 0;
 					break;
 				}
+				if (last)
+					retval = 2;
 			} else {
 				warn("%s", path);
-				retval = 1;
+				retval = 0;
 				break;
 			}
 		} else if (vflag)
%%%


Cheers,
-- 
Ruslan Ermilov
ru@FreeBSD.org
FreeBSD committer

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFK7sxqRfpzJluFF4RAjBRAJ4vjE51N5GklJjcAu+yU1hXhwjvsgCgkBgQ
FKCY6JqSBsdZ7SznScOAlGY=
=QIFY
-----END PGP SIGNATURE-----

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20061010152433.GA35377>