From 2de2aa0181cf7ec69cbfc8fe62db5372deff0c8f Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Tue, 7 May 2013 10:02:19 +0300 Subject: [PATCH] misc: add get_block_size helper function It returns file-system block size. Change-Id: Idc512a90c58d76925e46f3bd3782093f2c102d5e Signed-off-by: Artem Bityutskiy --- mic/utils/misc.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mic/utils/misc.py b/mic/utils/misc.py index 83ab8d6..e7dbf2f 100644 --- a/mic/utils/misc.py +++ b/mic/utils/misc.py @@ -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. -- 2.7.4