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.
18 from qmp
import QEMUMonitorProtocol
20 fuse
.fuse_python_api
= (0, 2)
23 def __init__(self
, qmp
, *args
, **kwds
):
24 Fuse
.__init
__(self
, *args
, **kwds
)
30 def get_ino(self
, path
):
31 if path
in self
.ino_map
:
32 return self
.ino_map
[path
]
33 self
.ino_map
[path
] = self
.ino_count
35 return self
.ino_map
[path
]
37 def is_object(self
, path
):
39 items
= self
.qmp
.command('qom-list', path
=path
)
44 def is_property(self
, path
):
46 path
, prop
= path
.rsplit('/', 1)
47 for item
in self
.qmp
.command('qom-list', path
=path
):
48 if item
['name'] == prop
:
54 def is_link(self
, path
):
56 path
, prop
= path
.rsplit('/', 1)
57 for item
in self
.qmp
.command('qom-list', path
=path
):
58 if item
['name'] == prop
:
59 if item
['type'].startswith('link<'):
66 def read(self
, path
, length
, offset
):
67 if not self
.is_property(path
):
70 path
, prop
= path
.rsplit('/', 1)
72 data
= str(self
.qmp
.command('qom-get', path
=path
, property=prop
))
73 data
+= '\n' # make values shell friendly
77 if offset
> len(data
):
80 return str(data
[offset
:][:length
])
82 def readlink(self
, path
):
83 if not self
.is_link(path
):
85 path
, prop
= path
.rsplit('/', 1)
86 prefix
= '/'.join(['..'] * (len(path
.split('/')) - 1))
87 return prefix
+ str(self
.qmp
.command('qom-get', path
=path
,
90 def getattr(self
, path
):
91 if self
.is_link(path
):
92 value
= posix
.stat_result((0o755 | stat
.S_IFLNK
,
102 elif self
.is_object(path
):
103 value
= posix
.stat_result((0o755 | stat
.S_IFDIR
,
113 elif self
.is_property(path
):
114 value
= posix
.stat_result((0o644 | stat
.S_IFREG
,
128 def readdir(self
, path
, offset
):
129 yield fuse
.Direntry('.')
130 yield fuse
.Direntry('..')
131 for item
in self
.qmp
.command('qom-list', path
=path
):
132 yield fuse
.Direntry(str(item
['name']))
134 if __name__
== '__main__':
137 fs
= QOMFS(QEMUMonitorProtocol(os
.environ
['QMP_SOCKET']))