Fix quota tests correctly to run on SuSE.
[xfstests-dev.git] / common.attr
1 ##/bin/sh
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 /etc/passwd /etc/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 # filtered ls
58 #
59 _acl_ls()
60 {
61     ls -ln $* | awk '{ print $1, $3, $4, $NF }' | _acl_filter_id
62
63
64 #
65 _acl_list()
66 {
67     _file1=$1
68
69     if [ $HOSTOS = "IRIX" ]; then
70         ls -dD $_file1 | _acl_filter_id
71     else 
72         chacl -l $_file1 | _acl_filter_id
73     fi
74 }
75
76 # create an ACL with n ACEs in it
77 #
78 _create_n_aces()
79 {
80     n=`expr $1 - 4`
81     acl='u::rwx,g::rwx,o::rwx,m::rwx' # 4 ace acl start
82     while [ $n -ne 0 ]; do
83        acl="$acl,u:$n:rwx"
84        n=`expr $n - 1` 
85     done
86     echo $acl
87 }
88
89 # filter user ace names to user ids
90 #
91 _filter_aces()
92 {
93     $AWK_PROG '
94         BEGIN {
95             FS=":"
96             while ( getline <"/etc/passwd" > 0 ) {
97                 idlist[$1] = $3 
98             }
99         }
100         /^user/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
101         /^default:user/ { if ($3 in idlist) sub($3, idlist[$3]); print; next}
102         {print}
103     '
104 }
105
106 # test if acl code will work
107 #
108 _acl_requirements()
109 {
110     xfsdir=$TEST_DIR
111     
112
113     if [ ! -x /bin/chacl -a ! -x /usr/bin/chacl -a ! -x /sbin/chacl ]; then
114         _notrun "chacl command not found"
115     fi
116
117     # test if acl_get syscall is operational
118     # and hence the ACL config has been turned on  
119     touch $xfsdir/syscalltest
120     if chacl -l $xfsdir/syscalltest 2>&1 | tee -a $here/$seq.full | grep 'Function not implemented' >/dev/null
121     then
122       cd $here
123       _notrun "requires kernel ACL support"
124     fi
125 }
126
127 # filter user ace names to user ids
128 #
129 _filter_aces()
130 {
131     tr '\[' '\012' |\
132     tr ']' '\012' |\
133     tr ',' '\012' |\
134     $AWK_PROG '
135         BEGIN {
136             FS=":"
137             while ( getline <"/etc/passwd" > 0 ) {
138                 idlist[$1] = $3 
139             }
140         }
141         /^u/ { if ($2 in idlist) sub($2, idlist[$2]); print; next}
142         {print}
143     ' |\
144     sed -e 's/u:/user:/' -e 's/g:/group:/' -e 's/o:/other:/' -e 's/m:/mask:/'
145 }
146
147 _list_acl()
148 {
149     file=$1
150
151     ls -dD $file | _acl_filter_id
152 }
153
154 # make sure this script returns success
155 /bin/true