summaryrefslogtreecommitdiff
path: root/hello-fusepy.py
blob: 41c30a56457d6a65df4e73ba308af8ab54167bc6 (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
import sys
import errno
import stat
from fuse import FUSE, Operations, FuseOSError

class HelloWorld(Operations):
    def __init__(self):
        self.files = {'/hello': 'hello world'}
        self.attr = {
            '/': {
                'st_mode': (stat.S_IFDIR | 0o755),  # Directory
                'st_nlink': 2
            },
            '/hello': {
                'st_mode': (stat.S_IFREG | 0o444),  # Regular file, read-only
                'st_nlink': 1,
                'st_size': len(self.files['/hello']),
            }
        }

    def getattr(self, path, fh=None):
        if path in self.attr:
            return self.attr[path]
        raise FuseOSError(errno.ENOENT)

    def readdir(self, path, fh):
        if path == '/':
            return ['.', '..'] + [file[1:] for file in self.files.keys()]
        else:
            raise FuseOSError(errno.ENOENT)

    def open(self, path, flags):
        if path not in self.files:
            raise FuseOSError(errno.ENOENT)
        return 0

    def read(self, path, size, offset, fh):
        if path in self.files:
            return self.files[path][offset:offset + size]
        raise FuseOSError(errno.ENOENT)

def main(mountpoint):
    FUSE(HelloWorld(), mountpoint, nothreads=True, foreground=True)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print(f'Usage: {sys.argv[0]} <mountpoint>')
        sys.exit(1)

    main(sys.argv[1])