Start to create structure

parent d19ce371
#!/usr/bin/env python #!/usr/bin/env python3
from __future__ import print_function, absolute_import, division from __future__ import print_function, absolute_import, division
import logging import logging
import os import os
from errno import EACCES from errno import EACCES
from os.path import realpath from os.path import realpath
from threading import Lock from threading import Lock
from fusepy import FUSE, FuseOSError, Operations, LoggingMixIn from fusepy import FUSE, FuseOSError, Operations, LoggingMixIn
class Loopback(LoggingMixIn, Operations): class Loopback(LoggingMixIn, Operations):
def __init__(self, root): def __init__(self, root):
self.root = realpath(root) self.root = realpath(root)
self.rwlock = Lock() self.rwlock = Lock()
def __call__(self, op, path, *args):
print(op)
print(path)
if path=='/' and op!='init':
return super(Loopback, self).__call__('base_'+op, path, *args)
if path=='/' and op=='init':
return super(Loopback, self).__call__(op, path, *args)
psplit=path.split('/', 2)
pstart=False
if len(psplit)>1:
pstart=psplit[1]
if pstart and pstart=='CONTENT':
return super(Loopback, self).__call__(op, self.root + path.replace('/CONTENT', ''), *args)
if pstart in ['TAGS', 'CATEGORIES'] and path.replace('/'+pstart, '') == '':
return super(Loopback, self).__call__(op, self.root, *args)
raise FuseOSError(EACCES)
def base_destroy(self,*args):
return self.destroy(*args)
def access(self, path, mode):
if not os.access(path, mode):
raise FuseOSError(EACCES)
chmod = os.chmod
chown = os.chown
def create(self, path, mode):
return os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode)
#def __call__(self, op, path, *args): def flush(self, path, fh):
# return super(Loopback, self).__call__(op, self.root + path, *args) return os.fsync(fh)
def access(self, path, mode): def fsync(self, path, datasync, fh):
if not os.access(path, mode): if datasync != 0:
raise FuseOSError(EACCES) return os.fdatasync(fh)
else:
return os.fsync(fh)
chmod = os.chmod def getattr(self, path, fh=None):
chown = os.chown st = os.lstat(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 create(self, path, mode): def base_getattr(self, path, fh=None):
return os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, mode) return {'st_atime': 1692365924.6988926, 'st_ctime': 1692365924.6988926, 'st_gid': 1000, 'st_mode': 16877, 'st_mtime': 1692365924.6988926, 'st_nlink': 2, 'st_size': 0, 'st_uid': 1000}
def flush(self, path, fh): def base_opendir(self, path, fh=None):
return os.fsync(fh) return 0
def fsync(self, path, datasync, fh): def base_releasedir(self, path, fh=None):
if datasync != 0: return 0
return os.fdatasync(fh)
else:
return os.fsync(fh)
def getattr(self, path, fh=None): getxattr = None
st = os.lstat(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'))
getxattr = None def link(self, target, source):
return os.link(self.root + source, target)
def link(self, target, source): listxattr = None
return os.link(self.root + source, target) mkdir = os.mkdir
mknod = os.mknod
open = os.open
listxattr = None def read(self, path, size, offset, fh):
mkdir = os.mkdir with self.rwlock:
mknod = os.mknod os.lseek(fh, offset, 0)
open = os.open return os.read(fh, size)
def read(self, path, size, offset, fh): def readdir(self, path, fh):
with self.rwlock: return ['.', '..'] + os.listdir(path)
os.lseek(fh, offset, 0)
return os.read(fh, size)
def readdir(self, path, fh): def base_readdir(self, path, fh):
return ['.', '..'] + os.listdir(path) return ['.', '..', 'CONTENT', 'TAGS', 'CATEGORIES']
readlink = os.readlink readlink = os.readlink
def release(self, path, fh): def release(self, path, fh):
return os.close(fh) return os.close(fh)
def rename(self, old, new): def rename(self, old, new):
return os.rename(old, self.root + new) return os.rename(old, self.root + new)
rmdir = os.rmdir rmdir = os.rmdir
def statfs(self, path): def statfs(self, path):
stv = os.statvfs(path) stv = os.statvfs(path)
return dict((key, getattr(stv, key)) for key in ( return dict((key, getattr(stv, key)) for key in (
'f_bavail', 'f_bfree', 'f_blocks', 'f_bsize', 'f_favail', 'f_bavail', 'f_bfree', 'f_blocks', 'f_bsize', 'f_favail',
'f_ffree', 'f_files', 'f_flag', 'f_frsize', 'f_namemax')) 'f_ffree', 'f_files', 'f_flag', 'f_frsize', 'f_namemax'))
def symlink(self, target, source): def symlink(self, target, source):
return os.symlink(source, target) return os.symlink(source, target)
def truncate(self, path, length, fh=None): def truncate(self, path, length, fh=None):
with open(path, 'r+') as f: with open(path, 'r+') as f:
f.truncate(length) f.truncate(length)
unlink = os.unlink unlink = os.unlink
utimens = os.utime utimens = os.utime
def write(self, path, data, offset, fh): def write(self, path, data, offset, fh):
with self.rwlock: with self.rwlock:
os.lseek(fh, offset, 0) os.lseek(fh, offset, 0)
return os.write(fh, data) return os.write(fh, data)
if __name__ == '__main__': if __name__ == '__main__':
import argparse import argparse
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('root') parser.add_argument('root')
parser.add_argument('mount') parser.add_argument('mount')
args = parser.parse_args() args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
fuse = FUSE( fuse = FUSE(
Loopback(args.root), args.mount, foreground=True, allow_other=True) Loopback(args.root), args.mount, foreground=True, allow_other=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment