4a3ac9ed88f5f00d1cb837cf2e88ac4dfb80d6b3
[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 # pick three unused user/group ids, store them as $acl[1-3]
25 #
26 _acl_setup_ids()
27 {
28     eval `(_cat_passwd; _cat_group) | awk -F: '
29       { ids[$3]=1 }
30       END {
31         j=1
32         for(i=1; i<1000000 && j<=3;i++){
33           if (! (i in ids)) {
34              printf "acl%d=%d;", j, i;           
35              j++
36           }
37         }       
38       }'`
39 }
40
41 # filter for the acl ids selected above
42 #
43 _acl_filter_id()
44 {
45     sed \
46        -e "s/u:$acl1/u:id1/" \
47        -e "s/u:$acl2/u:id2/" \
48        -e "s/u:$acl3/u:id3/" \
49        -e "s/g:$acl1/g:id1/" \
50        -e "s/g:$acl2/g:id2/" \
51        -e "s/g:$acl3/g:id3/" \
52        -e "s/ $acl1 / id1 /" \
53        -e "s/ $acl2 / id2 /" \
54        -e "s/ $acl3 / id3 /"
55 }
56
57 _getfacl_filter_id()
58 {
59     sed \
60        -e "s/user:$acl1/user:id1/" \
61        -e "s/user:$acl2/user:id2/" \
62        -e "s/user:$acl3/user:id3/" \
63        -e "s/group:$acl1/group:id1/" \
64        -e "s/group:$acl2/group:id2/" \
65        -e "s/group:$acl3/group:id3/" \
66        -e "s/: $acl1/: id1/" \
67        -e "s/: $acl2/: id2/" \
68        -e "s/: $acl3/: id3/"
69 }
70
71 # filtered ls
72 #
73 _acl_ls()
74 {
75     _ls_l -n $* | awk '{ print $1, $3, $4, $NF }' | _acl_filter_id
76
77
78 #
79 _acl_list()
80 {
81     _file1=$1
82
83     if [ $HOSTOS = "IRIX" ]; then
84         ls -dD $_file1 | _acl_filter_id
85     else 
86         chacl -l $_file1 | _acl_filter_id
87     fi
88 }
89
90 # create an ACL with n ACEs in it
91 #
92 _create_n_aces()
93 {
94     let n=$1-4
95     acl='u::rwx,g::rwx,o::rwx,m::rwx' # 4 ace acl start
96     while [ $n -ne 0 ]; do
97         acl="$acl,u:$n:rwx"
98         let n=$n-1
99     done
100     echo $acl
101 }
102
103 # filter user ace names to user ids
104 #
105 _filter_aces()
106 {
107     tmp_file=`mktemp /tmp/ace.XXXXXX`
108
109     (_cat_passwd; _cat_group) > $tmp_file
110
111     $AWK_PROG -v tmpfile=$tmp_file '
112         BEGIN {
113             FS=":"
114             while ( getline <tmpfile > 0 ) {
115                 idlist[$1] = $3 
116             }
117         }
118         /^user/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
119         /^u/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
120         /^default:user/ { if ($3 in idlist) sub($3, idlist[$3]); print; next}
121         {print}
122     '
123     rm -f $tmp_file
124 }
125
126 _filter_aces_notypes()
127 {
128     tr '\[' '\012' | tr ']' '\012' | tr ',' '\012' | _filter_aces|\
129     sed -e 's/u:/user:/' -e 's/g:/group:/' -e 's/o:/other:/' -e 's/m:/mask:/'
130 }
131
132 _require_acls()
133 {
134     if [ ! -x /bin/chacl -a ! -x /usr/bin/chacl -a ! -x /sbin/chacl ]; then
135         _notrun "chacl command not found"
136     fi
137
138     #
139     # Test if chacl is able to list ACLs on the target filesystems.  On really
140     # old kernels the system calls might not be implemented at all, but the
141     # more common case is that the tested filesystem simply doesn't support
142     # ACLs.
143     #
144     touch $TEST_DIR/syscalltest
145     chacl -l $TEST_DIR/syscalltest > $TEST_DIR/syscalltest.out 2>&1
146     cat $TEST_DIR/syscalltest.out >> $seqres.full
147
148     if grep -q 'Function not implemented' $TEST_DIR/syscalltest.out; then
149       _notrun "kernel does not support ACLs"
150     fi
151     if grep -q 'Operation not supported' $TEST_DIR/syscalltest.out; then
152       _notrun "ACLs not supported by this filesystem type: $FSTYP"
153     fi
154
155     rm -f $TEST_DIR/syscalltest.out
156 }
157
158 _list_acl()
159 {
160     file=$1
161
162     ls -dD $file | _acl_filter_id
163 }
164
165 _require_attrs()
166 {
167     [ -n $ATTR_PROG ] || _notrun "attr command not found"
168     [ -n $GETFATTR_PROG ] || _notrun "getfattr command not found"
169     [ -n $SETFATTR_PROG ] || _notrun "setfattr command not found"
170
171     #
172     # Test if chacl is able to write an attribute on the target filesystems.
173     # On really old kernels the system calls might not be implemented at all,
174     # but the more common case is that the tested filesystem simply doesn't
175     # support attributes.  Note that we can't simply list attributes as
176     # various security modules generate synthetic attributes not actually
177     # stored on disk.
178     #
179     touch $TEST_DIR/syscalltest
180     attr -s "user.xfstests" -V "attr" $TEST_DIR > $TEST_DIR/syscalltest.out 2>&1
181     cat $TEST_DIR/syscalltest.out >> $seqres.full
182
183     if grep -q 'Function not implemented' $TEST_DIR/syscalltest.out; then
184       _notrun "kernel does not support attrs"
185     fi
186     if grep -q 'Operation not supported' $TEST_DIR/syscalltest.out; then
187       _notrun "attrs not supported by this filesystem type: $FSTYP"
188     fi
189
190     rm -f $TEST_DIR/syscalltest.out
191 }
192
193 # getfattr -R returns info in readdir order which varies from fs to fs.
194 # This sorts the output by filename
195 _sort_getfattr_output()
196 {
197     awk '{a[FNR]=$0}END{n = asort(a); for(i=1; i <= n; i++) print a[i]"\n"}' RS=''
198 }
199
200 # set maximum total attr space based on fs type
201 if [ "$FSTYP" == "xfs" -o "$FSTYP" == "udf" ]; then
202         MAX_ATTRS=1000
203 else # Assume max ~1 block of attrs
204         BLOCK_SIZE=`stat -f $TEST_DIR | grep "Block size" | cut -d " " -f3`
205         # user.attribute_XXX="value.XXX" is about 32 bytes; leave some overhead
206         let MAX_ATTRS=$BLOCK_SIZE/40
207 fi
208
209 export MAX_ATTRS
210
211 # Set max attr value size based on fs type
212 if [ "$FSTYP" == "xfs" -o "$FSTYP" == "udf" -o "$FSTYP" == "btrfs" ]; then
213         MAX_ATTRVAL_SIZE=64
214 else # Assume max ~1 block of attrs
215         BLOCK_SIZE=`stat -f $TEST_DIR | grep "Block size" | cut -d " " -f3`
216         # leave a little overhead
217         let MAX_ATTRVAL_SIZE=$BLOCK_SIZE-256
218 fi
219
220 export MAX_ATTRVAL_SIZE
221 # make sure this script returns success
222 /bin/true