forked from fusepy/fusepy
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontext.py
More file actions
executable file
·66 lines (51 loc) · 1.91 KB
/
context.py
File metadata and controls
executable file
·66 lines (51 loc) · 1.91 KB
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
#!/usr/bin/env python
import argparse
import errno
import logging
import stat
import time
from typing import Any, Optional
import mfusepy as fuse
class Context(fuse.Operations):
'Example filesystem to demonstrate fuse_get_context()'
@fuse.overrides(fuse.Operations)
def getattr(self, path: str, fh: Optional[int] = None) -> dict[str, Any]:
uid, gid, pid = fuse.fuse_get_context()
if path == '/':
st: dict[str, Any] = {'st_mode': (stat.S_IFDIR | 0o755), 'st_nlink': 2}
elif path == '/uid':
size = len(f'{uid}\n')
st = {'st_mode': (stat.S_IFREG | 0o444), 'st_size': size}
elif path == '/gid':
size = len(f'{gid}\n')
st = {'st_mode': (stat.S_IFREG | 0o444), 'st_size': size}
elif path == '/pid':
size = len(f'{pid}\n')
st = {'st_mode': (stat.S_IFREG | 0o444), 'st_size': size}
else:
raise fuse.FuseOSError(errno.ENOENT)
st['st_ctime'] = st['st_mtime'] = st['st_atime'] = time.time()
return st
@fuse.overrides(fuse.Operations)
def read(self, path: str, size: int, offset: int, fh: int) -> bytes:
uid, gid, pid = fuse.fuse_get_context()
def encoded(x):
return (f'{x}\n').encode()
if path == '/uid':
return encoded(uid)
if path == '/gid':
return encoded(gid)
if path == '/pid':
return encoded(pid)
raise RuntimeError(f'unexpected path: {path!r}')
@fuse.overrides(fuse.Operations)
def readdir(self, path: str, fh: int) -> fuse.ReadDirResult:
return ['.', '..', 'uid', 'gid', 'pid']
def cli(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('mount')
args = parser.parse_args(args)
logging.basicConfig(level=logging.DEBUG)
fuse.FUSE(Context(), args.mount, foreground=True, ro=True)
if __name__ == '__main__':
cli()