diff mbox series

[PATCHv2,1/2] systemd.bbclass: Clean up empty parent directories

Message ID 20240823170633.2776886-1-pkj@axis.com
State Accepted, archived
Commit 73159c0bbc636a08934f47690885d75fd37b701a
Headers show
Series [PATCHv2,1/2] systemd.bbclass: Clean up empty parent directories | expand

Commit Message

Peter Kjellerstedt Aug. 23, 2024, 5:06 p.m. UTC
Previously, rm_systemd_unitdir() would remove one parent directory of
${systemd_unitdir} if it was empty after removing ${systemd_unitdir}.
rm_sysvinit_initddir() would not remove any parent directory. Thus, if
the only directory created in /etc was /etc/init.d, an empty /etc would
remain after the cleanup and would be packaged.

Simplify rm_systemd_unitdir() and rm_sysvinit_initddir() by rewriting
them in shell, and use rmdir -p to remove all empty parent directories.

Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
---

PATCHv2:
* Rewrote rm_systemd_unitdir() to avoid having `rmdir -p` potentially
  delete ${D}.
* Add an argument to `read` used in rm_sysvinit_initddir() while
  determining if ${systemd_system_unitdir} is empty. Calling read
  without arguments is apparently a bashism.

 meta/classes-recipe/systemd.bbclass | 45 +++++++++++++----------------
 1 file changed, 20 insertions(+), 25 deletions(-)
diff mbox series

Patch

diff --git a/meta/classes-recipe/systemd.bbclass b/meta/classes-recipe/systemd.bbclass
index 0f7e3b5a08..fa6d3775ec 100644
--- a/meta/classes-recipe/systemd.bbclass
+++ b/meta/classes-recipe/systemd.bbclass
@@ -208,33 +208,28 @@  python systemd_populate_packages() {
 
 PACKAGESPLITFUNCS =+ "systemd_populate_packages"
 
-python rm_systemd_unitdir (){
-    import shutil
-    if not bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d):
-        systemd_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_unitdir'))
-        if os.path.exists(systemd_unitdir):
-            shutil.rmtree(systemd_unitdir)
-        systemd_libdir = os.path.dirname(systemd_unitdir)
-        if (os.path.exists(systemd_libdir) and not os.listdir(systemd_libdir)):
-            os.rmdir(systemd_libdir)
+rm_systemd_unitdir() {
+	rm -rf ${D}${systemd_unitdir}
+	# Change into ${D} and use a relative path with rmdir -p to avoid
+	# having it remove ${D} if it becomes empty.
+	(cd ${D} && rmdir -p $(dirname ${systemd_unitdir#/}) 2>/dev/null || :)
 }
 
-python rm_sysvinit_initddir (){
-    import shutil
-    sysv_initddir = oe.path.join(d.getVar("D"), (d.getVar('INIT_D_DIR') or "/etc/init.d"))
-
-    if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
-        not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) and \
-        os.path.exists(sysv_initddir):
-        systemd_system_unitdir = oe.path.join(d.getVar("D"), d.getVar('systemd_system_unitdir'))
+rm_sysvinit_initddir() {
+	local sysv_initddir=${INIT_D_DIR}
+	: ${sysv_initddir:=${sysconfdir}/init.d}
 
-        # If systemd_system_unitdir contains anything, delete sysv_initddir
-        if (os.path.exists(systemd_system_unitdir) and os.listdir(systemd_system_unitdir)):
-            shutil.rmtree(sysv_initddir)
+	# If systemd_system_unitdir contains anything, delete sysv_initddir
+        if find ${D}${systemd_system_unitdir} -mindepth 1 -maxdepth 1 2>/dev/null | read DUMMY; then
+		rm -rf ${D}$sysv_initddir
+		rmdir -p $(dirname ${D}$sysv_initddir) 2>/dev/null || :
+	fi
 }
 
-do_install[postfuncs] += "${RMINITDIR} "
-RMINITDIR:class-target = " rm_sysvinit_initddir rm_systemd_unitdir "
-RMINITDIR:class-nativesdk = " rm_sysvinit_initddir rm_systemd_unitdir "
-RMINITDIR = ""
-
+do_install[postfuncs] += "${RMINITDIR}"
+RMINITDIR = " \
+    ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', '', 'rm_systemd_unitdir', d)} \
+    ${@'rm_sysvinit_initddir' if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) and \
+                                 not bb.utils.contains('DISTRO_FEATURES', 'sysvinit', True, False, d) else ''} \
+"
+RMINITDIR:class-native = ""