Created
April 14, 2020 10:22
-
-
Save pgp/37dcb569ca4754d6c7d400af5af33040 to your computer and use it in GitHub Desktop.
Delayed FUSE passthrough filesystem in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import with_statement | |
import os | |
import sys | |
import errno | |
import time | |
from threading import Timer | |
from fuse import FUSE, FuseOSError, Operations | |
""" | |
Delayed filesystem passthrough | |
Usage example: | |
pip install fusepy | |
mkdir /dev/shm/{source,dest} | |
python delayed_fuse_passthrough.py /dev/shm/{source,dest} | |
Web sources: | |
https://thepythoncorner.com/dev/writing-a-fuse-filesystem-in-python/ | |
https://stackoverflow.com/questions/5205995/python-decorator-that-call-a-function-with-delay | |
""" | |
DEFAULT_SECONDS = 1 | |
class Passthrough(Operations): | |
def __init__(self, root): | |
self.root = root | |
# Helpers | |
# ======= | |
def sleep_before_op(self,seconds=DEFAULT_SECONDS): | |
if seconds != 0: | |
time.sleep(seconds) | |
def _full_path(self, partial): | |
self.sleep_before_op() | |
if partial.startswith("/"): | |
partial = partial[1:] | |
path = os.path.join(self.root, partial) | |
return path | |
# Filesystem methods | |
# ================== | |
def access(self, path, mode): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
if not os.access(full_path, mode): | |
raise FuseOSError(errno.EACCES) | |
def chmod(self, path, mode): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
return os.chmod(full_path, mode) | |
def chown(self, path, uid, gid): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
return os.chown(full_path, uid, gid) | |
def getattr(self, path, fh=None): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
st = os.lstat(full_path) | |
return dict((key, getattr(st, key)) for key in ('st_atime', 'st_ctime', | |
'st_gid', 'st_mode', 'st_mtime', 'st_nlink', 'st_size', 'st_uid')) | |
def readdir(self, path, fh): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
dirents = ['.', '..'] | |
if os.path.isdir(full_path): | |
dirents.extend(os.listdir(full_path)) | |
for r in dirents: | |
yield r | |
def readlink(self, path): | |
self.sleep_before_op() | |
pathname = os.readlink(self._full_path(path)) | |
if pathname.startswith("/"): | |
# Path name is absolute, sanitize it. | |
return os.path.relpath(pathname, self.root) | |
else: | |
return pathname | |
def mknod(self, path, mode, dev): | |
self.sleep_before_op() | |
return os.mknod(self._full_path(path), mode, dev) | |
def rmdir(self, path): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
return os.rmdir(full_path) | |
def mkdir(self, path, mode): | |
self.sleep_before_op() | |
return os.mkdir(self._full_path(path), mode) | |
def statfs(self, path): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
stv = os.statvfs(full_path) | |
return dict((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')) | |
def unlink(self, path): | |
self.sleep_before_op() | |
return os.unlink(self._full_path(path)) | |
def symlink(self, name, target): | |
self.sleep_before_op() | |
return os.symlink(name, self._full_path(target)) | |
def rename(self, old, new): | |
self.sleep_before_op() | |
return os.rename(self._full_path(old), self._full_path(new)) | |
def link(self, target, name): | |
self.sleep_before_op() | |
return os.link(self._full_path(target), self._full_path(name)) | |
def utimens(self, path, times=None): | |
self.sleep_before_op() | |
return os.utime(self._full_path(path), times) | |
# File methods | |
# ============ | |
def open(self, path, flags): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
return os.open(full_path, flags) | |
def create(self, path, mode, fi=None): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
return os.open(full_path, os.O_WRONLY | os.O_CREAT, mode) | |
def read(self, path, length, offset, fh): | |
self.sleep_before_op() | |
os.lseek(fh, offset, os.SEEK_SET) | |
return os.read(fh, length) | |
def write(self, path, buf, offset, fh): | |
self.sleep_before_op() | |
os.lseek(fh, offset, os.SEEK_SET) | |
return os.write(fh, buf) | |
def truncate(self, path, length, fh=None): | |
self.sleep_before_op() | |
full_path = self._full_path(path) | |
with open(full_path, 'r+') as f: | |
f.truncate(length) | |
def flush(self, path, fh): | |
self.sleep_before_op() | |
return os.fsync(fh) | |
def release(self, path, fh): | |
self.sleep_before_op() | |
return os.close(fh) | |
def fsync(self, path, fdatasync, fh): | |
self.sleep_before_op() | |
return self.flush(path, fh) | |
def main(mountpoint, root): | |
FUSE(Passthrough(root), mountpoint, nothreads=True, foreground=True) | |
if __name__ == '__main__': | |
main(sys.argv[2], sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment