unpack_strategy/directory: try preserving hardlinks
Try running `cp -al` to preserve hardlinks on both macOS and Linux. If that fails, fall back on `cp -a` which can preserve hardlinks on Linux (but not macOS) assuming target filesystem supports them.
This commit is contained in:
parent
864d5baa70
commit
cfb8ebb5d8
@ -9,6 +9,7 @@ RSpec.describe UnpackStrategy::Directory do
|
|||||||
mktmpdir.tap do |path|
|
mktmpdir.tap do |path|
|
||||||
FileUtils.touch path/"file"
|
FileUtils.touch path/"file"
|
||||||
FileUtils.ln_s "file", path/"symlink"
|
FileUtils.ln_s "file", path/"symlink"
|
||||||
|
FileUtils.ln path/"file", path/"hardlink"
|
||||||
FileUtils.mkdir path/"folder"
|
FileUtils.mkdir path/"folder"
|
||||||
FileUtils.ln_s "folder", path/"folderSymlink"
|
FileUtils.ln_s "folder", path/"folderSymlink"
|
||||||
end
|
end
|
||||||
@ -26,6 +27,11 @@ RSpec.describe UnpackStrategy::Directory do
|
|||||||
expect(unpack_dir/"folderSymlink").to be_a_symlink
|
expect(unpack_dir/"folderSymlink").to be_a_symlink
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "preserves hardlinks" do
|
||||||
|
strategy.extract(to: unpack_dir)
|
||||||
|
expect((unpack_dir/"file").stat.ino).to eq (unpack_dir/"hardlink").stat.ino
|
||||||
|
end
|
||||||
|
|
||||||
it "preserves permissions of contained files" do
|
it "preserves permissions of contained files" do
|
||||||
FileUtils.chmod 0644, path/"file"
|
FileUtils.chmod 0644, path/"file"
|
||||||
|
|
||||||
|
@ -20,11 +20,29 @@ module UnpackStrategy
|
|||||||
|
|
||||||
sig { override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).void }
|
sig { override.params(unpack_dir: Pathname, basename: Pathname, verbose: T::Boolean).void }
|
||||||
def extract_to_dir(unpack_dir, basename:, verbose:)
|
def extract_to_dir(unpack_dir, basename:, verbose:)
|
||||||
path.children.each do |child|
|
path_children = path.children
|
||||||
system_command! "cp",
|
return if path_children.empty?
|
||||||
args: ["-pR", (child.directory? && !child.symlink?) ? "#{child}/." : child,
|
|
||||||
unpack_dir/child.basename],
|
existing = unpack_dir.children
|
||||||
verbose:
|
|
||||||
|
# We run a few cp attempts in the following order:
|
||||||
|
#
|
||||||
|
# 1. Start with `-al` to create hardlinks rather than copying files if the source and
|
||||||
|
# target are on the same filesystem. On macOS, this is the only cp option that can
|
||||||
|
# preserve hardlinks but it is only available since macOS 12.3 (file_cmds-353.100.22).
|
||||||
|
# 2. Try `-a` as GNU `cp -a` preserves hardlinks. macOS `cp -a` is identical to `cp -pR`.
|
||||||
|
# 3. Fall back on `-pR` to handle the case where GNU `cp -a` failed. This may happen if
|
||||||
|
# installing into a filesystem that doesn't support hardlinks like an exFAT USB drive.
|
||||||
|
cp_arg_attempts = ["-a", "-pR"]
|
||||||
|
cp_arg_attempts.unshift("-al") if path.stat.dev == unpack_dir.stat.dev
|
||||||
|
|
||||||
|
cp_arg_attempts.each do |arg|
|
||||||
|
args = [arg, *path_children, unpack_dir]
|
||||||
|
must_succeed = print_stderr = (arg == cp_arg_attempts.last)
|
||||||
|
result = system_command("cp", args:, verbose:, must_succeed:, print_stderr:)
|
||||||
|
break if result.success?
|
||||||
|
|
||||||
|
FileUtils.rm_r(unpack_dir.children - existing)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user