common/rc: generalize _get_filesize()
authorChao Yu <yuchao0@huawei.com>
Fri, 1 Nov 2019 10:28:21 +0000 (18:28 +0800)
committerEryu Guan <guaneryu@gmail.com>
Sat, 2 Nov 2019 06:30:14 +0000 (14:30 +0800)
There are some testcases use below two kind of commands to get file
size, generalize the second way as global function _get_filesize()
to simply codes.

1. ls -l $1 | $AWK_PROG '{print $5}'
2. stat -c %s $1

Signed-off-by: Chao Yu <yuchao0@huawei.com>
Reviewed-by: Eryu Guan <guaneryu@gmail.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
18 files changed:
common/populate
common/rc
common/verity
tests/btrfs/086
tests/generic/013
tests/generic/275
tests/generic/286
tests/generic/297
tests/generic/298
tests/generic/315
tests/generic/399
tests/generic/497
tests/generic/514
tests/generic/572
tests/overlay/060
tests/xfs/014
tests/xfs/076
tests/xfs/306

index eeac66d70c4a030bd29efcf4e14d8fc119d8e382..0b036051c8c30c738813826fe633942d76d74d17 100644 (file)
@@ -777,7 +777,7 @@ _fill_fs()
                fi
 
                if [ -f $dir/$file_count ]; then
                fi
 
                if [ -f $dir/$file_count ]; then
-                       bytes_written=$(stat -c '%s' $dir/$file_count)
+                       bytes_written=$(_get_filesize $dir/$file_count)
                fi
 
                # If there was no room to make the file, then divide it in
                fi
 
                # If there was no room to make the file, then divide it in
index 657d05d36d9968e3c96e11909b71df115f9e57b3..ef0c250923e4c38752eddcd679c66f3f8406ded2 100644 (file)
--- a/common/rc
+++ b/common/rc
@@ -165,6 +165,11 @@ if [ ! -z "$REPORT_LIST" ]; then
        _assert_report_list
 fi
 
        _assert_report_list
 fi
 
+_get_filesize()
+{
+    stat -c %s "$1"
+}
+
 _mount()
 {
     $MOUNT_PROG `_mount_ops_filter $*`
 _mount()
 {
     $MOUNT_PROG `_mount_ops_filter $*`
index b4c0e2dcb99fa2b439ea88e3662c1ec08cae7e41..a8d3de06b3c7e52bc4a11298bb96951bbd9bf3c4 100644 (file)
@@ -183,7 +183,7 @@ _fsv_scratch_corrupt_bytes()
        sync    # Sync to avoid unwritten extents
 
        cat > $tmp.bytes
        sync    # Sync to avoid unwritten extents
 
        cat > $tmp.bytes
-       local end=$(( offset + $(stat -c %s $tmp.bytes ) ))
+       local end=$(( offset + $(_get_filesize $tmp.bytes ) ))
 
        # For each extent that intersects the requested range in order, add a
        # command that writes the next part of the data to that extent.
 
        # For each extent that intersects the requested range in order, add a
        # command that writes the next part of the data to that extent.
@@ -234,7 +234,7 @@ _fsv_scratch_corrupt_merkle_tree()
        ext4|f2fs)
                # ext4 and f2fs store the Merkle tree after the file contents
                # itself, starting at the next 65536-byte aligned boundary.
        ext4|f2fs)
                # ext4 and f2fs store the Merkle tree after the file contents
                # itself, starting at the next 65536-byte aligned boundary.
-               (( offset += ($(stat -c %s $file) + 65535) & ~65535 ))
+               (( offset += ($(_get_filesize $file) + 65535) & ~65535 ))
                _fsv_scratch_corrupt_bytes $file $offset
                ;;
        *)
                _fsv_scratch_corrupt_bytes $file $offset
                ;;
        *)
index 750b54de769a43b0abcb24f8d2caddb5ded1a391..f80134a3d867ce77d496c63bbba277d9a947bf12 100755 (executable)
@@ -54,7 +54,7 @@ touch $SCRATCH_MNT/bar
 # supposed to happen and for example it made inode eviction enter an infinite
 # loop that dumped a warning trace on each iteration.
 $CLONER_PROG -s 0 -d 65536 -l 0 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
 # supposed to happen and for example it made inode eviction enter an infinite
 # loop that dumped a warning trace on each iteration.
 $CLONER_PROG -s 0 -d 65536 -l 0 $SCRATCH_MNT/foo $SCRATCH_MNT/bar
-echo "bar file size after clone operation: $(stat -c %s $SCRATCH_MNT/bar)"
+echo "bar file size after clone operation: `_get_filesize $SCRATCH_MNT/bar`"
 
 status=0
 exit
 
 status=0
 exit
index 9e533ee835962b6fa8c0913c7aa85e8ff05da9bc..bc596102783e905a811de9f2c6b63c6b7f076f6a 100755 (executable)
@@ -24,11 +24,6 @@ _cleanup()
     rm -rf $TEST_DIR/fsstress.$$.*
 }
 
     rm -rf $TEST_DIR/fsstress.$$.*
 }
 
-_filesize()
-{
-    ls -l $1 | $AWK_PROG '{print "    filesize = " $5}'
-}
-
 # get standard environment, filters and checks
 . ./common/rc
 . ./common/filter
 # get standard environment, filters and checks
 . ./common/rc
 . ./common/filter
index a934c19c6dfcee161f02445c0bda536f4c3e70a0..adc8285677f17493af1c190b2e8d6f064ec4c014 100755 (executable)
@@ -73,7 +73,7 @@ echo "Bytes written until ENOSPC:" >>$seqres.full
 du $SCRATCH_MNT/tmp1 >>$seqres.full
 
 # And at least some of it should succeed.
 du $SCRATCH_MNT/tmp1 >>$seqres.full
 
 # And at least some of it should succeed.
-_filesize=`ls -l $SCRATCH_MNT/tmp1 | awk '{print $5}'`
+_filesize=`_get_filesize $SCRATCH_MNT/tmp1`
 [ $_filesize -lt $((128 * 1024)) ] && \
        _fail "Partial write until enospc failed; wrote $_filesize bytes."
 
 [ $_filesize -lt $((128 * 1024)) ] && \
        _fail "Partial write until enospc failed; wrote $_filesize bytes."
 
index 0d980e791e5190cfc31ee2a135ec41c55710f9ad..00cb87b7f4fe384bb997aa5d7f05727e1665f5fe 100755 (executable)
@@ -57,7 +57,7 @@ test01()
 
        $here/src/seek_copy_test $src $dest
        
 
        $here/src/seek_copy_test $src $dest
        
-       test $(stat -c "%s" $src) = $(stat -c "%s" $dest) ||
+       test $(_get_filesize $src) = $(_get_filesize $dest) ||
                _fail "TEST01: file size check failed"
 
        cmp $src $dest || _fail "TEST01: file bytes check failed"
                _fail "TEST01: file size check failed"
 
        cmp $src $dest || _fail "TEST01: file bytes check failed"
@@ -85,7 +85,7 @@ test02()
 
        $here/src/seek_copy_test $src $dest
 
 
        $here/src/seek_copy_test $src $dest
 
-       test $(stat -c "%s" $src) = $(stat -c "%s" $dest) ||
+       test $(_get_filesize $src) = $(_get_filesize $dest) ||
                _fail "TEST02: file size check failed"
 
        cmp $src $dest || _fail "TEST02: file bytes check failed"
                _fail "TEST02: file size check failed"
 
        cmp $src $dest || _fail "TEST02: file bytes check failed"
@@ -127,7 +127,7 @@ test03()
        echo >>$seqres.full
        $here/src/seek_copy_test $src $dest
 
        echo >>$seqres.full
        $here/src/seek_copy_test $src $dest
 
-       test $(stat -c "%s" $src) = $(stat -c "%s" $dest) ||
+       test $(_get_filesize $src) = $(_get_filesize $dest) ||
                _fail "TEST03: file size check failed"
 
        cmp $src $dest || _fail "TEST03: file bytes check failed"
                _fail "TEST03: file size check failed"
 
        cmp $src $dest || _fail "TEST03: file bytes check failed"
@@ -169,7 +169,7 @@ test04()
        echo >>$seqres.full
        $here/src/seek_copy_test $src $dest
 
        echo >>$seqres.full
        $here/src/seek_copy_test $src $dest
 
-       test $(stat -c "%s" $src) = $(stat -c "%s" $dest) ||
+       test $(_get_filesize $src) = $(_get_filesize $dest) ||
                _fail "TEST04: file size check failed"
 
        cmp $src $dest || _fail "TEST04: file bytes check failed"
                _fail "TEST04: file size check failed"
 
        cmp $src $dest || _fail "TEST04: file bytes check failed"
index 5950a8da3d652f40471080c269817ce5884a57ab..eb08a9e314b9026de93ceafb1a824408b6541583 100755 (executable)
@@ -66,7 +66,7 @@ done
 
 echo "Try to kill reflink after a shorter period of time"
 kill_after=2   # give us a shorter time to die
 
 echo "Try to kill reflink after a shorter period of time"
 kill_after=2   # give us a shorter time to die
-n=$(stat -c '%s' $testdir/file1)
+n=$(_get_filesize $testdir/file1)
 echo "performing kill test on $n bytes..." >> $seqres.full
 touch $TEST_DIR/before
 $TIMEOUT_PROG -s INT ${kill_after}s $XFS_IO_PROG -f -c "reflink $testdir/file1 0 $n $n" $testdir/file1 >> $seqres.full 2>&1
 echo "performing kill test on $n bytes..." >> $seqres.full
 touch $TEST_DIR/before
 $TIMEOUT_PROG -s INT ${kill_after}s $XFS_IO_PROG -f -c "reflink $testdir/file1 0 $n $n" $testdir/file1 >> $seqres.full 2>&1
index b87012dd97d160b9cc82ba8b0419a182f1c41141..8b9cb7d683629b30a008235a616d2fd41254b985 100755 (executable)
@@ -66,7 +66,7 @@ done
 
 echo "Try to kill reflink after a shorter period of time"
 kill_after=2   # give us a shorter time to die
 
 echo "Try to kill reflink after a shorter period of time"
 kill_after=2   # give us a shorter time to die
-n=$(stat -c '%s' $testdir/file1)
+n=$(_get_filesize $testdir/file1)
 echo "performing kill test on $n bytes..." >> $seqres.full
 touch $TEST_DIR/before
 urk=$($TIMEOUT_PROG -s KILL ${kill_after}s $XFS_IO_PROG -f -c "reflink $testdir/file1 0 $n $n" $testdir/file1 >> $seqres.full 2>&1)
 echo "performing kill test on $n bytes..." >> $seqres.full
 touch $TEST_DIR/before
 urk=$($TIMEOUT_PROG -s KILL ${kill_after}s $XFS_IO_PROG -f -c "reflink $testdir/file1 0 $n $n" $testdir/file1 >> $seqres.full 2>&1)
index fd49b579638d1125bfbee47f539a8d2b1c500982..808d7d741ef631fb3143e2743aa661808a45e839 100755 (executable)
@@ -52,7 +52,7 @@ $XFS_IO_PROG -f -c 'falloc -k 0 $(($avail_begin/2))' \
        $TEST_DIR/testfile.$seq >>$seqres.full 2>&1
 
 # Verify the file size, it should keep unchanged as 0 in this case
        $TEST_DIR/testfile.$seq >>$seqres.full 2>&1
 
 # Verify the file size, it should keep unchanged as 0 in this case
-fsize=`ls -l $TEST_DIR/testfile.$seq | awk '{print $5}'`
+fsize=`_get_filesize $TEST_DIR/testfile.$seq`
 [ "$fsize" -eq 0 ] || _fail "File size is changed to ($fsize Bytes)"
 
 # Truncate the file size back to 0
 [ "$fsize" -eq 0 ] || _fail "File size is changed to ($fsize Bytes)"
 
 # Truncate the file size back to 0
index b2aaac1365ceeb89e5559136dd200270d5aa0cda..a9b3997ae59e98058e40b576ac371b55445ee231 100755 (executable)
@@ -89,7 +89,7 @@ while true; do
 
        file_size=0
        if [ -e $file ]; then
 
        file_size=0
        if [ -e $file ]; then
-               file_size=$(stat -c %s $file)
+               file_size=$(_get_filesize $file)
        fi
 
        # We shouldn't have been able to write more data than we had space for.
        fi
 
        # We shouldn't have been able to write more data than we had space for.
index 3d5502ef7c088e83cb2d56f98d46e4377317d78f..faeaa142c2593ece3d323ad54d028f441aa1f7c7 100755 (executable)
@@ -60,9 +60,9 @@ swapfile_cycle() {
 # Create a swap file with a large discontiguous range(?)
 echo "large discontig swap" | tee -a $seqres.full
 _pwrite_byte 0x58 0 $((len * 2)) $swapfile >> $seqres.full
 # Create a swap file with a large discontiguous range(?)
 echo "large discontig swap" | tee -a $seqres.full
 _pwrite_byte 0x58 0 $((len * 2)) $swapfile >> $seqres.full
-old_sz="$(stat -c '%s' $swapfile)"
+old_sz="$(_get_filesize $swapfile)"
 $XFS_IO_PROG -c "fcollapse $((len / 2)) $len" $swapfile >> $seqres.full 2>&1
 $XFS_IO_PROG -c "fcollapse $((len / 2)) $len" $swapfile >> $seqres.full 2>&1
-new_sz="$(stat -c '%s' $swapfile)"
+new_sz="$(_get_filesize $swapfile)"
 if [ $old_sz -gt $new_sz ]; then
        swapfile_cycle $swapfile
 fi
 if [ $old_sz -gt $new_sz ]; then
        swapfile_cycle $swapfile
 fi
@@ -71,9 +71,9 @@ rm -f $swapfile
 # Create a swap file with a small discontiguous range(?)
 echo "small discontig swap" | tee -a $seqres.full
 _pwrite_byte 0x58 0 $((len + 1024)) $swapfile >> $seqres.full
 # Create a swap file with a small discontiguous range(?)
 echo "small discontig swap" | tee -a $seqres.full
 _pwrite_byte 0x58 0 $((len + 1024)) $swapfile >> $seqres.full
-old_sz="$(stat -c '%s' $swapfile)"
+old_sz="$(_get_filesize $swapfile)"
 $XFS_IO_PROG -c "fcollapse 66560 1024" $swapfile >> $seqres.full 2>&1
 $XFS_IO_PROG -c "fcollapse 66560 1024" $swapfile >> $seqres.full 2>&1
-new_sz="$(stat -c '%s' $swapfile)"
+new_sz="$(_get_filesize $swapfile)"
 if [ $old_sz -gt $new_sz ]; then
        swapfile_cycle $swapfile
 fi
 if [ $old_sz -gt $new_sz ]; then
        swapfile_cycle $swapfile
 fi
index 0a71e382d0f989d0fe908ddec8db9953659a4d04..550a64751b0fa74a9c531fd2914fb2005671e333 100755 (executable)
@@ -39,7 +39,7 @@ chmod a+rwx $SCRATCH_MNT
 $XFS_IO_PROG -f -c "pwrite -S 0x18 0 1m" $SCRATCH_MNT/foo >>$seqres.full
 su -s/bin/bash - $qa_user -c "ulimit -f 64 ; $XFS_IO_PROG -f -c \"reflink $SCRATCH_MNT/foo\" $SCRATCH_MNT/bar" >> $seqres.full 2>&1
 
 $XFS_IO_PROG -f -c "pwrite -S 0x18 0 1m" $SCRATCH_MNT/foo >>$seqres.full
 su -s/bin/bash - $qa_user -c "ulimit -f 64 ; $XFS_IO_PROG -f -c \"reflink $SCRATCH_MNT/foo\" $SCRATCH_MNT/bar" >> $seqres.full 2>&1
 
-sz="$(stat -c '%s' $SCRATCH_MNT/bar)"
+sz="$(_get_filesize $SCRATCH_MNT/bar)"
 if [ "$sz" -ne 0 ] && [ "$sz" -ne 65536 ]; then
        echo "Oddball file size $sz??"
 fi
 if [ "$sz" -ne 0 ] && [ "$sz" -ne 65536 ]; then
        echo "Oddball file size $sz??"
 fi
index 534237868c0c890fb55de5055ab87c6702b6442d..a695724156762ceba0b7469e18e5ff69c5378447 100755 (executable)
@@ -163,10 +163,10 @@ head -c 100000 /dev/urandom > $fsv_orig_file
 cp $fsv_orig_file $fsv_file
 _fsv_enable $fsv_file >> $seqres.full
 cmp $fsv_file $fsv_orig_file
 cp $fsv_orig_file $fsv_file
 _fsv_enable $fsv_file >> $seqres.full
 cmp $fsv_file $fsv_orig_file
-stat -c %s $fsv_file
+_get_filesize $fsv_file
 _scratch_cycle_mount
 cmp $fsv_file $fsv_orig_file
 _scratch_cycle_mount
 cmp $fsv_file $fsv_orig_file
-stat -c %s $fsv_file
+_get_filesize $fsv_file
 
 _fsv_scratch_begin_subtest "Trying to measure non-verity file fails with ENODATA"
 echo foo > $fsv_file
 
 _fsv_scratch_begin_subtest "Trying to measure non-verity file fails with ENODATA"
 echo foo > $fsv_file
index 77ff01486386c3c047c39bcdd7a6872bb1081a97..34140e1256702bd813bbadcb14bebf81c8d2be19 100755 (executable)
@@ -99,7 +99,7 @@ check_file_size()
 {
        local target=$1 expected_size=$2 actual_size
 
 {
        local target=$1 expected_size=$2 actual_size
 
-       actual_size=$(stat -c "%s" $target)
+       actual_size=$(_get_filesize $target)
 
        [ "$actual_size" == "$expected_size" ] || echo "Expected file size $expected_size but actual size is $actual_size"
 }
 
        [ "$actual_size" == "$expected_size" ] || echo "Expected file size $expected_size but actual size is $actual_size"
 }
index 84e4e71ab3bdaee98774d049ba6cbc9d721d589d..be470a2dfc6ef7e8ebe2274a39894cbb3cf3d522 100755 (executable)
@@ -53,7 +53,7 @@ _spec_prealloc_file()
        # write a 4k aligned amount of data to keep the calculations simple
        $XFS_IO_PROG -c "pwrite 0 128m" $file >> $seqres.full
 
        # write a 4k aligned amount of data to keep the calculations simple
        $XFS_IO_PROG -c "pwrite 0 128m" $file >> $seqres.full
 
-       size=`stat -c "%s" $file`
+       size=`_get_filesize $file`
        blocks=`stat -c "%b" $file`
        blocksize=`stat -c "%B" $file`
 
        blocks=`stat -c "%b" $file`
        blocksize=`stat -c "%B" $file`
 
index e8b191c49a4a33512c786a995c5706f54ab7b65c..2dcdd30c7d6a00d1ee0edcf5f2318553b10d93ec 100755 (executable)
@@ -93,7 +93,7 @@ _consume_freesp $SCRATCH_MNT/spc
 # space. Note that we start at the end of the file and work backwards as a
 # reverse allocation pattern increases the chances of both left and right sparse
 # record merges.
 # space. Note that we start at the end of the file and work backwards as a
 # reverse allocation pattern increases the chances of both left and right sparse
 # record merges.
-offset=`stat -c "%s" $SCRATCH_MNT/spc`
+offset=`_get_filesize $SCRATCH_MNT/spc`
 offset=$((offset - $CHUNK_SIZE * 2))
 while [ $offset -ge 0 ]; do
        $XFS_IO_PROG -c "fpunch $offset $CHUNK_SIZE" $SCRATCH_MNT/spc \
 offset=$((offset - $CHUNK_SIZE * 2))
 while [ $offset -ge 0 ]; do
        $XFS_IO_PROG -c "fpunch $offset $CHUNK_SIZE" $SCRATCH_MNT/spc \
index b56b18f2ea3631b3b05e7b7aa6e8e774e9ab54a6..edbb6076e8917e1eecfd10894028ffd7b45d49b7 100755 (executable)
@@ -61,7 +61,7 @@ done
 $XFS_IO_PROG -xc "resblks 16" $SCRATCH_MNT >> $seqres.full 2>&1
 dd if=/dev/zero of=$SCRATCH_MNT/file bs=4k >> $seqres.full 2>&1
 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/file >> $seqres.full 2>&1
 $XFS_IO_PROG -xc "resblks 16" $SCRATCH_MNT >> $seqres.full 2>&1
 dd if=/dev/zero of=$SCRATCH_MNT/file bs=4k >> $seqres.full 2>&1
 $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/file >> $seqres.full 2>&1
-size=`stat -c %s $SCRATCH_MNT/file`
+size=`_get_filesize $SCRATCH_MNT/file`
 for i in $(seq 0 8192 $size); do
        $XFS_IO_PROG -c "fpunch $i 4k" $SCRATCH_MNT/file >> $seqres.full 2>&1
 done
 for i in $(seq 0 8192 $size); do
        $XFS_IO_PROG -c "fpunch $i 4k" $SCRATCH_MNT/file >> $seqres.full 2>&1
 done