Module: UnpackStrategy Private

Extended by:
T::Helpers, Utils::Output::Mixin
Includes:
SystemCommand::Mixin, Utils::Output::Mixin
Included in:
Air, Bzip2, Cab, Directory, Dmg, Dmg::Mount, Fossil, GenericUnar, Gzip, Lha, Lzip, Lzma, P7Zip, Pax, Rar, Tar, Uncompressed, Xar, Xz, Zip, Zstd
Defined in:
unpack_strategy.rb,
extend/os/mac/unpack_strategy/zip.rb,
unpack_strategy/xz.rb,
unpack_strategy/air.rb,
unpack_strategy/cab.rb,
unpack_strategy/cvs.rb,
unpack_strategy/dmg.rb,
unpack_strategy/git.rb,
unpack_strategy/jar.rb,
unpack_strategy/lha.rb,
unpack_strategy/otf.rb,
unpack_strategy/pax.rb,
unpack_strategy/pkg.rb,
unpack_strategy/rar.rb,
unpack_strategy/sit.rb,
unpack_strategy/tar.rb,
unpack_strategy/ttf.rb,
unpack_strategy/xar.rb,
unpack_strategy/zip.rb,
unpack_strategy/gzip.rb,
unpack_strategy/lzip.rb,
unpack_strategy/lzma.rb,
unpack_strategy/path.rb,
unpack_strategy/zstd.rb,
unpack_strategy/bzip2.rb,
unpack_strategy/p7zip.rb,
unpack_strategy/bazaar.rb,
unpack_strategy/fossil.rb,
unpack_strategy/compress.rb,
unpack_strategy/lua_rock.rb,
unpack_strategy/directory.rb,
unpack_strategy/mercurial.rb,
unpack_strategy/executable.rb,
unpack_strategy/subversion.rb,
unpack_strategy/generic_unar.rb,
unpack_strategy/uncompressed.rb,
unpack_strategy/microsoft_office_xml.rb,
unpack_strategy/self_extracting_executable.rb

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.

Defined Under Namespace

Modules: ClassMethods Classes: Air, Bazaar, Bzip2, Cab, Compress, Cvs, Directory, Dmg, Executable, Fossil, GenericUnar, Git, Gzip, Jar, Lha, LuaRock, Lzip, Lzma, Mercurial, MicrosoftOfficeXml, Otf, P7Zip, Path, Pax, Pkg, Rar, SelfExtractingExecutable, Sit, Subversion, Tar, Ttf, Uncompressed, Xar, Xz, Zip, Zstd

Constant Summary collapse

UnpackStrategyType =

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.

T.type_alias { T.all(T::Class[UnpackStrategy], UnpackStrategy::ClassMethods) }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_once, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning

Methods included from SystemCommand::Mixin

#command_sandbox, #system_command, #system_command!

Instance Attribute Details

#merge_xattrs ⇒ Boolean (readonly)

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.

Returns:

  • (Boolean)


157
158
159
# File 'unpack_strategy.rb', line 157

def merge_xattrs
  @merge_xattrs
end

#path ⇒ Path (readonly)

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.

Returns:



154
155
156
# File 'unpack_strategy.rb', line 154

def path
  @path
end

#temporary_directory ⇒ Pathname (readonly)

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.

Parent directory for nested archive staging and tar decompression.

Returns:



161
162
163
# File 'unpack_strategy.rb', line 161

def temporary_directory
  @temporary_directory
end

Class Method Details

.detect(path, prioritize_extension: false, type: nil, ref_type: nil, ref: nil, merge_xattrs: false, temporary_directory: HOMEBREW_TEMP) ⇒ UnpackStrategy

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 (Pathname)
  • prioritize_extension (Boolean) (defaults to: false)
  • type (Symbol, nil) (defaults to: nil)
  • ref_type (Symbol, nil) (defaults to: nil)
  • ref (String, nil) (defaults to: nil)
  • merge_xattrs (Boolean) (defaults to: false)
  • temporary_directory (Pathname) (defaults to: HOMEBREW_TEMP)

Returns:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'unpack_strategy.rb', line 131

def self.detect(path, prioritize_extension: false, type: nil, ref_type: nil, ref: nil, merge_xattrs: false,
                temporary_directory: HOMEBREW_TEMP)
  path = Path.new(path)
  strategy = from_type(type) if type

  if prioritize_extension && path.extname.present?
    strategy ||= from_extension(path.extname)

    strategy ||= strategies.find { |s| (s < Directory || s == Fossil) && s.can_extract?(path) }
  else
    strategy ||= from_magic(path)
    strategy ||= from_extension(path.extname)
  end

  strategy ||= Uncompressed
  if (replacement = DEPRECATED_STRATEGY_REPLACEMENTS[strategy.to_s])
    odeprecated strategy.to_s, replacement
  end

  strategy.new(path, ref_type:, ref:, merge_xattrs:, temporary_directory:)
end

.from_extension(extension) ⇒ UnpackStrategyType?

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:

Returns:



117
118
119
120
# File 'unpack_strategy.rb', line 117

def self.from_extension(extension)
  strategies.sort_by { |s| -(s.extensions.map(&:length).max || 0) }
            .find { |s| extension.end_with?(*s.extensions) }
end

.from_magic(path) ⇒ UnpackStrategyType?

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:

Returns:



123
124
125
# File 'unpack_strategy.rb', line 123

def self.from_magic(path)
  strategies.find { |s| s.can_extract?(path) }
end

.from_name(name) ⇒ UnpackStrategyType

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:

Returns:



91
92
93
94
# File 'unpack_strategy.rb', line 91

def self.from_name(name)
  [*strategies, Directory, Dmg::Mount, Uncompressed].find { |strategy| strategy.name == name } ||
    raise(ArgumentError, "Unknown unpack strategy: #{name}")
end

.from_type(type) ⇒ UnpackStrategyType?

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:

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'unpack_strategy.rb', line 97

def self.from_type(type)
  odeprecated "the `:seven_zip` container type", ":p7zip" if type == :seven_zip

  type = {
    naked:     :uncompressed,
    nounzip:   :uncompressed,
    seven_zip: :p7zip,
  }.fetch(type, type)

  begin
    # The strategy class name is derived dynamically from the type.
    # rubocop:disable Sorbet/ConstantsFromStrings
    const_get(type.to_s.split("_").map(&:capitalize).join.gsub(/\d+[a-z]/, &:upcase))
    # rubocop:enable Sorbet/ConstantsFromStrings
  rescue NameError
    nil
  end
end

Instance Method Details

#dependencies ⇒ Array<Cask::Cask>, Array<Formula>

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.

Returns:



244
245
246
# File 'unpack_strategy.rb', line 244

def dependencies
  []
end

#each_directory(pathname, &_block) ⇒ void

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.

This method returns an undefined value.

Helper method for iterating over directory trees.

Parameters:



255
256
257
258
259
# File 'unpack_strategy.rb', line 255

def each_directory(pathname, &_block)
  pathname.find do |path|
    yield path if !path.symlink? && path.directory?
  end
end

#extract(to: nil, basename: nil, verbose: false) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • to (Pathname, nil) (defaults to: nil)
  • basename (String, Pathname, nil) (defaults to: nil)
  • verbose (Boolean) (defaults to: false)


184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'unpack_strategy.rb', line 184

def extract(to: nil, basename: nil, verbose: false)
  basename ||= path.basename
  unpack_dir = Pathname(to || Dir.pwd).expand_path
  unpack_dir.mkpath
  # Mount disk images outside the extraction sandbox; Dmg::Mount sandboxes the copy.
  if !is_a?(Dmg) && Sandbox.isolate_operation?
    move = is_a?(Directory) && move?
    Sandbox.operation(
      "extract",
      JSON.generate(strategy: self.class.name, path: path.to_s, to: unpack_dir.to_s, basename: basename.to_s,
                    verbose:, ref_type: @ref_type, ref: @ref, merge_xattrs:, move:),
      read_paths: [path], write_paths: [unpack_dir, *(path if move)], temporary_directory:,
    )
    return
  end

  extract_to_dir(unpack_dir, basename: Pathname(basename), verbose:)
end

#extract_nestedly(to: nil, basename: nil, verbose: false, prioritize_extension: false) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • to (Pathname, nil) (defaults to: nil)
  • basename (String, Pathname, nil) (defaults to: nil)
  • verbose (Boolean) (defaults to: false)
  • prioritize_extension (Boolean) (defaults to: false)


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'unpack_strategy.rb', line 211

def extract_nestedly(to: nil, basename: nil, verbose: false, prioritize_extension: false)
  Mktemp.new("homebrew-unpack", parent: temporary_directory).run(chdir: false) do |unpack_dir|
    tmp_unpack_dir = unpack_dir.tmpdir
    raise "Failed to create a temporary directory to unpack #{path}" if tmp_unpack_dir.nil?

    extract(to: tmp_unpack_dir, basename:, verbose:)
    raise "Extraction directory is a symlink: #{tmp_unpack_dir}" if tmp_unpack_dir.symlink?

    children = tmp_unpack_dir.children

    if children.size == 1 && !children.fetch(0).directory? && !children.fetch(0).symlink?
      first_child = children.first
      next if first_child.nil?

      s = UnpackStrategy.detect(first_child, prioritize_extension:, temporary_directory:)

      s.extract_nestedly(to:, verbose:, prioritize_extension:)

      next
    end

    # Ensure all extracted directories are writable.
    each_directory(tmp_unpack_dir) do |path|
      next if path.writable?

      FileUtils.chmod "u+w", path, verbose:
    end

    Directory.new(tmp_unpack_dir, move: true).extract(to:, verbose:)
  end
end

#initialize(path, ref_type: nil, ref: nil, merge_xattrs: false, temporary_directory: HOMEBREW_TEMP) ⇒ void

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)
  • ref_type (Symbol, nil) (defaults to: nil)
  • ref (String, nil) (defaults to: nil)
  • merge_xattrs (Boolean) (defaults to: false)
  • temporary_directory (Pathname) (defaults to: HOMEBREW_TEMP)


167
168
169
170
171
172
173
# File 'unpack_strategy.rb', line 167

def initialize(path, ref_type: nil, ref: nil, merge_xattrs: false, temporary_directory: HOMEBREW_TEMP)
  @path = T.let(Path.new(Pathname(path).expand_path), Path)
  @ref_type = ref_type
  @ref = ref
  @merge_xattrs = merge_xattrs
  @temporary_directory = temporary_directory
end