Ceb3004fcd7431660b8e16a91ca89b53

I'm trying to clear a remote directory via sftp. I'm new to ruby and net-sftp so it wouldn't surprise me if there is a more elegant or built in way to do this. Also, it feels like there might be a faster way. I've extended the net::sftp::session in the following way. Thoughts?

require 'net/sftp'

class Net::SFTP::Session
  def rm_r!(path)
    self.dir.entries(path).each do |entry|
      next if entry.name == '.' or entry.name == '..'
			
      child_path = File.join(path, entry.name)
			
      if self.stat!(child_path).directory?
        self.rm_r!(child_path) 
      else
        puts "remove! #{child_path}"
        self.remove!(child_path)
      end
    end

    puts "rmdir! #{path}"
    self.rmdir!(path)
  end
	
  def clear!(path)
    puts "clear! #{path}"
    self.dir.entries(path).each do |entry|
      self.rm_r!(File.join(path, entry.name))
    end
  end
end

Refactorings

No refactoring yet !

3e54d4042d90094f2a6adba06865974e

Seanbo

September 3, 2009, September 03, 2009 23:03, permalink

No rating. Login to rate!

Why not just exec an rsync from the shell and send the command over an ssh tunnel? I realize the point is to refactor the code, but why write a pure ruby script that could potentially be broken by changes to net/sftp? This assumes you've done a key exchange between the local and remote machines...

`rsync --delete -a /path/to/some/emptydir/ <remoteserver>:<user>/remote/dir/to/clear/`

Your refactoring





Format Copy from initial code

or Cancel