Module: VirtFS::ThinIODelegatorMethods

Includes:
ThinIODelegatorMethodsBufferio
Included in:
ThinFileDelegator
Defined in:
lib/virtfs/thin_io_delegator_methods.rb

Overview

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.

Constant Summary

Constants included from ThinIODelegatorMethodsBufferio

VirtFS::ThinIODelegatorMethodsBufferio::MAX_CHAR_LEN, VirtFS::ThinIODelegatorMethodsBufferio::MIN_READ_BUF_SZ

Instance Attribute Summary

Attributes included from ThinIODelegatorMethodsBufferio

#end_byte_addr, #fs_io_obj, #min_read_buf_sz, #read_buffer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ThinIODelegatorMethodsBufferio

#<<, #bytes, #chars, #each, #each_byte, #each_char, #each_codepoint, #each_common, #getbyte, #getc, #gets, #lineno, #lineno=, #lines, #print, #printf, #putc, #puts, #read, #readbyte, #readchar, #readline, #readlines, #sync, #sync=, #ungetbyte, #ungetc, #write

Class Method Details

.finalize(obj) ⇒ Object



59
60
61
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 59

def self.finalize(obj)
  proc { obj.close }
end

Instance Method Details

#autoclose=(bool) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# 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?Boolean

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 74

def autoclose?
  file_open
  @autoclose
end

#binmodeObject



79
80
81
82
83
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 79

def binmode
  file_open
  @parsed_args.binmode
  self
end

#binmode?Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 85

def binmode?
  file_open
  @parsed_args.binmode?
end

#closeObject



90
91
92
93
94
95
96
97
# 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) ⇒ Object



104
105
106
# 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?Boolean

Returns:

  • (Boolean)


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

def close_on_exec?
  file_open
  @fs_io_obj.close_on_exec?
end

#close_readObject

Raises:

  • (IOError)


108
109
110
111
112
113
# 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_writeObject

Raises:

  • (IOError)


115
116
117
118
119
120
# 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?Boolean

Returns:

  • (Boolean)


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

def closed?
  @parsed_args.closed?
end

#disable_finalizerObject



55
56
57
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 55

def disable_finalizer
  # XXX
end

#enable_finalizerObject



51
52
53
# 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

#eofObject Also known as: eof?



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

def eof
  file_open && for_reading
  @seek_pos > @end_byte_addr
end

#external_encodingObject



132
133
134
135
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 132

def external_encoding
  file_open
  @parsed_args.external_encoding
end

#fcntl(cms, arg) ⇒ Object



137
138
139
140
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 137

def fcntl(cms, arg)
  file_open
  @fs_io_obj.fcntl(cms, arg)
end

#fdatasyncObject



142
143
144
145
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 142

def fdatasync
  file_open
  @fs_io_obj.fdatasync
end

#filenoObject Also known as: to_i



147
148
149
150
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 147

def fileno
  file_open
  @fs_io_obj.fileno
end

#flushObject



153
154
155
156
157
158
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 153

def flush
  file_open
  @io_buffer.flush
  @fs_io_obj.flush
  self
end

#fsyncObject



160
161
162
163
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 160

def fsync
  file_open
  @fs_io_obj.fsync
end

#initialize(fs_io_obj, parsed_args) ⇒ Object

Instance methods



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

#internal_encodingObject



165
166
167
168
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 165

def internal_encoding
  file_open
  @parsed_args.internal_encoding
end

#ioctl(cmd, arg) ⇒ Object



170
171
172
173
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 170

def ioctl(cmd, arg)
  file_open
  @fs_io_obj.ioctl(cmd, arg)
end

#isattyObject Also known as: tty?



175
176
177
178
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 175

def isatty
  file_open
  @fs_io_obj.isatty
end

#pidObject



181
182
183
184
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 181

def pid
  file_open
  @fs_io_obj.pid
end

#posObject Also known as: tell



186
187
188
189
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 186

def pos
  file_open
  @seek_pos
end

#pos=(p) ⇒ Object

Raises:

  • (SystemCallError)


192
193
194
195
196
# 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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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 = "") ⇒ Object



198
199
200
201
# 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) ⇒ Object

Raises:

  • (ArgumentError)


203
204
205
206
207
208
209
210
211
212
# 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

#rewindObject



214
215
216
217
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 214

def rewind
  file_open
  @seek_pos = 0
end

#seek(offset, whence = IO::SEEK_SET) ⇒ Object



219
220
221
222
223
# 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) ⇒ Object



225
226
227
228
229
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 225

def set_encoding(*args)
  file_open
  @parsed_args.set_encoding(*args)
  self
end

#statObject



231
232
233
234
# 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) ⇒ Object



236
237
238
239
240
241
242
# 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) ⇒ Object

Raises:

  • (SystemCallError)


244
245
246
247
248
249
250
251
252
253
254
# 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) ⇒ Object



256
257
258
259
260
261
# 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

#to_ioObject



263
264
265
# File 'lib/virtfs/thin_io_delegator_methods.rb', line 263

def to_io
  self
end

#write_nonblock(buf) ⇒ Object



267
268
269
270
# 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