From: Benjamin Drung <benjamin.drung@canonical.com>
Date: Wed, 6 Aug 2025 19:50:49 +0200
Subject: refactor(lsinitrd): introduce cpio_list function

In preparation to support 3cpio as alternative archive tool, move the
cpio listing call into `cpio_list`.

Forwarded: https://github.com/dracut-ng/dracut-ng/pull/1530
---
 lsinitrd.sh | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lsinitrd.sh b/lsinitrd.sh
index 0d45731..c06763a 100755
--- a/lsinitrd.sh
+++ b/lsinitrd.sh
@@ -199,6 +199,11 @@ cpio_extract_to_stdout() {
     $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --to-stdout -- "$@" 2> /dev/null
 }
 
+# Takes optional pattern arguments
+cpio_list() {
+    $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --list -- "$@"
+}
+
 extract_squash_img() {
     local _img _tmp
 
@@ -276,9 +281,9 @@ list_modules() {
 list_files() {
     echo "========================================================================"
     if [ "$sorted" -eq 1 ]; then
-        $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --list | sort -n -k5
+        cpio_list | sort -n -k5
     else
-        $CAT "$image" 2> /dev/null | cpio --extract --verbose --quiet --list | sort -k9
+        cpio_list | sort -k9
     fi
     ((ret += $?))
     echo "========================================================================"
@@ -411,7 +416,7 @@ case $bin in
         CAT="cat --"
         is_early=$(cpio_extract_to_stdout early_cpio 2> /dev/null)
         # Debian mkinitramfs does not create the file 'early_cpio', so let's check if firmware files exist
-        [[ "$is_early" ]] || is_early=$(cpio --list --verbose --quiet --to-stdout -- 'kernel/*/microcode/*.bin' < "$image" 2> /dev/null)
+        [[ "$is_early" ]] || is_early=$(cpio_list 'kernel/*/microcode/*.bin' 2> /dev/null)
         if [[ "$is_early" ]]; then
             if [[ -n $unpack ]]; then
                 # should use --unpackearly for early CPIO
