Sha256: 15eff24a603df7557b95a7479712f01ebff21caabdd4ff46755fc3acb4423245

Contents?: true

Size: 1.99 KB

Versions: 167

Compression:

Stored size: 1.99 KB

Contents

require 'net/ssh/loggable'
require 'net/sftp/operations/file'

module Net; module SFTP; module Operations

  # 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.
  class FileFactory
    # The SFTP session object that drives this file factory.
    attr_reader :sftp

    # Create a new instance on top of the given SFTP session instance.
    def initialize(sftp)
      @sftp = sftp
    end

    # :call-seq:
    #   open(name, flags="r", mode=nil) -> file
    #   open(name, flags="r", mode=nil) { |file| ... }
    #
    # 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
    def open(name, flags="r", mode=nil, &block)
      handle = sftp.open!(name, flags, :permissions => mode)
      file = Operations::File.new(sftp, handle)

      if block_given?
        begin
          yield file
        ensure
          file.close
        end
      else
        return file
      end
    end

    # Returns +true+ if the argument refers to a directory on the remote host.
    def directory?(path)
      sftp.lstat!(path).directory?
    end
  end

end; end; end

Version data entries

167 entries across 129 versions & 10 rubygems

Version Path
net-sftp-backports-4.0.8.backports lib/net/sftp/operations/file_factory.rb
net-sftp-backports-4.0.7.backports lib/net/sftp/operations/file_factory.rb
net-sftp-backports-4.0.6.backports lib/net/sftp/operations/file_factory.rb
net-sftp-backports-4.0.5.backports lib/net/sftp/operations/file_factory.rb
net-sftp-backports-4.0.4.backports lib/net/sftp/operations/file_factory.rb
net-sftp-backports-4.0.3.backports lib/net/sftp/operations/file_factory.rb
net-sftp-backports-4.0.2.backports lib/net/sftp/operations/file_factory.rb
net-sftp-backports-4.0.1.backports lib/net/sftp/operations/file_factory.rb
net-sftp-backports-4.0.0.backports lib/net/sftp/operations/file_factory.rb
net-sftp-4.0.0 lib/net/sftp/operations/file_factory.rb
net-sftp-4.0.0.rc1 lib/net/sftp/operations/file_factory.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/net-sftp-3.0.0/lib/net/sftp/operations/file_factory.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/net-sftp-3.0.0/lib/net/sftp/operations/file_factory.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/net-sftp-3.0.0/lib/net/sftp/operations/file_factory.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/net-sftp-3.0.0/lib/net/sftp/operations/file_factory.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/net-sftp-3.0.0/lib/net/sftp/operations/file_factory.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/net-sftp-3.0.0/lib/net/sftp/operations/file_factory.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/net-sftp-2.1.2/lib/net/sftp/operations/file_factory.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/net-sftp-2.1.2/lib/net/sftp/operations/file_factory.rb
net-sftp-3.0.0 lib/net/sftp/operations/file_factory.rb