-
Notifications
You must be signed in to change notification settings - Fork 3
/
myfuse.py
297 lines (250 loc) · 7.11 KB
/
myfuse.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#############################################################################
# myfuse.py - Implementation of seFS
#
# seFS Version 1.0
# Copyright 2012
# Chetan Giridhar cjgiridhar@gmail.com
# Vishal Kanaujia vishalkanaujia@gmail.com
# Created: 04/03/2012 Last Modified: 06/03/2012
#
# This file is part of seFS.
#
# seFS is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License v3 as published by
# the Free Software Foundation;
#
# seFS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#############################################################################
import errno
import fuse
import stat
import time
from seFS import seFS
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
fuse.fuse_python_api = (0, 2)
class Buffer: # {{{1
"""
This class wraps cStringIO.StringIO with two additions: The __len__
method and a dirty flag to determine whether a buffer has changed.
"""
def __init__(self):
self.buf = cStringIO.StringIO()
self.dirty = False
def __getattr__(self, attr, default=None):
""" Delegate to the StringIO object. """
return getattr(self.buf, attr, default)
def __len__(self):
""" Get the total size of the buffer in bytes. """
position = self.buf.tell()
self.buf.seek(0, os.SEEK_END)
length = self.buf.tell()
self.buf.seek(position, os.SEEK_SET)
return length
def truncate(self, *args):
""" Truncate the file at the current position and set the dirty flag. """
if len(self) > self.buf.tell():
self.dirty = True
return self.buf.truncate(*args)
def write(self, *args):
""" Write a string to the file and set the dirty flag. """
self.dirty = True
return self.buf.write(*args)
class MyFS(fuse.Fuse):
def __init__(self, *args, **kw):
fuse.Fuse.__init__(self, *args, **kw)
# Set some options required by the Python FUSE binding.
self.flags = 0
self.multithreaded = 0
self.fd = 0
self.sefs = seFS()
ret = self.sefs.open('/')
print "Created root with %s" %ret
self.sefs.write('/', "Root of the seFS")
t = int(time.time())
mytime = (t, t, t)
ret = self.sefs.utime('/', mytime)
print ret
self.sefs.setinode('/', 1)
self.is_dirty = False
def getdir(self, path):
print "in getdir"
def getattr(self, path):
sefs = seFS()
st = fuse.Stat()
c = fuse.FuseGetContext()
print c
print "getattr called path= %s", path
if path == '/':
st.st_nlink = 2
st.st_mode = stat.S_IFDIR | 0755
else:
print "For a regular file %s" %path
st.st_mode = stat.S_IFREG | 0777
st.st_nlink = 1
st.st_uid, st.st_gid = (c['uid'], c['gid'])
ret = sefs.search(path)
print "From database getattr ret=", ret
if ret is True:
tup = sefs.getutime(path)
print tup
st.st_mtime = int(tup[0].strip().split('.')[0])
st.st_ctime = int(tup[1].strip().split('.')[0])
st.st_atime = int(tup[2].strip().split('.')[0])
st.st_ino = int(sefs.getinode(path))
print "inode = %d" %st.st_ino
if sefs.getlength(path) is not None:
st.st_size = int(sefs.getlength(path))
else:
st.st_size = 0
return st
else:
return - errno.ENOENT
def readdir(self, path, offset):
print "readdir", path
yield fuse.Direntry('.')
yield fuse.Direntry('..')
sefs = seFS()
all_files = sefs.ls()
print "Rest of the files in root dir"
print all_files
for e in all_files:
if str(e) == '/':
continue
yield fuse.Direntry(str(e[1:]))
def link(self, target_path, link_path, nested=False):
print "In link****"
def open(self, path, flags):
print "open: trying to open %s" %path
sefs = seFS()
ret = sefs.search(path)
print ret
if ret is True:
return 0
return -errno.ENOENT
def truncate(self, length):
print "In truncate"
return 0
def mkdir(self, path, mode):
flags = None
self.create(path, flags, mode| stat.S_IFDIR)
return 0
def create(self, path, flags=None, mode=None):
print "trying to create %s", path
print path
print flags
sefs = seFS()
ret = self.open(path, flags)
print ret
if ret == -errno.ENOENT:
# Create the file in database
ret = sefs.open(path)
print ret
print "Creating the file %s" %path
t = int(time.time())
mytime = (t, t, t)
ret = sefs.utime(path, mytime)
print ret
self.fd = len(sefs.ls())
print "In create:fd = %d" %(self.fd)
sefs.setinode(path, self.fd)
print sefs.ls()
else:
print "The file %s exists!!" %path
return 0
def write(self, path, data, offset):
print "In write path=%s" %path
length = len(data)
print "The data is %s len=%d offset=%d" %(str(data), length, offset)
sefs = seFS()
ret = sefs.write(path, data)
return length
def release(self, path, flags):
print "In release"
if self.is_dirty is True:
print "Flushing buffer"
print self.buf.read()
sefs = seFS()
ret = sefs.write(path, self.buf.read())
print self.buf.read()
#self.buf.close()
#del self.buf
self.is_dirty = False
print ret
return 0
def readlink(self, path):
print "In readlink"
def link(self, target_path, link_path, nested=False):
print "In link"
def truncate(self, path, size):
print "In truncate"
def mknod (self, path, mode, dev):
print "mknod called"
return 0
def access(self, path, flag):
print "access path=%s" %path
sefs = seFS()
if sefs.search(path) is True:
print "In access, found the file %s" %path
return 0
else:
print "Could not find the file %s" %path
return -errno.EACCES
def read(self, path, size, offset):
try:
print "In read %s %d %d" %(path, size, offset)
sefs = seFS()
ret = sefs.read(path)
print "read(): %s" %(ret)
fbuf = StringIO()
fbuf.write(str(ret))
return fbuf.getvalue()
except Exception, e:
print "read failed"
return e
def chmod(self, path, mode):
print "In chmod %s %s" %(path, str(mode))
#self.files[path]['st_mode'] &= 0770000
#self.files[path]['st_mode'] |= mode
return 0
def chown(self, path, uid, gid):
print "In chown %s %d %d" %(path, uid, gid)
#self.files[path]['st_uid'] = uid
#self.files[path]['st_gid'] = gid
return 0
def utime(self, path, times=None):
print "In utime %s" %(path)
print times
atime, mtime = times
sefs = seFS()
ret = sefs.utime(path, (atime, mtime, atime))
print ret
return 0
def utimens(self, path, ts_acc, ts_mod):
print "In utimens %s" %(path)
print ts_acc, ts_mod
atime = ts_acc.tv_sec + (ts_acc.tv_nsec / 1000000.0)
mtime = ts_mod.tv_sec + (ts_mod.tv_nsec / 1000000.0)
ctime = atime
mytime = (mtime, ctime, atime)
sefs = seFS()
ret = sefs.utime(path, mytime)
print ret
def unlink(self,path):
print "In unlink path %s" %path
sefs = seFS()
ret = sefs.remove(path)
return
def fgetattr(path, fh=None):
print "In fgetattr"
return 0
if __name__ == '__main__':
fs = MyFS()
fs.parse(errex=1)
fs.main()