[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 06/12] qcow2_format.py: use strings to specify c-type of struc
From: |
Vladimir Sementsov-Ogievskiy |
Subject: |
[PATCH v4 06/12] qcow2_format.py: use strings to specify c-type of struct fields |
Date: |
Thu, 4 Jun 2020 20:41:29 +0300 |
We are going to move field-parsing to super-class, this will be simpler
with simple string specifiers instead of variables.
For some reason python doesn't allow to define ctypes in class too, as
well as fields: it's not available than in 'for' operator. Don't worry:
ctypes will be moved to metaclass soon.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
tests/qemu-iotests/qcow2_format.py | 50 +++++++++++++++++-------------
1 file changed, 28 insertions(+), 22 deletions(-)
diff --git a/tests/qemu-iotests/qcow2_format.py
b/tests/qemu-iotests/qcow2_format.py
index 80e7c3f81d..1fd9473b7f 100644
--- a/tests/qemu-iotests/qcow2_format.py
+++ b/tests/qemu-iotests/qcow2_format.py
@@ -18,36 +18,42 @@ class QcowHeaderExtension:
return QcowHeaderExtension(magic, len(data), data)
-class QcowHeader:
+# Mapping from c types to python struct format
+ctypes = {
+ 'u8': 'B',
+ 'u16': 'H',
+ 'u32': 'I',
+ 'u64': 'Q'
+}
+
- uint32_t = 'I'
- uint64_t = 'Q'
+class QcowHeader:
fields = (
# Version 2 header fields
- (uint32_t, '{:#x}', 'magic'),
- (uint32_t, '{}', 'version'),
- (uint64_t, '{:#x}', 'backing_file_offset'),
- (uint32_t, '{:#x}', 'backing_file_size'),
- (uint32_t, '{}', 'cluster_bits'),
- (uint64_t, '{}', 'size'),
- (uint32_t, '{}', 'crypt_method'),
- (uint32_t, '{}', 'l1_size'),
- (uint64_t, '{:#x}', 'l1_table_offset'),
- (uint64_t, '{:#x}', 'refcount_table_offset'),
- (uint32_t, '{}', 'refcount_table_clusters'),
- (uint32_t, '{}', 'nb_snapshots'),
- (uint64_t, '{:#x}', 'snapshot_offset'),
+ ('u32', '{:#x}', 'magic'),
+ ('u32', '{}', 'version'),
+ ('u64', '{:#x}', 'backing_file_offset'),
+ ('u32', '{:#x}', 'backing_file_size'),
+ ('u32', '{}', 'cluster_bits'),
+ ('u64', '{}', 'size'),
+ ('u32', '{}', 'crypt_method'),
+ ('u32', '{}', 'l1_size'),
+ ('u64', '{:#x}', 'l1_table_offset'),
+ ('u64', '{:#x}', 'refcount_table_offset'),
+ ('u32', '{}', 'refcount_table_clusters'),
+ ('u32', '{}', 'nb_snapshots'),
+ ('u64', '{:#x}', 'snapshot_offset'),
# Version 3 header fields
- (uint64_t, 'mask', 'incompatible_features'),
- (uint64_t, 'mask', 'compatible_features'),
- (uint64_t, 'mask', 'autoclear_features'),
- (uint32_t, '{}', 'refcount_order'),
- (uint32_t, '{}', 'header_length'),
+ ('u64', 'mask', 'incompatible_features'),
+ ('u64', 'mask', 'compatible_features'),
+ ('u64', 'mask', 'autoclear_features'),
+ ('u32', '{}', 'refcount_order'),
+ ('u32', '{}', 'header_length'),
)
- fmt = '>' + ''.join(field[0] for field in fields)
+ fmt = '>' + ''.join(ctypes[f[0]] for f in fields)
def __init__(self, fd):
--
2.21.0
- Re: [PATCH v4 01/12] qcow2.py: python style fixes, (continued)
- [PATCH v4 09/12] qcow2_format.py: QcowHeaderExtension: add dump method, Vladimir Sementsov-Ogievskiy, 2020/06/04
- [PATCH v4 06/12] qcow2_format.py: use strings to specify c-type of struct fields,
Vladimir Sementsov-Ogievskiy <=
- [PATCH v4 07/12] qcow2_format.py: separate generic functionality of structure classes, Vladimir Sementsov-Ogievskiy, 2020/06/04
- [PATCH v4 03/12] qcow2_format.py: drop new line printing at end of dump(), Vladimir Sementsov-Ogievskiy, 2020/06/04
- [PATCH v4 05/12] qcow2_format.py: use modern string formatting, Vladimir Sementsov-Ogievskiy, 2020/06/04
- [PATCH v4 10/12] qcow2_format: refactor QcowHeaderExtension as a subclass of Qcow2Struct, Vladimir Sementsov-Ogievskiy, 2020/06/04