Class: VirtFS::VIO

Inherits:
Object
  • Object
show all
Includes:
IOInstanceDelegate
Defined in:
lib/virtfs/v_io.rb

Overview

VirtFS IO representation - implements the core Ruby IO methods, dispatching to underlying mounted VirtFS filesystems

Direct Known Subclasses

VFile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_obj) ⇒ VIO

VFile initializer

Parameters:

  • io_obj (VirtFS::FS::IO)

    handle to filesystem specific io obj



14
15
16
# File 'lib/virtfs/v_io.rb', line 14

def initialize(io_obj)
  __setobj__(io_obj)
end

Class Method Details

.binread(f, length = nil, offset = 0) ⇒ Array<Byte>

Read specified number of bytes from file

Parameters:

  • f (String)

    file to ready

  • length (Integer) (defaults to: nil)

    number of bytes to read

  • offset (Integer) (defaults to: 0)

    position to start reading at

Returns:

  • (Array<Byte>)

    bytes read from file (up to length size)



56
57
58
59
60
61
62
# File 'lib/virtfs/v_io.rb', line 56

def binread(f, length = nil, offset = 0)
  VFile.open(f, "rb") do |fobj|
    fobj.pos = offset
    return fobj.read unless length
    return fobj.read(length)
  end
end

.copy_stream(from, to, max_length = nil, offset = 0) ⇒ Object

Copy stream from source to destination

Parameters:

  • from (String)

    file stream to copy

  • to (String)

    file stream to copy to

  • max_length (Integer) (defaults to: nil)

    max number of bytes to copy

  • offset (Integer) (defaults to: 0)

    position to start coying from



72
73
74
75
76
77
78
79
# File 'lib/virtfs/v_io.rb', line 72

def copy_stream(from, to, max_length = nil, offset = 0) # rubocop:disable CyclomaticComplexity
  from_file = from.is_a?(VIO) ? from : VFile.open(from, "rb")
  to_file   = to.is_a?(VIO)   ? to   : VFile.open(to, "wb") # rubocop:disable SpaceAroundOperators
  return copy_from_to(from_file, to_file, max_length, offset)
ensure
  from_file.close unless from_file.nil? || from.is_a?(VIO)
  to_file.close   unless to_file.nil?   || to.is_a?(VIO)    # rubocop:disable SpaceAroundOperators
end

.foreach(portname, *args, &block) ⇒ Object

Invoke block for each file matching pattern

IO.foreach( portname, separator=$/ <, options> ) { | line | . . . } -> nil IO.foreach( portname, limit <, options> ) { | line | . . . } -> nil IO.foreach( portname, separator, limit <, options> ) { | line | . . . } -> nil



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/virtfs/v_io.rb', line 87

def foreach(portname, *args, &block)
  return VfsRealIO.foreach(portname, *args) unless filename?(portname)
  return to_enum(__method__, portname, *args) unless block_given?

  separator, limit, options = parse_args(args)

  VFile.open(portname, "r", options) do |fobj|
    fobj.each(separator, limit, &block)
  end
  nil
end

.new(integer_fd, mode = "r", hash_options = {}) ⇒ Object Also known as: for_fd

Instantiate IO instance.

Parameters:

  • integer_fd (Integer)

    file descriptor

  • mode (String) (defaults to: "r")

    mode to open IO instance

  • hash_options (defaults to: {})

    options to forward to IO initialiezr



145
146
147
148
149
150
151
152
153
154
# File 'lib/virtfs/v_io.rb', line 145

def new(integer_fd, mode = "r", hash_options = {})
  #
  # Directly instantiating an IO instance (not through File)
  # will return a standard IO object.
  #
  fs_obj = VfsRealIO.new(integer_fd, mode, hash_options)
  obj = allocate
  obj.send(:initialize, fs_obj)
  obj
end

.open(*args) ⇒ Object

Open IO Instance and invoke block w/ it before closing

See Also:

  • #new


160
161
162
163
164
165
166
167
168
# File 'lib/virtfs/v_io.rb', line 160

def open(*args)
  io_obj = new(*args) # IO.new or File.new
  return io_obj unless block_given?
  begin
    return yield(io_obj)
  ensure
    io_obj.close
  end
end

.pipe(*args, &block) ⇒ Object



99
100
101
102
103
# File 'lib/virtfs/v_io.rb', line 99

def pipe(*args, &block)
  # XXX - should wrap VfsRealIO objects in common delegator class
  # so is_a? and kind_of? will work with all IO objects.
  VfsRealIO.pipe(*args, &block) # TODO: wrap returned read and write IO
end

.popen(*args, &block) ⇒ Object



105
106
107
# File 'lib/virtfs/v_io.rb', line 105

def popen(*args, &block)
  VfsRealIO.popen(*args, &block) # TODO: wrap returned IO
end

.read(portname, *args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/virtfs/v_io.rb', line 109

def read(portname, *args)
  return VfsRealIO.read(portname, *args) unless filename?(portname)

  length, offset, options = length_offset_options(args)

  VFile.open(portname, "r", options) do |fobj|
    fobj.pos = offset
    return fobj.read unless length
    return fobj.read(length)
  end
end

.readlines(portname, *args) ⇒ Object



121
122
123
124
# File 'lib/virtfs/v_io.rb', line 121

def readlines(portname, *args)
  return VfsRealIO.readlines(portname, *args) unless filename?(portname)
  foreach(portname, *args).to_a
end

.select(*args) ⇒ Object



126
127
128
# File 'lib/virtfs/v_io.rb', line 126

def select(*args)
  VfsRealIO.select(*args)
end

.sysopen(*args) ⇒ Object



130
131
132
# File 'lib/virtfs/v_io.rb', line 130

def sysopen(*args)
  VfsRealIO.sysopen(*args)
end

.try_convert(obj) ⇒ Object



134
135
136
137
# File 'lib/virtfs/v_io.rb', line 134

def try_convert(obj)
  return nil unless obj.respond_to?(:to_io)
  obj.to_io # TODO: wrap?
end

Instance Method Details

#<<(obj) ⇒ Object

Some methods need to return the IO object. Methods in the delegator object can’t do that, so we intercept them and do it here.



23
24
25
26
# File 'lib/virtfs/v_io.rb', line 23

def <<(obj)
  super
  self
end

#binmodeObject



28
29
30
31
# File 'lib/virtfs/v_io.rb', line 28

def binmode
  super
  self
end

#reopen(*args) ⇒ Object



33
34
35
36
# File 'lib/virtfs/v_io.rb', line 33

def reopen(*args)
  __setobj__(super)
  self
end

#set_encoding(*args) ⇒ Object

rubocop:disable Style/AccessorMethodName



38
39
40
41
# File 'lib/virtfs/v_io.rb', line 38

def set_encoding(*args) # rubocop:disable Style/AccessorMethodName
  super
  self
end

#to_ioObject



43
44
45
# File 'lib/virtfs/v_io.rb', line 43

def to_io
  self
end