class Archive::Tar::Minitar::Command::CommandCreate

Public Instance Methods

altname() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 363
def altname
  "cr"
end
call(args, opts = {}, ioe = {}) click to toggle source
# File lib/archive/tar/minitar/command.rb, line 367
def call(args, opts = {}, ioe = {})
  argv    = []

  while (arg = args.shift)
    case arg
    when '--compress', '-z'
      opts[:compress] = true
    else
      argv << arg
    end
  end

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

  output = argv.shift
  if '-' == output
    opts[:name] = "STDOUT"
    output = ioe[:output]
    opts[:output] = ioe[:error]
  else
    opts[:name] = output
    output = File.open(output, "wb")
    opts[:output] = ioe[:output]
  end

  if opts[:name] =~ /\.tar\.gz$|\.tgz$/ or opts[:compress]
    output = Zlib::GzipWriter.new(output)
  end

  files = []
  if argv.include?("--")
      # Read stdin for the list of files.
    files = ""
    files << ioe[:input].read while not ioe[:input].eof?
    files = files.split(/\r\n|\n|\r/)
    args.delete("--")
  end

  files << argv.to_a
  files.flatten!

  if opts[:verbose]
    watcher = lambda do |action, name, stats|
      opts[:output] << "#{name}\n" if action == :dir or action == :file_done
    end
    finisher = lambda { opts[:output] << "\n" }
  elsif opts[:progress]
    progress = ProgressBar.new(opts[:name], 1)
    watcher = lambda do |action, name, stats|
      case action
      when :file_start, :dir
        progress.title = File.basename(name)
        if action == :dir
          progress.total += 1
          progress.inc
        else
          progress.total += stats[:size]
        end
      when :file_progress
        progress.inc(stats[:currinc])
      end
    end
    finisher = lambda do
      progress.title = opts[:name]
      progress.finish
    end
  else
    watcher = nil
    finisher = lambda { }
  end

  Archive::Tar::Minitar.pack(files, output, &watcher)
  finisher.call
  0
ensure
  output.close if output and not output.closed?
end
help() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 449
    def help
      help = <<-EOH
    minitar create [OPTIONS] <tarfile|-> <file|directory|-->+

Creates a new tarfile. If the tarfile is named .tar.gz or .tgz, then it
will be compressed automatically. If the tarfile is "-", then it will be
output to standard output (stdout) so that minitar may be piped.

The files or directories that will be packed into the tarfile are
specified after the name of the tarfile itself. Directories will be
processed recursively. If the token "--" is found in the list of files
to be packed, additional filenames will be read from standard input
(stdin). If any file is not found, the packaging will be halted.

create Options:
    --compress, -z  Compresses the tarfile with gzip.

      EOH
    end
name() click to toggle source
# File lib/archive/tar/minitar/command.rb, line 359
def name
  "create"
end