Utils::Cp: Drop special case for coreutils cp

As per review feedback:

https://github.com/Homebrew/brew/pull/17373#issuecomment-2132673915
This commit is contained in:
Daiki Mizukami 2024-05-27 18:01:57 +09:00
parent 942906b74a
commit b4dcb94ad6
No known key found for this signature in database
GPG Key ID: 10478E598B944AA2
2 changed files with 12 additions and 62 deletions

View File

@ -21,8 +21,6 @@ RSpec.describe FormulaInstaller do
expect(formula).to be_bottled
expect(formula).to pour_bottle
stub_formula_loader formula("coreutils") { url "coreutils-1.0" }
stub_formula_loader formula("uutils-coreutils") { url "uutils-coreutils-1.0" }
stub_formula_loader formula("gcc") { url "gcc-1.0" }
stub_formula_loader formula("glibc") { url "glibc-1.0" }
stub_formula_loader formula

View File

@ -17,7 +17,11 @@ module Utils
).returns(SystemCommand::Result)
}
def copy(source, target, sudo: false, verbose: false, command: SystemCommand)
command.run! executable, args: ["-p", *extra_flags, *source, target], sudo:, verbose:
# On macOS, `cp -p` guarantees to preserve extended attributes (including quarantine
# information) in addition to file mode. Other implementations like coreutils does not
# necessarily guarantee the same behavior, but that is fine because we don't really need to
# preserve extended attributes except when copying Cask artifacts.
command.run! "cp", args: ["-p", *extra_flags, *source, target], sudo:, verbose:
end
sig {
@ -30,78 +34,26 @@ module Utils
).returns(SystemCommand::Result)
}
def copy_recursive(source, target, sudo: false, verbose: false, command: SystemCommand)
command.run! executable, args: ["-pR", *extra_flags, *source, target], sudo:, verbose:
command.run! "cp", args: ["-pR", *extra_flags, *source, target], sudo:, verbose:
end
private
GCP = (HOMEBREW_PREFIX/"opt/coreutils/libexec/gnubin/cp").freeze
UCP = (HOMEBREW_PREFIX/"opt/uutils-coreutils/libexec/uubin/cp").freeze
sig { returns(T.any(String, Pathname)) }
def executable
case type
when :macos
Pathname("/bin/cp")
when :coreutils
GCP
when :uutils
UCP
else
"cp"
end
end
MACOS_FLAGS = [
# Perform a lightweight copy-on-write clone if applicable.
"-c",
].freeze
GNU_FLAGS = [
# Unlike BSD cp, `gcp -p` doesn't guarantee to preserve extended attributes, including
# quarantine information on macOS.
"--preserve=all",
"--no-preserve=links",
# Equivalent to `-c` on macOS.
"--reflink=auto",
].freeze
# Use the lightweight `clonefile(2)` syscall if applicable.
MACOS_FLAGS = ["-c"].freeze
GENERIC_FLAGS = [].freeze
sig { returns(T::Array[String]) }
def extra_flags
case type
when :macos
# The `cp` command on older macOS versions also had the `-c` option, but before Sonoma, the
# command would fail if the `clonefile` syscall isn't applicable (the underlying filesystem
# doesn't support the feature or the source and the target are on different filesystems).
if MacOS.version >= :sonoma
MACOS_FLAGS
when :coreutils, :uutils
GNU_FLAGS
else
GENERIC_FLAGS
end
end
sig { returns(T.nilable(Symbol)) }
def type
return @type if defined?(@type)
# The `cp` command on some older macOS versions also had the `-c` option, but before Sonoma,
# the command would fail if the `clonefile` syscall isn't applicable (the underlying
# filesystem doesn't support the feature or the source and the target are on different
# filesystems).
return @type = :macos if MacOS.version >= :sonoma
{
coreutils: "coreutils",
uutils: "uutils-coreutils",
}.each do |type, formula|
begin
formula = Formula[formula]
rescue FormulaUnavailableError
next
end
return @type = type if formula.optlinked?
end
@type = nil
end
end
end
end