338c71
From 6e36198f10a2f63b89c89ebb5d5c185b20fb3a63 Mon Sep 17 00:00:00 2001
338c71
From: Kamil Dudka <kdudka@redhat.com>
338c71
Date: Mon, 29 Mar 2010 17:20:34 +0000
338c71
Subject: [PATCH] coreutils-df-direct.patch
338c71
338c71
---
338c71
 doc/coreutils.texi |  7 ++++++
338c71
 src/df.c           | 34 ++++++++++++++++++++++++++--
338c71
 tests/df/direct.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++
338c71
 3 files changed, 94 insertions(+), 2 deletions(-)
338c71
 create mode 100755 tests/df/direct.sh
338c71
04161d
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
338c71
index 5b9a597..6810c15 100644
04161d
--- a/doc/coreutils.texi
04161d
+++ b/doc/coreutils.texi
338c71
@@ -11898,6 +11898,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
338c71
index 48025b9..c8efa5b 100644
04161d
--- a/src/df.c
04161d
+++ b/src/df.c
338c71
@@ -125,6 +125,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
 
338c71
@@ -252,13 +255,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'},
338c71
@@ -561,7 +566,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
338c71
@@ -1464,6 +1472,17 @@ 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
+	{
338c71
+	  get_dev (NULL, resolved, name, NULL, NULL, false, false, NULL, false);
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;
338c71
@@ -1534,6 +1553,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);
338c71
@@ -1624,6 +1644,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
             {
338c71
@@ -1720,6 +1743,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
338c71
-- 
338c71
2.31.1
338c71