Skip to content

Instantly share code, notes, and snippets.

@justinwyer
Created November 10, 2011 13:24
Show Gist options
  • Save justinwyer/1354842 to your computer and use it in GitHub Desktop.
Save justinwyer/1354842 to your computer and use it in GitHub Desktop.
def write(self, fob, VixDiskLibSectorType offset, VixDiskLibSectorType length):
"""
Writes a sector range.
:param offset: Absolute offset.
:param nblocks: Number of blocks to read.
:param buff: np.ndarray of bytes
Note: SECTORS_PER_BLOCK = DEFAULT_BLOCK_SIZE (1048576 or 1MB) / VIXDISKLIB_SECTOR_SIZE (512)
SECTORS_PER_BLOCK = 2048 sectors
1 block = 1048576 bytes or 1MB
"""
cdef VixDiskLibSectorType position = offset
cdef uint64 buffer_length = SECTORS_PER_BLOCK
while True:
if (position + buffer_length) > (offset + length):
buffer_length = (offset + length) - position
nbytes = buffer_length * VIXDISKLIB_SECTOR_SIZE
log.debug("Writing %ld sectors to position %ld" % (buffer_length, position))
if self.buff == None:
self.buff = np.empty(nbytes, dtype=DTYPE)
if self.buff.size != nbytes:
log.debug("Resizing buffer to %d" % nbytes)
self.buff.resize(nbytes)
pydata = fob.read(nbytes)
self.buff.data = <char *>pydata
vix_error = VixDiskLib_Write(self.handle, position, buffer_length, <uint8 *>self.buff.data)
if vix_error != VIX_OK:
self._handleError("Error writing to the disk: %s" % self.vmdk_path, vix_error)
position += buffer_length
if not (position < (offset + length)):
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment