From f131c87f91a62892ea3f19c1b80f7af66b3456f9 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Sat, 24 Nov 2012 17:15:19 +0200 Subject: [PATCH] Fiemap: introduce constants Introduce constants for various FIEMAP-related stuff like format strings for C structures, their sizes, and the ioctl number. First of all, this is just nicer. But more importantly, we'll soon introduce another function which will use these constants as well. Change-Id: Iac00bc93d8a590dc242b8ce7b20f2fe3d98a186d Signed-off-by: Artem Bityutskiy --- bmaptools/Fiemap.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/bmaptools/Fiemap.py b/bmaptools/Fiemap.py index a7748e4..2f79ae0 100644 --- a/bmaptools/Fiemap.py +++ b/bmaptools/Fiemap.py @@ -13,6 +13,17 @@ import fcntl import itertools from bmaptools import BmapHelpers +# Format string for 'struct fiemap' +_FIEMAP_FORMAT = "=QQLLLL" +# sizeof(struct fiemap) +_FIEMAP_SIZE = struct.calcsize(_FIEMAP_FORMAT) +# Format string for 'struct fiemap_extent' +_FIEMAP_EXTENT_FORMAT = "=QQQQQLLLL" +# sizeof(struct fiemap_extent) +_FIEMAP_EXTENT_SIZE = struct.calcsize(_FIEMAP_EXTENT_FORMAT) +# The FIEMAP ioctl number +_FIEMAP_IOCTL = 0xC020660B + class Error(Exception): """ A class for exceptions generated by this module. We currently support only one type of exceptions, and we basically throw human-readable problem @@ -85,19 +96,16 @@ class Fiemap: # Prepare a 'struct fiemap' buffer which contains a single # 'struct fiemap_extent' element. - struct_fiemap_format = "=QQLLLL" - struct_size = struct.calcsize(struct_fiemap_format) - buf = struct.pack(struct_fiemap_format, - block * self.block_size, + buf = struct.pack(_FIEMAP_FORMAT, block * self.block_size, self.block_size, 0, 0, 1, 0) - # sizeof(struct fiemap_extent) == 56 - buf += "\0"*56 + buf += '\0' * _FIEMAP_EXTENT_SIZE + # Python strings are "immutable", meaning that python will pass a copy # of the string to the ioctl, unless we turn it into an array. buf = array.array('B', buf) try: - fcntl.ioctl(self._f_image, 0xC020660B, buf, 1) + fcntl.ioctl(self._f_image, _FIEMAP_IOCTL, buf, 1) except IOError as err: error_msg = "the FIBMAP ioctl failed for '%s': %s" \ % (self._image_path, err) @@ -107,7 +115,8 @@ class Fiemap: raise Error(error_msg) - res = struct.unpack(struct_fiemap_format, buf[:struct_size]) + res = struct.unpack(_FIEMAP_FORMAT, buf[:_FIEMAP_SIZE]) + # res[3] is the 'fm_mapped_extents' field of 'struct fiemap'. If it # contains zero, the block is not mapped, otherwise it is mapped. return bool(res[3]) -- 2.7.4