From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>,
Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
"open list:Block layer core" <qemu-block@nongnu.org>,
Max Reitz <mreitz@redhat.com>
Subject: [PULL 12/15] qcow2_format: refactor QcowHeaderExtension as a subclass of Qcow2Struct
Date: Tue, 9 Jun 2020 15:52:42 -0500 [thread overview]
Message-ID: <20200609205245.3548257-13-eblake@redhat.com> (raw)
In-Reply-To: <20200609205245.3548257-1-eblake@redhat.com>
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Only two fields we can parse by generic code, but that is better than
nothing. Keep further refactoring of variable-length fields for another
day.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
Message-Id: <20200606081806.23897-12-vsementsov@virtuozzo.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
---
tests/qemu-iotests/qcow2_format.py | 53 +++++++++++++++++++++---------
1 file changed, 37 insertions(+), 16 deletions(-)
diff --git a/tests/qemu-iotests/qcow2_format.py b/tests/qemu-iotests/qcow2_format.py
index d4ad5431b296..32371e42da4e 100644
--- a/tests/qemu-iotests/qcow2_format.py
+++ b/tests/qemu-iotests/qcow2_format.py
@@ -97,16 +97,41 @@ class Qcow2Struct(metaclass=Qcow2StructMeta):
print('{:<25} {}'.format(f[2], value_str))
-class QcowHeaderExtension:
+class QcowHeaderExtension(Qcow2Struct):
- def __init__(self, magic, length, data):
- if length % 8 != 0:
- padding = 8 - (length % 8)
- data += b'\0' * padding
+ fields = (
+ ('u32', '{:#x}', 'magic'),
+ ('u32', '{}', 'length')
+ # length bytes of data follows
+ # then padding to next multiply of 8
+ )
- self.magic = magic
- self.length = length
- self.data = data
+ def __init__(self, magic=None, length=None, data=None, fd=None):
+ """
+ Support both loading from fd and creation from user data.
+ For fd-based creation current position in a file will be used to read
+ the data.
+
+ This should be somehow refactored and functionality should be moved to
+ superclass (to allow creation of any qcow2 struct), but then, fields
+ of variable length (data here) should be supported in base class
+ somehow. So, it's a TODO. We'll see how to properly refactor this when
+ we have more qcow2 structures.
+ """
+ if fd is None:
+ assert all(v is not None for v in (magic, length, data))
+ self.magic = magic
+ self.length = length
+ if length % 8 != 0:
+ padding = 8 - (length % 8)
+ data += b'\0' * padding
+ self.data = data
+ else:
+ assert all(v is None for v in (magic, length, data))
+ super().__init__(fd=fd)
+ padded = (self.length + 7) & ~7
+ self.data = fd.read(padded)
+ assert self.data is not None
def dump(self):
data = self.data[:self.length]
@@ -115,8 +140,7 @@ class QcowHeaderExtension:
else:
data = '<binary>'
- print(f'{"magic":<25} {self.magic:#x}')
- print(f'{"length":<25} {self.length}')
+ super().dump()
print(f'{"data":<25} {data}')
@classmethod
@@ -182,14 +206,11 @@ class QcowHeader(Qcow2Struct):
end = self.cluster_size
while fd.tell() < end:
- (magic, length) = struct.unpack('>II', fd.read(8))
- if magic == 0:
+ ext = QcowHeaderExtension(fd=fd)
+ if ext.magic == 0:
break
else:
- padded = (length + 7) & ~7
- data = fd.read(padded)
- self.extensions.append(QcowHeaderExtension(magic, length,
- data))
+ self.extensions.append(ext)
def update_extensions(self, fd):
--
2.27.0
next prev parent reply other threads:[~2020-06-09 20:59 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-09 20:52 [PULL 00/15] bitmaps patches for 2020-06-09 Eric Blake
2020-06-09 20:52 ` [PULL 01/15] qemu-img: Fix doc typo for 'bitmap' subcommand Eric Blake
2020-06-09 20:52 ` [PULL 02/15] qcow2.py: python style fixes Eric Blake
2020-06-09 20:52 ` [PULL 03/15] qcow2.py: add licensing blurb Eric Blake
2020-06-09 20:52 ` [PULL 04/15] qcow2.py: move qcow2 format classes to separate module Eric Blake
2020-06-09 20:52 ` [PULL 05/15] qcow2_format.py: drop new line printing at end of dump() Eric Blake
2020-06-09 20:52 ` [PULL 06/15] qcow2_format.py: use tuples instead of lists for fields Eric Blake
2020-06-09 20:52 ` [PULL 07/15] qcow2_format.py: use modern string formatting Eric Blake
2020-06-09 20:52 ` [PULL 08/15] qcow2_format.py: use strings to specify c-type of struct fields Eric Blake
2020-06-09 20:52 ` [PULL 09/15] qcow2_format.py: separate generic functionality of structure classes Eric Blake
2020-06-09 20:52 ` [PULL 10/15] qcow2_format.py: add field-formatting class Eric Blake
2020-06-09 20:52 ` [PULL 11/15] qcow2_format.py: QcowHeaderExtension: add dump method Eric Blake
2020-06-09 20:52 ` Eric Blake [this message]
2020-06-09 20:52 ` [PULL 13/15] qcow2: QcowHeaderExtension print names for extension magics Eric Blake
2020-06-09 20:52 ` [PULL 14/15] qcow2_format.py: dump bitmaps header extension Eric Blake
2020-06-18 13:13 ` Max Reitz
2020-06-18 13:28 ` Vladimir Sementsov-Ogievskiy
2020-06-18 15:09 ` Max Reitz
2020-06-09 20:52 ` [PULL 15/15] iotests: Fix 291 across more file systems Eric Blake
2020-06-11 17:00 ` [PULL 00/15] bitmaps patches for 2020-06-09 Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200609205245.3548257-13-eblake@redhat.com \
--to=eblake@redhat.com \
--cc=andrey.shinkevich@virtuozzo.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).