Update copyright annotations and license boilerplates to correspond with SGI Legals...
[xfstests-dev.git] / common.quota
1 ##/bin/sh
2 #
3 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # Functions useful for quota tests
6 #
7
8
9 # checks that the XFS quota support in the kernel is enabled
10 # and that we have valid quota user tools installed.
11
12 _require_quota()
13 {
14     src/feature -q $TEST_DEV
15     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS quota"
16     [ -x /usr/sbin/xfs_quota ] || _notrun "Quota user tools not installed"
17 }
18
19 #
20 # checks that the XFS project quota support in the kernel is enabled.
21
22 _require_prjquota()
23 {
24     src/feature -p $TEST_DEV
25     [ $? -ne 0 ] && _notrun "Installed kernel does not support XFS project quotas"
26 }
27
28 #
29 # checks for user nobody in /etc/passwd and /etc/group.
30 #
31 _require_nobody()
32 {
33     grep -q '^nobody' /etc/passwd
34     [ $? -ne 0 ] && _notrun "/etc/passwd does not contain user nobody."
35
36     grep -q '^nobody' /etc/group
37     [ $? -ne 0 ] && _notrun "/etc/group does not contain user nobody."
38 }
39
40 # create a file as a specific user (uid)
41 # takes filename, id, type (u/g), blocksize, blockcount
42
43 _file_as_id()
44 {
45     [ $# != 5 ] && _notrun "broken call to _file_as_id in test $seq"
46
47     if [ $3 = u ]
48     then
49         magik='$>'      # perlspeak for effective uid
50     elif [ $3 = g ]
51     then
52         magik='$)'      # perlspeak for effective gid
53     else
54         _notrun "broken type in call to _file_as_id in test $seq"
55     fi
56
57     perl <<EOF >>$seq.full 2>&1
58         \$| = 1;
59         $magik = $2;
60         if ($5 == 0) {
61             print "touch $1";
62             exec "touch $1";
63         } else {
64             print "dd if=/dev/zero of=$1 bs=$4 count=$5";
65             exec "dd if=/dev/zero of=$1 bs=$4 count=$5";
66         }
67 EOF
68 # for debugging the above euid change, try... [need write in cwd]
69 #       exec "dd if=/dev/zero of=$1 bs=$4 count=$5 >>$seq.full 2>&1";
70 }
71
72 _choose_uid()
73 {
74     grep '^nobody' /etc/passwd | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
75 }
76
77 _choose_gid()
78 {
79     grep '^nobody' /etc/group | perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[2],$a[0] }'
80 }
81
82 _choose_prid()
83 {
84     if [ ! -f /etc/projid ]; then
85         echo 0
86         return
87     fi
88     perl -ne '@a = split(/:/); END { printf "id=%d name=%s\n", $a[1],$a[0] }' \
89         /etc/projid
90 }
91
92 #
93 # filter to get the guts out
94 # and make linux and irix similar
95 #
96 _filter_repquota()
97 {
98     tee -a $seq.full | $AWK_PROG '
99         /File limits/ {
100                 gotit = 1
101         }
102         gotit==1 {
103                 sub(/Disk/, "Block") 
104                 gsub(/timeleft/, "grace")
105                 gsub(/[.]0 /,"")
106                 sub(/- /, "* ")
107                 sub(/[+] /, "* ")
108                 gsub(/1week/,"7days")
109                 gsub(/NOT STARTED/,"")
110                 gsub(/[ \t]+/, " ")
111                 if ($0 ~ / [-+]/)
112                     $1 = "name"
113                 if ($0 !~ /^$/)
114                     print
115                 next
116         }
117     ' | sed -e '/-----------------/d'
118 }
119
120 _qmount()
121 {
122     umount $SCRATCH_DEV >/dev/null 2>&1
123     _scratch_mount || _fail "qmount failed"
124     chmod ugo+rwx $SCRATCH_MNT
125 }
126
127 _qsetup()
128 {
129     # setup exactly what it is we'll be testing
130     enforce=1
131     if src/feature -u $SCRATCH_DEV
132     then
133         type=u ; eval `_choose_uid`; ln -s $seq.usrquota $seq.out
134     elif src/feature -g $SCRATCH_DEV
135     then
136         type=g ; eval `_choose_gid`; ln -s $seq.grpquota $seq.out
137     elif src/feature -p $SCRATCH_DEV
138     then
139         type=p ; eval `_choose_prid`; ln -s $seq.prjquota $seq.out
140     elif src/feature -U $SCRATCH_DEV
141     then
142         type=u ; eval `_choose_uid`; ln -s $seq.uqnoenforce $seq.out
143         enforce=0
144     elif src/feature -G $SCRATCH_DEV
145     then
146         type=g ; eval `_choose_gid`; ln -s $seq.gqnoenforce $seq.out
147         enforce=0
148     elif src/feature -P $SCRATCH_DEV
149     then
150         type=p ; eval `_choose_prid`; ln -s $seq.pqnoenforce $seq.out
151         enforce=0
152     else
153         _notrun "No quota support at mount time"
154     fi
155
156     echo "Using output from '" `ls -l $seq.out` "'" >>$seq.full
157     echo "and using type=$type id=$id" >>$seq.full
158 }
159
160
161 #-----------------------------------------------------------------------------------
162 #
163 # wrappers for Linux/IRIX differences
164 #
165 _setquota()
166 {
167     _opt_type=$1
168     _id=$2
169     _bsoft=$3
170     _bhard=$4
171     _isoft=$5
172     _ihard=$6
173     _fs=$7
174
175     [ $# -eq 7 ] || _fail "_setquota failed with wrong # args: $*"
176
177     if [ $HOSTOS = "Linux" ]; then
178         echo "setquota $_opt_type $_id $_bsoft $_bhard $_isoft $_ihard $_fs" >>$seq.full
179         setquota $_opt_type $_id $_bsoft $_bhard $_isoft $_ihard $_fs
180     else
181         # IRIX
182         if [ $_opt_type = "-u" ]; then
183                 _opt_type=""
184                 id_param="uid"
185         else 
186                 id_param="gid"
187         fi
188         echo "/usr/etc/edquota $_opt_type -f $_fs -l $id_param=$_id,bsoft=$_bsoft,bhard=$_bhard,isoft=$_isoft,ihard=$_ihard" >>$seq.full
189         /usr/etc/edquota $_opt_type -f $_fs -l $id_param=$_id,bsoft=$_bsoft,bhard=$_bhard,isoft=$_isoft,ihard=$_ihard
190     fi
191 }
192
193 _quota()
194 {
195     echo "quota $*" >>$seq.full
196     if [ $HOSTOS = "Linux" ]; then
197         quota $*
198     else
199         # clear -u
200         args=`echo $*|sed -e 's/-u//'`
201         quota -v $args
202     fi
203 }
204
205 _repquota()
206 {
207     echo "repquota $*" >>$seq.full
208     if [ $HOSTOS = "Linux" ]; then
209         repquota $*
210     else
211         # clear -u
212         args=`echo $*|sed -e 's/-u//'`
213         repquota -v $args
214     fi
215 }
216
217 #-----------------------------------------------------------------------------------
218
219 # make sure this script returns success
220 /bin/true