maint: use standard spacing in shebang line in tests
[coreutils.git] / tests / df / skip-duplicates.sh
blob40696047304a336a4ee980f7dd1e801d67014fff
1 #!/bin/sh
2 # Test df's behavior when the mount list contains duplicate entries.
3 # This test is skipped on systems that lack LD_PRELOAD support; that's fine.
5 # Copyright (C) 2012-2015 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21 print_ver_ df
22 require_gcc_shared_
24 # We use --local here so as to not activate
25 # potentially very many remote mounts.
26 df --local || skip_ 'df fails'
28 export CU_NONROOT_FS=$(df --local --output=target 2>&1 | grep /. | head -n1)
29 export CU_REMOTE_FS=$(df --local --output=target 2>&1 | grep /. |
30 tail -n+2 | head -n1)
32 unique_entries=1
33 test -z "$CU_NONROOT_FS" || unique_entries=$(expr $unique_entries + 1)
34 test -z "$CU_REMOTE_FS" || unique_entries=$(expr $unique_entries + 2)
36 grep '^#define HAVE_MNTENT_H 1' $CONFIG_HEADER > /dev/null \
37 || skip_ "no mntent.h available to confirm the interface"
39 grep '^#define HAVE_GETMNTENT 1' $CONFIG_HEADER > /dev/null \
40 || skip_ "getmntent is not used on this system"
42 # Simulate an mtab file to test various cases.
43 cat > k.c <<EOF || framework_failure_
44 #define _GNU_SOURCE
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <errno.h>
48 #include <mntent.h>
49 #include <string.h>
50 #include <dlfcn.h>
52 #define STREQ(a, b) (strcmp (a, b) == 0)
54 FILE* fopen(const char *path, const char *mode)
56 static FILE* (*fopen_func)(char const *, char const *);
58 /* get reference to original (libc provided) fopen */
59 if (!fopen_func)
61 fopen_func = (FILE*(*)(char const *, char const *))
62 dlsym(RTLD_NEXT, "fopen");
63 if (!fopen_func)
65 fprintf (stderr, "Failed to find fopen()\n");
66 errno = ESRCH;
67 return NULL;
71 /* Returning ENOENT here will get read_file_system_list()
72 to fall back to using getmntent() below. */
73 if (STREQ (path, "/proc/self/mountinfo"))
75 errno = ENOENT;
76 return NULL;
78 else
79 return fopen_func(path, mode);
82 #define STREQ(a, b) (strcmp (a, b) == 0)
84 struct mntent *getmntent (FILE *fp)
86 static char *nonroot_fs;
87 static char *remote_fs;
88 static int done;
90 /* Prove that LD_PRELOAD works. */
91 if (!done)
93 fclose (fopen ("x", "w"));
94 ++done;
97 static struct mntent mntents[] = {
98 {.mnt_fsname="/short", .mnt_dir="/invalid/mount/dir", .mnt_opts=""},
99 {.mnt_fsname="fsname", .mnt_dir="/", .mnt_opts=""},
100 {.mnt_fsname="/fsname", .mnt_dir="/.", .mnt_opts=""},
101 {.mnt_fsname="/fsname", .mnt_dir="/", .mnt_opts=""},
102 {.mnt_fsname="virtfs", .mnt_dir="/NONROOT", .mnt_type="t1", .mnt_opts=""},
103 {.mnt_fsname="virtfs2", .mnt_dir="/NONROOT", .mnt_type="t2", .mnt_opts=""},
104 {.mnt_fsname="netns", .mnt_dir="net:[1234567]", .mnt_opts=""},
105 {.mnt_fsname="rem:ote1",.mnt_dir="/REMOTE", .mnt_opts=""},
106 {.mnt_fsname="rem:ote1",.mnt_dir="/REMOTE", .mnt_opts=""},
107 {.mnt_fsname="rem:ote2",.mnt_dir="/REMOTE", .mnt_opts=""},
110 if (done == 1)
112 nonroot_fs = getenv ("CU_NONROOT_FS");
113 if (!nonroot_fs || !*nonroot_fs)
114 nonroot_fs = "/"; /* merge into / entries. */
116 remote_fs = getenv ("CU_REMOTE_FS");
119 if (done == 1 && !getenv ("CU_TEST_DUPE_INVALID"))
120 done++; /* skip the first entry. */
122 while (done++ <= 10)
124 if (!mntents[done-2].mnt_type)
125 mntents[done-2].mnt_type = "-";
126 if (!mntents[done-2].mnt_opts)
127 mntents[done-2].mnt_opts = "-";
128 if (STREQ (mntents[done-2].mnt_dir, "/NONROOT"))
129 mntents[done-2].mnt_dir = nonroot_fs;
130 if (STREQ (mntents[done-2].mnt_dir, "/REMOTE"))
132 if (!remote_fs || !*remote_fs)
133 continue;
134 else
135 mntents[done-2].mnt_dir = remote_fs;
137 return &mntents[done-2];
140 return NULL;
144 # Then compile/link it:
145 gcc_shared_ k.c k.so \
146 || framework_failure_ 'failed to build shared library'
148 # Test if LD_PRELOAD works:
149 LD_PRELOAD=$LD_PRELOAD:./k.so df
150 test -f x || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"
152 # The fake mtab file should only contain entries
153 # having the same device number; thus the output should
154 # consist of a header and unique entries.
155 LD_PRELOAD=$LD_PRELOAD:./k.so df -T >out || fail=1
156 test $(wc -l <out) -eq $(expr 1 + $unique_entries) || { fail=1; cat out; }
158 # With --total we should suppress the duplicate but separate remote file system
159 LD_PRELOAD=$LD_PRELOAD:./k.so df --total >out || fail=1
160 test "$CU_REMOTE_FS" && elide_remote=1 || elide_remote=0
161 test $(wc -l <out) -eq $(expr 2 + $unique_entries - $elide_remote) ||
162 { fail=1; cat out; }
164 # Ensure we don't fail when unable to stat (currently) unavailable entries
165 LD_PRELOAD=$LD_PRELOAD:./k.so CU_TEST_DUPE_INVALID=1 df -T >out || fail=1
166 test $(wc -l <out) -eq $(expr 1 + $unique_entries) || { fail=1; cat out; }
168 # df should also prefer "/fsname" over "fsname"
169 if test "$unique_entries" = 2; then
170 test $(grep -c '/fsname' <out) -eq 1 || { fail=1; cat out; }
171 # ... and "/fsname" with '/' as Mounted on over '/.'
172 test $(grep -cF '/.' <out) -eq 0 || { fail=1; cat out; }
175 # df should use the last seen devname (mnt_fsname) and devtype (mnt_type)
176 test $(grep -c 'virtfs2.*t2' <out) -eq 1 || { fail=1; cat out; }
178 # Ensure that filtering duplicates does not affect -a processing.
179 LD_PRELOAD=$LD_PRELOAD:./k.so df -a >out || fail=1
180 total_fs=6; test "$CU_REMOTE_FS" && total_fs=$(expr $total_fs + 3)
181 test $(wc -l <out) -eq $total_fs || { fail=1; cat out; }
182 # Ensure placeholder "-" values used for the eclipsed "virtfs"
183 test $(grep -c 'virtfs *-' <out) -eq 1 || { fail=1; cat out; }
185 # Ensure that filtering duplicates does not affect
186 # argument processing (now without the fake getmntent()).
187 df '.' '.' >out || fail=1
188 test $(wc -l <out) -eq 3 || { fail=1; cat out; }
190 Exit $fail