Skip to content

Instantly share code, notes, and snippets.

@i-rinat
Created June 23, 2019 23:14
Show Gist options
  • Save i-rinat/18081f0f325560fab0a6668c0fc4e0df to your computer and use it in GitHub Desktop.
Save i-rinat/18081f0f325560fab0a6668c0fc4e0df to your computer and use it in GitHub Desktop.
import errno
import os
import sys
import fuse
fuse.fuse_python_api = (0, 2)
class Stat(fuse.Stat):
def __init__(self, sb):
for attr in ('st_atime', 'st_ctime', 'st_gid', 'st_mode', 'st_mtime',
'st_nlink', 'st_size', 'st_uid', 'st_dev', 'st_ino'):
setattr(self, attr, getattr(sb, attr))
class ZeroedFS(fuse.Fuse):
def __init__(self, *args, **kw):
fuse.Fuse.__init__(self, *args, **kw)
self.root = '/usr'
def process_options(self, opts, args):
print args
def _fullpath(self, path):
return os.path.join(self.root,
path[1:] if path.startswith("/") else path)
def access(self, path, mode):
if not os.access(self._fullpath(path), mode):
return -errno.EACCES
return 0
def chmod(self, path, mode):
return -errno.EACCES
def chown(self, path, uid, gid):
return -errno.EACCES
def getattr(self, path, fh=None):
sb = os.lstat(self._fullpath(path))
return Stat(sb)
def readdir(self, path, fh):
fp = self._fullpath(path)
if not os.path.isdir(fp):
return
yield fuse.Direntry('.')
yield fuse.Direntry('..')
for entry in os.listdir(fp):
yield fuse.Direntry(entry)
def readlink(self, path):
return os.readlink(self._fullpath(path))
def mknod(self, path, mode, dev):
return -errno.EACCES
def rmdir(self, path):
return -errno.EACCES
def mkdir(self, path):
return -errno.EACCES
# ~ def statfs(self, path):
# ~ # TODO: ?
# ~ pass
def unlink(self, path):
return -errno.EACCES
def symlink(self, name, target):
return -errno.EACCES
def rename(self, old, new):
return -errno.EACCES
def link(self, target, name):
return -errno.EACCES
def utimens(self, path, times=None):
return os.utime(self._fullpath(path), times)
def open(self, path, flags):
pass
def create(self, path, mode, fi=None):
return -errno.EACCES
def read(self, path, length, offset):
sb = os.lstat(self._fullpath(path))
data_left = max(0, sb.st_size - offset)
return bytes(bytearray(min(length, data_left)))
def write(self, path, buf, offset):
return -errno.EACCES
def truncate(self, path, length, fh=None):
return -errno.EACCES
def release(self, path, fh):
pass
def fsync(self, path, fdatasync, fh):
return
def main():
usage = """
ZeroedFS: Now without semi-colons!
""" + fuse.Fuse.fusage
server = ZeroedFS(version="%prog " + fuse.__version__,
usage=usage, dash_s_do='setsingle')
server.parse(errex=1)
server.main()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment