Net::SFTP: open_handle => null string

Does anyone know off the top of their head why the 'open_handle' method
might return a string?

require 'net/sftp'

Net::SFTP.start('remotehost1') do |sftp|
sftp.put_file(foo, foobar)
sftp.open_handle(foobar) do |handle|
puts handle.inspect # => "\000\000\000\000"
puts handle.class # => "String"
puts data.read # => Raises error: method not defined
end
end

It's not that the connection is bad, because the 'put_file' method works
okay, and the file gets uploaded to the remote host just fine.

Looking in the documentation leads me to believe that the call is right.
Taking a cursory look through the source suggests that the return should be
some object that delegates the IO operation to the Net::SFTP::Operation
subclasses--if I'm reading it correctly :slight_smile:

···

--
Lou

No, it is correct. open_handle returns an opaque value that represents a remote file (or directory) handle. It just happens to be implemented as a string. To use it, you call sftp.read and sftp.write, passing the handle to it. (You can look at the synchronous.rb file in the examples subdirectory of the net-sftp distro for more examples of how to use open-handle).

- Jamis

···

On Sep 29, 2005, at 12:57 PM, Louis J Scoras wrote:

Does anyone know off the top of their head why the 'open_handle' method
might return a string?

require 'net/sftp'

Net::SFTP.start('remotehost1') do |sftp|
sftp.put_file(foo, foobar)
sftp.open_handle(foobar) do |handle|
puts handle.inspect # => "\000\000\000\000"
puts handle.class # => "String"
puts data.read # => Raises error: method not defined
end

It's not that the connection is bad, because the 'put_file' method works
okay, and the file gets uploaded to the remote host just fine.

Looking in the documentation leads me to believe that the call is right.
Taking a cursory look through the source suggests that the return should be
some object that delegates the IO operation to the Net::SFTP::Operation
subclasses--if I'm reading it correctly :slight_smile:

--
Lou

Hrmm... I only assumed it was wrong because I got an error when I tried
sending read/write to the handle:

sftp_t.rb:8: undefined method `read' for "\000\000\000\000":String
(NoMethodError)
from sftp_t.rb:5:in `open_handle'
from sftp_t.rb:5
from sftp_t.rb:3:in `initialize'
from /usr/lib/ruby/1.8/net/sftp/session.rb:45:in `call'
from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:152:in `do_version'
from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:183:in `do_data'
from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:19:in `to_proc'
from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:103:in `call'
from /usr/lib/ruby/1.8/net/ssh/connection/channel.rb:496:in `callback'
from /usr/lib/ruby/1.8/net/ssh/connection/channel.rb:447:in `do_data'
from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:285:in `do_channel_data'
from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:151:in `process'
from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:134:in `loop'
from /usr/lib/ruby/1.8/net/ssh/session.rb:161:in `loop'
from /usr/lib/ruby/1.8/net/sftp/session.rb:66:in `initialize'
from /usr/lib/ruby/1.8/net/sftp.rb:29:in `start'

···

On 9/29/05, Jamis Buck <jamis@37signals.com> wrote:

No, it is correct. open_handle returns an opaque value that
represents a remote file (or directory) handle. It just happens to be
implemented as a string. To use it, you call sftp.read and
sftp.write, passing the handle to it. (You can look at the
synchronous.rb file in the examples subdirectory of the net-sftp
distro for more examples of how to use open-handle).

- Jamis

--
Lou

Yah, that would be problematic. :slight_smile: You have to send read/write to the sftp instance, and pass the handle to it. Very un-Rubyish, I know:

   sftp.open_handle("foo.txt") do |handle|
     data = sftp.read(handle)
   end

Please do take a look at the examples subdirectory. It will clear up a lot of questions about how to use these methods. Likewise, the Net::SFTP FAQ may be enlightening: http://net-ssh.rubyforge.org/sftp/faq.html\.

- Jamis

···

On Sep 29, 2005, at 1:48 PM, Louis J Scoras wrote:

On 9/29/05, Jamis Buck <jamis@37signals.com> wrote:

No, it is correct. open_handle returns an opaque value that
represents a remote file (or directory) handle. It just happens to be
implemented as a string. To use it, you call sftp.read and
sftp.write, passing the handle to it. (You can look at the
synchronous.rb file in the examples subdirectory of the net-sftp
distro for more examples of how to use open-handle).

- Jamis

Hrmm... I only assumed it was wrong because I got an error when I tried
sending read/write to the handle:

sftp_t.rb:8: undefined method `read' for "\000\000\000\000":String
(NoMethodError)
from sftp_t.rb:5:in `open_handle'
from sftp_t.rb:5
from sftp_t.rb:3:in `initialize'
from /usr/lib/ruby/1.8/net/sftp/session.rb:45:in `call'
from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:152:in `do_version'
from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:183:in `do_data'
from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:19:in `to_proc'
from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:103:in `call'
from /usr/lib/ruby/1.8/net/ssh/connection/channel.rb:496:in `callback'
from /usr/lib/ruby/1.8/net/ssh/connection/channel.rb:447:in `do_data'
from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:285:in `do_channel_data'
from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:151:in `process'
from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:134:in `loop'
from /usr/lib/ruby/1.8/net/ssh/session.rb:161:in `loop'
from /usr/lib/ruby/1.8/net/sftp/session.rb:66:in `initialize'
from /usr/lib/ruby/1.8/net/sftp.rb:29:in `start'

Doh! You know, I did stare right at the example, closed it, and then still
made the mistake. I need a bigger screen, hehe.

Thanks for the help. The library's very nice, by the way.

···

On 9/29/05, Jamis Buck <jamis@37signals.com> wrote:

Yah, that would be problematic. :slight_smile: You have to send read/write to the
sftp instance, and pass the handle to it. Very un-Rubyish, I know:

sftp.open_handle("foo.txt") do |handle|
data = sftp.read(handle)
end

Please do take a look at the examples subdirectory. It will clear up
a lot of questions about how to use these methods. Likewise, the
Net::SFTP FAQ may be enlightening: http://net-ssh.rubyforge.org/sftp/
faq.html.

- Jamis

--
Lou