1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ait/os/bin/mirrorfuse/mirrorfuse.py Tue Nov 12 11:31:34 2013 +0100
1.3 @@ -0,0 +1,270 @@
1.4 +#!/bin/env python
1.5 +# -*- coding: utf-8 -*-
1.6 +
1.7 +# ------------------------------------------------------------
1.8 +# mirrorfuse
1.9 +#
1.10 +# create a mirror filesystem folder as a new filesystem to mount
1.11 +#
1.12 +# This is directly based on xmp.py of the
1.13 +# dev-python/fuse-python example
1.14 +#
1.15 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
1.16 +#
1.17 +# Copyright (C) 2013 AIT Austrian Institute of Technology
1.18 +# AIT Austrian Institute of Technology GmbH
1.19 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
1.20 +# http://www.ait.ac.at
1.21 +#
1.22 +# This program is free software; you can redistribute it and/or
1.23 +# modify it under the terms of the GNU General Public License
1.24 +# as published by the Free Software Foundation version 2.
1.25 +#
1.26 +# This program is distributed in the hope that it will be useful,
1.27 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1.28 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.29 +# GNU General Public License for more details.
1.30 +#
1.31 +# You should have received a copy of the GNU General Public License
1.32 +# along with this program; if not, write to the Free Software
1.33 +# Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.34 +# Boston, MA 02110-1301, USA.
1.35 +# ------------------------------------------------------------
1.36 +
1.37 +
1.38 +# ------------------------------------------------------------
1.39 +# imports
1.40 +
1.41 +import errno
1.42 +import fcntl
1.43 +import fuse
1.44 +import os
1.45 +import sys
1.46 +
1.47 +from fuse import Fuse
1.48 +
1.49 +# ------------------------------------------------------------
1.50 +# const
1.51 +
1.52 +
1.53 +__version__ = "0.1"
1.54 +
1.55 +
1.56 +# ------------------------------------------------------------
1.57 +# code
1.58 +
1.59 +
1.60 +class MirrorFuse(Fuse):
1.61 +
1.62 + """This is the Mirror FUSE in python.
1.63 +
1.64 + This is to represnt a file hierarchy elsewhere (and intercept each file system call)
1.65 + """
1.66 +
1.67 + def __init__(self, *args, **kw):
1.68 + fuse.fuse_python_api = (0, 2)
1.69 + super(MirrorFuse, self).__init__(*args, **kw)
1.70 + self.root = '/'
1.71 + self.os_server_url = ''
1.72 +
1.73 + def getattr(self, path):
1.74 + return os.lstat("." + path)
1.75 +
1.76 +
1.77 + #
1.78 + # links are not allowed for a mirrored FS
1.79 + #
1.80 + def readlink(self, path):
1.81 + eturn -errno.EACCES
1.82 +
1.83 +
1.84 + def readdir(self, path, offset):
1.85 + for e in os.listdir("." + path):
1.86 + yield fuse.Direntry(e)
1.87 +
1.88 +
1.89 + def unlink(self, path):
1.90 + sys.stdout.write("===\nInsert Hook here! Deleting file %s\n===\n" % path)
1.91 + os.unlink("." + path)
1.92 +
1.93 +
1.94 + def rmdir(self, path):
1.95 + sys.stdout.write("===\nInsert Hook here! Deleting folder %s\n===\n" % path)
1.96 + os.rmdir("." + path)
1.97 +
1.98 +
1.99 + #
1.100 + # links are not allowed for a mirrored FS
1.101 + #
1.102 + def symlink(self, path, path1):
1.103 + eturn -errno.EACCES
1.104 +
1.105 +
1.106 + def rename(self, path, path1):
1.107 + sys.stdout.write("===\nInsert Hook here! Moving file %s --> %s\n===\n" % path % path1)
1.108 + os.rename("." + path, "." + path1)
1.109 +
1.110 +
1.111 + #
1.112 + # links are not allowed for a mirrored FS
1.113 + #
1.114 + def link(self, path, path1):
1.115 + return -errno.EACCES
1.116 +
1.117 +
1.118 + #
1.119 + # changing access mode is not allowed in mirrored FS
1.120 + #
1.121 + def chmod(self, path, mode):
1.122 + return -errno.EACCES
1.123 +
1.124 +
1.125 + #
1.126 + # changing ownership is not allowed in mirrored FS
1.127 + #
1.128 + def chown(self, path, user, group):
1.129 + return -errno.EACCES
1.130 +
1.131 +
1.132 + def truncate(self, path, len):
1.133 + f = open("." + path, "a")
1.134 + f.truncate(len)
1.135 + f.close()
1.136 +
1.137 +
1.138 + def mknod(self, path, mode, dev):
1.139 + sys.stdout.write("===\nInsert Hook here! Creating file %s\n===\n" % path)
1.140 + os.mknod("." + path, mode, dev)
1.141 +
1.142 +
1.143 + def mkdir(self, path, mode):
1.144 + sys.stdout.write("===\nInsert Hook here! Creating folder %s\n===\n" % path)
1.145 + os.mkdir("." + path, mode)
1.146 +
1.147 +
1.148 + def utime(self, path, times):
1.149 + os.utime("." + path, times)
1.150 +
1.151 +
1.152 + def access(self, path, mode):
1.153 + if not os.access("." + path, mode):
1.154 + return -errno.EACCES
1.155 +
1.156 +
1.157 + def statfs(self):
1.158 + return os.statvfs(".")
1.159 +
1.160 +
1.161 + def fsinit(self):
1.162 + os.chdir(self.root)
1.163 +
1.164 +
1.165 + def main(self, *a, **kw):
1.166 + self.file_class = MirrorFuseFile
1.167 + return Fuse.main(self, *a, **kw)
1.168 +
1.169 +
1.170 +class MirrorFuseFile(object):
1.171 +
1.172 + """This is a single "File" in the Mirror FUSE"""
1.173 +
1.174 + def __init__(self, path, flags, *mode):
1.175 + sys.stdout.write("===\nInsert Hook here! Opening file %s\n===\n" % path)
1.176 + self.file = os.fdopen(os.open("." + path, flags, *mode), flag2mode(flags))
1.177 + self.fd = self.file.fileno()
1.178 +
1.179 +
1.180 + def read(self, length, offset):
1.181 + self.file.seek(offset)
1.182 + return self.file.read(length)
1.183 +
1.184 +
1.185 + def write(self, buf, offset):
1.186 + self.file.seek(offset)
1.187 + self.file.write(buf)
1.188 + return len(buf)
1.189 +
1.190 +
1.191 + def release(self, flags):
1.192 + self.file.close()
1.193 +
1.194 +
1.195 + def _fflush(self):
1.196 + if 'w' in self.file.mode or 'a' in self.file.mode:
1.197 + self.file.flush()
1.198 +
1.199 + def fsync(self, isfsyncfile):
1.200 + self._fflush()
1.201 + if isfsyncfile and hasattr(os, 'fdatasync'):
1.202 + os.fdatasync(self.fd)
1.203 + else:
1.204 + os.fsync(self.fd)
1.205 +
1.206 +
1.207 + def flush(self):
1.208 + self._fflush()
1.209 + os.close(os.dup(self.fd))
1.210 +
1.211 +
1.212 + def fgetattr(self):
1.213 + return os.fstat(self.fd)
1.214 +
1.215 +
1.216 + def ftruncate(self, len):
1.217 + self.file.truncate(len)
1.218 +
1.219 +
1.220 + def lock(self, cmd, owner, **kw):
1.221 + op = {fcntl.F_UNLCK : fcntl.LOCK_UN, fcntl.F_RDLCK : fcntl.LOCK_SH, fcntl.F_WRLCK : fcntl.LOCK_EX}[kw['l_type']]
1.222 + if cmd == fcntl.F_GETLK:
1.223 + return -EOPNOTSUPP
1.224 + elif cmd == fcntl.F_SETLK:
1.225 + if op != fcntl.LOCK_UN:
1.226 + op |= fcntl.LOCK_NB
1.227 + elif cmd == fcntl.F_SETLKW:
1.228 + pass
1.229 + else:
1.230 + return -errno.EINVAL
1.231 +
1.232 + fcntl.lockf(self.fd, op, kw['l_start'], kw['l_len'])
1.233 +
1.234 +
1.235 +def flag2mode(flags):
1.236 +
1.237 + """Turn os flags into mode chars"""
1.238 +
1.239 + md = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'}
1.240 + m = md[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]
1.241 + if flags | os.O_APPEND:
1.242 + m = m.replace('w', 'a', 1)
1.243 + return m
1.244 +
1.245 +
1.246 +def main():
1.247 +
1.248 + usage = """
1.249 +mirror the a file tree from some point on.
1.250 +
1.251 +""" + Fuse.fusage
1.252 +
1.253 + # launch the Fuse server
1.254 + server = MirrorFuse(version = "%prog " + __version__, usage = usage, dash_s_do = 'setsingle')
1.255 + server.parser.add_option(mountopt = "root", metavar = "PATH", default='/', help="mirror filesystem from under PATH [default: %default]")
1.256 + server.parser.add_option(mountopt = "os_server_url", metavar = "URL", default='http://localhost:8080', help="URL to OpenSecurity Server [default: %default]")
1.257 + server.parse(values=server, errex=1)
1.258 +
1.259 + try:
1.260 + if server.fuse_args.mount_expected():
1.261 + os.chdir(server.root)
1.262 + except OSError:
1.263 + print >> sys.stderr, "can't enter root of underlying filesystem"
1.264 + sys.exit(1)
1.265 +
1.266 + server.main()
1.267 +
1.268 +
1.269 +# start
1.270 +if __name__ == "__main__":
1.271 + main()
1.272 +
1.273 +