6fdcbf2ba5112d8dfbb12f2ad2149a54a64ffd72
[xfstests-dev.git] / common / attr
1 ##/bin/bash
2 #-----------------------------------------------------------------------
3 #  Copyright (c) 2000-2004 Silicon Graphics, Inc.  All Rights Reserved.
4 #  This program is free software; you can redistribute it and/or modify
5 #  it under the terms of the GNU General Public License as published by
6 #  the Free Software Foundation; either version 2 of the License, or
7 #  (at your option) any later version.
8 #
9 #  This program is distributed in the hope that it will be useful,
10 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #  GNU General Public License for more details.
13 #
14 #  You should have received a copy of the GNU General Public License
15 #  along with this program; if not, write to the Free Software
16 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17 #  USA
18 #
19 #  Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
20 #  Mountain View, CA 94043, USA, or: http://www.sgi.com
21 #-----------------------------------------------------------------------
22 # common extended attribute and ACL support
23
24 # filesystems that want to test maximum supported acl counts need to
25 # add support in here
26 _acl_get_max()
27 {
28         case $FSTYP in
29         xfs)
30                 # CRC format filesystems have much larger ACL counts. The actual
31                 # number is into the thousands, but testing that meany takes too
32                 # long, so just test well past the old limit of 25.
33                 xfs_info $TEST_DIR | _filter_mkfs > /dev/null 2> $tmp.info
34                 . $tmp.info
35                 rm $tmp.info
36                 if [ $_fs_has_crcs -eq 0 ]; then
37                         echo 25
38                 else
39                         echo 5461
40                 fi
41                 ;;
42         *)
43                 echo 0
44                 ;;
45         esac
46 }
47
48 _require_acl_get_max()
49 {
50         if [ $(_acl_get_max) -eq 0 ]; then
51                 _notrun "$FSTYP does not define maximum ACL count"
52         fi
53 }
54
55 # pick three unused user/group ids, store them as $acl[1-3]
56 #
57 _acl_setup_ids()
58 {
59     eval `(_cat_passwd; _cat_group) | awk -F: '
60       { ids[$3]=1 }
61       END {
62         j=1
63         for(i=1; i<1000000 && j<=3;i++){
64           if (! (i in ids)) {
65              printf "acl%d=%d;", j, i;           
66              j++
67           }
68         }       
69       }'`
70 }
71
72 # filter for the acl ids selected above
73 #
74 _acl_filter_id()
75 {
76     sed \
77        -e "s/u:$acl1/u:id1/" \
78        -e "s/u:$acl2/u:id2/" \
79        -e "s/u:$acl3/u:id3/" \
80        -e "s/g:$acl1/g:id1/" \
81        -e "s/g:$acl2/g:id2/" \
82        -e "s/g:$acl3/g:id3/" \
83        -e "s/ $acl1 / id1 /" \
84        -e "s/ $acl2 / id2 /" \
85        -e "s/ $acl3 / id3 /"
86 }
87
88 _getfacl_filter_id()
89 {
90     sed \
91        -e "s/user:$acl1/user:id1/" \
92        -e "s/user:$acl2/user:id2/" \
93        -e "s/user:$acl3/user:id3/" \
94        -e "s/group:$acl1/group:id1/" \
95        -e "s/group:$acl2/group:id2/" \
96        -e "s/group:$acl3/group:id3/" \
97        -e "s/: $acl1/: id1/" \
98        -e "s/: $acl2/: id2/" \
99        -e "s/: $acl3/: id3/"
100 }
101
102 # filtered ls
103 #
104 _acl_ls()
105 {
106     _ls_l -n $* | awk '{ print $1, $3, $4, $NF }' | _acl_filter_id
107
108
109 #
110 _acl_list()
111 {
112     _file1=$1
113
114     if [ $HOSTOS = "IRIX" ]; then
115         ls -dD $_file1 | _acl_filter_id
116     else 
117         chacl -l $_file1 | _acl_filter_id
118     fi
119 }
120
121 # create an ACL with n ACEs in it
122 #
123 _create_n_aces()
124 {
125     let n=$1-4
126     acl='u::rwx,g::rwx,o::rwx,m::rwx' # 4 ace acl start
127     while [ $n -ne 0 ]; do
128         acl="$acl,u:$n:rwx"
129         let n=$n-1
130     done
131     echo $acl
132 }
133
134 # filter user ace names to user ids
135 #
136 _filter_aces()
137 {
138     tmp_file=`mktemp /tmp/ace.XXXXXX`
139
140     (_cat_passwd; _cat_group) > $tmp_file
141
142     $AWK_PROG -v tmpfile=$tmp_file '
143         BEGIN {
144             FS=":"
145             while ( getline <tmpfile > 0 ) {
146                 idlist[$1] = $3 
147             }
148         }
149         /^user/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
150         /^u/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
151         /^default:user/ { if ($3 in idlist) sub($3, idlist[$3]); print; next}
152         {print}
153     '
154     rm -f $tmp_file
155 }
156
157 _filter_aces_notypes()
158 {
159     tr '\[' '\012' | tr ']' '\012' | tr ',' '\012' | _filter_aces|\
160     sed -e 's/u:/user:/' -e 's/g:/group:/' -e 's/o:/other:/' -e 's/m:/mask:/'
161 }
162
163 _require_acls()
164 {
165     if [ ! -x /bin/chacl -a ! -x /usr/bin/chacl -a ! -x /sbin/chacl ]; then
166         _notrun "chacl command not found"
167     fi
168
169     #
170     # Test if chacl is able to list ACLs on the target filesystems.  On really
171     # old kernels the system calls might not be implemented at all, but the
172     # more common case is that the tested filesystem simply doesn't support
173     # ACLs.
174     #
175     touch $TEST_DIR/syscalltest
176     chacl -l $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
177     cat $TEST_DIR/syscalltest.out >> $seqres.full
178
179     if grep -q 'Function not implemented' $TEST_DIR/syscalltest.out; then
180       _notrun "kernel does not support ACLs"
181     fi
182     if grep -q 'Operation not supported' $TEST_DIR/syscalltest.out; then
183       _notrun "ACLs not supported by this filesystem type: $FSTYP"
184     fi
185
186     rm -f $TEST_DIR/syscalltest.out
187 }
188
189 _list_acl()
190 {
191     file=$1
192
193     ls -dD $file | _acl_filter_id
194 }
195
196 _require_attrs()
197 {
198     [ -n $ATTR_PROG ] || _notrun "attr command not found"
199     [ -n $GETFATTR_PROG ] || _notrun "getfattr command not found"
200     [ -n $SETFATTR_PROG ] || _notrun "setfattr command not found"
201
202     #
203     # Test if chacl is able to write an attribute on the target filesystems.
204     # On really old kernels the system calls might not be implemented at all,
205     # but the more common case is that the tested filesystem simply doesn't
206     # support attributes.  Note that we can't simply list attributes as
207     # various security modules generate synthetic attributes not actually
208     # stored on disk.
209     #
210     touch $TEST_DIR/syscalltest
211     attr -s "user.xfstests" -V "attr" $TEST_DIR > $TEST_DIR/syscalltest.out 2>&1
212     cat $TEST_DIR/syscalltest.out >> $seqres.full
213
214     if grep -q 'Function not implemented' $TEST_DIR/syscalltest.out; then
215       _notrun "kernel does not support attrs"
216     fi
217     if grep -q 'Operation not supported' $TEST_DIR/syscalltest.out; then
218       _notrun "attrs not supported by this filesystem type: $FSTYP"
219     fi
220
221     rm -f $TEST_DIR/syscalltest.out
222 }
223
224 _require_attr_v1()
225 {
226         _scratch_mkfs_xfs_supported -i attr=1 >/dev/null 2>&1 \
227                 || _notrun "attr v1 not supported on $SCRATCH_DEV"
228 }
229
230 # check if we support the noattr2 mount option
231 _require_noattr2()
232 {
233         _scratch_mkfs_xfs > /dev/null 2>&1 \
234                 || _fail "_scratch_mkfs_xfs failed on $SCRATCH_DEV"
235         _mount -o noattr2 $SCRATCH_DEV > /dev/null 2>&1 \
236                 || _notrun "noattr2 mount option not supported on $SCRATCH_DEV"
237         _scratch_unmount
238 }
239
240 # getfattr -R returns info in readdir order which varies from fs to fs.
241 # This sorts the output by filename
242 _sort_getfattr_output()
243 {
244     awk '{a[FNR]=$0}END{n = asort(a); for(i=1; i <= n; i++) print a[i]"\n"}' RS=''
245 }
246
247 # set maximum total attr space based on fs type
248 if [ "$FSTYP" == "xfs" -o "$FSTYP" == "udf" ]; then
249         MAX_ATTRS=1000
250 else # Assume max ~1 block of attrs
251         BLOCK_SIZE=`stat -f $TEST_DIR | grep "Block size" | cut -d " " -f3`
252         # user.attribute_XXX="value.XXX" is about 32 bytes; leave some overhead
253         let MAX_ATTRS=$BLOCK_SIZE/40
254 fi
255
256 export MAX_ATTRS
257
258 # Set max attr value size based on fs type
259 if [ "$FSTYP" == "xfs" -o "$FSTYP" == "udf" -o "$FSTYP" == "btrfs" ]; then
260         MAX_ATTRVAL_SIZE=64
261 else # Assume max ~1 block of attrs
262         BLOCK_SIZE=`stat -f $TEST_DIR | grep "Block size" | cut -d " " -f3`
263         # leave a little overhead
264         let MAX_ATTRVAL_SIZE=$BLOCK_SIZE-256
265 fi
266
267 export MAX_ATTRVAL_SIZE
268 # make sure this script returns success
269 /bin/true