aboutsummaryrefslogtreecommitdiffstats
path: root/FuseArchive/FileSystem.py
blob: c8752305158b31044019e4d9fe5a9182530d0228 (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
import fuse, os, logging, errno
from ChunkFile import ChunkFile

if not hasattr(fuse, '__version__'):
    raise RuntimeError, \
        "your fuse-py doesn't know of fuse.__version__, probably it's too old."

fuse.fuse_python_api = (0, 2)
fuse.feature_assert('stateful_files', 'has_init')

class FileSystem(fuse.Fuse):

    def __init__(self, *args, **kw):

        fuse.Fuse.__init__(self, *args, **kw)
        self.root = None

    # Fix getattr and fgetattr to?
    def getattr(self, path):
        treefile = "./tree" + path

        if os.path.isfile( treefile ):
            logging.debug( "Delegating getattr to File for " + path )

            # Check in the dirty cache first (to handle lseek and the
            # relatively broken implmentation in fuse/python)
            f = ChunkFile.get_dirty_file( path )
            if f:
                logging.info( "WORKAROUND: lseek appears to do a gettattr if whence is SEEK_END, using dirty cache object" )
                stats = f.fgetattr()
                # no release, it's still being used
            else:
                f = ChunkFile( path, os.O_RDONLY, 0 )
                stats = f.fgetattr()
                f.release( 0 )
        else:
            logging.debug( "Using os.lstat to get stats for %s" % path )
            stats = os.lstat( treefile )

        return stats

    def readlink(self, path):
        return os.readlink("./tree" + path)

    def readdir(self, path, offset):
        for e in os.listdir("./tree" + path):
            yield fuse.Direntry(e)

    def unlink(self, path):
        # Check if we need to free our chunks
        treefile = "./tree" + path
        if os.path.isfile( treefile ):
            if not os.path.islink( treefile ):
                # it's a normal file, will the link cound go to zero here?
                stats = os.stat( treefile )
                if stats.st_nlink == 1:
                    logging.debug( "Going to unlink file " + treefile +
                        "and this is the last link, releasing chunks" )
                    # Ask our file to release it's chunks since we are the
                    # last link
                    f = ChunkFile( path, os.O_RDWR, 0 )
                    f.pre_unlink()
                    f.release( 0 )

        os.unlink("./tree" + path)

    def rmdir(self, path):
        os.rmdir("./tree" + path)

    def symlink(self, path, path1):
        os.symlink(path, "./tree" + path1)

    def rename(self, path, path1):
        os.rename("./tree" + path, "./tree" + path1)

    def link(self, path, path1):
        os.link("./tree" + path, "./tree" + path1)

    def chmod(self, path, mode):
        os.chmod("./tree" + path, mode)

    def chown(self, path, user, group):
        os.chown("./tree" + path, user, group)

    def truncate(self, path, len):
        # Truncate using the ftruncate on the file
        logging.debug( "Using FuseArchiveFile to truncate %s to %d" % ( path, len) )
        f = ChunkFile( path, os.O_RDWR, 0 )
        f.ftruncate(len)
        f.release( 0 )

    def mknod(self, path, mode, dev):
        os.mknod("./tree" + path, mode, dev)

    def mkdir(self, path, mode):
        os.mkdir("./tree" + path, mode)

    def utime(self, path, times):
        os.utime("./tree" + path, times)

#######
# Following is the comment about utimens, however even uncommenting it
# doesn't currently work because there is an error in the fuse.py handling
# the message:
#
# Traceback (most recent call last):
#   File "/usr/lib/python2.5/site-packages/fuse.py", line 361, in __call__
#     return apply(self.func, args, kw)
#   File "/usr/lib/python2.5/site-packages/fuse.py", line 782, in wrap
#     ts_acc = Timespec(tv_sec = acc_sec, tv_nsec = acc_nsec)
# TypeError: __init__() takes exactly 2 non-keyword arguments (1 given)
#
# Which if you use rsync makes it think the copy failed because it uses
# untimens to set the time info on symlinks (search google for utimens
# rsync symlink)
#
# I haven't been able to find a workaround for this yet, though there is
# some information at:
#
# http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=455194
#
# which suggests it should be fixed even though it doesn't appear to be for
# me in both lenny and squeeze
#
# While I don't like it you can also modify the 2 Timespec() calls to have
# None as the first argument which will fix the typeerrors above, but
# you'll need to remember to do it again later if you upgrade the package.
#
#######
#    The following utimens method would do the same as the above utime method.
#    We can't make it better though as the Python stdlib doesn't know of
#    subsecond preciseness in acces/modify times.
#  
    def utimens(self, path, ts_acc, ts_mod):
        logging.debug( "Ignoring utimens, hope you like it rsync!" )
#      os.utime("." + path, (ts_acc.tv_sec, ts_mod.tv_sec))

    def access(self, path, mode):
        if not os.access("./tree" + path, mode):
            return -errno.EACCES

#    This is how we could add stub extended attribute handlers...
#    (We can't have ones which aptly delegate requests to the underlying fs
#    because Python lacks a standard xattr interface.)
#
#    def getxattr(self, path, name, size):
#        val = name.swapcase() + '@' + path
#        if size == 0:
#            # We are asked for size of the value.
#            return len(val)
#        return val
#
#    def listxattr(self, path, size):
#        # We use the "user" namespace to please XFS utils
#        aa = ["user." + a for a in ("foo", "bar")]
#        if size == 0:
#            # We are asked for size of the attr list, ie. joint size of attrs
#            # plus null separators.
#            return len("".join(aa)) + len(aa)
#        return aa

    def statfs(self):
        """
        Should return an object with statvfs attributes (f_bsize, f_frsize...).
        Eg., the return value of os.statvfs() is such a thing (since py 2.2).
        If you are not reusing an existing statvfs object, start with
        fuse.StatVFS(), and define the attributes.

        To provide usable information (ie., you want sensible df(1)
        output, you are suggested to specify the following attributes:

            - f_bsize - preferred size of file blocks, in bytes
            - f_frsize - fundamental size of file blcoks, in bytes
                [if you have no idea, use the same as blocksize]
            - f_blocks - total number of blocks in the filesystem
            - f_bfree - number of free blocks
            - f_files - total number of file inodes
            - f_ffree - nunber of free file inodes
        """

        return os.statvfs(".")

    def fsinit(self):
        os.chdir(self.root)

    def main(self, *a, **kw):

        self.file_class = ChunkFile

        # This is where fragments go
        if not os.path.exists( 'storage' ):
            os.mkdir( 'storage' )

        # This is where the real files exist
        if not os.path.exists( 'tree' ):
            os.mkdir( 'tree' )

        return fuse.Fuse.main(self, *a, **kw)