From 76a298c04254b49ca97b4c017a32de6b8b28d4c9 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Fri, 13 Nov 2020 15:33:18 +0100 Subject: [PATCH] bkctld-rm: delete empty jails in incs directory --- CHANGELOG.md | 1 + lib/bkctld-rm | 18 +++++++++++++++++- lib/includes | 4 ++++ test/incs.bats | 17 +++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db6f390..f7354bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * bkctld: add a [-f|--force] option to remove confirmation on some commands * bkctld-remove: confirmation before removal of jails if not in force mode +* bkctld-rm: delete empty jails in incs directory ### Changed diff --git a/lib/bkctld-rm b/lib/bkctld-rm index 3d5a99f..7b1df54 100755 --- a/lib/bkctld-rm +++ b/lib/bkctld-rm @@ -84,6 +84,21 @@ delete_inc_ext4() { fi } +delete_empty_inc() { + jail_name=$1 + + incs_path=$(incs_path "${jail_name}") + empty_incs_list=$(find "${incs_path}" -mindepth 0 -maxdepth 0 -type d -empty) + + for empty_inc in ${empty_incs_list}; do + if dry_run; then + echo "[dry-run] Delete empty \`${empty_inc}'" + else + rmdir "${empty_inc}" + notice "Delete empty \`${empty_inc}' : OK" + fi + done +} lock_file="${LOCKDIR}/rm-global.lock" # shellcheck disable=SC2064 @@ -104,7 +119,6 @@ for jail_name in ${jails_list}; do # If no incs policy is found, we don't remove incs if [ -n "${incs_policy_file}" ]; then # shellcheck disable=SC2046 - incs_to_delete=$(incs_to_delete "${jail_name}" "${incs_policy_file}") incs_total=$(echo ${incs_to_delete} | wc -w) incs_count=0 @@ -132,6 +146,8 @@ for jail_name in ${jails_list}; do else notice "Skip jail \`${jail_name}' : incs policy is missing" fi + # Delete empty incs directory for jail + delete_empty_inc "${jail_name}" done # Remove the lock file and cleanup tmp files diff --git a/lib/includes b/lib/includes index 5cd4586..5d979cd 100755 --- a/lib/includes +++ b/lib/includes @@ -184,6 +184,10 @@ incs_list() { jail_name=${1:?} find "$(incs_path "${jail_name}")" -mindepth 1 -maxdepth 1 -type d | sed 's!.*/!!' | sort -h } +# Return the list of empty incs directories +empty_incs_list() { + find ${INCDIR} -mindepth 1 -maxdepth 1 -type d -empty +} current_jail_incs_policy_file() { jail_name=${1:?} diff --git a/test/incs.bats b/test/incs.bats index 380d990..9120503 100644 --- a/test/incs.bats +++ b/test/incs.bats @@ -87,4 +87,21 @@ load test_helper assert_failure } +@test "empty inc directory are removed" { + # Create an inc + /usr/lib/bkctld/bkctld-inc + # no inc should be kept + echo '' > "${CONFDIR}/${JAILNAME}.d/incs_policy" + + # The inc directory is present + run test -d "${INCSPATH}" + assert_success + + /usr/lib/bkctld/bkctld-rm + + # The inc directory is absent + run test -d "${INCSPATH}" + assert_failure +} + # TODO: add many tests for incs (creation and removal)