Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/src/lib/archroot.sh
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2023-05-29 16:05:59 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2023-05-29 16:05:59 +0200
commitbe8ac63f28a402acbf7e21972ac5d3b45bed1f79 (patch)
tree6eb83c09f6549ae41988a25243a2b424a4f99a4e /src/lib/archroot.sh
parentbfc22eea7e4c6877fe8b9d89fa574cb0729466db (diff)
parenta07df0beeaeea1bf5665512bacc7a013eece4602 (diff)
merged with devtools 1.0.1 upstream (git repo migration)
Diffstat (limited to 'src/lib/archroot.sh')
-rw-r--r--src/lib/archroot.sh63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/lib/archroot.sh b/src/lib/archroot.sh
new file mode 100644
index 0000000..8386c6c
--- /dev/null
+++ b/src/lib/archroot.sh
@@ -0,0 +1,63 @@
+#!/hint/bash
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+:
+
+# shellcheck disable=2034
+CHROOT_VERSION='v5'
+
+##
+# usage : check_root $keepenv
+##
+check_root() {
+ local keepenv=$1
+ shift
+ local orig_argv=("$@")
+
+ (( EUID == 0 )) && return
+ if type -P sudo >/dev/null; then
+ exec sudo --preserve-env="${keepenv}" -- "${orig_argv[@]}"
+ else
+ exec su root -c "$(printf ' %q' "${orig_argv[@]}")"
+ fi
+}
+
+##
+# usage : is_btrfs( $path )
+# return : whether $path is on a btrfs
+##
+is_btrfs() {
+ [[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]]
+}
+
+##
+# usage : is_subvolume( $path )
+# return : whether $path is a the root of a btrfs subvolume (including
+# the top-level subvolume).
+##
+is_subvolume() {
+ [[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs && "$(stat -c %i "$1")" == 256 ]]
+}
+
+##
+# usage : subvolume_delete_recursive( $path )
+#
+# Find all btrfs subvolumes under and including $path and delete them.
+##
+subvolume_delete_recursive() {
+ local subvol
+
+ is_subvolume "$1" || return 0
+
+ while IFS= read -d $'\0' -r subvol; do
+ if ! subvolume_delete_recursive "$subvol"; then
+ return 1
+ fi
+ done < <(find "$1" -mindepth 1 -xdev -depth -inum 256 -print0)
+ if ! btrfs subvolume delete "$1" &>/dev/null; then
+ error "Unable to delete subvolume %s" "$subvol"
+ return 1
+ fi
+
+ return 0
+}