Esempio n. 1
0
    def __init__(self, image, buf_size = DEFAULT_BUFFER_SIZE):
        """ Initialize a class instance. The 'image' argument is full path to
        the file to operate on, or a file object to operate on.

        The 'buf_size' argument is the size of the buffer for 'struct
        fiemap_extent' elements which will be used when invoking the FIEMAP
        ioctl. The larger is the buffer, the less times the FIEMAP ioctl will
        be invoked. """

        self._f_image_needs_close = False

        if hasattr(image, "fileno"):
            self._f_image = image
            self._image_path = image.name
        else:
            self._image_path = image
            self._open_image_file()

        # Validate 'buf_size'
        if buf_size < MIN_BUFFER_SIZE:
            raise Error("too small buffer (%d bytes), minimum is %d bytes" \
                    % (buf_size, MIN_BUFFER_SIZE))

        # How many 'struct fiemap_extent' elements fit the buffer
        buf_size -= _FIEMAP_SIZE
        self._fiemap_extent_cnt = buf_size / _FIEMAP_EXTENT_SIZE
        self._buf_size = self._fiemap_extent_cnt * _FIEMAP_EXTENT_SIZE
        self._buf_size += _FIEMAP_SIZE

        # Allocate a mutable buffer for the FIEMAP ioctl
        self._buf = array.array('B', [0] * self._buf_size)

        self.image_size = os.fstat(self._f_image.fileno()).st_size

        try:
            self.block_size = get_block_size(self._f_image)
        except IOError as err:
            raise Error("cannot get block size for '%s': %s" \
                        % (self._image_path, err))

        self.blocks_cnt = self.image_size + self.block_size - 1
        self.blocks_cnt /= self.block_size

        # Synchronize the image file to make sure FIEMAP returns correct values
        try:
            self._f_image.flush()
        except IOError as err:
            raise Error("cannot flush image file '%s': %s" \
                        % (self._image_path, err))
        try:
            os.fsync(self._f_image.fileno()),
        except OSError as err:
            raise Error("cannot synchronize image file '%s': %s " \
                        % (self._image_path, err.strerror))

        # Check if the FIEMAP ioctl is supported
        self.block_is_mapped(0)
Esempio n. 2
0
    def __init__(self, image, buf_size=DEFAULT_BUFFER_SIZE):
        """ Initialize a class instance. The 'image' argument is full path to
        the file to operate on, or a file object to operate on.

        The 'buf_size' argument is the size of the buffer for 'struct
        fiemap_extent' elements which will be used when invoking the FIEMAP
        ioctl. The larger is the buffer, the less times the FIEMAP ioctl will
        be invoked. """

        self._f_image_needs_close = False

        if hasattr(image, "fileno"):
            self._f_image = image
            self._image_path = image.name
        else:
            self._image_path = image
            self._open_image_file()

        # Validate 'buf_size'
        if buf_size < MIN_BUFFER_SIZE:
            raise Error("too small buffer (%d bytes), minimum is %d bytes" \
                    % (buf_size, MIN_BUFFER_SIZE))

        # How many 'struct fiemap_extent' elements fit the buffer
        buf_size -= _FIEMAP_SIZE
        self._fiemap_extent_cnt = buf_size / _FIEMAP_EXTENT_SIZE
        self._buf_size = self._fiemap_extent_cnt * _FIEMAP_EXTENT_SIZE
        self._buf_size += _FIEMAP_SIZE

        # Allocate a mutable buffer for the FIEMAP ioctl
        self._buf = array.array('B', [0] * self._buf_size)

        self.image_size = os.fstat(self._f_image.fileno()).st_size

        try:
            self.block_size = get_block_size(self._f_image)
        except IOError as err:
            raise Error("cannot get block size for '%s': %s" \
                        % (self._image_path, err))

        self.blocks_cnt = self.image_size + self.block_size - 1
        self.blocks_cnt /= self.block_size

        # Synchronize the image file to make sure FIEMAP returns correct values
        try:
            self._f_image.flush()
        except IOError as err:
            raise Error("cannot flush image file '%s': %s" \
                        % (self._image_path, err))
        try:
            os.fsync(self._f_image.fileno()),
        except OSError as err:
            raise Error("cannot synchronize image file '%s': %s " \
                        % (self._image_path, err.strerror))

        # Check if the FIEMAP ioctl is supported
        self.block_is_mapped(0)
Esempio n. 3
0
    def __init__(self, image, log=None):
        """
        Initialize a class instance. The 'image' argument is full path to the
        file or file object to operate on.
        """

        self._log = log
        if self._log is None:
            self._log = logging.getLogger(__name__)

        self._f_image_needs_close = False

        if hasattr(image, "fileno"):
            self._f_image = image
            self._image_path = image.name
        else:
            self._image_path = image
            self._open_image_file()

        try:
            self.image_size = os.fstat(self._f_image.fileno()).st_size
        except IOError as err:
            raise Error("cannot get information about file '%s': %s" %
                        (self._f_image.name, err))

        try:
            self.block_size = get_block_size(self._f_image)
        except IOError as err:
            raise Error("cannot get block size for '%s': %s" %
                        (self._image_path, err))

        self.blocks_cnt = self.image_size + self.block_size - 1
        self.blocks_cnt /= self.block_size

        try:
            self._f_image.flush()
        except IOError as err:
            raise Error("cannot flush image file '%s': %s" %
                        (self._image_path, err))

        try:
            os.fsync(self._f_image.fileno()),
        except OSError as err:
            raise Error("cannot synchronize image file '%s': %s " %
                        (self._image_path, err.strerror))

        self._log.debug("opened image \"%s\"" % self._image_path)
        self._log.debug("block size %d, blocks count %d, image size %d" %
                        (self.block_size, self.blocks_cnt, self.image_size))