operations.py 1.56 KB
Newer Older
1
import llfuse, errno
2
3
4
5
6
7
8
9
10
11
import sys

fse = sys.getfilesystemencoding()

def bytes2str(s):
    return s.decode(fse, 'surrogateescape')

def str2bytes(s):
    return s.encode(fse, 'surrogateescape')

12
class Operations(llfuse.Operations):
13
14
15
16
17
18
    def __init__(self, root):
        super(llfuse.Operations, self).__init__()
        self._cache = {llfuse.ROOT_INODE: root}

    def getattr(self, inode):
        file = self._get_file(inode)
19
        return file.get_attr()
20
21

    def lookup(self, parent_inode, name):
22
23
        for filename, file in self._get_file(parent_inode).get_files():
            if filename == bytes2str(name):
24
                self._fill_in_file(file)
25
                return file.get_attr()
26
27
28
29
30
31
32
33
        raise llfuse.FUSEError(errno.ENOENT)

    def opendir(self, inode):
        if self._get_file(inode) is None:
            raise llfuse.FUSEError(errno.ENOENT)
        return inode

    def readdir(self, fh, off):
34
        files = list(self._get_file(fh).get_files())
35
        for i in range(off, len(files)):
36
            yield (str2bytes(files[i][0]), files[i][1].get_attr(), i+1)
37
38

    def open(self, inode, flags):
Gerion Entrup's avatar
Gerion Entrup committed
39
        self._get_file(inode).open()
40
41
        return inode

Gerion Entrup's avatar
Gerion Entrup committed
42
43
44
    def flush(self, fh):
        self._get_file(fh).close()

45
46
47
48
49
50
51
52
53
54
55
    def read(self, fh, off, size):
        file = self._get_file(fh)
        file.seek(off)
        return file.read(size)

    def _get_file(self, inode):
        if inode in self._cache:
            return self._cache[inode]
        raise llfuse.FUSEError(errno.ENOENT)

    def _fill_in_file(self, file):
56
        self._cache[file.get_inode()] = file
57
58