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