Module: Utils::Gzip Private

Defined in:
utils/gzip.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Helper functions for creating gzip files.

Constant Summary collapse

GZIP_BUFFER_SIZE =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

Apple's gzip also uses zlib so use the same buffer size here. https://github.com/apple-oss-distributions/file_cmds/blob/file_cmds-400/gzip/gzip.c#L147

T.let(64 * 1024, Integer)

Class Method Summary collapse

Class Method Details

.compress(*paths, reproducible: true, mtime: ENV["SOURCE_DATE_EPOCH"].to_i) ⇒ Array<Pathname>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • paths (String, Pathname)
  • reproducible (Boolean) (defaults to: true)
  • mtime (Integer, Time) (defaults to: ENV["SOURCE_DATE_EPOCH"].to_i)

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'utils/gzip.rb', line 54

def self.compress(*paths, reproducible: true, mtime: ENV["SOURCE_DATE_EPOCH"].to_i)
  if reproducible
    paths.map do |path|
      compress_with_options(path, mtime:)
    end
  else
    paths.map do |path|
      safe_system "gzip", path
      Pathname.new("#{path}.gz")
    end
  end
end

.compress_with_options(path, mtime: ENV["SOURCE_DATE_EPOCH"].to_i, orig_name: File.basename(path), output: "#{path}.gz") ⇒ Pathname

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • path (String, Pathname)
  • mtime (Integer, Time) (defaults to: ENV["SOURCE_DATE_EPOCH"].to_i)
  • orig_name (String) (defaults to: File.basename(path))
  • output (String, Pathname) (defaults to: "#{path}.gz")

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'utils/gzip.rb', line 19

def self.compress_with_options(path, mtime: ENV["SOURCE_DATE_EPOCH"].to_i, orig_name: File.basename(path),
                               output: "#{path}.gz")
  # Ideally, we would just set mtime = 0 if SOURCE_DATE_EPOCH is absent, but Ruby's
  # Zlib::GzipWriter does not properly handle the case of setting mtime = 0:
  # https://bugs.ruby-lang.org/issues/16285
  #
  # This was fixed in https://github.com/ruby/zlib/pull/10. Remove workaround
  # once we are using zlib gem version 1.1.0 or newer.
  if mtime.to_i.zero?
    odebug "Setting `mtime = 1` to avoid zlib gem bug when `mtime == 0`."
    mtime = 1
  end

  File.open(path, "rb") do |fp|
    odebug "Creating gzip file at #{output}"
    gz = Zlib::GzipWriter.open(output)
    gz.mtime = mtime
    gz.orig_name = orig_name
    gz.write(fp.read(GZIP_BUFFER_SIZE)) until fp.eof?
  ensure
    # GzipWriter should be closed in case of error as well
    gz.close
  end

  FileUtils.rm_f path
  Pathname.new(output)
end