Class: Net::SFTP::Operations::Dir

### Home Classes Methods

### In Files

- lib/net/sftp/operations/dir.rb

### Parent

Object

### Methods

- ::new
- #[]
- #entries
- #foreach
- #glob

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Net
- Net::SFTP
- Net::SFTP::Constants
- Net::SFTP::Constants::ACE
- Net::SFTP::Constants::ACE::Flag
- Net::SFTP::Constants::ACE::Mask
- Net::SFTP::Constants::ACE::Type
- Net::SFTP::Constants::LockTypes
- Net::SFTP::Constants::OpenFlags
- Net::SFTP::Constants::OpenFlags::FV1
- Net::SFTP::Constants::OpenFlags::FV5
- Net::SFTP::Constants::OpenFlags::FV6
- Net::SFTP::Constants::PacketTypes
- Net::SFTP::Constants::RenameFlags
- Net::SFTP::Constants::StatusCodes
- Net::SFTP::Exception
- Net::SFTP::Operations
- Net::SFTP::Operations::Dir
- Net::SFTP::Operations::Download
- Net::SFTP::Operations::File
- Net::SFTP::Operations::FileFactory
- Net::SFTP::Operations::Upload
- Net::SFTP::Packet
- Net::SFTP::Protocol
- Net::SFTP::Protocol::Base
- Net::SFTP::Protocol::V01
- Net::SFTP::Protocol::V01::Attributes
- Net::SFTP::Protocol::V01::Base
- Net::SFTP::Protocol::V01::Name
- Net::SFTP::Protocol::V02
- Net::SFTP::Protocol::V02::Base
- Net::SFTP::Protocol::V03
- Net::SFTP::Protocol::V03::Base
- Net::SFTP::Protocol::V04
- Net::SFTP::Protocol::V04::Attributes
- Net::SFTP::Protocol::V04::Base
- Net::SFTP::Protocol::V04::Name
- Net::SFTP::Protocol::V05
- Net::SFTP::Protocol::V05::Base
- Net::SFTP::Protocol::V06
- Net::SFTP::Protocol::V06::Attributes
- Net::SFTP::Protocol::V06::Base
- Net::SFTP::Request
- Net::SFTP::Response
- Net::SFTP::Session
- Net::SFTP::StatusException
- Net::SFTP::Version
- Net::SSH
- Net::SSH::Connection
- Net::SSH::Connection::Session

No matching classes.

# Net::SFTP::Operations::Dir

A convenience class for working with remote directories. It provides methods for searching and enumerating directory entries, similarly to the standard ::Dir class.

    sftp.dir.foreach("/remote/path") do |entry|
      puts entry.name
    end

    p sftp.dir.entries("/remote/path").map { |e| e.name }

    sftp.dir.glob("/remote/path", "**/*.rb") do |entry|
      puts entry.name
    end

### Attributes

sftp[R]

The SFTP session object that drives this directory factory.

### Public Class Methods

new(sftp) click to toggle source

Create a new instance on top of the given SFTP session instance.

    # File lib/net/sftp/operations/dir.rb, line 23
    def initialize(sftp)
      @sftp = sftp
    end

### Public Instance Methods

[](path, pattern) click to toggle source

Identical to calling glob with a `flags` parameter of 0 and no block. Simply returns the matched entries as an array.

    # File lib/net/sftp/operations/dir.rb, line 88
    def [](path, pattern)
      glob(path, pattern, 0)
    end

entries(path) click to toggle source

Returns an array of Name objects representing the items in the given remote directory, `path`.

    # File lib/net/sftp/operations/dir.rb, line 42
    def entries(path)
      results = []
      foreach(path) { |entry| results << entry }
      return results
    end

foreach(path) click to toggle source

Calls the block once for each entry in the named directory on the remote server. Yields a Name object to the block, rather than merely the name of the entry.

    # File lib/net/sftp/operations/dir.rb, line 30
    def foreach(path)
      handle = sftp.opendir!(path)
      while entries = sftp.readdir!(handle)
        entries.each { |entry| yield entry }
      end
      return nil
    ensure
      sftp.close!(handle) if handle
    end

glob(path, pattern, flags=0) click to toggle source

Works as ::Dir.glob, matching (possibly recursively) all directory entries under `path` against `pattern`. If a block is given, matches will be yielded to the block as they are found; otherwise, they will be returned in an array when the method finishes.

Because working over an SFTP connection is always going to be slower than working purely locally, don't expect this method to perform with the same level of alacrity that ::Dir.glob does; it will work best for shallow directory hierarchies with relatively few directories, though it should be able to handle modest numbers of files in each directory.

    # File lib/net/sftp/operations/dir.rb, line 58
    def glob(path, pattern, flags=0)
      flags |= ::File::FNM_PATHNAME
      path = path.chop if path[-1,1] == "/"

      results = [] unless block_given?
      queue = entries(path).reject { |e| e.name == "." || e.name == ".." }
      while queue.any?
        entry = queue.shift

        if entry.directory? && !%(. ..).include?(::File.basename(entry.name))
          queue += entries("#{path}/#{entry.name}").map do |e|
            e.name.replace("#{entry.name}/#{e.name}")
            e
          end
        end

        if ::File.fnmatch(pattern, entry.name, flags)
          if block_given?
            yield entry
          else
            results << entry
          end
        end
      end

      return results unless block_given?
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.