Sha256: 11ddc3488b96b933c7cb33c967f9b4683f27b056259c0d84c31064e6b0f41bd3

Contents?: true

Size: 1.9 KB

Versions: 29

Compression:

Stored size: 1.9 KB

Contents

require 'time'
require 'net/ftp/list/parser'

module Net
  class FTP
    module List

      # Parse Unix like FTP LIST entries.
      #
      # == MATCHES
      # 
      #   drwxr-xr-x   4 steve    group       4096 Dec 10 20:23 etc
      #   -rw-r--r--   1 root     other        531 Jan 29 03:26 README.txt
      #
      # == SYNOPSIS
      #
      #   entry = Net::FTP::List::Unix.new('drwxr-xr-x   4 steve    group       4096 Dec 10 20:23 etc')
      #   entry.dir?     # => true
      #   entry.basename # => 'etc'
      class Unix < Parser

        # Stolen straight from the ASF's commons Java FTP LIST parser library.
        # http://svn.apache.org/repos/asf/commons/proper/net/trunk/src/java/org/apache/commons/net/ftp/
        REGEXP = %r{
          ([bcdlfmpSs-])
          (((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-]))((r|-)(w|-)([xsStTL-])))\+?\s+
          (\d+)\s+
          (\S+)\s+
          (?:(\S+(?:\s\S+)*)\s+)?
          (\d+)\s+
          ((?:\d+[-/]\d+[-/]\d+)|(?:\S+\s+\S+))\s+
          (\d+(?::\d+)?)\s+
          (\S*)(\s*.*)
        }x

        # Parse a Unix like FTP LIST entries.
        def initialize(raw)
          super(raw)
          match = REGEXP.match(raw.strip) or raise ParserError
          
          case match[1]
            when /d/    then @dir = true
            when /l/    then @symlink = true
            when /[f-]/ then @file = true
            when /[bc]/ then # Do nothing with devices for now.
            else ParserError 'Unknown LIST entry type.'
          end

          # TODO: Permissions, users, groups, date/time.
          @mtime = Time.parse("#{match[19]} #{match[20]}")

          @basename = match[21].strip

          # filenames with spaces will end up in the last match
          @basename += match[22] unless match[22].nil?

          # strip the symlink stuff we don't care about
          @basename.sub!(/\s+\->.+$/, '') if @symlink
        end
      end

    end
  end
end

Version data entries

29 entries across 29 versions & 2 rubygems

Version Path
redcar-dev-0.12.1dev-java plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-dev-0.12.0dev-java plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.11 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.11.0dev plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.10 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.9.2 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.9.1 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.9.0 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.8.1 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.8 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.7 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.6.1 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.6 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.6.1dev plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.5.1 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.5 plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.5.6dev plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.5.5dev plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.5.4dev plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb
redcar-0.5.3dev plugins/project/vendor/net-ftp-list/lib/net/ftp/list/unix.rb