3 # QEMU Object Model test tools
5 # Copyright IBM, Corp. 2012
8 # Anthony Liguori <aliguori@us.ibm.com>
10 # This work is licensed under the terms of the GNU GPL, version 2 or later. See
11 # the COPYING file in the top-level directory.
19 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
20 from qemu.qmp import QEMUMonitorProtocol
22 fuse.fuse_python_api = (0, 2)
25 def __init__(self, qmp, *args, **kwds):
26 Fuse.__init__(self, *args, **kwds)
32 def get_ino(self, path):
33 if path in self.ino_map:
34 return self.ino_map[path]
35 self.ino_map[path] = self.ino_count
37 return self.ino_map[path]
39 def is_object(self, path):
41 items = self.qmp.command('qom-list', path=path)
46 def is_property(self, path):
48 path, prop = path.rsplit('/', 1)
49 for item in self.qmp.command('qom-list', path=path):
50 if item['name'] == prop:
56 def is_link(self, path):
58 path, prop = path.rsplit('/', 1)
59 for item in self.qmp.command('qom-list', path=path):
60 if item['name'] == prop:
61 if item['type'].startswith('link<'):
68 def read(self, path, length, offset):
69 if not self.is_property(path):
72 path, prop = path.rsplit('/', 1)
74 data = str(self.qmp.command('qom-get', path=path, property=prop))
75 data += '\n' # make values shell friendly
79 if offset > len(data):
82 return str(data[offset:][:length])
84 def readlink(self, path):
85 if not self.is_link(path):
87 path, prop = path.rsplit('/', 1)
88 prefix = '/'.join(['..'] * (len(path.split('/')) - 1))
89 return prefix + str(self.qmp.command('qom-get', path=path,
92 def getattr(self, path):
93 if self.is_link(path):
94 value = posix.stat_result((0o755 | stat.S_IFLNK,
104 elif self.is_object(path):
105 value = posix.stat_result((0o755 | stat.S_IFDIR,
115 elif self.is_property(path):
116 value = posix.stat_result((0o644 | stat.S_IFREG,
130 def readdir(self, path, offset):
131 yield fuse.Direntry('.')
132 yield fuse.Direntry('..')
133 for item in self.qmp.command('qom-list', path=path):
134 yield fuse.Direntry(str(item['name']))
136 if __name__ == '__main__':
139 fs = QOMFS(QEMUMonitorProtocol(os.environ['QMP_SOCKET']))