summaryrefslogtreecommitdiff
path: root/hello-fusepy.py
blob: 7fc86c137e54892ddd2168fcd4221abb72e7e494 (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
import sys
import errno
import stat
import logging
import requests
from fuse import (
    FUSE,
    Operations,
    FuseOSError,
    LoggingMixIn,
    fuse_exit,
    fuse_get_context,
)

api_url = "https://furry.engineer/users/uvok/outbox?min_id=0&page=true"


class Status(object):
    def __init__(self, id: str, content: str):
        self.id = id
        self.content = content


class HelloWorld(Operations, LoggingMixIn):
    def __init__(self):
        self.statuses: list[Status] = []
        self.fd = 0

    def getattr(self, path, fh=None):
        (uid, gid, _) = fuse_get_context()
        if path == "/":
            return {
                "st_mode": (stat.S_IFDIR | 0o700),  # Directory
                "st_nlink": 2,
                "st_uid": uid,
                "st_gid": gid,
            }
        found = next((s for s in self.statuses if "/" + s.id == path), None)
        if found:
            return {
                "st_mode": (stat.S_IFREG | 0o400),
                "st_size": len(found.content.encode("utf8")),
                "st_uid": uid,
                "st_gid": gid,
            }
        raise FuseOSError(errno.ENOENT)

    def load_statuses(self):
        res = requests.get(api_url)
        res.raise_for_status()
        stats = res.json()
        logging.debug(f"Status: ${stats['id']}")
        self.statuses = [
            Status(s["object"]["id"].split("/")[-1], s["object"]["content"])
            for s in stats["orderedItems"]
        ]
        pass

    def list_dir(self) -> list[str]:
        return [s.id for s in self.statuses]

    def readdir(self, path, fh):
        dir_entries = []
        if path != "/":
            raise FuseOSError(errno.ENOENT)
        dir_entries = [".", ".."]
        if not self.statuses:
            self.load_statuses()
        dir_entries += self.list_dir()
        return dir_entries

    def open(self, path, flags):
        self.fd += 1
        return self.fd

    def read(self, path, size, offset, fh):
        found = next(s for s in self.statuses if "/" + s.id == path)
        if found:
            return found.content.encode("utf8")
        raise FuseOSError(errno.ENOENT)


def main(mountpoint):
    try:
        myfuse = FUSE(HelloWorld(), mountpoint, nothreads=True, foreground=True)
    except:
        fuse_exit()
        raise


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

    main(sys.argv[1])