~ubuntu-branches/ubuntu/karmic/ltspfs/karmic

« back to all changes in this revision

Viewing changes to scripts/ltspfsmounter

  • Committer: Bazaar Package Importer
  • Author(s): Scott Balneaves
  • Date: 2006-09-18 13:01:06 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060918130106-ns6gmg2kl0oic3h6
Tags: 0.4.3-0ubuntu1
* Fixed variable check for static-devices in add_fstab_entry
* Bumped to 0.4.3
* UVF exception granted by mdz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import os
 
4
import sys
 
5
from subprocess import *
 
6
 
 
7
def get_var(name):
 
8
    return os.environ.get(name)
 
9
 
 
10
def add_ltspfsmount(conn, path, root, dev):
 
11
    hidden_mount = '%s/%s' % (root, dev)
 
12
    lbmount_command = ['lbmount', dev]
 
13
    ltspfs_mount = ['ltspfs', conn+':'+path, root+'/'+dev]
 
14
 
 
15
    if not os.access(root, 0):
 
16
        os.mkdir(root)
 
17
    if not os.access(hidden_mount, 0):
 
18
        os.mkdir(hidden_mount)
 
19
 
 
20
    env = os.environ.copy()
 
21
    env['DISPLAY'] = 'localhost:10.0'
 
22
 
 
23
    try:
 
24
                call(ltspfs_mount, env=env)
 
25
    except OSError, e:
 
26
        print >>sys.stderr, "mount failed:", e
 
27
    try:
 
28
        call(lbmount_command)
 
29
    except OSError, e:
 
30
        print >>sys.stderr, "suid mount failed:", e
 
31
 
 
32
def remove_ltspfsmount(root, dev):
 
33
    hidden_mount = "%s/%s" % (root, dev)
 
34
    lbumount_command=['lbmount', '--umount', dev]
 
35
    ltspfs_umount=['fusermount', '-uzq', root+'/'+dev]
 
36
 
 
37
    try:
 
38
        call(lbumount_command)
 
39
    except OSError, e:
 
40
        print >>sys.stderr, "suid umount failed:", e
 
41
    try:
 
42
        call(ltspfs_umount)
 
43
    except OSError, e:
 
44
        print >>sys.stderr, "umount failed:", e
 
45
 
 
46
    if os.access(hidden_mount, 0):
 
47
        os.rmdir(hidden_mount)
 
48
 
 
49
def cleanup(user):
 
50
    known_mounts = open( '/proc/mounts', 'r' ).readlines()
 
51
    for dir in ['/media', '/tmp']:
 
52
        for mount in known_mounts:
 
53
            if mount.startswith('ltspfs') and user in mount:
 
54
                mountpoint=mount.split()[1]
 
55
                device=mountpoint.split('/')[-1]
 
56
                if dir=='/media' and mountpoint.startswith(dir):
 
57
                    call(['lbmount', '--umount', device])
 
58
                elif dir=='/tmp' and mountpoint.startswith(dir):
 
59
                    call(['fusermount', '-uzq', mountpoint])
 
60
                    if os.access(mountpoint, 0):
 
61
                        os.rmdir(mountpoint)
 
62
    sys.exit(0)
 
63
 
 
64
def main():
 
65
    if len(sys.argv) < 3:
 
66
        print 'usage: %s mountpoint add|remove|cleanup' % sys.argv[0]
 
67
        sys.exit(1)
 
68
 
 
69
    path = sys.argv[1]
 
70
    command = sys.argv[2]
 
71
    root = "/tmp/.%s-ltspfs" % get_var('USER')
 
72
    conn = get_var('SSH_CONNECTION').split()[0]
 
73
    dev = path.split('/')[-1]
 
74
 
 
75
    if command=='add':
 
76
        add_ltspfsmount(conn, path, root, dev)
 
77
    elif command=='remove':
 
78
        remove_ltspfsmount(root, dev)
 
79
    elif command=='cleanup':
 
80
        cleanup(get_var('USER'))
 
81
    else:
 
82
        print 'unknown command'
 
83
 
 
84
if __name__ == "__main__":
 
85
    main()