Last active
December 20, 2015 11:58
-
-
Save ibizaman/e3609e01abd48e7bd605 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
""" | |
Taken from http://www.stavros.io/posts/python-fuse-filesystem/ | |
""" | |
from __future__ import with_statement | |
import os | |
import sys | |
import errno | |
from fuse import FUSE, FuseOSError, Operations | |
def print_args(func): | |
""" Decorator to print function call details | |
Show parameters names and effective values. Taken from | |
http://stackoverflow.com/a/25206079/1013628 | |
""" | |
def wrapper(*func_args, **func_kwargs): | |
arg_names = func.__code__.co_varnames[:func.__code__.co_argcount] | |
args = func_args[:len(arg_names)] | |
defaults = func.__defaults__ or () | |
args = args + defaults[len(defaults) - (func.__code__.co_argcount - len(args)):] | |
params = list(zip(arg_names, args)) | |
args = func_args[len(arg_names):] | |
if args: params.append(('args', args)) | |
if func_kwargs: params.append(('kwargs', func_kwargs)) | |
print(func.__name__ + ' (' + ', '.join('%s = %r' % p for p in params) + ' )') | |
return func(*func_args, **func_kwargs) | |
return wrapper | |
class Passthrough(Operations): | |
def __init__(self, root): | |
self.root = root | |
# Helpers | |
# ======= | |
def _full_path(self, partial): | |
if partial.startswith("/"): | |
partial = partial[1:] | |
path = os.path.join(self.root, partial) | |
return path | |
# Filesystem methods | |
# ================== | |
@print_args | |
def access(self, path, mode): | |
full_path = self._full_path(path) | |
if not os.access(full_path, mode): | |
raise FuseOSError(errno.EACCES) | |
@print_args | |
def chmod(self, path, mode): | |
full_path = self._full_path(path) | |
return os.chmod(full_path, mode) | |
@print_args | |
def chown(self, path, uid, gid): | |
full_path = self._full_path(path) | |
return os.chown(full_path, uid, gid) | |
@print_args | |
def getattr(self, path, fh=None): | |
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')) | |
@print_args | |
def readdir(self, path, fh): | |
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 | |
@print_args | |
def readlink(self, path): | |
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 | |
@print_args | |
def mknod(self, path, mode, dev): | |
return os.mknod(self._full_path(path), mode, dev) | |
@print_args | |
def rmdir(self, path): | |
full_path = self._full_path(path) | |
return os.rmdir(full_path) | |
@print_args | |
def mkdir(self, path, mode): | |
return os.mkdir(self._full_path(path), mode) | |
@print_args | |
def statfs(self, path): | |
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')) | |
@print_args | |
def unlink(self, path): | |
return os.unlink(self._full_path(path)) | |
@print_args | |
def symlink(self, name, target): | |
return os.symlink(name, self._full_path(target)) | |
@print_args | |
def rename(self, old, new): | |
return os.rename(self._full_path(old), self._full_path(new)) | |
@print_args | |
def link(self, target, name): | |
return os.link(self._full_path(target), self._full_path(name)) | |
@print_args | |
def utimens(self, path, times=None): | |
return os.utime(self._full_path(path), times) | |
# File methods | |
# ============ | |
@print_args | |
def open(self, path, flags): | |
full_path = self._full_path(path) | |
return os.open(full_path, flags) | |
@print_args | |
def create(self, path, mode, fi=None): | |
full_path = self._full_path(path) | |
return os.open(full_path, os.O_WRONLY | os.O_CREAT, mode) | |
@print_args | |
def read(self, path, length, offset, fh): | |
os.lseek(fh, offset, os.SEEK_SET) | |
return os.read(fh, length) | |
@print_args | |
def write(self, path, buf, offset, fh): | |
os.lseek(fh, offset, os.SEEK_SET) | |
return os.write(fh, buf) | |
@print_args | |
def truncate(self, path, length, fh=None): | |
full_path = self._full_path(path) | |
with open(full_path, 'r+') as f: | |
f.truncate(length) | |
@print_args | |
def flush(self, path, fh): | |
return os.fsync(fh) | |
@print_args | |
def release(self, path, fh): | |
return os.close(fh) | |
@print_args | |
def fsync(self, path, fdatasync, fh): | |
return self.flush(path, fh) | |
@print_args | |
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