Represents a single SFTP session, running atop an SSH session.
- close
- close_channel
- connect
- do_attrs
- do_data
- do_handle
- do_name
- do_status
- loop
- method_missing
- new
- register
- respond_to?
- state
- support?
[RW] | status | The status of the last synchronously executed operation. This is either nil, or an object that responds to :code, :message, and :language. |
Create a new SFTP session on top of the given SSH session.
[ show source ]
# File lib/net/sftp/session.rb, line 32 32: def initialize( session ) 33: @session = session 34: @log = @session.registry.log_for( "sftp.session" ) 35: 36: @session.registry.namespace_define :sftp do |ns| 37: ns.require "net/sftp/protocol/services", "Net::SFTP::Protocol" 38: ns.require "net/sftp/operations/services", "Net::SFTP::Operations" 39: 40: # register a reference to myself for other services to be able to 41: # access me. 42: ns.session( :pipeline => [] ) { self } 43: 44: @driver = ns.protocol.driver 45: @driver.on_open do |d| 46: d.on_attrs &method( :do_attrs ) 47: d.on_data &method( :do_data ) 48: d.on_handle &method( :do_handle ) 49: d.on_name &method( :do_name ) 50: d.on_status &method( :do_status ) 51: 52: if block_given? 53: begin 54: yield self 55: ensure 56: d.close 57: end 58: end 59: end 60: 61: @operations = ns.operations 62: end 63: 64: @requests = Hash.new 65: 66: @session.loop if block_given? 67: end
Closes the underlying SSH connection.
[ show source ]
# File lib/net/sftp/session.rb, line 87 87: def close 88: @session.close 89: end
Closes the SFTP connection, but leaves the SSH connection open.
[ show source ]
# File lib/net/sftp/session.rb, line 82 82: def close_channel 83: @driver.close 84: end
Waits for the underlying driver to reach a state of :open (or :closed). This makes it easier to use the SFTP routines synchronously without using the block form:
sftp = Net::SFTP::Session.new( ssh_session ) sftp.connect puts sftp.realpath( "." )
Without the call to connect, the call to realpath would fail because the SFTP protocol has not yet been negotiated and no underlying driver has been selected.
If no block is given, it teturns self, so it can be chained easily to other method calls. If a block is given, the session is yielded to the block as soon as the driver successfully reports it’s state as open, with the session’s channel being closed automatically when the block finishes.
require 'net/ssh' require 'net/sftp' Net::SSH.start( 'localhost' ) do |session| session.sftp.connect do |sftp| puts sftp.realpath( "." ) end end
[ show source ]
# File lib/net/sftp/session.rb, line 131 131: def connect 132: @session.loop do 133: @driver.state != :open && 134: @driver.state != :closed 135: end 136: if @driver.state == :open && block_given? 137: begin 138: yield self 139: ensure 140: close_channel 141: end 142: else 143: self 144: end 145: end
[ show source ]
# File lib/net/sftp/session.rb, line 179 179: def do_attrs( driver, id, attributes ) 180: @requests.delete( id ).do_attrs( attributes ) 181: end
[ show source ]
# File lib/net/sftp/session.rb, line 155 155: def do_data( driver, id, data ) 156: @requests.delete( id ).do_data( data ) 157: end
[ show source ]
# File lib/net/sftp/session.rb, line 167 167: def do_handle( driver, id, handle ) 168: @requests.delete( id ).do_handle( handle ) 169: end
[ show source ]
# File lib/net/sftp/session.rb, line 173 173: def do_name( driver, id, items ) 174: @requests.delete( id ).do_name( items ) 175: end
[ show source ]
# File lib/net/sftp/session.rb, line 161 161: def do_status( driver, id, code, message, language ) 162: @requests.delete( id ).do_status( code, message, language ) 163: end
Delegates to Net::SSH::Session#loop. Causes the underlying SSH connection to process events as long as the given block returns true, or (if no block is given) until there are no more open channels.
[ show source ]
# File lib/net/sftp/session.rb, line 101 101: def loop( &block ) 102: @session.loop( &block ) 103: end
Delegates the message to the operation that has been registered with the given name. If no such operation exists, control is passed to the superclass’ implementation of method_missing.
[ show source ]
# File lib/net/sftp/session.rb, line 192 192: def method_missing( sym, *args, &block ) 193: if @operations.has_key?( sym ) 194: @operations[ sym ].execute( *args, &block ) 195: else 196: super 197: end 198: end
Registers the given handler with the given request id. This is used internally by the operations, so that the session knows who to delegate a response to that has been received from the server.
[ show source ]
# File lib/net/sftp/session.rb, line 94 94: def register( id, handler ) 95: @requests[ id ] = handler 96: end
Returns true if the object responds to the given symbol, or if there is an operation registered under the given symbol.
[ show source ]
# File lib/net/sftp/session.rb, line 202 202: def respond_to?( sym ) 203: super || @operations.has_key?( sym ) 204: end
Return the state of the SFTP connection. (See Net::SFTP::Protocol::Driver#state.)
[ show source ]
# File lib/net/sftp/session.rb, line 77 77: def state 78: @driver.state 79: end
Returns true if the underlying driver responds to the given symbol. This can be used by clients to determine whether the SFTP protocol version in use supports a particular operation.
[ show source ]
# File lib/net/sftp/session.rb, line 209 209: def support?( sym ) 210: @driver.respond_to?( sym ) 211: end