This enables more flexibility in input types as long as they provide
binary I/O capabilities.

Signed-off-by: Andrej Utz <andrej....@st.oth-regensburg.de>
---
 pyjailhouse/config_parser.py | 59 +++++++++++++++++++-----------------
 tools/jailhouse-config-check |  4 +--
 2 files changed, 33 insertions(+), 30 deletions(-)

diff --git a/pyjailhouse/config_parser.py b/pyjailhouse/config_parser.py
index 3bb2686a..3f20bc61 100644
--- a/pyjailhouse/config_parser.py
+++ b/pyjailhouse/config_parser.py
@@ -16,6 +16,7 @@
 
 from __future__ import print_function
 import struct
+import io
 
 from .extendedenum import ExtendedEnum
 
@@ -39,29 +40,30 @@ class CStruct:
         return attrs
 
     @classmethod
-    def parse(cls, data):
-        obj, data = cls.parse_class(cls, data)
-        return obj, data
+    def parse(cls, stream):
+        obj = cls.parse_class(cls, stream)
+        return obj
 
     @staticmethod
-    def parse_class(cls, data):
+    def parse_class(cls, stream):
+        fmt = cls._BIN_FMT
+        data_tuple = fmt.unpack_from(stream.read(fmt.size))
         obj = cls()
         slots = obj._slots()
         if len(slots) > 0:
-            data_tuple = cls._BIN_FMT.unpack_from(data)
             for assigment in zip(slots, data_tuple):
                 setattr(obj, *assigment)
 
-        return obj, data[cls._BIN_FMT.size:]
+        return obj
 
     @staticmethod
-    def parse_array(cls, num, data):
+    def parse_array(cls, num, stream):
         array = []
         for i in range(num):
-            obj, data = cls.parse(data)
+            obj = cls.parse(stream)
             array += [obj]
 
-        return array, data
+        return array
 
 
 def flag_str(enum_class, value, separator=' | '):
@@ -197,28 +199,27 @@ class CellConfig(CStruct):
         self.cpu_reset_address = 0
 
     @classmethod
-    def parse(cls, data, root_cell=False):
+    def parse(cls, stream, root_cell=False):
         try:
             if not root_cell:
-                (signature, revision) = cls._BIN_FMT_HDR.unpack_from(data)
+                (signature, revision) = cls._BIN_FMT_HDR.unpack_from(
+                        stream.read(cls._BIN_FMT_HDR.size))
                 if signature != b'JHCELL':
                     raise RuntimeError('Not a cell configuration')
                 if revision != _CONFIG_REVISION:
                     raise RuntimeError('Configuration file revision mismatch')
-                data = data[cls._BIN_FMT_HDR.size:]
 
-            self, data = cls.parse_class(cls, data)
+            self = cls.parse_class(cls, stream)
             self.name = self.name.decode()
-            data = data[self._cpu_sets:] # skip CPU set
+            stream.seek(self._cpu_sets, io.SEEK_CUR) # skip CPU set
 
-            self.memory_regions, data = \
-                cls.parse_array(MemRegion, self.memory_regions, data)
-            self.cache_regions, data = \
-                cls.parse_array(CacheRegion, self.cache_regions, data)
-            self.irqchips, data = \
-                cls.parse_array(Irqchip, self.irqchips, data)
-            self.pio_regions, data = \
-                cls.parse_array(PIORegion, self.pio_regions, data)
+            self.memory_regions = \
+                cls.parse_array(MemRegion, self.memory_regions, stream)
+            self.cache_regions = \
+                cls.parse_array(CacheRegion, self.cache_regions, stream)
+            self.irqchips = cls.parse_array(Irqchip, self.irqchips, stream)
+            self.pio_regions = \
+                cls.parse_array(PIORegion, self.pio_regions, stream)
 
             return self
         except struct.error:
@@ -239,21 +240,23 @@ class SystemConfig(CStruct):
         self.root_cell = CellConfig()
 
     @classmethod
-    def parse(cls, data):
+    def parse(cls, stream):
         try:
             hdr_fmt = CellConfig._BIN_FMT_HDR
-            (signature, revision) = hdr_fmt.unpack_from(data)
+            (signature, revision) = \
+                hdr_fmt.unpack_from(stream.read(hdr_fmt.size))
             if signature != b'JHSYST':
                 raise RuntimeError('Not a root cell configuration')
             if revision != _CONFIG_REVISION:
                 raise RuntimeError('Configuration file revision mismatch')
 
-            self, data = cls.parse_class(cls, data[hdr_fmt.size:])
-            self.hypervisor_memory, data = MemRegion.parse(data)
+            self = cls.parse_class(cls, stream)
+            self.hypervisor_memory = MemRegion.parse(stream)
 
             offs = cls._BIN_FMT_CONSOLE_AND_PLATFORM.size
-            offs += CellConfig._BIN_FMT_HDR.size # skip header inside rootcell
-            self.root_cell = CellConfig.parse(data[offs:], root_cell=True)
+            offs += hdr_fmt.size # skip header inside rootcell
+            stream.seek(offs, io.SEEK_CUR)
+            self.root_cell = CellConfig.parse(stream, True)
             return self
         except struct.error:
             raise RuntimeError('Not a root cell configuration')
diff --git a/tools/jailhouse-config-check b/tools/jailhouse-config-check
index 539a640e..642e4fab 100755
--- a/tools/jailhouse-config-check
+++ b/tools/jailhouse-config-check
@@ -44,7 +44,7 @@ except IOError as e:
 print("Reading configuration set:")
 
 try:
-    sysconfig = config_parser.SystemConfig.parse(args.syscfg.read())
+    sysconfig = config_parser.SystemConfig.parse(args.syscfg)
     root_cell = sysconfig.root_cell
 except RuntimeError as e:
     print(str(e) + ": " + args.syscfg.name, file=sys.stderr)
@@ -55,7 +55,7 @@ print("  Root cell:     %s (%s)" % (root_cell.name, 
args.syscfg.name))
 non_root_cells = []
 for cfg in args.cellcfgs:
     try:
-        cell = config_parser.CellConfig.parse(cfg.read())
+        cell = config_parser.CellConfig.parse(cfg)
     except RuntimeError as e:
         print(str(e) + ": " + cfg.name, file=sys.stderr)
         exit(1)
-- 
2.27.0

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jailhouse-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jailhouse-dev/20200630064228.4742-4-andrej.utz%40st.oth-regensburg.de.

Reply via email to