Net::SFTP: open_handle => null string

L

Louis J Scoras

------=_Part_4105_13322455.1128020217396
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 # =3D> "\000\000\000\000"
puts handle.class # =3D> "String"
puts data.read # =3D> 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 :)
 
J

Jamis Buck

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
 
L

Louis J Scoras

------=_Part_4427_29827639.1128023334136
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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'
 
J

Jamis Buck

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'

Yah, that would be problematic. :) 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
 
L

Louis J Scoras

------=_Part_4479_24619978.1128024015704
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Yah, that would be problematic. :) 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 =3D 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
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top