Module: FFI::Libfuse::IO

Defined in:
lib/ffi/libfuse/io.rb

Overview

Helpers for reading/writing to io like objects

Class Method Summary collapse

Class Method Details

.read(io, size, offset = nil) ⇒ String

Helper to convert a ruby object to size bytes required by FuseOperations#read

Parameters:

  • io (#pread, #seek & #read, #to_s)

    the source to read from, either...

    • an IO like object via :pread(size, offset) or :seek(offset) then :read(size)
    • or the String from :to_s
  • size (Integer)
  • offset (Integer, nil) (defaults to: nil)

    if nil the io is assumed to be already positioned

Returns:

  • (String)

    the extracted data



18
19
20
21
22
23
24
25
26
27
# File 'lib/ffi/libfuse/io.rb', line 18

def read(io, size, offset = nil)
  return io.pread(size, offset) if offset && io.respond_to?(:pread)

  if (offset ? %i[seek read] : %i[read]).all? { |m| io.respond_to?(m) }
    io.seek(offset) if offset
    return io.read(size)
  end

  io.to_s[offset || 0, size] || ''
end

.write(io, data, offset = nil) ⇒ Integer

Helper to write date to IO or String like objects for use with #FuseOperations#write

Parameters:

  • io (#pwrite, #seek & #write, #[]=)

    an object that accepts String data via...

    • ruby io.pwrite(data, offset)
    • ruby io.seek(offset) ; io.write(data)
    • ruby io[offset, data.size] = data
  • data (String)
  • offset (nil, Integer) (defaults to: nil)

    if not nil start start writing at this position in io

Returns:

  • (Integer)

    number of bytes written

Raises:

  • (Errno::EBADF)

    if io does not support the requisite methods



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ffi/libfuse/io.rb', line 40

def write(io, data, offset = nil)
  if offset && io.respond_to?(:pwrite)
    io.pwrite(data, offset)
  elsif (offset ? %i[seek write] : %i[write]).all? { |m| io.respond_to?(m) }
    io.seek(offset) if offset
    io.write(data)
  elsif io.respond_to?(:[]=) # eg String
    io[offset || 0, data.size] = data
    data.size
  else
    raise "cannot :pwrite or :write to #{io}", Errno::EBADF
  end
end