]> arthur.barton.de Git - bup.git/blob - t/cleanup-mounts-under
test-restore-map-owner: accommodate python 3 and test there
[bup.git] / t / cleanup-mounts-under
1 #!/bin/sh
2 """": # -*-python-*-
3 bup_python="$(dirname "$0")/../cmd/bup-python" || exit $?
4 exec "$bup_python" "$0" ${1+"$@"}
5 """
6
7 from sys import stderr
8 import os.path, re, subprocess, sys
9
10 def mntent_unescape(x):
11     def replacement(m):
12         unescapes = {
13             "\\\\" : "\\",
14             "\\011" : "\t",
15             "\\012" : "\n",
16             "\\040" : " "
17         }
18         return unescapes.get(m.group(0))
19     return re.sub(r'(\\\\|\\011|\\012|\\040)', replacement, x)
20
21 targets = sys.argv[1:]
22
23 if not os.path.exists('/proc/mounts'):
24     print >> stderr, 'No /proc/mounts; skipping mount cleanup in', repr(targets)
25     sys.exit(0)
26
27 exit_status = 0
28 for target in targets:
29     if not os.path.isdir(target):
30         print >> stderr, repr(target), 'is not a directory'
31         exit_status = 1
32         continue
33     top = os.path.realpath(target)
34     proc_mounts = open('/proc/mounts', 'r')
35     for line in proc_mounts:
36         _, point, fstype, _ = line.split(' ', 3)
37         point = mntent_unescape(point)
38         if top == point or os.path.commonprefix((top + '/', point)) == top + '/':
39             if fstype.startswith('fuse'):
40                 if subprocess.call(['fusermount', '-uz', point]) != 0:
41                     exit_status = 1
42             else:
43                 if subprocess.call(['umount', '-l', point]) != 0:
44                     exit_status = 1
45
46 sys.exit(exit_status)