]> arthur.barton.de Git - bup.git/blob - cmd-fuse.py
0b0bab64ce286dce8125116b96c3e402bbd3ed64
[bup.git] / cmd-fuse.py
1 #!/usr/bin/env python
2 import sys, os, stat, errno, fuse, re, time, tempfile
3 import options, git, vfs
4 from helpers import *
5
6
7 class Stat(fuse.Stat):
8     def __init__(self):
9         self.st_mode = 0
10         self.st_ino = 0
11         self.st_dev = 0
12         self.st_nlink = 0
13         self.st_uid = 0
14         self.st_gid = 0
15         self.st_size = 0
16         self.st_atime = 0
17         self.st_mtime = 0
18         self.st_ctime = 0
19
20
21 cache = {}
22 def cache_get(top, path):
23     parts = path.split('/')
24     cache[('',)] = top
25     c = None
26     max = len(parts)
27     #log('cache: %r\n' % cache.keys())
28     for i in range(max):
29         pre = parts[:max-i]
30         #log('cache trying: %r\n' % pre)
31         c = cache.get(tuple(pre))
32         if c:
33             rest = parts[max-i:]
34             for r in rest:
35                 #log('resolving %r from %r\n' % (r, c.fullname()))
36                 c = c.lresolve(r)
37                 key = tuple(pre + [r])
38                 #log('saving: %r\n' % (key,))
39                 cache[key] = c
40             break
41     assert(c)
42     return c
43         
44     
45
46 class BupFs(fuse.Fuse):
47     def __init__(self, top):
48         fuse.Fuse.__init__(self)
49         self.top = top
50     
51     def getattr(self, path):
52         log('--getattr(%r)\n' % path)
53         try:
54             node = cache_get(self.top, path)
55             st = Stat()
56             st.st_mode = node.mode
57             st.st_nlink = node.nlinks()
58             st.st_size = node.size()
59             return st
60         except vfs.NoSuchFile:
61             return -errno.ENOENT
62
63     def readdir(self, path, offset):
64         log('--readdir(%r)\n' % path)
65         node = cache_get(self.top, path)
66         yield fuse.Direntry('.')
67         yield fuse.Direntry('..')
68         for sub in node.subs():
69             yield fuse.Direntry(sub.name)
70
71     def readlink(self, path):
72         log('--readlink(%r)\n' % path)
73         node = cache_get(self.top, path)
74         return node.readlink()
75
76     def open(self, path, flags):
77         log('--open(%r)\n' % path)
78         node = cache_get(self.top, path)
79         accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
80         if (flags & accmode) != os.O_RDONLY:
81             return -errno.EACCES
82         node.open()
83
84     def release(self, path, flags):
85         log('--release(%r)\n' % path)
86
87     def read(self, path, size, offset):
88         log('--read(%r)\n' % path)
89         n = cache_get(self.top, path)
90         return n.readbytes(offset, size)
91
92
93 if not hasattr(fuse, '__version__'):
94     raise RuntimeError, "your fuse module is too old for fuse.__version__"
95 fuse.fuse_python_api = (0, 2)
96
97
98 optspec = """
99 bup fuse [-d] [-f] <mountpoint>
100 --
101 d,debug   increase debug level
102 f,foreground  run in foreground
103 """
104 o = options.Options('bup fuse', optspec)
105 (opt, flags, extra) = o.parse(sys.argv[1:])
106
107 if len(extra) != 1:
108     o.fatal("exactly one argument expected")
109
110 git.check_repo_or_die()
111 top = vfs.RefList(None)
112 f = BupFs(top)
113 f.fuse_args.mountpoint = extra[0]
114 if opt.debug:
115     f.fuse_args.add('debug')
116 if opt.foreground:
117     f.fuse_args.setmod('foreground')
118 print f.multithreaded
119 f.multithreaded = False
120 f.fuse_args.add('allow_other')
121
122 f.main()