1#!/bin/sh
2# Ensure "df --total" computes accurate totals
3
4# Copyright (C) 2008-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ df
21require_perl_
22
23# Protect against inaccessible remote mounts etc.
24timeout 10 df || skip_ "df fails"
25
26cat <<\EOF > check-df || framework_failure_
27my ($total, $used, $avail) = (0, 0, 0);
28while (<>)
29  {
30    $. == 1
31      and next;  # skip first (header) line
32    # Recognize df output lines like these:
33    # /dev/sdc1                  0       0       0    -  /c
34    # tmpfs                1536000   12965 1523035    1% /tmp
35    # total                5285932  787409 4498523   15% -
36    /^(.*?) +(-?\d+|-) +(-?\d+|-) +(-?\d+|-) +(?:-|[0-9]+%) (.*)$/
37      or die "$0: invalid input line\n: $_";
38    if ($1 eq 'total' && $5 eq '-')
39      {
40        $total == $2 or die "$total != $2";
41        $used  == $3 or die "$used  != $3";
42        $avail == $4 or die "$avail != $4";
43        my $line = <>;
44        defined $line
45          and die "$0: extra line(s) after totals\n";
46        exit 0;
47      }
48    $total += $2 unless $2 eq '-';
49    $used  += $3 unless $3 eq '-';
50    $avail += $4 unless $4 eq '-';
51  }
52die "$0: missing line of totals\n";
53EOF
54
55# Use --block-size=512 to keep df from printing rounded-to-kilobyte
56# numbers which wouldn't necessarily add up to the displayed total.
57df --total -P --block-size=512 > space || framework_failure_
58cat space  # this helps when debugging any test failure
59df --total -i -P               > inode || framework_failure_
60cat inode
61
62$PERL check-df space || fail=1
63$PERL check-df inode || fail=1
64
65test "$fail" = 1 && dump_mount_list_
66
67Exit $fail
68