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