Class: Kitchen::Tgz

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/tgz.rb

Overview

Represents a Tar-Gzip file, allowing multiple files to be combined into a single file. This is useful for transmitting a large number of files across high-latency networks.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tgz_file_name = nil) ⇒ Tgz

Create a new Tgz object, in preparation for adding files to the tar-gzip archive.

Parameters:

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

    The output file name, in tar-gzipped format. If not specified, a temporary file name will be generated.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kitchen/tgz.rb', line 47

def initialize(tgz_file_name = nil)
  if tgz_file_name
    # user-provided output file name
    @tgz_file = File.open(tgz_file_name, "wb+")
    @path = tgz_file_name
  else
    # auto-generated output file name
    @tgz_file = Tempfile.new("tgz")
    @tgz_file.binmode
    @path = @tgz_file.path
  end

  #
  # Intermediate file for writing the 'tar' content (which will then be
  # gzipped into the output 'tgz' file)
  #
  @tar_file = Tempfile.new("tar")
  @tar_file.binmode
  @tar_writer = Gem::Package::TarWriter.new(@tar_file)
end

Instance Attribute Details

#pathString (readonly)

Returns the file system path of the generated tar-gzip file.

Returns:

  • (String)

    the file system path of the generated tar-gzip file.



38
39
40
# File 'lib/kitchen/tgz.rb', line 38

def path
  @path
end

Class Method Details

.original_size(file_name) ⇒ Integer

Return the original size of the uncompressed file.

Parameters:

  • file_name (String)

    name of the compressed file, in .tgz format.

Returns:

  • (Integer)

    the original (uncompressed) file’s size

Raises:



117
118
119
120
121
122
123
124
125
126
# File 'lib/kitchen/tgz.rb', line 117

def original_size(file_name)
  File.open(file_name, "r") do |file|
    # the first three bytes of a gzip file must be 0x1f, 0x8b, 0x08
    fail unless (file.readbyte == 0x1f) && (file.readbyte == 0x8b) &&
        (file.readbyte == 0x08)
    return original_file_length_field(file)
  end
rescue
  raise GzipFormatError, "Gzip file could not be opened, or has invalid format: #{file_name}"
end

Instance Method Details

#add_files(dir, files) ⇒ Object

Add a set of files or directories into the Tgz archive. Directories will be traversed, with all files and subdirectories being added (symlinks are ignored).

For example:

tgz.add_files('/home/me/my_dir', ['fileA', 'dirA/fileB'])

Parameters:

  • dir (String)

    the directory containing the files/sub-dirs to archive.

  • files (Array<String>)

    the files/sub-directories to be added, specified relative to the containing directory (dir).



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kitchen/tgz.rb', line 80

def add_files(dir, files)
  files.each do |file_name|
    full_path = "#{dir}/#{file_name}"
    next if File.symlink?(full_path)
    if File.directory?(full_path)
      add_directory_to_tar(dir, file_name, full_path)
    else
      add_file_to_tar(full_path, file_name)
    end
  end
end

#closeObject

Close the Tgz object, generating the tgz file (from the tar file), and ensuring that it’s flushed to disk.



96
97
98
99
100
101
102
103
104
# File 'lib/kitchen/tgz.rb', line 96

def close
  # ensure tar_writer flushes everything to our temporary 'tar' file.
  @tar_writer.close

  # proceed to convert the 'tar' file into a 'tgz' file.
  @tar_file.rewind
  create_tgz_file(@tar_file, @tgz_file)
  @tar_file.close
end