Class | Net::SFTP::Operations::FileFactory |
In: |
lib/net/sftp/operations/file_factory.rb
|
Parent: | Object |
A factory class for opening files and returning Operations::File instances that wrap the SFTP handles that represent them. This is a convenience class for use when working with files synchronously. Rather than relying on the programmer to provide callbacks that define a state machine that describes the behavior of the program, this class (and Operations::File) provide an interface where calls will block until they return, mimicking the IO class’ interface.
sftp | [R] | The SFTP session object that drives this file factory. |
Returns true if the argument refers to a directory on the remote host.
# File lib/net/sftp/operations/file_factory.rb, line 55 55: def directory?(path) 56: sftp.lstat!(path).directory? 57: end
Attempt to open a file on the remote server. The flags parameter accepts the same values as the standard Ruby ::File#open method. The mode parameter must be an integer describing the permissions to use if a new file is being created.
If a block is given, the new Operations::File instance will be yielded to it, and closed automatically when the block terminates. Otherwise the object will be returned, and it is the caller‘s responsibility to close the file.
sftp.file.open("/tmp/names.txt", "w") do |f| # ... end
# File lib/net/sftp/operations/file_factory.rb, line 39 39: def open(name, flags="r", mode=nil, &block) 40: handle = sftp.open!(name, flags, :permissions => mode) 41: file = Operations::File.new(sftp, handle) 42: 43: if block_given? 44: begin 45: yield file 46: ensure 47: file.close 48: end 49: else 50: return file 51: end 52: end