Class: VdlDisk

Inherits:
Object
  • Object
show all
Includes:
DRb::DRbUndumped
Defined in:
lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb

Overview

class VdlConnection

Constant Summary collapse

MIN_SECTORS_TO_CACHE =
64

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn_obj, path, flags) ⇒ VdlDisk

Returns a new instance of VdlDisk.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 181

def initialize(conn_obj, path, flags)
  @time_stamp = Time.now
  $vim_log.debug "VdlDisk.new <#{object_id}>: opening #{path}" if $vim_log && $vim_log.debug?
  @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

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



177
178
179
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 177

def flags
  @flags
end

#handleObject (readonly)

Returns the value of attribute handle.



177
178
179
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 177

def handle
  @handle
end

#infoObject (readonly)

Returns the value of attribute info.



177
178
179
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 177

def info
  @info
end

#pathObject (readonly)

Returns the value of attribute path.



177
178
179
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 177

def path
  @path
end

#sectorSizeObject (readonly)

Returns the value of attribute sectorSize.



177
178
179
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 177

def sectorSize
  @sectorSize
end

#timeStampObject (readonly)

Returns the value of attribute timeStamp.



177
178
179
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 177

def timeStamp
  @timeStamp
end

Instance Method Details

#bread(start_sector, num_sectors) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 213

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) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 240

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) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 226

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

#closeObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 198

def close
  $vim_log.debug "VdlDisk.close <#{ssId}>: closing #{@path}" if $vim_log && $vim_log.debug?
  @vddk.running = true
  @handle_lock.synchronize(:EX) do
    if !@handle
      $vim_log.debug "VdlDisk.close: #{@path} not open" if $vim_log && $vim_log.debug?
    else
      @connection.__close_disk__(self)
      @handle = nil
      @cache      = nil
      @cache_range = nil
    end
  end
end

#read(pos, len) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 259

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

#ssIdObject



294
295
296
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 294

def ssId
  object_id
end

#write(pos, buf, len) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/VMwareWebService/VixDiskLib/vdl_wrapper.rb', line 276

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