baf6440
From 6e36198f10a2f63b89c89ebb5d5c185b20fb3a63 Mon Sep 17 00:00:00 2001
baf6440
From: Kamil Dudka <kdudka@redhat.com>
baf6440
Date: Mon, 29 Mar 2010 17:20:34 +0000
baf6440
Subject: [PATCH] coreutils-df-direct.patch
baf6440
baf6440
---
baf6440
 doc/coreutils.texi |  7 ++++++
c25beef
 src/df.c           | 34 ++++++++++++++++++++++++++--
baf6440
 tests/df/direct.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++
c25beef
 3 files changed, 94 insertions(+), 2 deletions(-)
baf6440
 create mode 100755 tests/df/direct.sh
baf6440
8d9eac4
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
baf6440
index 5b9a597..6810c15 100644
8d9eac4
--- a/doc/coreutils.texi
8d9eac4
+++ b/doc/coreutils.texi
c25beef
@@ -12074,6 +12074,13 @@ some systems (notably Solaris), doing this yields more up to date results,
3775f44
 but in general this option makes @command{df} much slower, especially when
3775f44
 there are many or very busy file systems.
d059274
 
89d1aa9
+@item --direct
d059274
+@opindex --direct
d059274
+@cindex direct statfs for a file
d059274
+Do not resolve mount point and show statistics directly for a file. It can be
d059274
+especially useful for NFS mount points if there is a boundary between two
d059274
+storage policies behind the mount point.
d059274
+
89d1aa9
 @item --total
d059274
 @opindex --total
baf6440
 @cindex grand total of file system size, usage and available space
8d9eac4
diff --git a/src/df.c b/src/df.c
baf6440
index 48025b9..c8efa5b 100644
8d9eac4
--- a/src/df.c
8d9eac4
+++ b/src/df.c
baf6440
@@ -125,6 +125,9 @@ static bool print_type;
d059274
 /* If true, print a grand total at the end.  */
d059274
 static bool print_grand_total;
d059274
 
d059274
+/* If true, show statistics for a file instead of mount point.  */
d059274
+static bool direct_statfs;
d059274
+
4c4be9e
 /* Grand total data.  */
d059274
 static struct fs_usage grand_fsu;
d059274
 
baf6440
@@ -252,13 +255,15 @@ enum
bb33bc4
   NO_SYNC_OPTION = CHAR_MAX + 1,
89d1aa9
   SYNC_OPTION,
4c4be9e
   TOTAL_OPTION,
bb33bc4
-  OUTPUT_OPTION
bb33bc4
+  OUTPUT_OPTION,
d059274
+  DIRECT_OPTION
d059274
 };
d059274
 
d059274
 static struct option const long_options[] =
d059274
 {
d059274
   {"all", no_argument, NULL, 'a'},
d059274
   {"block-size", required_argument, NULL, 'B'},
d059274
+  {"direct", no_argument, NULL, DIRECT_OPTION},
d059274
   {"inodes", no_argument, NULL, 'i'},
d059274
   {"human-readable", no_argument, NULL, 'h'},
d059274
   {"si", no_argument, NULL, 'H'},
baf6440
@@ -583,7 +588,10 @@ get_header (void)
4c4be9e
   for (col = 0; col < ncolumns; col++)
4c4be9e
     {
50a3bad
       char *cell = NULL;
4c4be9e
-      char const *header = _(columns[col]->caption);
4c4be9e
+      char const *header = (columns[col]->field == TARGET_FIELD
4c4be9e
+                            && direct_statfs)?
4c4be9e
+                            _("File") :
4c4be9e
+                            _(columns[col]->caption);
d059274
 
4c4be9e
       if (columns[col]->field == SIZE_FIELD
4c4be9e
           && (header_mode == DEFAULT_MODE
c25beef
@@ -1486,6 +1494,17 @@ get_point (char const *point, const struct stat *statp)
d059274
 static void
50a3bad
 get_entry (char const *name, struct stat const *statp)
d059274
 {
d059274
+  if (direct_statfs)
d059274
+    {
d059274
+      char *resolved = canonicalize_file_name (name);
d059274
+      if (resolved)
d059274
+	{
c25beef
+	  get_dev (NULL, resolved, name, NULL, NULL, false, false, NULL, false);
d059274
+	  free (resolved);
d059274
+	  return;
d059274
+	}
d059274
+    }
d059274
+
d059274
   if ((S_ISBLK (statp->st_mode) || S_ISCHR (statp->st_mode))
baf6440
       && get_device (name))
d059274
     return;
c25beef
@@ -1556,6 +1575,7 @@ or all file systems by default.\n\
bb33bc4
   -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,\n\
bb33bc4
                            '-BM' prints sizes in units of 1,048,576 bytes;\n\
bb33bc4
                            see SIZE format below\n\
d059274
+      --direct          show statistics for a file instead of mount point\n\
9c33d82
   -h, --human-readable  print sizes in powers of 1024 (e.g., 1023M)\n\
9c33d82
   -H, --si              print sizes in powers of 1000 (e.g., 1.1G)\n\
3775f44
 "), stdout);
c25beef
@@ -1646,6 +1666,9 @@ main (int argc, char **argv)
d059274
               xstrtol_fatal (e, oi, c, long_options, optarg);
d059274
           }
d059274
           break;
d059274
+        case DIRECT_OPTION:
d059274
+          direct_statfs = true;
d059274
+          break;
d059274
         case 'i':
4c4be9e
           if (header_mode == OUTPUT_MODE)
4c4be9e
             {
c25beef
@@ -1742,6 +1765,13 @@ main (int argc, char **argv)
d059274
         }
d059274
     }
d059274
 
d059274
+  if (direct_statfs && show_local_fs)
d059274
+    {
d059274
+      error (0, 0, _("options --direct and --local (-l) are mutually "
d059274
+		     "exclusive"));
d059274
+      usage (EXIT_FAILURE);
d059274
+    }
d059274
+
d059274
   if (human_output_opts == -1)
d059274
     {
d059274
       if (posix_format)
8d9eac4
diff --git a/tests/df/direct.sh b/tests/df/direct.sh
2d00e2b
new file mode 100755
8d9eac4
index 0000000..8e4cfb8
8d9eac4
--- /dev/null
8d9eac4
+++ b/tests/df/direct.sh
826eac7
@@ -0,0 +1,55 @@
d059274
+#!/bin/sh
d059274
+# Ensure "df --direct" works as documented
d059274
+
d059274
+# Copyright (C) 2010 Free Software Foundation, Inc.
d059274
+
d059274
+# This program is free software: you can redistribute it and/or modify
d059274
+# it under the terms of the GNU General Public License as published by
d059274
+# the Free Software Foundation, either version 3 of the License, or
d059274
+# (at your option) any later version.
d059274
+
d059274
+# This program is distributed in the hope that it will be useful,
d059274
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
d059274
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d059274
+# GNU General Public License for more details.
d059274
+
d059274
+# You should have received a copy of the GNU General Public License
d059274
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
d059274
+
826eac7
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
826eac7
+print_ver_ df
d059274
+
b5f9200
+df || skip_ "df fails"
d059274
+
d059274
+DIR=`pwd` || framework_failure
d059274
+FILE="$DIR/file"
d059274
+touch "$FILE" || framework_failure
d059274
+echo "$FILE" > file_exp || framework_failure
d059274
+echo "Mounted on" > header_mounted_exp || framework_failure
d059274
+echo "File" > header_file_exp || framework_failure
d059274
+
d059274
+fail=0
d059274
+
d059274
+df --portability "$FILE" > df_out || fail=1
d059274
+df --portability --direct "$FILE" > df_direct_out || fail=1
d059274
+df --portability --direct --local "$FILE" > /dev/null 2>&1 && fail=1
d059274
+
d059274
+# check df header
d059274
+$AWK '{ if (NR==1) print $6 " " $7; }' df_out > header_mounted_out \
d059274
+  || framework_failure
d059274
+$AWK '{ if (NR==1) print $6; }' df_direct_out > header_file_out \
d059274
+  || framework_failure
d059274
+compare header_mounted_out header_mounted_exp || fail=1
d059274
+compare header_file_out header_file_exp || fail=1
d059274
+
d059274
+# check df output (without --direct)
d79f57c
+$AWK '{ if (NR==2) print $6; }' df_out > file_out \
d79f57c
+  || framework_failure
d79f57c
+compare file_out file_exp && fail=1
d059274
+
d059274
+# check df output (with --direct)
d79f57c
+$AWK '{ if (NR==2) print $6; }' df_direct_out > file_out \
d79f57c
+  || framework_failure
d79f57c
+compare file_out file_exp || fail=1
d059274
+
d059274
+Exit $fail
baf6440
-- 
baf6440
2.31.1
baf6440