generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / common / renameat2
1 ##/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Miklos Szeredi.  All Rights Reserved.
4 #
5 # renameat2 helpers
6
7 # Setup source or dest
8 #
9 _setup_one()
10 {
11         local path=$1
12         local type=$2
13
14         case $type in
15                 none)   ;;
16                 regu)   echo foo > $path;;
17                 symb)   ln -s foo $path;;
18                 dire)   mkdir $path;;
19                 tree)   mkdir $path; echo foo > $path/bar;;
20         esac
21 }
22
23 #
24 # Cleanup source or dest
25 #
26 _cleanup_one()
27 {
28         local path=$1
29
30         if test -d $path; then
31                 rm -f $path/bar
32                 rmdir $path
33         else
34                 rm -f $path
35         fi
36 }
37
38 #
39 # Check type of source or destination
40 #
41 _showtype_one()
42 {
43         local path=$1
44
45         if test -e $path -o -h $path; then
46                 if test -d $path -a -e $path/bar; then
47                         echo -n "tree"
48                 else
49                         echo -n `stat -c %F $path | cut -b-4`
50                 fi
51         else
52                 echo -n "none"
53         fi
54 }
55
56 #
57 # This runs renameat2 on all combinations of source and dest
58 #
59 _rename_tests_source_dest()
60 {
61         local source=$1
62         local dest=$2
63         local options=$3
64         local flags=$4
65
66         for stype in none regu symb dire tree; do
67                 for dtype in none regu symb dire tree; do
68                         echo -n "$options $stype/$dtype -> "
69                         _setup_one $source $stype
70                         _setup_one $dest $dtype
71                         $here/src/renameat2 $source $dest $flags
72                         if test $? == 0; then
73                                 _showtype_one $source
74                                 echo -n "/"
75                                 _showtype_one $dest
76                                 echo "."
77                         fi
78                         _cleanup_one $source
79                         _cleanup_one $dest
80                 done
81         done
82 }
83
84 #
85 # This runs _rename_tests_source_dest() for both same-directory and
86 # cross-directory renames
87 #
88 _rename_tests()
89 {
90         local testdir=$1
91         local flags=$2
92
93         #same directory renames
94         _rename_tests_source_dest $testdir/src $testdir/dst     "samedir " $flags
95
96         #cross directory renames
97         mkdir $testdir/x $testdir/y
98         _rename_tests_source_dest $testdir/x/src $testdir/y/dst "crossdir" $flags
99         rmdir $testdir/x $testdir/y
100 }
101
102 #
103 # This checks whether the renameat2 syscall is supported
104 #
105 _require_renameat2()
106 {
107         local flags=$1
108         local rename_dir=$TEST_DIR/$$
109         local cmd=""
110
111         if test ! -x $here/src/renameat2; then
112                 _notrun "renameat2 binary not found"
113         fi
114
115         mkdir $rename_dir
116         touch $rename_dir/foo
117         case $flags in
118         "noreplace")
119                 cmd="-n $rename_dir/foo $rename_dir/bar"
120                 ;;
121         "exchange")
122                 touch $rename_dir/bar
123                 cmd="-x $rename_dir/foo $rename_dir/bar"
124                 ;;
125         "whiteout")
126                 touch $rename_dir/bar
127                 cmd="-w $rename_dir/foo $rename_dir/bar"
128                 ;;
129         "")
130                 cmd=""
131                 ;;
132         *)
133                 rm -rf $rename_dir
134                 _fail "_require_renameat2: only support noreplace,exchange,whiteout rename flags"
135                 ;;
136         esac
137         if ! $here/src/renameat2 -t $cmd; then
138                 rm -rf $rename_dir
139                 _notrun "kernel doesn't support renameat2 syscall"
140         fi
141         rm -rf $rename_dir
142 }