Blame SOURCES/coreutils-df-direct.patch

04161d
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
04161d
index a507280..400e135 100644
04161d
--- a/doc/coreutils.texi
04161d
+++ b/doc/coreutils.texi
04161d
@@ -11303,6 +11303,13 @@ some systems (notably SunOS), doing this yields more up to date results,
04161d
 but in general this option makes @command{df} much slower, especially when
04161d
 there are many or very busy file systems.
04161d
 
04161d
+@item --direct
04161d
+@opindex --direct
04161d
+@cindex direct statfs for a file
04161d
+Do not resolve mount point and show statistics directly for a file. It can be
04161d
+especially useful for NFS mount points if there is a boundary between two
04161d
+storage policies behind the mount point.
04161d
+
04161d
 @item --total
04161d
 @opindex --total
04161d
 @cindex grand total of disk size, usage and available space
04161d
diff --git a/src/df.c b/src/df.c
04161d
index 8f760db..a7385fd 100644
04161d
--- a/src/df.c
04161d
+++ b/src/df.c
04161d
@@ -120,6 +120,9 @@ static bool print_type;
04161d
 /* If true, print a grand total at the end.  */
04161d
 static bool print_grand_total;
04161d
 
04161d
+/* If true, show statistics for a file instead of mount point.  */
04161d
+static bool direct_statfs;
04161d
+
04161d
 /* Grand total data.  */
04161d
 static struct fs_usage grand_fsu;
04161d
 
04161d
@@ -247,13 +250,15 @@ enum
04161d
   NO_SYNC_OPTION = CHAR_MAX + 1,
04161d
   SYNC_OPTION,
04161d
   TOTAL_OPTION,
04161d
-  OUTPUT_OPTION
04161d
+  OUTPUT_OPTION,
04161d
+  DIRECT_OPTION
04161d
 };
04161d
 
04161d
 static struct option const long_options[] =
04161d
 {
04161d
   {"all", no_argument, NULL, 'a'},
04161d
   {"block-size", required_argument, NULL, 'B'},
04161d
+  {"direct", no_argument, NULL, DIRECT_OPTION},
04161d
   {"inodes", no_argument, NULL, 'i'},
04161d
   {"human-readable", no_argument, NULL, 'h'},
04161d
   {"si", no_argument, NULL, 'H'},
04161d
@@ -509,7 +514,10 @@ get_header (void)
04161d
   for (col = 0; col < ncolumns; col++)
04161d
     {
04161d
       char *cell = NULL;
04161d
-      char const *header = _(columns[col]->caption);
04161d
+      char const *header = (columns[col]->field == TARGET_FIELD
04161d
+                            && direct_statfs)?
04161d
+                            _("File") :
04161d
+                            _(columns[col]->caption);
04161d
 
04161d
       if (columns[col]->field == SIZE_FIELD
04161d
           && (header_mode == DEFAULT_MODE
04161d
@@ -1397,6 +1405,19 @@ get_point (const char *point, const struct stat *statp)
04161d
 static void
04161d
 get_entry (char const *name, struct stat const *statp)
04161d
 {
04161d
+  if (direct_statfs)
04161d
+    {
04161d
+      char *resolved = canonicalize_file_name (name);
04161d
+      if (resolved)
04161d
+	{
04161d
+         char *mp = find_mount_point (name, statp);
04161d
+	  get_dev (NULL, mp, resolved, NULL, NULL, false, false, NULL, false);
04161d
+         free(mp);
04161d
+	  free (resolved);
04161d
+	  return;
04161d
+	}
04161d
+    }
04161d
+
04161d
   if ((S_ISBLK (statp->st_mode) || S_ISCHR (statp->st_mode))
04161d
       && get_disk (name))
04161d
     return;
04161d
@@ -1467,6 +1488,7 @@ or all file systems by default.\n\
04161d
   -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,\n\
04161d
                            '-BM' prints sizes in units of 1,048,576 bytes;\n\
04161d
                            see SIZE format below\n\
04161d
+      --direct          show statistics for a file instead of mount point\n\
04161d
   -h, --human-readable  print sizes in powers of 1024 (e.g., 1023M)\n\
04161d
   -H, --si              print sizes in powers of 1000 (e.g., 1.1G)\n\
04161d
 "), stdout);
04161d
@@ -1557,6 +1579,9 @@ main (int argc, char **argv)
04161d
               xstrtol_fatal (e, oi, c, long_options, optarg);
04161d
           }
04161d
           break;
04161d
+        case DIRECT_OPTION:
04161d
+          direct_statfs = true;
04161d
+          break;
04161d
         case 'i':
04161d
           if (header_mode == OUTPUT_MODE)
04161d
             {
04161d
@@ -1653,6 +1678,13 @@ main (int argc, char **argv)
04161d
         }
04161d
     }
04161d
 
04161d
+  if (direct_statfs && show_local_fs)
04161d
+    {
04161d
+      error (0, 0, _("options --direct and --local (-l) are mutually "
04161d
+		     "exclusive"));
04161d
+      usage (EXIT_FAILURE);
04161d
+    }
04161d
+
04161d
   if (human_output_opts == -1)
04161d
     {
04161d
       if (posix_format)
04161d
diff --git a/tests/df/direct.sh b/tests/df/direct.sh
04161d
new file mode 100755
04161d
index 0000000..8e4cfb8
04161d
--- /dev/null
04161d
+++ b/tests/df/direct.sh
04161d
@@ -0,0 +1,55 @@
04161d
+#!/bin/sh
04161d
+# Ensure "df --direct" works as documented
04161d
+
04161d
+# Copyright (C) 2010 Free Software Foundation, Inc.
04161d
+
04161d
+# This program is free software: you can redistribute it and/or modify
04161d
+# it under the terms of the GNU General Public License as published by
04161d
+# the Free Software Foundation, either version 3 of the License, or
04161d
+# (at your option) any later version.
04161d
+
04161d
+# This program is distributed in the hope that it will be useful,
04161d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
04161d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
04161d
+# GNU General Public License for more details.
04161d
+
04161d
+# You should have received a copy of the GNU General Public License
04161d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
04161d
+
04161d
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
04161d
+print_ver_ df
04161d
+
04161d
+df || skip_ "df fails"
04161d
+
04161d
+DIR=`pwd` || framework_failure
04161d
+FILE="$DIR/file"
04161d
+touch "$FILE" || framework_failure
04161d
+echo "$FILE" > file_exp || framework_failure
04161d
+echo "Mounted on" > header_mounted_exp || framework_failure
04161d
+echo "File" > header_file_exp || framework_failure
04161d
+
04161d
+fail=0
04161d
+
04161d
+df --portability "$FILE" > df_out || fail=1
04161d
+df --portability --direct "$FILE" > df_direct_out || fail=1
04161d
+df --portability --direct --local "$FILE" > /dev/null 2>&1 && fail=1
04161d
+
04161d
+# check df header
04161d
+$AWK '{ if (NR==1) print $6 " " $7; }' df_out > header_mounted_out \
04161d
+  || framework_failure
04161d
+$AWK '{ if (NR==1) print $6; }' df_direct_out > header_file_out \
04161d
+  || framework_failure
04161d
+compare header_mounted_out header_mounted_exp || fail=1
04161d
+compare header_file_out header_file_exp || fail=1
04161d
+
04161d
+# check df output (without --direct)
04161d
+$AWK '{ if (NR==2) print $6; }' df_out > file_out \
04161d
+  || framework_failure
04161d
+compare file_out file_exp && fail=1
04161d
+
04161d
+# check df output (with --direct)
04161d
+$AWK '{ if (NR==2) print $6; }' df_direct_out > file_out \
04161d
+  || framework_failure
04161d
+compare file_out file_exp || fail=1
04161d
+
04161d
+Exit $fail