]> git.apps.os.sepia.ceph.com Git - xfstests-dev.git/commitdiff
common: simplify grep pipe sed interactions
authorDarrick J. Wong <djwong@kernel.org>
Fri, 28 Oct 2022 17:42:10 +0000 (10:42 -0700)
committerZorro Lang <zlang@kernel.org>
Sun, 30 Oct 2022 10:39:20 +0000 (18:39 +0800)
Zorro pointed out that the idiom "program | grep | sed" isn't necessary
for field extraction -- sed is perfectly capable of performing a
substitution and only printing the lines that match that substitution.
Do that for the common helpers.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Zorro Lang <zlang@redhat.com>
Signed-off-by: Zorro Lang <zlang@kernel.org>
common/ext4
common/populate
common/xfs

index f4c3c4139a150657234d28324aa4d485d86b31b1..4a2eaa157fda71a1d9efae52365a9a551bbc82d0 100644 (file)
@@ -191,3 +191,12 @@ _scratch_ext4_options()
        [ "$USE_EXTERNAL" = yes -a ! -z "$SCRATCH_LOGDEV" ] && \
                SCRATCH_OPTIONS="$SCRATCH_OPTIONS ${log_opt}"
 }
+
+# Get the inode flags for a particular inode number
+_ext4_get_inum_iflags() {
+       local dev="$1"
+       local inumber="$2"
+
+       debugfs -R "stat <${inumber}>" "${dev}" 2> /dev/null | \
+                       sed -n 's/^.*Flags: \([0-9a-fx]*\).*$/\1/p'
+}
index d9d4c6c3002e0a34ee5aee542dedaa1db604d29a..6e00499734c7898900fb6267438307e3a590a85d 100644 (file)
@@ -641,7 +641,7 @@ __populate_check_ext4_dformat() {
        extents=0
        etree=0
        debugfs -R "stat <${inode}>" "${dev}" 2> /dev/null | grep 'ETB[0-9]' -q && etree=1
-       iflags="$(debugfs -R "stat <${inode}>" "${dev}" 2> /dev/null | grep 'Flags:' | sed -e 's/^.*Flags: \([0-9a-fx]*\).*$/\1/g')"
+       iflags="$(_ext4_get_inum_iflags "${dev}" "${inode}")"
        test "$(echo "${iflags}" | awk '{print and(strtonum($1), 0x80000);}')" -gt 0 && extents=1
 
        case "${format}" in
@@ -688,7 +688,7 @@ __populate_check_ext4_dir() {
 
        htree=0
        inline=0
-       iflags="$(debugfs -R "stat <${inode}>" "${dev}" 2> /dev/null | grep 'Flags:' | sed -e 's/^.*Flags: \([0-9a-fx]*\).*$/\1/g')"
+       iflags="$(_ext4_get_inum_iflags "${dev}" "${inode}")"
        test "$(echo "${iflags}" | awk '{print and(strtonum($1), 0x1000);}')" -gt 0 && htree=1
        test "$(echo "${iflags}" | awk '{print and(strtonum($1), 0x10000000);}')" -gt 0 && inline=1
 
index a95f63499ff66414618cf1d17076140033c34e6d..8ac1964e9cbd33dd2e96c130961e8ecf6e705086 100644 (file)
@@ -179,8 +179,7 @@ _xfs_get_rtextents()
 {
        local path="$1"
 
-       $XFS_INFO_PROG "$path" | grep 'rtextents' | \
-               sed -e 's/^.*rtextents=\([0-9]*\).*$/\1/g'
+       $XFS_INFO_PROG "$path" | sed -n "s/^.*rtextents=\([[:digit:]]*\).*/\1/p"
 }
 
 # Get the realtime extent size of a mounted filesystem.
@@ -188,8 +187,7 @@ _xfs_get_rtextsize()
 {
        local path="$1"
 
-       $XFS_INFO_PROG "$path" | grep 'realtime.*extsz' | \
-               sed -e 's/^.*extsz=\([0-9]*\).*$/\1/g'
+       $XFS_INFO_PROG "$path" | sed -n "s/^.*realtime.*extsz=\([[:digit:]]*\).*/\1/p"
 }
 
 # Get the size of an allocation unit of a file.  Normally this is just the
@@ -217,8 +215,7 @@ _xfs_get_dir_blocksize()
 {
        local fs="$1"
 
-       $XFS_INFO_PROG "$fs" | grep 'naming.*bsize' | \
-               sed -e 's/^.*bsize=//g' -e 's/\([0-9]*\).*$/\1/g'
+       $XFS_INFO_PROG "$fs" | sed -n "s/^naming.*bsize=\([[:digit:]]*\).*/\1/p"
 }
 
 # Set or clear the realtime status of every supplied path.  The first argument
@@ -1267,7 +1264,7 @@ _force_xfsv4_mount_options()
 # Find AG count of mounted filesystem
 _xfs_mount_agcount()
 {
-       $XFS_INFO_PROG "$1" | grep agcount= | sed -e 's/^.*agcount=\([0-9]*\),.*$/\1/g'
+       $XFS_INFO_PROG "$1" | sed -n "s/^.*agcount=\([[:digit:]]*\).*/\1/p"
 }
 
 # Wipe the superblock of each XFS AGs