| 1 | #!/usr/bin/env python |
|---|
| 2 | |
|---|
| 3 | # Copyright (C) 2008 Evan Broder <broder@mit.edu> |
|---|
| 4 | # Copyright (C) 2001 Jeff Epler <jepler@unpythonic.dhs.org> |
|---|
| 5 | # Copyright (C) 2006 Csaba Henk <csaba.henk@creo.hu> |
|---|
| 6 | # |
|---|
| 7 | # This program can be distributed under the terms of the GNU LGPL. |
|---|
| 8 | # |
|---|
| 9 | |
|---|
| 10 | import os, sys |
|---|
| 11 | from errno import * |
|---|
| 12 | from stat import * |
|---|
| 13 | import fcntl |
|---|
| 14 | import fuse |
|---|
| 15 | import pwd |
|---|
| 16 | from fuse import Fuse |
|---|
| 17 | |
|---|
| 18 | if not hasattr(fuse, '__version__'): |
|---|
| 19 | raise RuntimeError, \ |
|---|
| 20 | "your fuse-py doesn't know of fuse.__version__, probably it's too old." |
|---|
| 21 | |
|---|
| 22 | fuse.fuse_python_api = (0, 2) |
|---|
| 23 | |
|---|
| 24 | class ErrorFS(Fuse): |
|---|
| 25 | |
|---|
| 26 | def __init__(self, *args, **kw): |
|---|
| 27 | |
|---|
| 28 | Fuse.__init__(self, *args, **kw) |
|---|
| 29 | |
|---|
| 30 | self.root = '/tmp/errors' |
|---|
| 31 | self.global_log = open(os.path.join(self.root, 'global'), 'a') |
|---|
| 32 | |
|---|
| 33 | def getRealPath(self, path): |
|---|
| 34 | if path == '/': |
|---|
| 35 | return self.root |
|---|
| 36 | uid = fuse.FuseGetContext()['uid'] |
|---|
| 37 | username = pwd.getpwuid(uid).pw_name |
|---|
| 38 | realPath = os.path.join(self.root, username) |
|---|
| 39 | open(realPath, 'w') |
|---|
| 40 | |
|---|
| 41 | return realPath |
|---|
| 42 | |
|---|
| 43 | def getattr(self, path): |
|---|
| 44 | realPath = self.getRealPath(path) |
|---|
| 45 | return os.lstat(self.getRealPath(path)) |
|---|
| 46 | |
|---|
| 47 | def readdir(self, path, offset): |
|---|
| 48 | yield fuse.Direntry('error_log') |
|---|
| 49 | |
|---|
| 50 | def utime(self, path, times): |
|---|
| 51 | os.utime(self.getRealPath(path), times) |
|---|
| 52 | |
|---|
| 53 | def access(self, path, mode): |
|---|
| 54 | if not os.access(self.getRealPath(path), mode): |
|---|
| 55 | return -EACCES |
|---|
| 56 | |
|---|
| 57 | def fsinit(self): |
|---|
| 58 | os.chdir(self.root) |
|---|
| 59 | |
|---|
| 60 | def read(self, path, length, offset): |
|---|
| 61 | f = file(self.getRealPath(path)) |
|---|
| 62 | f.seek(offset) |
|---|
| 63 | return f.read(length) |
|---|
| 64 | |
|---|
| 65 | def write(self, path, buf, offset): |
|---|
| 66 | f = open(self.getRealPath(path), 'a') |
|---|
| 67 | f.write(buf) |
|---|
| 68 | f.close() |
|---|
| 69 | |
|---|
| 70 | self.global_file.write(buf) |
|---|
| 71 | |
|---|
| 72 | return len(buf) |
|---|
| 73 | |
|---|
| 74 | def truncate(self, path, len): |
|---|
| 75 | return |
|---|
| 76 | |
|---|
| 77 | def main(): |
|---|
| 78 | |
|---|
| 79 | usage = """ |
|---|
| 80 | Userspace nullfs-alike: mirror the filesystem tree from some point on. |
|---|
| 81 | |
|---|
| 82 | """ + Fuse.fusage |
|---|
| 83 | |
|---|
| 84 | server = ErrorFS(version="%prog " + fuse.__version__, |
|---|
| 85 | usage=usage, |
|---|
| 86 | dash_s_do='setsingle') |
|---|
| 87 | |
|---|
| 88 | server.parser.add_option(mountopt="root", metavar="PATH", default='/tmp/errors', |
|---|
| 89 | help="mirror filesystem from under PATH [default: %default]") |
|---|
| 90 | server.parse(values=server, errex=1) |
|---|
| 91 | |
|---|
| 92 | try: |
|---|
| 93 | if server.fuse_args.mount_expected(): |
|---|
| 94 | os.chdir(server.root) |
|---|
| 95 | except OSError: |
|---|
| 96 | print >> sys.stderr, "can't enter root of underlying filesystem" |
|---|
| 97 | sys.exit(1) |
|---|
| 98 | |
|---|
| 99 | server.main() |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | if __name__ == '__main__': |
|---|
| 103 | main() |
|---|