lib/junos-ez/utils/fs.rb
### -------------------------------------------------------------    
## ls - provides directory listing of files/subdirs.  if 
## directory is nil, then the current working directory
## is used.  The following options (opts) are supported
##
## :format => [:text, :xml, :hash], default = :hash
## :recurse => true     - recursive listing thru subdirs
## :detail => true      - file details, on if :recurse
### -------------------------------------------------------------    

provides directory listing of files/subdirs.  if
directory is nil, then the current working directory
is used.  The following options (opts) are supported


provides directory listing of files / subdirs . if directory is nil then the current working directory is used . The following options ( opts ) are supported
def ls( *args )
    directory = nil
    opts = {}
    case args.count
    when 1
      if args[0].kind_of? Hash
        opts = args[0]
      else
        directory = args[0]
      end
    when 2
      directory = args[0]
      opts = args[1]      
    end
    args = {}
    args[:path] = directory if directory
    args[:recursive] = true if opts[:recurse]
    args[:detail] = true if opts[:detail]      
    args.delete(:detail) if( args[:detail] and args[:recursive])
    outf = { :format => 'text' } if opts[:format] == :text
    got = @ndev.rpc.file_list( args, outf )
    return nil unless got
    return got.text if opts[:format] == :text
    return got if opts[:format] == :xml
    collect_detail = args[:detail] || args[:recursive]
    ls_hash = {}
    got.xpath('directory').each do |dir|
      dir_name = dir.xpath('directory-name').text.strip
      dir_hash = {}
      dir_hash[:fileblocks] = dir.xpath('total-file-blocks').text.to_i
      files_info = dir.xpath('file-information')
      dir_hash[:files] = {}       
      dir_hash[:dirs] = {}        
      files_info.each do |file|
        f_name = file.xpath('file-name').text.strip
        f_h = {}                                  
        if file.xpath('file-directory')[0]
          dir_hash[:dirs][f_name] = f_h
        else
          dir_hash[:files][f_name] = f_h    
        end
        next unless collect_detail
        f_h[:owner] = file.xpath('file-owner').text.strip
        f_h[:group] = file.xpath('file-group').text.strip
        f_h[:links] = file.xpath('file-links').text.to_i
        f_h[:size] = file.xpath('file-size').text.to_i
        xml_when_item(file.xpath('file-symlink-target')) { |i|
          f_h[:symlink] = i.text.strip
        }
        fp = file.xpath('file-permissions')[0]
        f_h[:permissions_text] = fp.attribute('format').value
        f_h[:permissions] = fp.text.to_i
        fd = file.xpath('file-date')[0]
        f_h[:date] = fd.attribute('format').value
        f_h[:date_epoc] = fd.text.to_i
      end 
      ls_hash[ dir_name ] = dir_hash        
    end 
    return nil if ls_hash.empty?
    ls_hash
  end
def ls ( * args ) directory = nil opts = { } case args . count when 1 if args [ 0 ] . kind_of? Hash opts = args [ 0 ] else directory = args [ 0 ] end when 2 directory = args [ 0 ] opts = args [ 1 ] end args = { } args [ :path ] = directory if directory args [ :recursive ] = true if opts [ :recurse ] args [ :detail ] = true if opts [ :detail ] args . delete ( :detail ) if ( args [ :detail ] and args [ :recursive ] ) outf = { :format => 'text' } if opts [ :format ] == :text got = @ndev . rpc . file_list ( args , outf ) return nil unless got return got . text if opts [ :format ] == :text return got if opts [ :format ] == :xml collect_detail = args [ :detail ] || args [ :recursive ] ls_hash = { } got . xpath ( 'directory' ) . each do | dir | dir_name = dir . xpath ( 'directory-name' ) . text . strip dir_hash = { } dir_hash [ :fileblocks ] = dir . xpath ( 'total-file-blocks' ) . text . to_i files_info = dir . xpath ( 'file-information' ) dir_hash [ :files ] = { } dir_hash [ :dirs ] = { } files_info . each do | file | f_name = file . xpath ( 'file-name' ) . text . strip f_h = { } if file . xpath ( 'file-directory' ) [ 0 ] dir_hash [ :dirs ] [ f_name ] = f_h else dir_hash [ :files ] [ f_name ] = f_h end next unless collect_detail f_h [ :owner ] = file . xpath ( 'file-owner' ) . text . strip f_h [ :group ] = file . xpath ( 'file-group' ) . text . strip f_h [ :links ] = file . xpath ( 'file-links' ) . text . to_i f_h [ :size ] = file . xpath ( 'file-size' ) . text . to_i xml_when_item ( file . xpath ( 'file-symlink-target' ) ) { | i | f_h [ :symlink ] = i . text . strip } fp = file . xpath ( 'file-permissions' ) [ 0 ] f_h [ :permissions_text ] = fp . attribute ( 'format' ) . value f_h [ :permissions ] = fp . text . to_i fd = file . xpath ( 'file-date' ) [ 0 ] f_h [ :date ] = fd . attribute ( 'format' ) . value f_h [ :date_epoc ] = fd . text . to_i end ls_hash [ dir_name ] = dir_hash end return nil if ls_hash . empty? ls_hash end
ls - provides directory listing of files/subdirs.
ls - provides directory listing of files / subdirs .
# args are the RPC arguments ...
 # RPC output format, default is XML
 # if we're here, then we need to conver the output 
 # to a Hash.  Joy!
 # sub-directories
 # each directory file
 # each directory

args
<not_specific>