forked from fusepy/fusepy
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathloopback.py
More file actions
executable file
·202 lines (165 loc) · 5.41 KB
/
loopback.py
File metadata and controls
executable file
·202 lines (165 loc) · 5.41 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python
import argparse
import errno
import logging
import os
import stat
import threading
import time
from typing import Any, Optional
import mfusepy as fuse
def with_root_path(func):
def wrapper(self, path, *args, **kwargs):
if path is not None:
path = self.root + path
return func(self, path, *args, **kwargs)
return wrapper
def static_with_root_path(func):
def wrapper(self, path, *args, **kwargs):
if path is not None:
path = self.root + path
return func(path, *args, **kwargs)
return wrapper
class Loopback(fuse.Operations):
use_ns = True
def __init__(self, root):
self.root = os.path.realpath(root)
self.rwlock = threading.Lock()
@with_root_path
@fuse.overrides(fuse.Operations)
def access(self, path: str, amode: int) -> int:
if not os.access(path, amode):
raise fuse.FuseOSError(errno.EACCES)
return 0
chmod = static_with_root_path(os.chmod)
chown = static_with_root_path(os.chown)
@with_root_path
@fuse.overrides(fuse.Operations)
def create(self, path: str, mode: int, fi=None) -> int:
return os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode)
@with_root_path
@fuse.overrides(fuse.Operations)
def flush(self, path: str, fh: int) -> int:
os.fsync(fh)
return 0
@with_root_path
@fuse.overrides(fuse.Operations)
def fsync(self, path: str, datasync: int, fh: int) -> int:
if datasync != 0:
os.fdatasync(fh)
else:
os.fsync(fh)
return 0
@fuse.log_callback
@with_root_path
@fuse.overrides(fuse.Operations)
def getattr(self, path: str, fh: Optional[int] = None) -> dict[str, Any]:
if fh is not None:
st = os.fstat(fh)
elif path is not None:
st = os.lstat(path)
else:
raise fuse.FuseOSError(errno.ENOENT)
return {
key.removesuffix('_ns'): getattr(st, key)
for key in (
'st_atime_ns',
'st_ctime_ns',
'st_gid',
'st_mode',
'st_mtime_ns',
'st_nlink',
'st_size',
'st_uid',
)
}
@with_root_path
@fuse.overrides(fuse.Operations)
def link(self, target: str, source: str):
return os.link(self.root + source, target)
mkdir = static_with_root_path(os.mkdir)
open = static_with_root_path(os.open)
@with_root_path
@fuse.overrides(fuse.Operations)
def mknod(self, path: str, mode: int, dev: int):
# OpenBSD calls mknod + open instead of create.
if stat.S_ISREG(mode):
# OpenBSD does not allow using os.mknod to create regular files.
fd = os.open(path, os.O_CREAT | os.O_WRONLY | os.O_EXCL, mode & 0o7777)
os.close(fd)
else:
os.mknod(path, mode, dev)
return 0
@with_root_path
@fuse.overrides(fuse.Operations)
def read(self, path: str, size: int, offset: int, fh: int) -> bytes:
with self.rwlock:
os.lseek(fh, offset, 0)
return os.read(fh, size)
@with_root_path
@fuse.overrides(fuse.Operations)
def readdir(self, path: str, fh: int) -> fuse.ReadDirResult:
return ['.', '..', *os.listdir(path)]
readlink = static_with_root_path(os.readlink)
@with_root_path
@fuse.overrides(fuse.Operations)
def release(self, path: str, fh: int) -> int:
os.close(fh)
return 0
@with_root_path
@fuse.overrides(fuse.Operations)
def rename(self, old: str, new: str):
return os.rename(old, self.root + new)
rmdir = static_with_root_path(os.rmdir)
@with_root_path
@fuse.overrides(fuse.Operations)
def statfs(self, path: str) -> dict[str, int]:
stv = os.statvfs(path)
return {
key: getattr(stv, key)
for key in (
'f_bavail',
'f_bfree',
'f_blocks',
'f_bsize',
'f_favail',
'f_ffree',
'f_files',
'f_flag',
'f_frsize',
'f_namemax',
)
}
@with_root_path
@fuse.overrides(fuse.Operations)
def symlink(self, target: str, source: str):
return os.symlink(source, target)
@with_root_path
@fuse.overrides(fuse.Operations)
def truncate(self, path: str, length: int, fh: Optional[int] = None) -> int:
with open(path, 'rb+') as f:
f.truncate(length)
return 0
unlink = static_with_root_path(os.unlink)
@with_root_path
@fuse.overrides(fuse.Operations)
def utimens(self, path: str, times: Optional[tuple[int, int]] = None) -> int:
now = int(time.time() * 1e9)
os.utime(path, ns=times or (now, now))
return 0
@fuse.log_callback
@with_root_path
@fuse.overrides(fuse.Operations)
def write(self, path: str, data, offset: int, fh: int) -> int:
with self.rwlock:
os.lseek(fh, offset, 0)
return os.write(fh, data)
def cli(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('root')
parser.add_argument('mount')
args = parser.parse_args(args)
logging.basicConfig(level=logging.DEBUG)
fuse.FUSE(Loopback(args.root), args.mount, foreground=True)
if __name__ == '__main__':
cli()