model.py 1.91 KB
Newer Older
Gerion Entrup's avatar
Gerion Entrup committed
1
2
3
4
5
6
import llfuse
import time
import os
import stat

class FuseIO():
7
    def __init__(self, mode):
Gerion Entrup's avatar
Gerion Entrup committed
8
9
        self._attr = llfuse.EntryAttributes()
        self._attr.st_mode = mode
10
        self._attr.st_nlink = 1
Gerion Entrup's avatar
Gerion Entrup committed
11
        self._attr.st_size = 0
12
        self._attr.st_ino = id(self)
13

14
        self._attr.generation = 0
Gerion Entrup's avatar
Gerion Entrup committed
15
16
        self._attr.st_rdev = 0

17
18
        self._attr.attr_timeout = 300
        self._attr.entry_timeout = 300
Gerion Entrup's avatar
Gerion Entrup committed
19
20
        self._attr.st_atime = int(time.time())
        self._attr.st_mtime = int(time.time())
21
        self._attr.st_ctime = int(time.time())
Gerion Entrup's avatar
Gerion Entrup committed
22
23

        self._attr.st_blksize = 512
24
25
26
27
        self._attr.st_blocks = 1

        self._attr.st_gid = os.getgid()
        self._attr.st_uid = os.getuid()
Gerion Entrup's avatar
Gerion Entrup committed
28

29
30
    def add_nlink(self):
        self._attr.st_nlink += 1
Gerion Entrup's avatar
Gerion Entrup committed
31

32
33
    def get_inode(self):
        return self._attr.st_ino
34

35
    def get_attr(self):
Gerion Entrup's avatar
Gerion Entrup committed
36
37
        return self._attr

38
39
40
41
42
43
44
45
46
47
class FuseFile(FuseIO):
    def __init__(self, mode):
        super().__init__(stat.S_IFREG | mode)
        self._content = b''
        self._pos = 0

    def set_content(self, content):
        self._content = content
        self._attr.st_size = len(content)

Gerion Entrup's avatar
Gerion Entrup committed
48
49
50
51
52
53
54
55
    def open(self):
        # Could be used for more complex Files
        pass

    def close(self):
        # Could be used for more complex Files
        pass

56
57
    def seek(self, offset):
        self._pos = offset
Gerion Entrup's avatar
Gerion Entrup committed
58

59
60
    def read(self, size):
        return self._content[self._pos:self._pos + size]
Gerion Entrup's avatar
Gerion Entrup committed
61
62

class FuseDir(FuseIO):
63

64
    def __init__(self, mode, root=False):
Gerion Entrup's avatar
Gerion Entrup committed
65
        super().__init__(stat.S_IFDIR | mode)
66
67
68
69
70
        self._files = {}
        self._attr.st_size = 4096
        if root:
            self._attr.st_ino = llfuse.ROOT_INODE
            self._attr.st_nlink = 2
71
72

    def get_files(self):
73
        return self._files.items()
74

75
76
77
78
79
    def add_file(self, filename, file):
        file.add_nlink()
        self._files[filename] = file
        if isinstance(file, FuseDir):
            self._attr.st_nlink += 1
80