fusetoys

various hacky fuse filesystem utilities
git clone https://a3nm.net/git/fusetoys/
Log | Files | Refs | README

commit 62cef99031565e1f73d071bc65b46c47ed613eaf
parent 9e56f4e90ec3b7b6c47f4b0f602562dad939723e
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 20 Aug 2023 09:06:49 -0700

nolock

Diffstat:
loopfs_nolock.py | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 112 insertions(+), 0 deletions(-)

diff --git a/loopfs_nolock.py b/loopfs_nolock.py @@ -0,0 +1,112 @@ +#!/usr/bin/python + +"""Dummy loopback FUSE filesystem""" +# difference with loopfs.py: do not try to have a RW cache, just pass open and +# read and write operations directly + +import fuse +import errno +import os +import sys +import threading + +fuse.fuse_python_api = (0, 2) + +class LoopFS(fuse.Fuse): + def __init__(self, *args, **kw): + fuse.Fuse.__init__(self, *args, **kw) + + def fsinit(self): + self.source = self.cmdline[1][0] + + def sourcePath(self, path): + return os.path.join(self.source, path[1:]) + + def access(self, path, mode): + if not os.access(self.sourcePath(path), mode): + return -errno.EACCES + + def chmod(self, path, mode): + return os.chmod(self.sourcePath(path), mode) + + def chown(self, path, mode): + return os.chown(self.sourcePath(path), mode) + + def create(self, path, flags, mode): + return os.open(self.sourcePath(path), os.O_WRONLY | os.O_CREAT, mode) + + def getattr(self, path): + return os.lstat(self.sourcePath(path)) + + def link(self, target, source): + os.link(self.sourcePath(source), self.sourcePath(target)) + + def mkdir(self, path, mode): + return os.mkdir(self.sourcePath(path), mode) + + def mknod(self, path, mode, rdev): + return os.mknod(self.sourcePath(path), mode, rdev) + + def open(self, path, flags): + return os.open(self.sourcePath(path), flags) + + def read(self, path, size, offset): + os.lseek(fh, offset, 0) + return os.read(fh, size) + + def readdir(self, path, offset): + path = self.sourcePath(path) + myIno = os.lstat(path).st_ino + print ("will yield dot") + yield fuse.Direntry('.', ino=myIno) + print ("will yield parent") + try: + parentIno = os.lstat(os.path.join(path, "..")).st_ino + print ("yielded parent") + except OSError as e: + parentIno = myIno # root + print ("faked parent") + yield fuse.Direntry('..', ino=parentIno) + print ("will yield children") + for name in os.listdir(path): + print ("yield %s" % os.path.join(path, name)) + ino = os.lstat(os.path.join(path, name)).st_ino + yield fuse.Direntry(name, ino=ino) + print ("alldone") + + def readlink(self, path): + return os.readlink(self.sourcePath(path)) + + def rename(self, old, new): + return os.rename(self.sourcePath(old), self.sourcePath(new)) + + def rmdir(self, path): + return os.rmdir(self.sourcePath(path)) + + def statfs(self): + return os.statvfs(self.sourceRoot) + + def symlink(self, target, source): + return os.symlink(self.sourcePath(source), self.sourcePath(target)) + + def truncate(self, path, length, fh=None): + with open(self.sourcePath(path), 'r+') as f: + return f.truncate(length) + + def unlink(self, path): + return os.unlink(self.sourcePath(path)) + + def utimens(self, path, ts_acc, ts_mod): + times = (ts_acc.tv_sec, ts_mod.tv_sec) + return os.utime(self.sourcePath(path), times) + + def write(self, path, data, offset): + os.lseek(fh, offset, 0) + return os.write(fh, data) + + +if __name__ == "__main__": + loopfs = LoopFS() + fuse_opts = loopfs.parse(['-o', 'fsname=loopfs'] + sys.argv[1:]) + loopfs.main() +