]> arthur.barton.de Git - bup.git/blob - t/lib.sh
Don't compute local script_home in lib.sh realpath (depended on pwd).
[bup.git] / t / lib.sh
1 # Assumes shell is Bash, and pipefail is set.
2
3 bup_t_lib_script_home=$(cd "$(dirname $0)" && pwd)
4
5 force-delete()
6 {
7     local rc=0
8     # Try *hard* to delete $@.  Among other things, some systems have
9     # r-xr-xr-x for root and other system dirs.
10     rm -rf "$@" # Maybe we'll get lucky.
11     for f in "$@"; do
12         test -e "$f" || continue
13         if test "$(type -p setfacl)"; then
14             setfacl -Rb "$f"
15         fi
16         if test "$(type -p chattr)"; then
17             chattr -R -aisu "$f"
18         fi
19         chmod -R u+rwX "$f"
20         rm -r "$f"
21         if test -e "$f"; then
22             rc=1
23             find "$f" -ls
24             lsattr -aR "$f"
25             getfacl -R "$f"
26         fi
27     done
28     return $rc
29 }
30
31 realpath()
32 {
33     test "$#" -eq 1 || return $?
34     echo "$1" | \
35         PYTHONPATH="$bup_t_lib_script_home/../lib" python -c \
36         "import sys, bup.helpers; print bup.helpers.realpath(sys.stdin.readline())" \
37         || return $?
38 }
39
40 current-filesystem()
41 {
42     local kernel="$(uname -s)" || return $?
43     case "$kernel" in
44         NetBSD)
45             df -G . | sed -En 's/.* ([^ ]*) fstype.*/\1/p'
46             ;;
47         SunOS)
48             df -g . | sed -En 's/.* ([^ ]*) fstype.*/\1/p'
49             ;;
50         *)
51             df -T . | awk 'END{print $2}'
52     esac
53 }
54
55 path-filesystems()
56 (
57     # Return filesystem for each dir from $1 to /.
58     # Perhaps for /foo/bar, "ext4\next4\nbtrfs\n".
59     test "$#" -eq 1 || exit $?
60     cd "$1" || exit $?
61     current-filesystem || exit $?
62     dir="$(pwd)" || exit $?
63     while test "$dir" != /; do
64         cd .. || exit $?
65         dir="$(pwd)" || exit $?
66         current-filesystem || exit $?
67     done
68     exit 0
69 )