~doctormo/+junk/gremlinfuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/python
#
# Taken from http://sourceforge.net/apps/mediawiki/fuse/?title=FUSE_Python_tutorial
#
# Modified by Martin Owens 2013
#
# This fuse script attempts to cause trouble for the file system, so you can
# test your applications and how they deal with mis-behaving file systems.
# Especially useful for network file system issues.
#

import os      # for filesystem modes(O_RDONLY, etc)
import sys     # Get ARGV inputs
import time    # Pause and wait for nothing
import fuse    # User Space FileSystem
import stat    # for file properties
import errno   # for error number codes(ENOENT, etc)
               # - note: these must be returned as negatives

fuse.fuse_python_api =(0, 2)

_path = '/gizmo.svg'
_content = """<?xml version="1.0" encoding="UTF-8"?>
<svg height="120" width="120"><g>
<rect x="10" y="10" width="100" height="100" style="fill:#aaccee;stroke:#6699cc;stroke-width:12;stroke-opacity:1;"/>
</g></svg>"""

def dirFromList(list):
    """
    Return a properly formatted list of items suitable to a directory listing.
    [['a', 'b', 'c']] => [[('a', 0),('b', 0),('c', 0)]]
    """
    return [[(x, 0) for x in list]]

def getDepth(path):
    """
    Return the depth of a given path, zero-based from root('/')
    """
    if path == '/':
        return 0
    else:
        return path.count('/')

def getParts(path):
    """
    Return the slash-separated parts of a given path as a list
    """
    if path == '/':
        return [['/']]
    else:
        return path.split('/')

class MyStat(fuse.Stat):
    def __init__(self):
        self.st_mode = 0
        self.st_ino = 0
        self.st_dev = 0
        self.st_nlink = 0
        self.st_uid = 0
        self.st_gid = 0
        self.st_size = 0
        self.st_atime = 0
        self.st_mtime = 0
        self.st_ctime = 0

class GremlinFS(fuse.Fuse):
    """Simple File system for causing mischief"""
    def __init__(self, *args, **kw):
        self.what = kw.pop('what', None) or 'attr'
        fuse.Fuse.__init__(self, *args, **kw)
        self.fuse_args.setmod('foreground')
        sys.stderr.write("File System Online: Breaking on %s!\n" % self.what)

    def midnight_feast(self, name, path):
        """Block a call forever!"""
        if path != _path:
            print " [ ] Ignoring %s for %s" % (name, path)
            return
        print " [X] Blocking %s for '%s'" % (name, path)
        if sys.stdin.read(1) == 'q':
            self.parser.exit(status=0, msg="Exiting!")
        print " [:] Continuing for '%s'" % (path)

    def getattr(self, path):
        if self.what == 'attr':
            self.midnight_feast("Get Attribute", path)
        st = MyStat()
        if path in ['/', '']:
            st.st_mode = stat.S_IFDIR | 0755
            st.st_nlink = 2
        elif path == _path:
            st.st_mode = stat.S_IFREG | 0444
            st.st_nlink = 1
            st.st_size = len(_content)
        else:
            return -errno.ENOSYS
        return st

    def chmod(self, path, mode ):
        if self.what == 'chmod':
            self.midnight_feast("Change Mode", path)
        return -errno.ENOSYS

    def chown(self, path, uid, gid ):
        if self.what == 'chown':
            self.midnight_feast("Change Owner", path)
        return -errno.ENOSYS

    def fsync(self, path, isFsyncFile ):
        if self.what == 'fsync':
            self.midnight_feast("File Sync", path)
        return -errno.ENOSYS

    def link(self, targetPath, linkPath ):
        if self.what == 'link':
            self.midnight_feast("Link Path", path)
        return -errno.ENOSYS

    def mkdir(self, path, mode ):
        if self.what == 'mkdir':
            self.midnight_feast("Make Directory", path)
        return -errno.ENOSYS

    def mknod(self, path, mode, dev ):
        if self.what == 'mknod':
            self.midnight_feast("Make Node", path)
        return -errno.ENOSYS

    def open(self, path, flags ):
        if self.what == 'open':
            self.midnight_feast("Open File", path)
        if path != _path:
            return -errno.ENOENT
        accmode = os.O_RDONLY | os.O_WRONLY | os.O_RDWR
        if (flags & accmode) != os.O_RDONLY:
            return -errno.EACCES

    def read(self, path, size, offset):
        if self.what == 'read':
            self.midnight_feast("Read File", path)
        if path != _path:
            return -errno.ENOENT
        slen = len(_content)
        if offset < slen:
            if offset + size > slen:
                size = slen - offset
            buf = _content[offset:offset+size]
        else:
            buf = ''
        return buf

    def readdir(self, path, offset ):
        if self.what == 'readdir':
            self.midnight_feast("Read Directory", path)
        for r in  '.', '..', _path:
            yield fuse.Direntry(r)

    def readlink(self, path ):
        if self.what == 'readlink':
            self.midnight_feast("Read Link", path)
        return -errno.ENOSYS

    def release(self, path, flags ):
        if self.what == 'release':
            self.midnight_feast("Release", path)
        return -errno.ENOSYS

    def rename(self, oldPath, newPath ):
        if self.what == 'rename':
            self.midnight_feast("Rename Path", oldPath)
        return -errno.ENOSYS

    def rmdir(self, path ):
        if self.what == 'rmdir':
            self.midnight_feast("Remove Directory", path)
        return -errno.ENOSYS

    def statfs(self ):
        if self.what == 'statfs':
            self.midnight_feast("File System Status", None)
        return -errno.ENOSYS

    def symlink(self, targetPath, linkPath ):
        if self.what == 'symlink':
            self.midnight_feast("Make Symbolic Link", targetPath)
        return -errno.ENOSYS

    def truncate(self, path, size ):
        if self.what == 'truncate':
            self.midnight_feast("Truncate File", path)
        return -errno.ENOSYS

    def unlink(self, path ):
        if self.what == 'unlink':
            self.midnight_feast("Delete File", path)
        return -errno.ENOSYS

    def utime(self, path, times ):
        if self.what == 'utime':
            self.midnight_feast("Get Update Time", path)
        return -errno.ENOSYS

    def write(self, path, buf, offset ):
        if self.what == 'write':
            self.midnight_feast("Write to File", path)
        return -errno.ENOSYS

import signal

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'usage: %s <mountpoint> [freeze-on-what]' % sys.argv[0]
        exit(1)
    what = len(sys.argv) == 3 and sys.argv[2] or None

    fs = GremlinFS(what=what)
    fs.parse(errex=1)
    fs.flags = 0
    fs.multithreaded = 0
    fs.main()