coreutils/coreutils-df-direct.patch

188 lines
6.1 KiB
Diff
Raw Permalink Normal View History

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