class VdlDisk

Constants

MIN_SECTORS_TO_CACHE

Attributes

flags[R]
handle[R]
info[R]
path[R]
sectorSize[R]
timeStamp[R]

Public Class Methods

new(conn_obj, path, flags) click to toggle source
# File lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb, line 180
def initialize(conn_obj, path, flags)
  @time_stamp = Time.now
  logger.debug { "VdlDisk.new <#{object_id}>: opening #{path}" }
  @connection = conn_obj
  @handle = VixDiskLibApi.open(@connection.vdl_connection, path, flags)
  @path = path
  @flags = flags
  @sectorSize = FFI::VixDiskLib::API::VIXDISKLIB_SECTOR_SIZE
  @info = VixDiskLibApi.get_info(@handle)
  @handle_lock = Sync.new
  @cache = @cache_range = nil

  @num_sectors = @info[:capacity]
  @num_bytes   = @num_sectors * @sectorSize
  @vddk        = conn_obj.vddk
end

Public Instance Methods

bread(start_sector, num_sectors) click to toggle source
# File lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb, line 212
def bread(start_sector, num_sectors)
  @vddk.running = true
  @handle_lock.sync_lock(:SH) if (unlock = !@handle_lock.sync_locked?)

  raise VixDiskLibError, "VdlDisk.bread: disk is not open" unless @handle
  return nil if start_sector >= @num_sectors
  num_sectors = @num_sectors - start_sector if (start_sector + num_sectors) > @num_sectors

  return VixDiskLibApi.read(@handle, start_sector, num_sectors)
ensure
  @handle_lock.sync_unlock if unlock
end
breadCached(start_sector, num_sectors) click to toggle source
# File lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb, line 239
def breadCached(start_sector, num_sectors)
  @vddk.running = true
  if @cache_range.nil? ||
     !@cache_range.include?(start_sector) ||
     !@cache_range.include?(start_sector + num_sectors - 1)
    sectors_to_read = [MIN_SECTORS_TO_CACHE, num_sectors].max
    @cache        = bread(start_sector, sectors_to_read)
    sectors_read   = @cache.length / @sectorSize
    end_sector     = start_sector + sectors_read - 1
    @cache_range   = Range.new(start_sector, end_sector)
  end

  sector_offset = start_sector - @cache_range.first
  buffer_offset = sector_offset * @sectorSize
  length       = num_sectors * @sectorSize

  @cache[buffer_offset, length]
end
bwrite(start_sector, num_sectors, buf) click to toggle source
# File lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb, line 225
def bwrite(start_sector, num_sectors, buf)
  @vddk.running = true
  @handle_lock.sync_lock(:SH) if (unlock = !@handle_lock.sync_locked?)

  raise VixDiskLibError, "VdlDisk.bwrite: disk is not open" unless @handle
  return nil if start_sector >= @num_sectors
  num_sectors = @num_sectors - start_sector if (start_sector + num_sectors) > @num_sectors

  VixDiskLibApi.write(@handle, start_sector, num_sectors, buf)
  return num_sectors
ensure
  @handle_lock.sync_unlock if unlock
end
close() click to toggle source
# File lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb, line 197
def close
  logger.debug { "VdlDisk.close <#{ssId}>: closing #{@path}" }
  @vddk.running = true
  @handle_lock.synchronize(:EX) do
    if !@handle
      logger.debug { "VdlDisk.close: #{@path} not open" }
    else
      @connection.__close_disk__(self)
      @handle = nil
      @cache      = nil
      @cache_range = nil
    end
  end
end
read(pos, len) click to toggle source
# File lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb, line 258
def read(pos, len)
  @vddk.running = true
  @handle_lock.synchronize(:SH) do
    raise VixDiskLibError, "VdlDisk.read: disk is not open" unless @handle

    return nil if pos >= @num_bytes
    len = @num_bytes - pos if (pos + len) > @num_bytes

    start_sector, start_offset = pos.divmod(@sectorSize)
    end_sector = (pos + len - 1) / @sectorSize
    num_sector = end_sector - start_sector + 1

    r_buf = breadCached(start_sector, num_sector)
    return r_buf[start_offset, len]
  end
end
ssId() click to toggle source
# File lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb, line 293
def ssId
  object_id
end
write(pos, buf, len) click to toggle source
# File lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb, line 275
def write(pos, buf, len)
  @vddk.running = true
  @handle_lock.synchronize(:SH) do
    raise VixDiskLibError, "VdlDisk.write: disk is not open" unless @handle

    return nil if pos >= @num_bytes
    len = @num_bytes - pos if (pos + len) > @num_bytes

    start_sector, start_offset = pos.divmod(@sectorSize)
    end_sector = (pos + len - 1) / @sectorSize
    num_sector = end_sector - start_sector + 1
    r_buf = bread(start_sector, num_sector)
    r_buf[start_offset, len] = buf[0, len]
    bwrite(start_sector, num_sector, r_buf)
    return len
  end
end