bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ANDing out weird mode bits in calls to 'open' and 'mkdir'


From: Paul Eggert
Subject: ANDing out weird mode bits in calls to 'open' and 'mkdir'
Date: Wed, 18 Oct 2006 13:58:31 -0700
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

I installed this change.  It shouldn't affect actual behavior on any
hosts that I know about (unless you count the output of strace or
truss :-), but it does make the system calls more consistent with what
POSIX requires and what Solaris 10 /bin/cp does.

2006-10-18  Paul Eggert  <address@hidden>

        * src/copy.c (copy_internal): Don't pass mkdir a mode greater than
        7777.  This matches historical 'cp' behavior and avoids some
        (though not all) implementation-defined behavior of mkdir.
        * src/cp.c (make_dir_parents_private): Likewise.
        * src/copy.c (copy_internal): Don't pass 'open' a mode greater
        than 777.  This is required by POSIX.  It doesn't make any difference
        in actual behavior on any host that I know of.

Index: src/copy.c
===================================================================
RCS file: /fetish/cu/src/copy.c,v
retrieving revision 1.214
diff -p -u -r1.214 copy.c
--- src/copy.c  14 Oct 2006 05:20:27 -0000      1.214
+++ src/copy.c  18 Oct 2006 20:53:25 -0000
@@ -1505,7 +1505,11 @@ copy_internal (char const *src_name, cha
 
       if (new_dst || !S_ISDIR (dst_sb.st_mode))
        {
-         if (mkdir (dst_name, src_mode) != 0)
+         /* POSIX says mkdir's behavior is implementation-defined when
+            (src_mode & ~S_IRWXUGO) != 0.  However, common practice is
+            to ask mkdir to copy all the CHMOD_MODE_BITS, letting mkdir
+            decide what to do with S_ISUID | S_ISGID | S_ISVTX.  */
+         if (mkdir (dst_name, src_mode & CHMOD_MODE_BITS) != 0)
            {
              error (0, errno, _("cannot create directory %s"),
                     quote (dst_name));
@@ -1628,9 +1632,11 @@ copy_internal (char const *src_name, cha
     {
       copied_as_regular = true;
       /* POSIX says the permission bits of the source file must be
-        used as the 3rd argument in the open call, but that's not consistent
-        with historical practice.  */
-      if (! copy_reg (src_name, dst_name, x, src_mode, &new_dst, &src_sb))
+        used as the 3rd argument in the open call.  Historical
+        practice passed all the source mode bits to 'open', but the extra
+        bits were ignored, so it should be the same either way.  */
+      if (! copy_reg (src_name, dst_name, x, src_mode & S_IRWXUGO,
+                     &new_dst, &src_sb))
        goto un_backup;
     }
   else if (S_ISFIFO (src_type))
Index: src/cp.c
===================================================================
RCS file: /fetish/cu/src/cp.c,v
retrieving revision 1.224
diff -p -u -r1.224 cp.c
--- src/cp.c    8 Sep 2006 17:08:53 -0000       1.224
+++ src/cp.c    18 Oct 2006 20:53:25 -0000
@@ -428,7 +428,11 @@ make_dir_parents_private (char const *co
                }
              src_mode = stats.st_mode;
 
-             if (mkdir (dir, src_mode))
+             /* POSIX says mkdir's behavior is implementation-defined when
+                (src_mode & ~S_IRWXUGO) != 0.  However, common practice is
+                to ask mkdir to copy all the CHMOD_MODE_BITS, letting mkdir
+                decide what to do with S_ISUID | S_ISGID | S_ISVTX.  */
+             if (mkdir (dir, src_mode & CHMOD_MODE_BITS) != 0)
                {
                  error (0, errno, _("cannot make directory %s"),
                         quote (dir));




reply via email to

[Prev in Thread] Current Thread [Next in Thread]