module VirtFS::ThinIODelegatorMethods

Dispatches File calls to/from VirtFS and the 'Thin' subsystem

IO objects are not instantiated directly, because IO.new delegates to VfsRealIO. These instances methods are only called through File objects.

Public Class Methods

finalize(obj) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 59
def self.finalize(obj)
  proc { obj.close }
end
new(fs_io_obj, parsed_args) click to toggle source

Instance methods

# File lib/virtfs/thin_io_delegator_methods.rb, line 16
def initialize(fs_io_obj, parsed_args)
  @fs_io_obj       = fs_io_obj
  @size            = @fs_io_obj.size
  @start_byte_addr = 0
  @end_byte_addr   = @size - 1

  @parsed_args = parsed_args
  @seek_pos    = @parsed_args.append? ? @size : 0 # The current byte position within the file.

  @binary_encoding   = Encoding.find("ASCII-8BIT")
  @autoclose         = @parsed_args.autoclose?

  bio_init

  enable_finalizer if @autoclose
end

Public Instance Methods

autoclose=(bool) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 63
def autoclose=(bool)
  file_open
  initial_val = @autoclose
  if (@autoclose = bool ? true : false)
    enable_finalizer if initial_val == false
  else
    disable_finalizer if initial_val == true
  end
  bool
end
autoclose?() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 74
def autoclose?
  file_open
  @autoclose
end
binmode() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 79
def binmode
  file_open
  @parsed_args.binmode
  self
end
binmode?() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 85
def binmode?
  file_open
  @parsed_args.binmode?
end
close() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 90
def close
  file_open
  @io_buffer.flush
  @fs_io_obj.close
  @parsed_args.close
  @autoclose = false
  nil
end
close_on_exec=(bool) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 104
def close_on_exec=(bool)
  @fs_io_obj.close_on_exec = bool
end
close_on_exec?() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 99
def close_on_exec?
  file_open
  @fs_io_obj.close_on_exec?
end
close_read() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 108
def close_read
  file_open
  raise IOError, "closing non-duplex IO for reading" unless @parsed_args.rdonly?
  @parsed_args.close_read
  @fs_io_obj.close_read
end
close_write() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 115
def close_write
  file_open
  raise IOError, "closing non-duplex IO for writing" unless @parsed_args.wronly?
  @parsed_args.close_write
  @fs_io_obj.close_write
end
closed?() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 122
def closed?
  @parsed_args.closed?
end
disable_finalizer() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 55
def disable_finalizer
  # XXX
end
enable_finalizer() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 51
def enable_finalizer
  # XXX ObjectSpace.define_finalizer(self, self.class.finalize(fs_file_obj))
end
eof() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 126
def eof
  file_open && for_reading
  @seek_pos > @end_byte_addr
end
Also aliased as: eof?
eof?()
Alias for: eof
external_encoding() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 132
def external_encoding
  file_open
  @parsed_args.external_encoding
end
fcntl(cms, arg) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 137
def fcntl(cms, arg)
  file_open
  @fs_io_obj.fcntl(cms, arg)
end
fdatasync() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 142
def fdatasync
  file_open
  @fs_io_obj.fdatasync
end
fileno() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 147
def fileno
  file_open
  @fs_io_obj.fileno
end
Also aliased as: to_i
flush() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 153
def flush
  file_open
  @io_buffer.flush
  @fs_io_obj.flush
  self
end
fsync() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 160
def fsync
  file_open
  @fs_io_obj.fsync
end
internal_encoding() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 165
def internal_encoding
  file_open
  @parsed_args.internal_encoding
end
ioctl(cmd, arg) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 170
def ioctl(cmd, arg)
  file_open
  @fs_io_obj.ioctl(cmd, arg)
end
isatty() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 175
def isatty
  file_open
  @fs_io_obj.isatty
end
Also aliased as: tty?
pid() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 181
def pid
  file_open
  @fs_io_obj.pid
end
pos() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 186
def pos
  file_open
  @seek_pos
end
Also aliased as: tell
pos=(p) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 192
def pos=(p)
  file_open
  raise SystemCallError.new(p.to_s, Errno::EINVAL::Errno) if p < 0
  @seek_pos = p
end
re_initialize(io_obj) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 33
def re_initialize(io_obj)
  close
  io_obj.flush

  @fs_io_obj   = io_obj.instance_variable_get(:@fs_io_obj)
  @parsed_args = io_obj.instance_variable_get(:@parsed_args)
  @seek_pos    = io_obj.instance_variable_get(:@seek_pos)

  @size            = @fs_io_obj.size
  @start_byte_addr = 0
  @end_byte_addr   = @size - 1
  @autoclose       = @parsed_args.autoclose?

  bio_reinit(io_obj)

  enable_finalizer if @autoclose
end
readpartial(limit, result = "") click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 198
def readpartial(limit, result = "")
  file_open && for_reading
  @fs_io_obj.readpartial(limit, result)
end
reopen(*args) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 203
def reopen(*args)
  raise ArgumentError, "wrong number of arguments (#{args.length} for 1..2)" if args.empty? || args.length > 2
  if args[0].respond_to?(:to_str)
    VFile.new(*args).__getobj__
  elsif args[0].respond_to?(:__getobj__)
    args[0].__getobj__.dup
  else
    args[0]
  end
end
rewind() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 214
def rewind
  file_open
  @seek_pos = 0
end
seek(offset, whence = IO::SEEK_SET) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 219
def seek(offset, whence = IO::SEEK_SET)
  file_open
  sysseek(offset, whence)
  0
end
set_encoding(*args) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 225
def set_encoding(*args)
  file_open
  @parsed_args.set_encoding(*args)
  self
end
stat() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 231
def stat
  file_open
  @fs_io_obj.stat # XXX wrap in VirtFS::Stat
end
sysread(len, buffer = nil) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 236
def sysread(len, buffer = nil)
  file_open && for_reading && not_at_eof
  rv = @fs_io_obj.raw_read(@seek_pos, len)
  @seek_pos += rv.bytesize
  buffer.replace(rv) unless buffer.nil?
  rv
end
sysseek(offset, whence = IO::SEEK_SET) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 244
def sysseek(offset, whence = IO::SEEK_SET)
  file_open
  new_pos  = case whence
             when IO::SEEK_CUR then @seek_pos + offset
             when IO::SEEK_END then @size + offset
             when IO::SEEK_SET then @start_byte_addr + offset
             end

  raise SystemCallError.new(offset.to_s, Errno::EINVAL::Errno) if new_pos < 0
  @seek_pos = new_pos
end
syswrite(buf) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 256
def syswrite(buf)
  file_open && for_writing
  rv = @fs_io_obj.raw_write(@seek_pos, buf)
  update_write_pos(rv)
  rv
end
tell()
Alias for: pos
to_i()
Alias for: fileno
to_io() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 263
def to_io
  self
end
tty?()
Alias for: isatty
write_nonblock(buf) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 267
def write_nonblock(buf)
  file_open && for_writing
  @fs_io_obj.write_nonblock(buf)
end

Private Instance Methods

file_open() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 274
def file_open
  raise IOError, "closed stream" if closed?
  true
end
for_reading() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 279
def for_reading
  raise IOError, "not opened for reading" unless @parsed_args.read?
  true
end
for_writing() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 284
def for_writing
  raise IOError, "not opened for writing" unless @parsed_args.write?
  true
end
not_at_eof() click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 289
def not_at_eof
  raise EOFError, "end of file reached" if eof?
  true
end
update_write_pos(len) click to toggle source
# File lib/virtfs/thin_io_delegator_methods.rb, line 294
def update_write_pos(len)
  @seek_pos += len
  return if @seek_pos <= @size
  @size = @seek_pos
  @end_byte_addr = @seek_pos - 1
end