Class: Ronin::PostEx::RemoteFile

Inherits:
Resource
  • Object
show all
Includes:
FakeIO
Defined in:
lib/ronin/post_ex/remote_file.rb,
lib/ronin/post_ex/remote_file/stat.rb

Overview

  • file_close(fd : Integer)
  • fs_readfile(path : String) -> String | nil
  • fs_stat(path : String) => Hash[Symbol, Object] | nil

Defined Under Namespace

Classes: Stat

Constant Summary collapse

SEEK_SET =

Seeks from beginning of file.

File::SEEK_SET
SEEK_CUR =

Seeks from current position.

File::SEEK_CUR
SEEK_END =

Seeks from end of file.

File::SEEK_END
SEEK_DATA =

Seeks to next data.

(File::SEEK_DATA) && File::SEEK_DATA) || 3
SEEK_HOLE =

Seeks to next hole.

(File::SEEK_HOLE) && File::SEEK_HOLE) || 4
WHENCE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Mapping of SEEK_* constants to their String values.

{
  SEEK_SET  => 'SEEK_SET',
  SEEK_CUR  => 'SEEK_CUR',
  SEEK_END  => 'SEEK_END',
  SEEK_DATA => 'SEEK_DATA',
  SEEK_HOLE => 'SEEK_HOLE'
}

Instance Attribute Summary

Attributes inherited from Resource

#session

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#interact, #supports, #supports?

Constructor Details

#initialize(session, path, mode = 'r') ⇒ RemoteFile

Note:

This method may use the file_open method, if it is defined by Ronin::PostEx::Resource#session.

Creates a new remote controlled File object.

Parameters:

  • session (Sessions::Session#file_read, Sessions::Session#file_write)

    The session object that defines the file_read and file_write methods.

  • path (String)

    The path of the remote file.

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

    The mode to open the file in.



71
72
73
74
75
76
77
78
# File 'lib/ronin/post_ex/remote_file.rb', line 71

def initialize(session,path,mode='r')
  @session = session

  @path = path.to_s
  @mode = mode.to_s

  super()
end

Class Method Details

.open(session, path) {|file| ... } ⇒ RemoteFile?

Opens a file.

Parameters:

Yields:

  • (file)

    The given block will be passed the newly created file object. When the block has returned, the File object will be closed.

Yield Parameters:

Returns:

  • (RemoteFile, nil)

    If no block is given, then the newly opened remote file object will be returned. If a block was given, then nil will be returned.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ronin/post_ex/remote_file.rb', line 100

def self.open(session,path)
  io = new(session,path)

  if block_given?
    yield(io)
    io.close
    return
  else
    return io
  end
end

Instance Method Details

#closenil

Flushes the file before closing it.

Returns:

  • (nil)


312
313
314
315
# File 'lib/ronin/post_ex/remote_file.rb', line 312

def close
  flush if @mode.include?('w')
  super()
end

#fcntl(command, argument) ⇒ Integer

Note:

This method requires the file_fnctl API method.

Executes a low-level command to control or query the file stream.

Parameters:

  • command (String, Array<Integer>)

    The FCNTL command.

  • argument (Object)

    Argument of the command.

Returns:

  • (Integer)

    The return value from the fcntl.

Raises:

  • (NotImplementedError)

    The API object does not define file_fcntl.



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ronin/post_ex/remote_file.rb', line 240

def fcntl(command,argument)
  unless @session.respond_to?(:file_fcntl)
    raise(NotImplementedError,"#{@session.inspect} does not define file_fcntl")
  end

  if @fd == nil
    raise(RuntimeError,"file_ioctl requires a file-descriptor")
  end

  return @session.file_fcntl(@fd,command,argument)
end

#flushself

Note:

This method may use the file_flush API method, if it is defined

Flushes the file.

by Ronin::PostEx::Resource#session.

Returns:

  • (self)


299
300
301
302
303
304
305
# File 'lib/ronin/post_ex/remote_file.rb', line 299

def flush
  if @session.respond_to?(:file_flush)
    @session.file_flush
  end

  return self
end

#inspectString

Inspects the open file.

Returns:

  • (String)

    The inspected open file.



323
324
325
# File 'lib/ronin/post_ex/remote_file.rb', line 323

def inspect
  "#<#{self.class}:#{@path}>"
end

#ioctl(command, argument) ⇒ Integer

Note:

This method requires the file_ioctl API method.

Executes a low-level command to control or query the IO stream.

Parameters:

  • command (String, Array<Integer>)

    The IOCTL command.

  • argument (Object)

    Argument of the command.

Returns:

  • (Integer)

    The return value from the ioctl.

Raises:

  • (NotImplementedError)

    The API object does not define file_ioctl.

  • (RuntimeError)

    The file_ioctl method requires a file-descriptor.



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ronin/post_ex/remote_file.rb', line 210

def ioctl(command,argument)
  unless @session.respond_to?(:file_ioctl)
    raise(NotImplementedError,"#{@session.inspect} does not define file_ioctl")
  end

  if @fd == nil
    raise(RuntimeError,"file_ioctl requires a file-descriptor")
  end

  return @session.file_ioctl(@fd,command,argument)
end

#reopen(path) ⇒ RemoteFile

Note:

This method may use the file_close and file_open API methods, if they are defined by Ronin::PostEx::Resource#session.

Re-opens the file.

Parameters:

  • path (String)

    The new path for the file.

Returns:



266
267
268
269
270
271
# File 'lib/ronin/post_ex/remote_file.rb', line 266

def reopen(path)
  close

  @path = path.to_s
  return open
end

#seek(new_pos, whence = SEEK_SET) ⇒ Integer

Note:

This method may use the file_seek API method, if it is defined

Sets the position in the file to read.

by Ronin::PostEx::Resource#session.

Parameters:

  • new_pos (Integer)

    The new position to read from.

  • whence (Integer) (defaults to: SEEK_SET)

    The origin point to seek from.

Returns:

  • (Integer)

    The new position within the file.

Raises:

  • (ArgumentError)

    An invalid whence value was given.



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ronin/post_ex/remote_file.rb', line 156

def seek(new_pos,whence=SEEK_SET)
  clear_buffer!

  unless WHENCE.has_key?(whence)
    raise(ArgumentError,"invalid whence value: #{whence.inspect}")
  end

  if @session.respond_to?(:file_seek)
    @session.file_seek(@fd,new_pos,WHENCE[whence])
  end

  @pos = new_pos
end

#statStat

Note:

This method relies on the fs_stat API method.

The status information for the file.

Returns:

  • (Stat)

    The status information.



282
283
284
285
286
287
288
# File 'lib/ronin/post_ex/remote_file.rb', line 282

def stat
  if @fd
    Stat.new(@session, fd: @fd)
  else
    Stat.new(@session, path: @path)
  end
end

#tellInteger

Note:

This method may use the file_tell API method, if it is defined by Ronin::PostEx::Resource#session.

The current offset in the file.

Returns:

  • (Integer)

    The current offset in bytes.



181
182
183
184
185
186
187
# File 'lib/ronin/post_ex/remote_file.rb', line 181

def tell
  if @session.respond_to?(:file_tell)
    @pos = @session.file_tell(@fd)
  else
    @pos
  end
end