]> arthur.barton.de Git - bup.git/blob - t/cleanup-mounts-under
9a72dedd87dba85722a703e8cb25fbe8c2dc2d91
[bup.git] / t / cleanup-mounts-under
1 #!/usr/bin/env python
2
3 # This cannot rely on bup-python because it runs during clean, and so
4 # it also needs to be compatible with Python 2 and 3.
5
6 import os.path, re, subprocess, sys
7
8 def mntent_unescape(x):
9     def replacement(m):
10         unescapes = {
11             "\\\\" : "\\",
12             "\\011" : "\t",
13             "\\012" : "\n",
14             "\\040" : " "
15         }
16         return unescapes.get(m.group(0))
17     return re.sub(r'(\\\\|\\011|\\012|\\040)', replacement, x)
18
19 targets = sys.argv[1:]
20 exit_status = 0
21 for target in targets:
22     if not os.path.isdir(target):
23         print >> sys.stderr, target, 'is not a directory'
24         exit_status = 1
25         continue
26     top = os.path.realpath(target)
27     proc_mounts = open('/proc/mounts', 'r')
28     for line in proc_mounts:
29         _, point, fstype, _ = line.split(' ', 3)
30         point = mntent_unescape(point)
31         if top == point or os.path.commonprefix((top + '/', point)) == top + '/':
32             if fstype.startswith('fuse'):
33                 if subprocess.call(['fusermount', '-uz', point]) != 0:
34                     exit_status = 1
35             else:
36                 if subprocess.call(['umount', '-l', point]) != 0:
37                     exit_status = 1
38
39 sys.exit(exit_status)