-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextents-to-remove
executable file
·84 lines (69 loc) · 1.92 KB
/
extents-to-remove
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
# SPDX-FileCopyrightText: 2016-2020 Graham R. Cobb
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (C) 2016 Graham R. Cobb
# Released under GPL V3.0 -- see LICENSE
#
# extents-to-remove [-s] <disk> <directory>...
#
# Generates the extent list of extents which will be removed from the disk if the named
# directory/ies are removed.
#
# Directory names must be relative to the named disk.
#
# If the -s option is specified then the resulting list is automatically sent to extents-size.
#
# Note: this script may take a long time as the extents list for the whole disk must be generated!
#
# Example: to find out how much space would be recovered by removing two directories from a disk
# you might use:
#
# extents-to-remove -s /mnt/data some/directory some/other/directory
#
# Make sure errors are passed back
set -o pipefail
if [ "$1" = "-s" ]
then
shift
$0 "$@" | extents-size
exit $?
fi
if [ $# -lt 1 ]
then
echo "$0: Disk name is required" >&2
echo "Usage: $0 [-s] <disk> <directory>..." >&2
exit 1
fi
# Remove trailing slash
disk=${1%/}
shift
declare -a dirs
declare -a findargs
while [ $# -gt 0 ]
do
# Remove any trailing slash
dir="${1%/}"
dirs[${#dirs[*]}]="$disk/$dir"
findargs[${#findargs[*]}]="-path"
findargs[${#findargs[*]}]="$disk/$dir"
findargs[${#findargs[*]}]="-prune"
findargs[${#findargs[*]}]="-o"
shift
done
dirextents="$(mktemp)" || exit $?
#echo extents-list "${dirs[@]}"
extents-list "${dirs[@]}" >$dirextents
status=$?
if [ $status != 0 ] ; then rm $dirextents ; exit $status ; fi
remextents="$(mktemp)"
status=$?
if [ $status != 0 ] ; then rm $dirextents ; exit $status ; fi
#echo extents-list "$disk" "${findargs[@]}"
extents-list "$disk" "${findargs[@]}" >$remextents
status=$?
if [ $status != 0 ] ; then rm $dirextents ; rm $remextents ; exit $status ; fi
extents-difference $dirextents $remextents
status=$?
rm $dirextents $remextents
exit $status