summaryrefslogtreecommitdiff
path: root/fs/expose/fuse.py
blob: d96c4aefb50f2555b718e0137766535e699e3db6 (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env python


import base

import fuse
fuse.fuse_python_api = (0, 2)


from datetime import datetime
import time
from os import errno

import sys
from stat import *

def showtb(f):

    def run(*args, **kwargs):
        print
        print "-"*80
        print f, args, kwargs
        try:
            ret = f(*args, **kwargs)
            print "\tReturned:", repr(ret)
            return ret
        except Exception, e:
            print e
            raise
        print "-"*80
        print
    return run

"""
::*'''<code>open(path, flags)</code>'''

::*'''<code>create(path, flags, mode)</code>'''

::*'''<code>read(path, length, offset, fh=None)</code>'''

::*'''<code>write(path, buf, offset, fh=None)</code>'''

::*'''<code>fgetattr(path, fh=None)</code>'''

::*'''<code>ftruncate(path, len, fh=None)</code>'''

::*'''<code>flush(path, fh=None)</code>'''

::*'''<code>release(path, fh=None)</code>'''

::*'''<code>fsync(path, fdatasync, fh=None)</code>'''

"""


class FuseFile(object):

    def __init__(self, f):
        self.f = f





_run_t = time.time()
class FSFUSE(fuse.Fuse):

    def __init__(self, fs, *args, **kwargs):
        fuse.Fuse.__init__(self, *args, **kwargs)
        self._fs = fs

    @showtb
    def fsinit(self):
        return 0

    def __getattr__(self, name):
        print name
        raise AttributeError

    #@showtb
    def getattr(self, path):

        if not self._fs.exists(path):
            return -errno.ENOENT

        class Stat(fuse.Stat):
            def __init__(self, context, fs, path):
                fuse.Stat.__init__(self)
                info = fs.getinfo(path)
                isdir = fs.isdir(path)

                fsize = fs.getsize(path) or 1024
                self.st_ino = 0
                self.st_dev = 0
                self.st_nlink = 2 if isdir else 1
                self.st_blksize = fsize
                self.st_mode = info.get('st_mode', S_IFDIR | 0755 if isdir else S_IFREG | 0666)
                print self.st_mode
                self.st_uid = context['uid']
                self.st_gid = context['gid']
                self.st_rdev = 0
                self.st_size = fsize
                self.st_blocks = 1

                for key, value in info.iteritems():
                    if not key.startswith('_'):
                        setattr(self, key, value)

                def do_time(attr, key):
                    if not hasattr(self, attr):
                        if key in info:
                            info_t = info[key]
                            setattr(self, attr, time.mktime(info_t.timetuple()))
                        else:
                            setattr(self, attr, _run_t)

                do_time('st_atime', 'accessed_time')
                do_time('st_mtime', 'modified_time')
                do_time('st_ctime', 'created_time')

                #for v in dir(self):
                #    if not v.startswith('_'):
                #        print v, getattr(self, v)

        return Stat(self.GetContext(), self._fs, path)

    @showtb
    def chmod(self, path, mode):
        return 0

    @showtb
    def chown(self, path, user, group):
        return 0

    @showtb
    def utime(self, path, times):
        return 0

    @showtb
    def utimens(self, path, times):
        return 0

    @showtb
    def fsyncdir(self):
        pass

    @showtb
    def bmap(self):
        return 0

    @showtb
    def ftruncate(self, path, flags, fh):
        if fh is not None:
            fh.truncate()
            fh.flush()
        return 0

    def fsdestroy(self):
        return 0

    @showtb
    def statfs(self):
        return (0, 0, 0, 0, 0, 0, 0)



    #def setattr
    #
    #
    #@showtb
    #def getdir(self, path, offset):
    #    paths = ['.', '..']
    #    paths += self._fs.listdir(path)
    #    print repr(paths)
    #
    #    for p in paths:
    #        yield fuse.Direntry(p)

    @showtb
    def opendir(self, path):
        return 0

    @showtb
    def getxattr(self, path, name, default):
        return self._fs.getattr(path, name, default)

    @showtb
    def setxattr(self, path, name, value):
        self._fs.setattr(path, name)
        return 0

    @showtb
    def removeattr(self, path, name):
        self._fs.removeattr(path, name)
        return 0

    @showtb
    def listxattr(self, path, something):
        return self._fs.listattrs(path)

    @showtb
    def open(self, path, flags):
        return self._fs.open(path, flags=flags)

    @showtb
    def create(self, path, flags, mode):
        return self._fs.open(path, "w")

    @showtb
    def read(self, path, length, offset, fh=None):
        if fh:
            fh.seek(offset)
            return fh.read(length)

    @showtb
    def write(self, path, buf, offset, fh=None):
        if fh:
            fh.seek(offset)
            # FUSE seems to expect a return value of the number of bytes written,
            # but Python file objects don't return that information,
            # so we will assume all bytes are written...
            bytes_written = fh.write(buf) or len(buf)
            return bytes_written

    @showtb
    def release(self, path, flags, fh=None):
        if fh:
            fh.close()
            return 0

    @showtb
    def flush(self, path, fh=None):
        if fh:
            try:
                fh.flush()
            except base.FSError:
                return 0
            return 0

    @showtb
    def access(self, path, *args, **kwargs):
        return 0


    #@showtb
    def readdir(self, path, offset):
        paths = ['.', '..']
        paths += self._fs.listdir(path)
        return [fuse.Direntry(p) for p in paths]

    #@showtb
    #def fgetattr(self, path, fh=None):
    #    fh.flush()
    #    return self.getattr(path)

    @showtb
    def readlink(self, path):
        return path

    @showtb
    def symlink(self, path, path1):
        return 0


    @showtb
    def mknod(self, path, mode, rdev):
        f = None
        try:
            f = self._fs.open(path, mode)
        finally:
            f.close()
        return 0

    @showtb
    def mkdir(self, path, mode):
        self._fs.mkdir(path, mode)
        return 0

    @showtb
    def rmdir(self, path):
        self._fs.removedir(path, True)
        return 0

    @showtb
    def unlink(self, path):
        try:
            self._fs.remove(path)
        except base.FSError:
            return 0
        return 0

    #symlink(target, name)

    @showtb
    def rename(self, old, new):
        self._fs.rename(old, new)
        return 0



    #@showtb
    #def read(self, path, size, offset):
    #    pass



def main(fs):
    usage="""
        FSFS: Exposes an FS
    """ + fuse.Fuse.fusage

    server = FSFUSE(fs, version="%prog 0.1",
                    usage=usage, dash_s_do='setsingle')

    #server.readdir('.', 0)

    server.parse(errex=1)
    server.main()


if __name__ == "__main__":

    import memoryfs
    import osfs
    mem_fs = memoryfs.MemoryFS()
    mem_fs.makedir("test")
    mem_fs.createfile("a.txt", "This is a test")
    mem_fs.createfile("test/b.txt", "This is in a sub-dir")


    #fs = osfs.OSFS('/home/will/fusetest/')
    #main(fs)

    main(mem_fs)

    # To run do ./fuserserver.py -d -f testfs
    # This will map a fs.memoryfs to testfs/ on the local filesystem under tests/fs
    # To unmouont, do fusermount -u testfs