2

我从Net::SFTPRuby (ruby 1.9.3p194) 中得到了一些意想不到的行为。

变体 #1失败。它启动一个 SFTP 块并用于session.exec!运行 shell 命令。

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

变体 #2成功。它启动一个 SSH 块并用于sftp.upload!复制文件。

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

任何想法或解释将不胜感激。

这是变体 #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'
4

1 回答 1

1

这是我为解决此问题而编写的代码:

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
于 2014-09-23T23:02:36.997 回答