Sha256: 12822809f625fb74b5d739647ea6e0dd83f4eb69373abf06131adfd29a9014e9

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

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.

          @basename = match[21].match(/^(.+)(?:\s+\->.+)?$/)[0].strip
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
net-ftp-list-0.4 lib/net/ftp/list/unix.rb
net-ftp-list-0.5 lib/net/ftp/list/unix.rb