xfstests: randholes: clean up readblks()
[xfstests-dev.git] / common
1 ##/bin/bash 
2 #
3 # Copyright (c) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it would 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 the Free Software Foundation,
16 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 #
18 #
19 # common procedures for QA scripts
20 #
21 # $Header: /disk7/depot/linux/pcp/dev/qa/RCS/common,v 2.52 2000/04/05 18:24:51 kenmcd Exp $
22 #
23
24 _setenvironment()
25 {
26     MSGVERB="text:action"
27     export MSGVERB
28 }
29
30 here=`pwd`
31 rm -f $here/$iam.out
32 _setenvironment
33
34 check=${check-true}
35
36 diff="diff -u"
37 verbose=false
38 group=false
39 xgroup=false
40 showme=false
41 sortme=false
42 expunge=true
43 have_test_arg=false
44 randomize=false
45 rm -f $tmp.list $tmp.tmp $tmp.sed
46
47 # Autodetect fs type based on what's on $TEST_DEV
48 if [ "$HOSTOS" == "Linux" ]
49 then
50     export FSTYP=`blkid -c /dev/null -s TYPE -o value $TEST_DEV`
51 else
52     export FSTYP=xfs
53 fi
54
55 for r
56 do
57
58     if $group
59     then
60         # arg after -g
61        group_list=$(sed -n < group \
62                        -e 's/#.*//' \
63                        -e 's/$/ /' \
64                        -e "/^[0-9][0-9][0-9].* $r /"'{ s/ .*//p }')
65         if [ -z "$group_list" ]
66         then
67             echo "Group \"$r\" is empty or not defined?"
68             exit 1
69         fi
70         [ ! -s $tmp.list ] && touch $tmp.list
71         for t in $group_list
72         do
73             if grep -s "^$t\$" $tmp.list >/dev/null
74             then
75                 :
76             else
77                 echo "$t" >>$tmp.list
78             fi
79         done
80         group=false
81         continue
82
83     elif $xgroup
84     then
85         # arg after -x
86         [ ! -s $tmp.list ] && ls [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] >$tmp.list 2>/dev/null
87         group_list=`sed -n <group -e 's/$/ /' -e "/^[0-9][0-9][0-9].* $r /"'{
88 s/ .*//p
89 }'`
90         if [ -z "$group_list" ]
91         then
92             echo "Group \"$r\" is empty or not defined?"
93             exit 1
94         fi
95         numsed=0
96         rm -f $tmp.sed
97         for t in $group_list
98         do
99             if [ $numsed -gt 100 ]
100             then
101                 sed -f $tmp.sed <$tmp.list >$tmp.tmp
102                 mv $tmp.tmp $tmp.list
103                 numsed=0
104                 rm -f $tmp.sed
105             fi
106             echo "/^$t\$/d" >>$tmp.sed
107             numsed=`expr $numsed + 1`
108         done
109         sed -f $tmp.sed <$tmp.list >$tmp.tmp
110         mv $tmp.tmp $tmp.list
111         xgroup=false
112         continue
113     fi
114
115     xpand=true
116     case "$r"
117     in
118
119         -\? | -h | --help)      # usage
120             echo "Usage: $0 [options] [testlist]"'
121
122 common options
123     -v                  verbose
124
125 check options
126     -xfs                test XFS (default)
127     -udf                test UDF
128     -nfs                test NFS
129     -l                  line mode diff
130     -xdiff              graphical mode diff
131     -udiff              show unified diff (default)
132     -n                  show me, do not run tests
133     -q                  quick [deprecated]
134     -T                  output timestamps
135     -r                  randomize test order
136     
137 testlist options
138     -g group[,group...] include tests from these groups
139     -x group[,group...] exclude tests from these groups
140     NNN                 include test NNN
141     NNN-NNN             include test range (eg. 012-021)
142 '
143             exit 0
144             ;;
145
146         -udf)   # -udf ... set FSTYP to udf
147             FSTYP=udf
148             xpand=false
149             ;;
150
151         -xfs)   # -xfs ... set FSTYP to xfs
152             FSTYP=xfs
153             xpand=false
154             ;;
155
156         -nfs)   # -nfs ... set FSTYP to nfs
157             FSTYP=nfs
158             xpand=false
159             ;;
160
161         -g)     # -g group ... pick from group file
162             group=true
163             xpand=false
164             ;;
165
166         -l)     # line mode for diff, was default before
167             diff="diff"
168             xpand=false
169             ;;
170
171         -xdiff) # graphical diff mode
172             xpand=false
173
174             if [ ! -z "$DISPLAY" ]
175             then
176                 which xdiff >/dev/null 2>&1 && diff=xdiff
177                 which gdiff >/dev/null 2>&1 && diff=gdiff
178                 which tkdiff >/dev/null 2>&1 && diff=tkdiff
179                 which xxdiff >/dev/null 2>&1 && diff=xxdiff
180             fi
181             ;;
182
183         -udiff) # show a unified diff, default now, keep for backward compat
184             xpand=false
185             diff="$diff -u"
186             ;;
187
188         -q)     # "quick", no longer used - always quick :-)
189             xpand=false
190             ;;
191
192         -n)     # show me, don't do it
193             showme=true
194             xpand=false
195             ;;
196         -r)     # randomize test order
197             randomize=true
198             xpand=false
199             ;;
200
201         -T)     # turn on timestamp output
202             timestamp=true
203             xpand=false
204             ;;
205
206         -v)
207             verbose=true
208             xpand=false
209             ;;
210         -x)     # -x group ... exclude from group file
211             xgroup=true
212             xpand=false
213             ;;
214         '[0-9][0-9][0-9] [0-9][0-9][0-9][0-9]')
215             echo "No tests?"
216             status=1
217             exit $status
218             ;;
219
220         [0-9]*-[0-9]*)
221             eval `echo $r | sed -e 's/^/start=/' -e 's/-/ end=/'`
222             ;;
223
224         [0-9]*-)
225             eval `echo $r | sed -e 's/^/start=/' -e 's/-//'`
226             end=`echo [0-9][0-9][0-9] [0-9][0-9][0-9][0-9] | sed -e 's/\[0-9]//g' -e 's/  *$//' -e 's/.* //'`
227             if [ -z "$end" ]
228             then
229                 echo "No tests in range \"$r\"?"
230                 status=1
231                 exit $status
232             fi
233             ;;
234
235         *)
236             start=$r
237             end=$r
238             ;;
239
240     esac
241
242     # get rid of leading 0s as can be interpreted as octal
243     start=`echo $start | sed 's/^0*//'`
244     end=`echo $end | sed 's/^0*//'`
245
246     if $xpand
247     then
248         have_test_arg=true
249         $AWK_PROG </dev/null '
250 BEGIN   { for (t='$start'; t<='$end'; t++) printf "%03d\n",t }' \
251         | while read id
252         do
253             if grep -s "^$id " group >/dev/null
254             then
255                 # in group file ... OK
256                 echo $id >>$tmp.list
257             else
258                 if [ -f expunged ] && $expunge && egrep "^$id([         ]|\$)" expunged >/dev/null
259                 then
260                     # expunged ... will be reported, but not run, later
261                     echo $id >>$tmp.list
262                 else
263                     # oops
264                     echo "$id - unknown test, ignored"
265                 fi
266             fi
267         done
268     fi
269
270 done
271
272 if [ -s $tmp.list ]
273 then
274     # found some valid test numbers ... this is good
275     :
276 else
277     if $have_test_arg
278     then
279         # had test numbers, but none in group file ... do nothing
280         touch $tmp.list
281     else
282         # no test numbers, do everything from group file
283         sed -n -e '/^[0-9][0-9][0-9]*/s/[       ].*//p' <group >$tmp.list
284     fi
285 fi
286
287 # should be sort -n, but this did not work for Linux when this
288 # was ported from IRIX
289 #
290 list=`sort $tmp.list`
291 rm -f $tmp.list $tmp.tmp $tmp.sed
292
293 if $randomize
294 then
295     list=`echo $list | awk -f randomize.awk`
296 fi
297
298 case "$FSTYP" in
299     xfs)
300          [ "$XFS_LOGPRINT_PROG" = "" ] && _fatal "xfs_logprint not found"
301          [ "$XFS_REPAIR_PROG" = "" ] && _fatal "xfs_repair not found"
302          [ "$XFS_CHECK_PROG" = "" ] && _fatal "xfs_check not found"
303          [ "$XFS_DB_PROG" = "" ] && _fatal "xfs_db not found"
304          [ "$MKFS_XFS_PROG" = "" ] && _fatal "mkfs_xfs not found" 
305          ;;
306     udf)
307          [ "$MKFS_UDF_PROG" = "" ] && _fatal "mkfs_udf/mkudffs not found"
308          ;;
309     nfs)
310          ;;
311 esac