coreutils
[Top][All Lists]
Advanced

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

[coreutils] [PATCH 1/2] stat: support printing birthtime


From: Eric Blake
Subject: [coreutils] [PATCH 1/2] stat: support printing birthtime
Date: Thu, 30 Sep 2010 17:32:47 -0600

* src/stat.c (print_stat): New %w and %W formats.
(do_stat): Include %w in verbose format.
(usage): Document them.
* doc/coreutils.texi (stat invocation): Likewise.
* NEWS: Likewise.
Suggested by Andre "Osku" Schmidt.
---

I've tested that this works on cygwin.  On Fedora 13 with an
ext4 partition, the birthtime appears to not be returned in
stat().  If the kernel guys will ever commit to a stable
xstat() interface, which we can then write gnulib wrappers
for, we can use that instead.  I'm assuming this will also
work on BSD systems, although I have not yet tested that.

I wasn't sure how to write a test for this - how to tell if
a filesystem has birthtime support exposed by the kernel?
Ideas on that front are welcome.

For an example of what it looks like on a file system with
all four times:

$ src/stat ChangeLog
  File: `ChangeLog'
  Size: 496660    488        IO Block: 65536  regular file
Device: 10897b43h/277445443dInode: 562949954522488  Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1007/  eblake)   Gid: (  513/    None)
Access: 2010-09-30 16:58:48.859000000 -0600
Modify: 2010-09-30 16:52:50.000000000 -0600
Change: 2010-09-30 16:58:06.176250000 -0600
 Birth: 2010-09-30 16:58:06.098125000 -0600

 NEWS               |    5 +++--
 doc/coreutils.texi |    2 ++
 src/stat.c         |   26 ++++++++++++++++++++++++--
 3 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index 11a8b74..0c7cc38 100644
--- a/NEWS
+++ b/NEWS
@@ -36,8 +36,9 @@ GNU coreutils NEWS                                    -*- 
outline -*-

   sort now supports -d, -f, -i, -R, and -V in any combination.

-  stat now accepts the %m format directive to output
-  the mount point for a file.
+  stat now accepts the %m format directive to output the mount point
+  for a file.  It also accepts the %w and %W format directives for
+  outputting the birth time of a file, if one is available.

 ** Changes in behavior

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 52f1b20..c5dda4f 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -10696,6 +10696,8 @@ stat invocation
 @item %T - Minor device type in hex
 @item %u - User ID of owner
 @item %U - User name of owner
+@item %w - Time of file birth, or @samp{-} if unknown
+@item %W - Time of last birth as seconds since Epoch, or @samp{-}
 @item %x - Time of last access
 @item %X - Time of last access as seconds since Epoch
 @item %y - Time of last modification
diff --git a/src/stat.c b/src/stat.c
index f985978..c465e77 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -788,6 +788,26 @@ print_stat (char *pformat, size_t prefix_len, char m,
     case 'o':
       out_uint (pformat, prefix_len, statbuf->st_blksize);
       break;
+    case 'w':
+      {
+        struct timespec t = get_stat_birthtime (statbuf);
+        if (t.tv_nsec < 0)
+          out_string (pformat, prefix_len, "-");
+        else
+          out_string (pformat, prefix_len, human_time (t));
+      }
+      break;
+    case 'W':
+      {
+        struct timespec t = get_stat_birthtime (statbuf);
+        if (t.tv_nsec < 0)
+          out_string (pformat, prefix_len, "-");
+        else if (TYPE_SIGNED (time_t))
+          out_int (pformat, prefix_len, t.tv_sec);
+        else
+          out_uint (pformat, prefix_len, t.tv_sec);
+      }
+      break;
     case 'x':
       out_string (pformat, prefix_len, human_time (get_stat_atime (statbuf)));
       break;
@@ -1056,7 +1076,7 @@ do_stat (char const *filename, bool terse, char const 
*format)
                 "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";
+                "Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: %w\n";
             }
           else
             {
@@ -1065,7 +1085,7 @@ do_stat (char const *filename, bool terse, char const 
*format)
                 "  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";
+                "Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: %w\n";
             }
         }
     }
@@ -1130,6 +1150,8 @@ The valid format sequences for files (without 
--file-system):\n\
       fputs (_("\
   %u   User ID of owner\n\
   %U   User name of owner\n\
+  %x   Time of file birth, or - if unknown\n\
+  %X   Time of file birth as seconds since Epoch, or - if unknown\n\
   %x   Time of last access\n\
   %X   Time of last access as seconds since Epoch\n\
   %y   Time of last modification\n\
-- 
1.7.2.3




reply via email to

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