2

I'm getting some unexpected behavior from Net::SFTP in Ruby (ruby 1.9.3p194).

Variant #1 fails. It starts an SFTP block and uses session.exec! to run a shell command.

Net::SFTP.start(...) do |sftp|
  sftp.session.exec! "mkdir -p ..."  # Fails here.
  sftp.upload!(...)
end

Variant #2 succeeds. It starts an SSH block and uses sftp.upload! to copy a file.

Net::SSH.start(...)  do |ssh|
  ssh.exec! "mkdir -p ..."
  ssh.sftp.upload!(...)
end

Any ideas or explanations would be appreciated.

Here's the stack trace from Variant #1:

can't add a new key into hash during iteration

net-ssh-2.6.3/lib/net/ssh/connection/session.rb:296:in `[]='
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:296:in `open_channel'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:320:in `exec'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:354:in `exec!'
dor-services-3.20.0/lib/dor/services/digital_stacks_service.rb:28:in `block in transfer_to_document_store'
net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `call'
net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `block in do_version'
net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `each'
net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `do_version'
net-sftp-2.0.5/lib/net/sftp/session.rb:909:in `when_channel_polled'
net-ssh-2.6.3/lib/net/ssh/connection/channel.rb:311:in `call'
net-ssh-2.6.3/lib/net/ssh/connection/channel.rb:311:in `process'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:214:in `block in preprocess'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:214:in `each'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:214:in `preprocess'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:197:in `process'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:161:in `block in loop'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:161:in `loop'
net-ssh-2.6.3/lib/net/ssh/connection/session.rb:161:in `loop'
net-sftp-2.0.5/lib/net/sftp/session.rb:802:in `loop'
net-sftp-2.0.5/lib/net/sftp/session.rb:787:in `connect!'
net-sftp-2.0.5/lib/net/sftp.rb:32:in `start'
FMc
  • 41,963
  • 13
  • 79
  • 132
  • Why are you using a sub-shell to `mkdir`, when Net::SFTP supports [`mkdir(...)`](http://net-ssh.rubyforge.org/sftp/v2/api/classes/Net/SFTP/Session.html#M000143) and `mkdir!(...)`? – the Tin Man Jan 24 '13 at 01:30
  • @theTinMan I wondered the same thing when I first saw the code. After some experimenting, my understanding is that `mkdir!(...)` does not do what `mkdir -p` does -- create intermediate directories if they don't exist. In any case, Variant #1 should work ... but it doesn't. – FMc Jan 24 '13 at 16:02
  • Correct, it doesn't do the intermediate directories that `-p` will do but that would be a simple step to do. – the Tin Man Jan 24 '13 at 16:05

1 Answers1

1

Here's the code I wrote to work-around this problem:

def mkdir_p(sftp, path)
  memo = "/"
  # [0..-2] to skip the filename
  path.split("/")[0..-2].each do |dir|
    next if dir.empty?
    if sftp.dir.entries(memo).map { |entry| entry.name }.include?(dir)
      memo += "/#{dir}"
    else
      memo += "/#{dir}"
      puts "Creating the following directory: #{memo}"
      sftp.mkdir!(memo)
    end
  end
end
Powers
  • 18,150
  • 10
  • 103
  • 108