All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Darrick J. Wong" <djwong@kernel.org>
To: djwong@kernel.org, guaneryu@gmail.com, zlang@redhat.com
Cc: linux-xfs@vger.kernel.org, fstests@vger.kernel.org, guan@eryu.me
Subject: [PATCH 4/4] common: simplify grep pipe sed interactions
Date: Wed, 26 Oct 2022 12:03:36 -0700	[thread overview]
Message-ID: <166681101686.3403789.9650647383820032871.stgit@magnolia> (raw)
In-Reply-To: <166681099421.3403789.78493769502226810.stgit@magnolia>

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 --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


  parent reply	other threads:[~2022-10-26 19:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26 19:03 [PATCHSET v23.2 0/4] fstests: refactor xfs geometry computation Darrick J. Wong
2022-10-26 19:03 ` [PATCH 1/4] xfs: refactor filesystem feature detection logic Darrick J. Wong
2022-10-26 19:03 ` [PATCH 2/4] xfs: refactor filesystem directory block size extraction logic Darrick J. Wong
2022-10-27 16:59   ` Zorro Lang
2022-10-27 17:03     ` Darrick J. Wong
2022-10-28  6:08       ` Zorro Lang
2022-10-28 16:21         ` Darrick J. Wong
2022-10-28 16:55           ` Zorro Lang
2022-10-26 19:03 ` [PATCH 3/4] xfs: refactor filesystem realtime geometry detection logic Darrick J. Wong
2022-10-26 19:03 ` Darrick J. Wong [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-10-28 17:41 [PATCHSET v23.3 0/4] fstests: refactor xfs geometry computation Darrick J. Wong
2022-10-28 17:42 ` [PATCH 4/4] common: simplify grep pipe sed interactions Darrick J. Wong
2022-10-30  3:31   ` Zorro Lang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=166681101686.3403789.9650647383820032871.stgit@magnolia \
    --to=djwong@kernel.org \
    --cc=fstests@vger.kernel.org \
    --cc=guan@eryu.me \
    --cc=guaneryu@gmail.com \
    --cc=linux-xfs@vger.kernel.org \
    --cc=zlang@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.