1 |
#!/usr/bin/python2.5 |
---|
2 |
|
---|
3 |
# pyHesiodFS: |
---|
4 |
# Copyright (C) 2007 Quentin Smith <quentin@mit.edu> |
---|
5 |
# "Hello World" pyFUSE example: |
---|
6 |
# Copyright (C) 2006 Andrew Straw <strawman@astraw.com> |
---|
7 |
# |
---|
8 |
# This program can be distributed under the terms of the GNU LGPL. |
---|
9 |
# See the file COPYING. |
---|
10 |
# |
---|
11 |
|
---|
12 |
import sys, os, stat, errno |
---|
13 |
import fuse |
---|
14 |
from fuse import Fuse |
---|
15 |
|
---|
16 |
import hesiod |
---|
17 |
|
---|
18 |
new_fuse = hasattr(fuse, '__version__') |
---|
19 |
|
---|
20 |
fuse.fuse_python_api = (0, 2) |
---|
21 |
|
---|
22 |
hello_path = '/README.txt' |
---|
23 |
hello_str = """This is the pyhesiodfs FUSE autmounter. To access a Hesiod filsys, just access |
---|
24 |
%(mountpoint)s/name. |
---|
25 |
|
---|
26 |
If you're using the Finder, try pressing Cmd+Shift+G and then entering |
---|
27 |
%(mountpoint)s/name""" |
---|
28 |
|
---|
29 |
if not hasattr(fuse, 'Stat'): |
---|
30 |
fuse.Stat = object |
---|
31 |
|
---|
32 |
class MyStat(fuse.Stat): |
---|
33 |
def __init__(self): |
---|
34 |
self.st_mode = 0 |
---|
35 |
self.st_ino = 0 |
---|
36 |
self.st_dev = 0 |
---|
37 |
self.st_nlink = 0 |
---|
38 |
self.st_uid = 0 |
---|
39 |
self.st_gid = 0 |
---|
40 |
self.st_size = 0 |
---|
41 |
self.st_atime = 0 |
---|
42 |
self.st_mtime = 0 |
---|
43 |
self.st_ctime = 0 |
---|
44 |
|
---|
45 |
def toTuple(self): |
---|
46 |
return (self.st_mode, self.st_ino, self.st_dev, self.st_nlink, |
---|
47 |
self.st_uid, self.st_gid, self.st_size, self.st_atime, |
---|
48 |
self.st_mtime, self.st_ctime) |
---|
49 |
|
---|
50 |
class PyHesiodFS(Fuse): |
---|
51 |
|
---|
52 |
def __init__(self, *args, **kwargs): |
---|
53 |
Fuse.__init__(self, *args, **kwargs) |
---|
54 |
try: |
---|
55 |
self.fuse_args.add("allow_other", True) |
---|
56 |
except AttributeError: |
---|
57 |
self.allow_other = 1 |
---|
58 |
|
---|
59 |
if sys.platform == 'darwin': |
---|
60 |
self.fuse_args.add("noappledouble", True) |
---|
61 |
self.fuse_args.add("noapplexattr", True) |
---|
62 |
self.fuse_args.add("volname", "MIT") |
---|
63 |
self.fuse_args.add("fsname", "pyHesiodFS") |
---|
64 |
self.mounts = {} |
---|
65 |
|
---|
66 |
def getattr(self, path): |
---|
67 |
st = MyStat() |
---|
68 |
if path == '/': |
---|
69 |
st.st_mode = stat.S_IFDIR | 0755 |
---|
70 |
st.st_nlink = 2 |
---|
71 |
elif path == hello_path: |
---|
72 |
st.st_mode = stat.S_IFREG | 0444 |
---|
73 |
st.st_nlink = 1 |
---|
74 |
st.st_size = len(hello_str) |
---|
75 |
elif '/' not in path[1:]: |
---|
76 |
if self.findLocker(path[1:]): |
---|
77 |
st.st_mode = stat.S_IFLNK | 0777 |
---|
78 |
st.st_nlink = 1 |
---|
79 |
st.st_size = len(self.findLocker(path[1:])) |
---|
80 |
else: |
---|
81 |
return -errno.ENOENT |
---|
82 |
else: |
---|
83 |
return -errno.ENOENT |
---|
84 |
if new_fuse: |
---|
85 |
return st |
---|
86 |
else: |
---|
87 |
return st.toTuple() |
---|
88 |
|
---|
89 |
def getCachedLockers(self): |
---|
90 |
return self.mounts.keys() |
---|
91 |
|
---|
92 |
def findLocker(self, name): |
---|
93 |
"""Lookup a locker in hesiod and return its path""" |
---|
94 |
if name in self.mounts: |
---|
95 |
return self.mounts[name] |
---|
96 |
else: |
---|
97 |
try: |
---|
98 |
filsys = hesiod.FilsysLookup(name) |
---|
99 |
except IOError, e: |
---|
100 |
if e.errno in (errno.ENOENT, errno.EMSGSIZE): |
---|
101 |
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT)) |
---|
102 |
else: |
---|
103 |
raise IOError(errno.EIO, os.strerror(errno.EIO)) |
---|
104 |
# FIXME check if the first locker is valid |
---|
105 |
if len(filsys.filsys) >= 1: |
---|
106 |
pointers = filsys.filsys |
---|
107 |
pointer = pointers[0] |
---|
108 |
if pointer['type'] != 'AFS' and pointer['type'] != 'LOC': |
---|
109 |
print >>sys.stderr, "Unknown locker type "+pointer.type+" for locker "+name+" ("+repr(pointer)+" )" |
---|
110 |
return None |
---|
111 |
else: |
---|
112 |
self.mounts[name] = pointer['location'] |
---|
113 |
print >>sys.stderr, "Mounting "+name+" on "+pointer['location'] |
---|
114 |
return pointer['location'] |
---|
115 |
else: |
---|
116 |
print >>sys.stderr, "Couldn't find filsys for "+name |
---|
117 |
return None |
---|
118 |
|
---|
119 |
def getdir(self, path): |
---|
120 |
return [(i, 0) for i in (['.', '..', hello_path[1:]] + self.getCachedLockers())] |
---|
121 |
|
---|
122 |
def readdir(self, path, offset): |
---|
123 |
for (r, zero) in self.getdir(path): |
---|
124 |
yield fuse.Direntry(r) |
---|
125 |
|
---|
126 |
def readlink(self, path): |
---|
127 |
return self.findLocker(path[1:]) |
---|
128 |
|
---|
129 |
def open(self, path, flags): |
---|
130 |
if path != hello_path: |
---|
131 |
return -errno.ENOENT |
---|
132 |
accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR |
---|
133 |
if (flags & accmode) != os.O_RDONLY: |
---|
134 |
return -errno.EACCES |
---|
135 |
|
---|
136 |
def read(self, path, size, offset): |
---|
137 |
if path != hello_path: |
---|
138 |
return -errno.ENOENT |
---|
139 |
slen = len(hello_str) |
---|
140 |
if offset < slen: |
---|
141 |
if offset + size > slen: |
---|
142 |
size = slen - offset |
---|
143 |
buf = hello_str[offset:offset+size] |
---|
144 |
else: |
---|
145 |
buf = '' |
---|
146 |
return buf |
---|
147 |
|
---|
148 |
def main(): |
---|
149 |
global hello_str |
---|
150 |
try: |
---|
151 |
usage = Fuse.fusage |
---|
152 |
server = PyHesiodFS(version="%prog " + fuse.__version__, |
---|
153 |
usage=usage, |
---|
154 |
dash_s_do='setsingle') |
---|
155 |
server.parse(errex=1) |
---|
156 |
except AttributeError: |
---|
157 |
usage=""" |
---|
158 |
pyHesiodFS [mountpath] [options] |
---|
159 |
|
---|
160 |
""" |
---|
161 |
if sys.argv[1] == '-f': |
---|
162 |
sys.argv.pop(1) |
---|
163 |
server = PyHesiodFS() |
---|
164 |
|
---|
165 |
hello_str = hello_str % {'mountpoint': server.parse(errex=1).mountpoint} |
---|
166 |
server.main() |
---|
167 |
|
---|
168 |
if __name__ == '__main__': |
---|
169 |
main() |
---|