Class: Archive::Tar::Minitar::Command::CommandList

Inherits:
CommandPattern show all
Defined in:
lib/archive/tar/minitar/command.rb

Instance Method Summary collapse

Methods inherited from CommandPattern

<<, [], #[], add, command, command?, default_ioe

Instance Method Details

#altnameObject



622
623
624
# File 'lib/archive/tar/minitar/command.rb', line 622

def altname
  "ls"
end

#call(args, opts = {}, ioe = {}) ⇒ Object



634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'lib/archive/tar/minitar/command.rb', line 634

def call(args, opts = {}, ioe = {})
  argv    = []
  output  = nil
  dest    = "."
  files   = []
  opts[:field] = "name"

  while (arg = args.shift)
    case arg
    when '--sort', '-S'
      opts[:sort]   = true
      opts[:field]  = args.shift
    when '--reverse', '-R'
      opts[:reverse] = true
      opts[:sort]    = true
    when '--uncompress', '-z'
      opts[:uncompress] = true
    when '-l'
      opts[:verbose] = true
    else
      argv << arg
    end
  end

  if argv.size < 1
    ioe[:output] << "Not enough arguments.\n\n"
    CommandPattern["help"][["list"]]
    return 255
  end

  input = argv.shift
  if '-' == input
    opts[:name] = "STDIN"
    input = ioe[:input]
  else
    opts[:name] = input
    input = File.open(input, "rb")
  end

  if opts[:name] =~ /\.tar\.gz$|\.tgz$/ or opts[:uncompress]
    input = Zlib::GzipReader.new(input)
  end

  files << argv.to_a
  files.flatten!

  if opts[:verbose] or opts[:progress]
    format  = "%10s %4d %8s %8s %8d %12s %s"
    datefmt = "%b %d  %Y"
    timefmt = "%b %d %H:%M"
    fields  = %w(permissions inodes user group size date fullname)
  else
    format  = "%s"
    fields  = %w(fullname)
  end

  opts[:field] = opts[:field].intern
  opts[:field] = :full_name if opts[:field] == :name

  output = []

  Archive::Tar::Minitar::Input.open(input) do |inp|
    today = Time.now
    oneyear = Time.mktime(today.year - 1, today.month, today.day)
    inp.each do |entry|
      value = format % fields.map do |ff|
        case ff
        when "permissions"
          s = entry.directory? ? "d" : "-"
          s << modestr(entry.mode / 0100)
          s << modestr(entry.mode / 0010)
          s << modestr(entry.mode)
        when "inodes"
          entry.size / 512
        when "user"
          entry.uname || entry.uid || 0
        when "group"
          entry.gname || entry.gid || 0
        when "size"
          entry.size
        when "date"
          if Time.at(entry.mtime) > (oneyear)
            Time.at(entry.mtime).strftime(timefmt)
          else
            Time.at(entry.mtime).strftime(datefmt)
          end
        when "fullname"
          entry.full_name
        end
      end

      if opts[:sort]
        output << [entry.send(opts[:field]), value]
      else
        ioe[:output] << value << "\n"
      end

    end
  end

  if opts[:sort]
    output = output.sort { |a, b| a[0] <=> b[0] }
    if opts[:reverse]
      output.reverse_each { |oo| ioe[:output] << oo[1] << "\n" }
    else
      output.each { |oo| ioe[:output] << oo[1] << "\n" }
    end
  end

  0
end

#helpObject



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# File 'lib/archive/tar/minitar/command.rb', line 746

def help
  help = <<-EOH
minitar list [OPTIONS] <tarfile|-> [<file>+]

Lists files in an existing tarfile. If the tarfile is named .tar.gz or
.tgz, then it will be uncompressed automatically. If the tarfile is "-",
then it will be read from standard input (stdin) so that minitar may be
piped.

If --verbose or --progress is specified, then the file list will be
similar to that produced by the Unix command "ls -l".

list Options:
--uncompress, -z      Uncompresses the tarfile with gzip.
--sort [<FIELD>], -S  Sorts the list of files by the specified
                      field. The sort defaults to the filename.
--reverse, -R         Reverses the sort.
-l                    Lists the files in detail.

Sort Fields:
  name, mtime, size

  EOH
end

#modestr(mode) ⇒ Object



626
627
628
629
630
631
632
# File 'lib/archive/tar/minitar/command.rb', line 626

def modestr(mode)
  s = "---"
  s[0] = ?r if (mode & 4) == 4
  s[1] = ?w if (mode & 2) == 2
  s[2] = ?x if (mode & 1) == 1
  s
end

#nameObject



618
619
620
# File 'lib/archive/tar/minitar/command.rb', line 618

def name
  "list"
end