diff mbox series

[4/4] common: simplify grep pipe sed interactions

Message ID 166681101686.3403789.9650647383820032871.stgit@magnolia (mailing list archive)
State New, archived
Headers show
Series fstests: refactor xfs geometry computation | expand

Commit Message

Darrick J. Wong Oct. 26, 2022, 7:03 p.m. UTC
From: Darrick J. Wong <djwong@kernel.org>

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>
---
 common/ext4     |    9 +++++++++
 common/populate |    4 ++--
 common/xfs      |   11 ++++-------
 3 files changed, 15 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/common/ext4 b/common/ext4
index f4c3c4139a..4a2eaa157f 100644
--- a/common/ext4
+++ b/common/ext4
@@ -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'
+}
diff --git a/common/populate b/common/populate
index d9d4c6c300..6e00499734 100644
--- a/common/populate
+++ b/common/populate
@@ -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
 
diff --git a/common/xfs b/common/xfs
index 7c0f3eee19..d9105a9bb1 100644
--- a/common/xfs
+++ b/common/xfs
@@ -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
@@ -226,8 +224,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
@@ -1276,7 +1273,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