summaryrefslogtreecommitdiff
path: root/de_uvok/activitypub_fuse/status_fuse.py
blob: 00ee7479f9b971c46dd3397c408304828a2ba16b (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
from datetime import datetime as dtp
import errno
import stat
from threading import Lock
from fuse import (
    Operations,
    FuseOSError,
    LoggingMixIn,
    fuse_get_context,
)

from .status import Status


class StatusFileSystem(Operations, LoggingMixIn):
    def __init__(self):
        self._lock = Lock()

        with self._lock:
            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,
            }
        with self._lock:
            found = next((s for s in self.statuses if s.id == path[1:]), None)

        if found:
            published_dt = dtp.fromisoformat(found.published)
            pubunix = published_dt.timestamp()
            return {
                "st_mode": (stat.S_IFREG | 0o400),
                "st_size": len(found.content.encode("utf8")),
                "st_nlink": 1,
                "st_uid": uid,
                "st_gid": gid,
                "st_ctime": pubunix,
                "st_mtime": pubunix,
            }
        raise FuseOSError(errno.ENOENT)

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

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

    def add_statuses(self, statuses: list[Status]):
        with self._lock:
            self.statuses.extend(statuses)

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

    def read(self, path, size, offset, fh): # type: ignore
        with self._lock:
            found = next(s for s in self.statuses if s.id == path[1:])

        if found:
            return found.content.encode("utf8")
        raise FuseOSError(errno.ENOENT)