coreutils
[Top][All Lists]
Advanced

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

[PATCH v4 4/4] stat: support statx DONT_SYNC and FORCE_SYNC flags


From: Jeff Layton
Subject: [PATCH v4 4/4] stat: support statx DONT_SYNC and FORCE_SYNC flags
Date: Wed, 1 May 2019 09:57:52 -0400

Add a new --cache= command-line option that set the appropriate hint
flags in the statx call. These are primarily used with network
filesystems to indicate what level of cache coherency the application
can tolerate. The new option is only implemented when built with
HAVE_STATX.

* NEWS: mention the enhancements
* src/stat.c: add new option to control synchronization with server
---
 NEWS       |  8 ++++++++
 src/stat.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 12c864dcc9ad..9d7b111a345c 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,14 @@ GNU coreutils NEWS                                    -*- 
outline -*-
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** New Features
+
+   stat now uses the statx() system call to do its bidding when it is
+   available. It will craft a statx mask with only the needed attributes.
+   When built with statx support, it also supports new command line
+   options to control synchronization with the server on network
+   filesytems.
+
 ** Bug fixes
 
   df now correctly parses the /proc/self/mountinfo file for unusual entries
diff --git a/src/stat.c b/src/stat.c
index 2bbc75792b5a..67b334921d48 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -201,6 +201,9 @@ static struct option const long_options[] =
   {"format", required_argument, NULL, 'c'},
   {"printf", required_argument, NULL, PRINTF_OPTION},
   {"terse", no_argument, NULL, 't'},
+#if HAVE_STATX && defined STATX_INO
+  {"cached", required_argument, NULL, 'C'},
+#endif
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -1200,6 +1203,15 @@ do_statfs (char const *filename, char const *format)
 }
 
 #if HAVE_STATX && defined STATX_INO
+/* Allowed command-line options */
+static const char *optstring = "c:C:fLt";
+
+/* Ask statx to avoid syncing? */
+static bool dont_sync;
+
+/* Ask statx to force sync? */
+static bool force_sync;
+
 struct printarg {
   struct statx *stx;
   struct stat *st;
@@ -1472,6 +1484,11 @@ do_stat (char const *filename, char const *format, char 
const *format2)
       flags = AT_SYMLINK_NOFOLLOW;
     }
 
+  if (dont_sync)
+    flags |= AT_STATX_DONT_SYNC;
+  else if (force_sync)
+    flags |= AT_STATX_FORCE_SYNC;
+
   fd = statx(fd, pathname, flags, format_to_mask(format), &stx);
   if (fd < 0)
     {
@@ -1492,6 +1509,9 @@ do_stat (char const *filename, char const *format, char 
const *format2)
   return ! fail;
 }
 #else /* HAVE_STATX && defined STATX_INO */
+/* Allowed command-line options */
+static const char *optstring = "c:fLt";
+
 static struct timespec
 get_birthtime (int fd, char const *filename, struct stat const *st)
 {
@@ -1800,6 +1820,12 @@ Display file or file system status.\n\
   -L, --dereference     follow links\n\
   -f, --file-system     display file system status instead of file status\n\
 "), stdout);
+#if HAVE_STATX && defined STATX_INO
+      fputs (_("\
+  -C, --cached=MODE     specify whether and how to use cached values;\n\
+                          typically useful for network filesystems\n\
+"), stdout);
+#endif
       fputs (_("\
   -c  --format=FORMAT   use the specified FORMAT instead of the default;\n\
                           output a newline after each use of FORMAT\n\
@@ -1811,6 +1837,16 @@ Display file or file system status.\n\
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
 
+#if HAVE_STATX && defined STATX_INO
+      fputs (_("\n\
+The valid MODE arguments to --cache are:\n\
+\n\
+  always:  always use cached values\n\
+  never:   never use cached values\n\
+  default: leave it up to the underlying filesystem\n\
+"), stdout);
+#endif
+
       fputs (_("\n\
 The valid format sequences for files (without --file-system):\n\
 \n\
@@ -1916,7 +1952,7 @@ main (int argc, char *argv[])
 
   atexit (close_stdout);
 
-  while ((c = getopt_long (argc, argv, "c:fLt", long_options, NULL)) != -1)
+  while ((c = getopt_long (argc, argv, optstring, long_options, NULL)) != -1)
     {
       switch (c)
         {
@@ -1932,6 +1968,20 @@ main (int argc, char *argv[])
           trailing_delim = "\n";
           break;
 
+#if HAVE_STATX && defined STATX_INO
+       case 'C':
+         if (!strcmp(optarg, "never"))
+            force_sync = true;
+         else if (!strcmp(optarg, "always"))
+            dont_sync = true;
+         else if (strcmp(optarg, "default"))
+            {
+              error (0, 0, _("Unknown --cached= option \"%s\""), optarg);
+              usage (EXIT_FAILURE);
+           }
+         break;
+#endif
+
         case 'L':
           follow_links = true;
           break;
-- 
2.21.0




reply via email to

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