]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
refactor(dracut): add create_directories function
authorBenjamin Drung <benjamin.drung@canonical.com>
Thu, 14 Aug 2025 19:23:08 +0000 (21:23 +0200)
committerNeal Gompa (ニール・ゴンパ) <ngompa13@gmail.com>
Tue, 28 Oct 2025 23:55:56 +0000 (19:55 -0400)
Reduce code duplication by introducing the helper function
`create_directories`.

Note: `test -h` is the same as `test -L`.

dracut.sh

index a746c84ec39bf3fb3dee59508aa3627e8548f007..3237e893bc7bffff6211b257955da0464e79a24f 100755 (executable)
--- a/dracut.sh
+++ b/dracut.sh
@@ -2062,6 +2062,21 @@ if [[ $print_cmdline ]]; then
     exit 0
 fi
 
+create_directories() {
+    local prefix="$1"
+    shift
+    for d in "$@"; do
+        d=${d#/}
+        [[ -e "${initdir}${prefix}/$d" ]] && continue
+        if [ -L "/$d" ]; then
+            inst_symlink "/$d" "${prefix}/$d"
+        else
+            # shellcheck disable=SC2174
+            mkdir -m 0755 -p "${initdir}${prefix}/$d"
+        fi
+    done
+}
+
 # Create some directory structure first
 # shellcheck disable=SC2174
 [[ $prefix ]] && mkdir -m 0755 -p "${initdir}${prefix}"
@@ -2079,38 +2094,14 @@ if [[ $prefix ]]; then
 fi
 
 if [[ $kernel_only != yes ]]; then
-    for d in usr usr/bin usr/sbin bin etc lib sbin tmp var var/tmp $libdirs; do
-        d=${d#/}
-        [[ -e "${initdir}${prefix}/$d" ]] && continue
-        if [ -L "/$d" ]; then
-            inst_symlink "/$d" "${prefix}/$d"
-        else
-            # shellcheck disable=SC2174
-            mkdir -m 0755 -p "${initdir}${prefix}/$d"
-        fi
-    done
-
-    for d in dev proc sys sysroot root run; do
-        if [ -L "/$d" ]; then
-            inst_symlink "/$d"
-        else
-            # shellcheck disable=SC2174
-            mkdir -m 0755 -p "$initdir/$d"
-        fi
-    done
-
+    # shellcheck disable=SC2086
+    create_directories "$prefix" usr usr/bin usr/sbin bin etc lib sbin tmp var var/tmp $libdirs
+    create_directories "" dev proc sys sysroot root run
     ln -sfn ../run "$initdir/var/run"
     ln -sfn ../run/lock "$initdir/var/lock"
 else
-    for d in lib $libdirs; do
-        [[ -e "${initdir}${prefix}/$d" ]] && continue
-        if [ -h "/$d" ]; then
-            inst "/$d" "${prefix}/$d"
-        else
-            # shellcheck disable=SC2174
-            mkdir -m 0755 -p "${initdir}${prefix}/$d"
-        fi
-    done
+    # shellcheck disable=SC2086
+    create_directories "$prefix" lib $libdirs
 fi
 
 mkdir -p "${initdir}"/lib/dracut