xfstests: detect gdbm library correctly
[xfstests-dev.git] / common.defrag
1 ##/bin/bash
2 #
3 # Copyright (c) 2009 Eric Sandeen
4 # All Rights Reserved.
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it would be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write the Free Software Foundation,
17 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 #
19 #
20 # Functions useful for defragmentation tests
21 #
22
23 _require_defrag()
24 {
25     case "$FSTYP" in
26     xfs)
27         DEFRAG_PROG=/usr/sbin/xfs_fsr
28         ;;
29     ext4|ext4dev)
30         DEFRAG_PROG=/usr/bin/e4defrag
31         ;;
32     *)
33         _notrun "defragmentation not supported for fstype \"$FSTYP\""
34         ;;
35     esac
36
37     _require_command $DEFRAG_PROG
38     _require_command /usr/sbin/filefrag
39 }
40
41 _extent_count()
42 {
43         filefrag $1 | awk '{print $2}'
44         filefrag -v $1  >> $seq.full 2>&1
45 }
46
47 # Defrag file, check it, and remove it.
48 _defrag()
49 {
50         echo -n "Before: "
51         _extent_count $1
52         CSUM_BEFORE=`md5sum $1`
53         STAT_BEFORE=`stat -c "a: %x m: %y c: %z" $1`
54         $DEFRAG_PROG -v $1 >> $seq.full 2>&1
55         _scratch_remount
56         STAT_AFTER=`stat -c "a: %x m: %y c: %z" $1`
57         CSUM_AFTER=`md5sum $1`
58         echo -n "After: "
59         _extent_count $1
60         if [ "$CSUM_BEFORE" != "$CSUM_AFTER" ]; then
61                 _fail "file checksum changed post-defrag ($CSUM_BEFORE/$CSUM_AFTER)"
62         fi
63         if [ "$STAT_BEFORE" != "$STAT_AFTER" ]; then
64                 _fail "file timestamps changed post-defrag:\n$STAT_BEFORE\n$STAT_AFTER"
65         fi
66         rm -f $1
67 }
68