misc: add get_block_size helper function
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 7 May 2013 07:02:19 +0000 (10:02 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 7 May 2013 07:07:00 +0000 (10:07 +0300)
It returns file-system block size.

Change-Id: Idc512a90c58d76925e46f3bd3782093f2c102d5e
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
mic/utils/misc.py

index 83ab8d6..e7dbf2f 100644 (file)
@@ -267,6 +267,18 @@ def human_size(size):
     mant = float(size/math.pow(1024, expo))
     return "{0:.1f}{1:s}".format(mant, measure[expo])
 
+def get_block_size(file_obj):
+    """ Returns block size for file object 'file_obj'. Errors are indicated by
+    the 'IOError' exception. """
+
+    from fcntl import ioctl
+    import struct
+
+    # Get the block size of the host file-system for the image file by calling
+    # the FIGETBSZ ioctl (number 2).
+    binary_data = ioctl(file_obj, 2, struct.pack('I', 0))
+    return struct.unpack('I', binary_data)[0]
+
 def check_space_pre_cp(src, dst):
     """Check whether disk space is enough before 'cp' like
     operations, else exception will be raised.