Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix permissions before moving extracted files. #14711

Merged
merged 2 commits into from Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/audit.rb
Expand Up @@ -491,6 +491,7 @@ def audit_signing
Dir.mktmpdir do |tmpdir|
tmpdir = Pathname(tmpdir)
primary_container.extract_nestedly(to: tmpdir, basename: downloaded_path.basename, verbose: false)

artifacts.each do |artifact|
path = case artifact
when Artifact::Moved
Expand All @@ -501,7 +502,6 @@ def audit_signing
next unless path.exist?

result = system_command("codesign", args: ["--verify", path], print_stderr: false)

next if result.success?

message = "Signature verification failed:\n#{result.merged_output}\nmacOS on ARM requires applications " \
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/download_strategy.rb
Expand Up @@ -95,10 +95,10 @@ def quiet?
# @api public
def stage(&block)
UnpackStrategy.detect(cached_location,
prioritise_extension: true,
prioritize_extension: true,
ref_type: @ref_type, ref: @ref)
.extract_nestedly(basename: basename,
prioritise_extension: true,
prioritize_extension: true,
verbose: verbose? && !quiet?)
chdir(&block) if block
end
Expand Down
30 changes: 21 additions & 9 deletions Library/Homebrew/unpack_strategy.rb
Expand Up @@ -104,10 +104,10 @@ def self.from_magic(path)
strategies.find { |s| s.can_extract?(path) }
end

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

if prioritise_extension && path.extname.present?
if prioritize_extension && path.extname.present?
strategy ||= from_extension(path.extname)
strategy ||= strategies.select { |s| s < Directory || s == Fossil }
.find { |s| s.can_extract?(path) }
Expand Down Expand Up @@ -135,14 +135,27 @@ def initialize(path, ref_type: nil, ref: nil, merge_xattrs: nil)
def extract_to_dir(unpack_dir, basename:, verbose:); end
private :extract_to_dir

def extract(to: nil, basename: nil, verbose: nil)
sig {
params(
to: T.nilable(Pathname), basename: T.nilable(T.any(String, Pathname)), verbose: T::Boolean,
).returns(T.untyped)
}
def extract(to: nil, basename: nil, verbose: false)
basename ||= path.basename
unpack_dir = Pathname(to || Dir.pwd).expand_path
unpack_dir.mkpath
extract_to_dir(unpack_dir, basename: Pathname(basename), verbose: verbose || false)
extract_to_dir(unpack_dir, basename: Pathname(basename), verbose: verbose)
end

def extract_nestedly(to: nil, basename: nil, verbose: false, prioritise_extension: false)
sig {
params(
to: T.nilable(Pathname),
basename: T.nilable(T.any(String, Pathname)),
verbose: T::Boolean,
prioritize_extension: T::Boolean,
).returns(T.untyped)
}
def extract_nestedly(to: nil, basename: nil, verbose: false, prioritize_extension: false)
Dir.mktmpdir do |tmp_unpack_dir|
tmp_unpack_dir = Pathname(tmp_unpack_dir)

Expand All @@ -153,15 +166,14 @@ def extract_nestedly(to: nil, basename: nil, verbose: false, prioritise_extensio
if children.count == 1 && !children.first.directory?
FileUtils.chmod "+rw", children.first, verbose: verbose

s = UnpackStrategy.detect(children.first, prioritise_extension: prioritise_extension)
s = UnpackStrategy.detect(children.first, prioritize_extension: prioritize_extension)

s.extract_nestedly(to: to, verbose: verbose, prioritise_extension: prioritise_extension)
s.extract_nestedly(to: to, verbose: verbose, prioritize_extension: prioritize_extension)
next
end

Directory.new(tmp_unpack_dir).extract(to: to, verbose: verbose)

FileUtils.chmod_R "+w", tmp_unpack_dir, force: true, verbose: verbose
Directory.new(tmp_unpack_dir).extract(to: to, verbose: verbose)
end
end

Expand Down
14 changes: 11 additions & 3 deletions Library/Homebrew/unpack_strategy/uncompressed.rb
Expand Up @@ -8,14 +8,22 @@ class Uncompressed

include UnpackStrategy

def extract_nestedly(prioritise_extension: false, **options)
extract(**options)
sig {
params(
to: T.nilable(Pathname),
basename: T.nilable(T.any(String, Pathname)),
verbose: T::Boolean,
prioritize_extension: T::Boolean,
).returns(T.untyped)
}
def extract_nestedly(to: nil, basename: nil, verbose: false, prioritize_extension: false)
extract(to: to, basename: basename, verbose: verbose)
end

private

sig { override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).returns(T.untyped) }
def extract_to_dir(unpack_dir, basename:, verbose:)
def extract_to_dir(unpack_dir, basename:, verbose: false)
FileUtils.cp path, unpack_dir/basename, preserve: true, verbose: verbose
end
end
Expand Down