coreutils
[Top][All Lists]
Advanced

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

[coreutils] [PATCH] stat: print SELinux context when available


From: Eric Blake
Subject: [coreutils] [PATCH] stat: print SELinux context when available
Date: Fri, 1 Oct 2010 10:15:11 -0600

* src/stat.c (do_stat, do_statfs): Include context when present.
* NEWS: Update blurb explaining the replacement for -Z.
---

I know Pádraig already expressed 60:40 against, but sometimes, seeing
the actual patch can help discussion.

Hmm, in looking at this, I'm wondering if an even better patch would
be to build the default format string incrementally, using asnprintf,
rather than repeating so many lines.

 NEWS       |    5 +++-
 src/stat.c |   86 +++++++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 69 insertions(+), 22 deletions(-)

diff --git a/NEWS b/NEWS
index 2ce03e3..b77bd77 100644
--- a/NEWS
+++ b/NEWS
@@ -73,7 +73,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-

   stat no longer accepts the --context (-Z) option.  Initially it was
   merely accepted and ignored, for compatibility.  Starting two years
-  ago, with coreutils-7.0, its use evoked a warning.
+  ago, with coreutils-7.0, its use evoked a warning.  Printing the
+  SELinux context can be done with the %C format directive, and the
+  default output when no format is specified now automatically
+  includes %C when context information is available.

   stat now outputs the full sub-second resolution for the atime,
   mtime, and ctime values since the Epoch, when using the %X, %Y, and
diff --git a/src/stat.c b/src/stat.c
index 69db5c7..765b630 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -1031,13 +1031,27 @@ do_statfs (char const *filename, bool terse, char const 
*format)

   if (format == NULL)
     {
-      format = (terse
-                ? "%n %i %l %t %s %S %b %f %a %c %d\n"
-                : "  File: \"%n\"\n"
-                "    ID: %-8i Namelen: %-7l Type: %T\n"
-                "Block size: %-10s Fundamental block size: %S\n"
-                "Blocks: Total: %-10b Free: %-10f Available: %a\n"
-                "Inodes: Total: %-10c Free: %d\n");
+      if (0 < is_selinux_enabled ())
+        {
+          format = (terse
+                    ? "%n %i %l %t %s %S %b %f %a %c %d %C\n"
+                    : "  File: \"%n\"\n"
+                    "    ID: %-8i Namelen: %-7l Type: %T\n"
+                    "Block size: %-10s Fundamental block size: %S\n"
+                    "Blocks: Total: %-10b Free: %-10f Available: %a\n"
+                    "Inodes: Total: %-10c Free: %d\n"
+                    "Context: %C\n");
+        }
+      else
+        {
+          format = (terse
+                    ? "%n %i %l %t %s %S %b %f %a %c %d\n"
+                    : "  File: \"%n\"\n"
+                    "    ID: %-8i Namelen: %-7l Type: %T\n"
+                    "Block size: %-10s Fundamental block size: %S\n"
+                    "Blocks: Total: %-10b Free: %-10f Available: %a\n"
+                    "Inodes: Total: %-10c Free: %d\n");
+        }
     }

   bool fail = print_it (format, filename, print_statfs, &statfsbuf);
@@ -1073,7 +1087,10 @@ do_stat (char const *filename, bool terse, char const 
*format)
     {
       if (terse)
         {
-          format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
+          if (0 < is_selinux_enabled ())
+            format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o %C\n";
+          else
+            format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o\n";
         }
       else
         {
@@ -1081,22 +1098,49 @@ do_stat (char const *filename, bool terse, char const 
*format)
              implemented.  */
           if (S_ISBLK (statbuf.st_mode) || S_ISCHR (statbuf.st_mode))
             {
-              format =
-                "  File: %N\n"
-                "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
-                "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
-                " Device type: %t,%T\n"
-                "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
-                "Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: %w\n";
+              if (0 < is_selinux_enabled ())
+                {
+                  format =
+                    "  File: %N\n"
+                    "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
+                    "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
+                    " Device type: %t,%T\n"
+                    "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
+                    "Context: %C\n"
+                    "Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: 
%w\n";
+                }
+              else
+                {
+                  format =
+                    "  File: %N\n"
+                    "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
+                    "Device: %Dh/%dd\tInode: %-10i  Links: %-5h"
+                    " Device type: %t,%T\n"
+                    "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
+                    "Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: 
%w\n";
+                }
             }
           else
             {
-              format =
-                "  File: %N\n"
-                "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
-                "Device: %Dh/%dd\tInode: %-10i  Links: %h\n"
-                "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
-                "Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: %w\n";
+              if (0 < is_selinux_enabled ())
+                {
+                  format =
+                    "  File: %N\n"
+                    "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
+                    "Device: %Dh/%dd\tInode: %-10i  Links: %h\n"
+                    "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
+                    "Context: %C\n"
+                    "Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: 
%w\n";
+                }
+              else
+                {
+                  format =
+                    "  File: %N\n"
+                    "  Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
+                    "Device: %Dh/%dd\tInode: %-10i  Links: %h\n"
+                    "Access: (%04a/%10.10A)  Uid: (%5u/%8U)   Gid: (%5g/%8G)\n"
+                    "Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: 
%w\n";
+                }
             }
         }
     }
-- 
1.7.2.3




reply via email to

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