-
Notifications
You must be signed in to change notification settings - Fork 6
/
hdfuse5.py
217 lines (186 loc) · 6.6 KB
/
hdfuse5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python
# Copyright (c) 2011 Tobias Richter <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import with_statement
from errno import EACCES
from sys import argv, exit
from threading import Lock
import os
import h5py
from fuse import FUSE, FuseOSError, Operations, LoggingMixIn
#class HDFuse5(LoggingMixIn, Operations):
class HDFuse5(Operations):
def __init__(self, root):
self.root = os.path.realpath(root)
self.rwlock = Lock()
def __call__(self, op, path, *args):
return super(HDFuse5, self).__call__(op, self.root + path, *args)
class PotentialHDFFile:
def __init__(self, path):
self.dsattrs = { "user.ndim" : (lambda x : x.value.ndim),
"user.shape" : (lambda x : x.value.shape),
"user.dtype" : (lambda x : x.value.dtype),
"user.size" : (lambda x : x.value.size),
"user.itemsize" : (lambda x : x.value.itemsize),
"user.dtype.itemsize" : (lambda x : x.value.dtype.itemsize),
}
self.fullpath = path
self.nexusfile = None
self.nexushandle = None
self.internalpath = "/"
if os.path.lexists(path):
self.testHDF(path)
else:
components = path.split("/")
for i in range(len(components),0,-1):
test = "/".join(components[:i])
if self.testHDF(test):
self.internalpath = "/".join(components[i-len(components):])
#print self.internalpath
break
def testHDF(self, path):
if os.path.isfile(path):
try:
self.nexushandle = h5py.File(path,'r')
self.nexusfile = path
#print path + " is hdf"
return True
except:
pass
return False
def __del__(self):
if self.nexushandle != None:
try:
#print "closing handle for "+self.fullpath
self.nexushandle.close()
except:
pass
def makeIntoDir(self, statdict):
statdict["st_mode"] = statdict["st_mode"] ^ 0100000 | 0040000
for i in [ [ 0400 , 0100 ] , [ 040 , 010 ] , [ 04, 01 ] ]:
if (statdict["st_mode"] & i[0]) != 0:
statdict["st_mode"] = statdict["st_mode"] | i[1]
return statdict
def getattr(self):
if self.nexusfile != None:
st = os.lstat(self.nexusfile)
else:
st = os.lstat(self.fullpath)
statdict = dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime',
'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid'))
if self.nexusfile != None:
if self.internalpath == "/":
statdict = self.makeIntoDir(statdict)
elif isinstance(self.nexushandle[self.internalpath],h5py.Group):
statdict = self.makeIntoDir(statdict)
statdict["st_size"] = 0
elif isinstance(self.nexushandle[self.internalpath],h5py.Dataset):
ob=self.nexushandle[self.internalpath].value
statdict["st_size"] = ob.size * ob.itemsize
return statdict
def getxattr(self, name):
if self.nexushandle == None:
return ""
rawname = name[5:]
if rawname in self.nexushandle[self.internalpath].attrs.keys():
return self.nexushandle[self.internalpath].attrs[rawname].__str__()
if isinstance(self.nexushandle[self.internalpath],h5py.Dataset):
if name in self.dsattrs.keys():
return self.dsattrs[name](self.nexushandle[self.internalpath]).__str__()
return ""
def listxattr(self):
if self.nexushandle == None:
return []
xattrs = []
for i in self.nexushandle[self.internalpath].attrs.keys():
xattrs.append("user."+i)
if isinstance(self.nexushandle[self.internalpath],h5py.Dataset):
for i in self.dsattrs.keys():
xattrs.append(i)
return xattrs
def listdir(self):
if self.nexushandle == None:
return ['.', '..'] + [name.encode('utf-8') for name in os.listdir(self.fullpath)]
else:
items = self.nexushandle[self.internalpath].items()
return ['.', '..'] + [item[0].encode('utf-8') for item in items]
def access(self, mode):
path = self.fullpath
if self.nexusfile != None:
path = self.nexusfile
if mode == os.X_OK:
mode = os.R_OK
if not os.access(path, mode):
raise FuseOSError(EACCES)
def read(self, size, offset, fh, lock):
if self.nexushandle == None or self.internalpath == "/":
with lock:
os.lseek(fh, offset, 0)
return os.read(fh, size)
if isinstance(self.nexushandle[self.internalpath],h5py.Dataset):
return self.nexushandle[self.internalpath].value.tostring()[offset:offset+size]
def open(self, flags):
if self.nexushandle == None or self.internalpath == "/":
return os.open(self.fullpath, flags)
return 0
def close(self, fh):
if self.nexushandle == None or self.internalpath == "/":
return os.close(fh)
return 0
def access(self, path, mode):
self.PotentialHDFFile(path).access(mode);
def read(self, path, size, offset, fh):
return self.PotentialHDFFile(path).read(size, offset, fh, self.rwlock)
def getattr(self, path, fh=None):
return self.PotentialHDFFile(path).getattr();
def getxattr(self, path, name):
return self.PotentialHDFFile(path).getxattr(name);
def listxattr(self, path):
return self.PotentialHDFFile(path).listxattr();
def readdir(self, path, fh):
return self.PotentialHDFFile(path).listdir();
def release(self, path, fh):
return self.PotentialHDFFile(path).close(fh);
def statfs(self, path):
stv = os.statvfs(path)
return dict((key, getattr(stv, key)) for key in ('f_bavail', 'f_bfree',
'f_blocks', 'f_bsize', 'f_favail', 'f_ffree', 'f_files', 'f_flag',
'f_frsize', 'f_namemax'))
def open(self, path, flags):
return self.PotentialHDFFile(path).open(flags);
truncate = None
write = None
rename = None
symlink = None
setxattr = None
removexattr = None
link = None
mkdir = None
mknod = None
rmdir = None
unlink = None
chmod = None
chown = None
create = None
fsync = None
flush = None
utimens = os.utime
readlink = os.readlink
if __name__ == "__main__":
if len(argv) != 3:
print 'usage: %s <root> <mountpoint>' % argv[0]
exit(1)
#signal.signal(signal.SIGINT, signal.SIG_DFL)
#fuse = FUSE(HDFuse5(argv[1]), argv[2], foreground=True)
fuse = FUSE(HDFuse5(argv[1]), argv[2])