Merge pull request #13964 from alebcay/codesign-ensure-writable

This commit is contained in:
Carlo Cabrera 2022-10-14 09:26:39 +08:00 committed by GitHub
commit 463f0d6339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,36 +31,59 @@ class Keg
return unless Hardware::CPU.arm? return unless Hardware::CPU.arm?
odebug "Codesigning #{file}" odebug "Codesigning #{file}"
# Use quiet_system to squash notifications about resigning binaries prepare_codesign_writable_files(file) do
# which already have valid signatures. # Use quiet_system to squash notifications about resigning binaries
return if quiet_system("codesign", "--sign", "-", "--force", # which already have valid signatures.
"--preserve-metadata=entitlements,requirements,flags,runtime", return if quiet_system("codesign", "--sign", "-", "--force",
file) "--preserve-metadata=entitlements,requirements,flags,runtime",
file)
# If the codesigning fails, it may be a bug in Apple's codesign utility # If the codesigning fails, it may be a bug in Apple's codesign utility
# A known workaround is to copy the file to another inode, then move it back # A known workaround is to copy the file to another inode, then move it back
# erasing the previous file. Then sign again. # erasing the previous file. Then sign again.
# #
# TODO: remove this once the bug in Apple's codesign utility is fixed # TODO: remove this once the bug in Apple's codesign utility is fixed
Dir::Tmpname.create("workaround") do |tmppath| Dir::Tmpname.create("workaround") do |tmppath|
FileUtils.cp file, tmppath FileUtils.cp file, tmppath
FileUtils.mv tmppath, file, force: true FileUtils.mv tmppath, file, force: true
end
# Try signing again
odebug "Codesigning (2nd try) #{file}"
result = system_command("codesign", args: [
"--sign", "-", "--force",
"--preserve-metadata=entitlements,requirements,flags,runtime",
file
], print_stderr: false)
return if result.success?
# If it fails again, error out
onoe <<~EOS
Failed applying an ad-hoc signature to #{file}:
#{result.stderr}
EOS
end end
end
# Try signing again def prepare_codesign_writable_files(file)
odebug "Codesigning (2nd try) #{file}"
result = system_command("codesign", args: [ result = system_command("codesign", args: [
"--sign", "-", "--force", "--display", "--file-list", "-", file
"--preserve-metadata=entitlements,requirements,flags,runtime",
file
], print_stderr: false) ], print_stderr: false)
return if result.success? return unless result.success?
# If it fails again, error out files = result.stdout.lines.map { |f| Pathname(f.chomp) }
onoe <<~EOS saved_perms = {}
Failed applying an ad-hoc signature to #{file}: files.each do |f|
#{result.stderr} unless f.writable_real?
EOS saved_perms[f] = f.stat.mode
FileUtils.chmod "u+rw", f.to_path
end
end
yield
ensure
saved_perms&.each do |f, p|
f.chmod p if p
end
end end
def prepare_debug_symbols def prepare_debug_symbols