blob: cc51dc2bada8661081cdf459dd9923ed888bbb45 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#! /usr/bin/env bash
# Remove a directory on a btrfs filesystem. Inside, subvolumes are removed
# with btrfs subvolume delete and everything else with rm.
#
# Notes:
#
# 1. <dir> should not be a subvolume (use delete directly in this case).
#
# 2. Read-only subvolumes are changed to read-write before deleting.
#
usage="usage: $0 <dir>/"
owd="$(pwd)"
trap "{ cd '$owd'; exit 1; }" ERR
set -o errtrace # Trap in functions.
function info () { echo "$*" 1>&2; }
function error () { info "$*"; exit 1; }
dir="${1%/}"
if [ -z "$dir" ]; then
error "$usage"
fi
shopt -s nullglob dotglob
function rm_dir () # <dir>
{
local dir="$1"
local p
for p in "$dir"/*; do
if [ -d "$p" -a ! -L "$p" ]; then
# See if this is a subvolume: btrfs subvolume list requires root
# priviliges so we use the inode number which for subvolumes is always
# 256.
#
if [ "$(stat --format=%i "$p")" -eq 256 ]; then
rm_subvol "$p"
else
rm_dir "$p"
fi
else
rm "$p"
fi
done
rmdir "$dir"
}
function rm_subvol () # <dir>
{
local dir="$1"
if [ "$(btrfs property get -ts "$dir" ro)" = "ro=true" ]; then
btrfs property set -ts "$dir" ro false >/dev/null
fi
btrfs subvolume delete "$dir" >/dev/null
}
rm_dir "$dir"
|