Class: Ronin::Support::Archive::Tar::Writer

Inherits:
Gem::Package::TarWriter
  • Object
show all
Defined in:
lib/ronin/support/archive/tar/writer.rb

Overview

Handles writing tar encoded archive data.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(io_or_buffer, mode: 'w') {|tar| ... } ⇒ Writer

Initializes the tar writer.

Examples:

Initializing with an IO object:

tar = Archive::Tar::Writer.new(io)

Initializing with a buffer:

buffer = ""
tar   = Archive::Tar::Writer.new(buffer)

Initializin with a buffer and append mode:

buffer = "foo"
tar   = Archive::Tar::Writer.new(buffer, mode: 'a')

Parameters:

  • io_or_buffer (IO, StringIO, String)

    The IO object or buffer to write to. If a String is given, then it will be wrapped in a StringIO object using the optional mode argument.

  • mode (String) (defaults to: 'w')

    The optional mode to initialize the StringIO object to wrap around the given buffer String.

Yields:

  • (tar)

    If a block is given, it will be passed the new tar writer object.

Yield Parameters:

  • tar (Writer)

    The tar writer object.

Returns:

  • (Writer)

    The gzip writer object.

Since:

  • 1.0.0



69
70
71
72
73
74
75
76
# File 'lib/ronin/support/archive/tar/writer.rb', line 69

def self.new(io_or_buffer, mode: 'w', &block)
  io = case io_or_buffer
       when String then StringIO.new(io_or_buffer,mode)
       else             io_or_buffer
       end

  return super(io,&block)
end

.open(path) {|tar| ... } ⇒ Writer

Opens the tar archive file for writing.

Parameters:

  • path (String)

    The path to the tar archive.

Yields:

  • (tar)

    If a block is given, then it will be passed the new tar writer object.

Yield Parameters:

  • tar (Writer)

    The newly created tar writer object.

Returns:

  • (Writer)

    If no block is given, than the tar writer object will be returned.

Since:

  • 1.0.0



94
95
96
97
98
99
100
101
102
# File 'lib/ronin/support/archive/tar/writer.rb', line 94

def self.open(path,&block)
  if block
    File.open(path,'wb') do |file|
      new(file,&block)
    end
  else
    return new(File.new(path,'wb'))
  end
end

Instance Method Details

#add_file(name, contents = nil, mode: 0644) {|file| ... } ⇒ self

Adds a file to the tar archive.

Parameters:

  • name (String)

    The name or relative path of the new file.

  • contents (String, nil) (defaults to: nil)

    The optional contents of the file.

  • mode (Integer) (defaults to: 0644)

    The permission mode for the new file.

Yields:

  • (file)

    If a block is given, it will be yielded an output stream for the file that can be written to.

Yield Parameters:

  • file (Gem::Package::TarWriter::RestrictedStream)

Returns:

  • (self)

See Also:

Since:

  • 1.0.0



126
127
128
129
130
131
132
133
134
# File 'lib/ronin/support/archive/tar/writer.rb', line 126

def add_file(name,contents=nil, mode: 0644, &block)
  if contents
    super(name,mode) do |io|
      io.write(contents)
    end
  else
    super(name,mode,&block)
  end
end

Adds a symlink to the tar archive.

Parameters:

  • name (String)

    The name or relative path of the new symlink.

  • target (String)

    The destination of the new symlink.

  • mode (Integer) (defaults to: 0777)

    The permission mode of the new symlink.

Since:

  • 1.0.0



174
175
176
# File 'lib/ronin/support/archive/tar/writer.rb', line 174

def add_symlink(name,target, mode: 0777)
  super(name,target,mode)
end

#allocate_file(name, size, mode: 0644) {|file| ... } ⇒ self

Adds a file, with the exact size, to the tar archive.

Parameters:

  • name (String)

    The name or relative path of the new file.

  • size (Integer)

    The size of the file in bytes.

  • mode (Integer) (defaults to: 0644)

    The permission mode for the new file.

Yields:

  • (file)

    If a block is given, it will be yielded an output stream for the file that can be written to.

Yield Parameters:

  • file (Gem::Package::TarWriter::BoundedStream)

Returns:

  • (self)

See Also:

Since:

  • 1.0.0



158
159
160
# File 'lib/ronin/support/archive/tar/writer.rb', line 158

def allocate_file(name,size, mode: 0644, &block)
  add_file_simple(name,mode,size,&block)
end

#mkdir(name, mode: 0755) ⇒ self

Adds a directory to the tar archive.

Parameters:

  • name (String)

    The name or relative path of the new directory.

  • mode (Integer) (defaults to: 0755)

    The permission mode of the new directory.

Returns:

  • (self)

Since:

  • 1.0.0



189
190
191
# File 'lib/ronin/support/archive/tar/writer.rb', line 189

def mkdir(name, mode: 0755)
  super(name,mode)
end