Skip to content

Commit

Permalink
Improve Cask::Utils.gain_permissions_remove for symlinks.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed May 13, 2023
1 parent 4e483c9 commit d7a0e26
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
35 changes: 23 additions & 12 deletions Library/Homebrew/cask/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,37 @@ def self.gain_permissions_mkpath(path, command: SystemCommand)
if dir.writable?
path.mkpath
else
command.run!("/bin/mkdir", args: ["-p", path], sudo: true)
command.run!("/bin/mkdir", args: ["-p", "--", path], sudo: true)
end
end

def self.gain_permissions_remove(path, command: SystemCommand)
if path.respond_to?(:rmtree) && path.exist?
gain_permissions(path, ["-R"], command) do |p|
if p.parent.writable?
directory = false
permission_flags = if path.symlink?
["-h"]
elsif path.directory?
directory = true
["-R"]
elsif path.exist?
[]
else
# Nothing to remove.
return
end

gain_permissions(path, permission_flags, command) do |p|
if p.parent.writable?
if directory
p.rmtree
else
command.run("/bin/rm",
args: ["-r", "-f", "--", p],
sudo: true)
FileUtils.rm_f p
end
else
recursive_flag = directory ? ["-R"] : []
command.run!("/bin/rm",
args: recursive_flag + ["-f", "--", p],
sudo: true)
end
elsif File.symlink?(path)
gain_permissions(path, ["-h"], command, &FileUtils.method(:rm_f))
end
end

Expand All @@ -51,13 +65,10 @@ def self.gain_permissions(path, command_args, command)
# dependent on whether the file argument has a trailing
# slash. This should do the right thing, but is fragile.
command.run("/usr/bin/chflags",
must_succeed: false,
args: command_args + ["--", "000", path])
command.run("/bin/chmod",
must_succeed: false,
args: command_args + ["--", "u+rwx", path])
command.run("/bin/chmod",
must_succeed: false,
args: command_args + ["-N", path])
tried_permissions = true
retry # rmtree
Expand Down
21 changes: 6 additions & 15 deletions Library/Homebrew/test/cask/artifact/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,21 +171,12 @@
end

it "overwrites the existing app" do
expect(command).to receive(:run).with(
"/bin/chmod", args: [
"-R", "--", "u+rwx", target_path
], must_succeed: false
).and_call_original
expect(command).to receive(:run).with(
"/bin/chmod", args: [
"-R", "-N", target_path
], must_succeed: false
).and_call_original
expect(command).to receive(:run).with(
"/usr/bin/chflags", args: [
"-R", "--", "000", target_path
], must_succeed: false
).and_call_original
expect(command).to receive(:run).with("/usr/bin/chflags",
args: ["-R", "--", "000", target_path]).and_call_original
expect(command).to receive(:run).with("/bin/chmod",
args: ["-R", "--", "u+rwx", target_path]).and_call_original
expect(command).to receive(:run).with("/bin/chmod",
args: ["-R", "-N", target_path]).and_call_original

stdout = <<~EOS
==> Removing App '#{target_path}'
Expand Down
45 changes: 42 additions & 3 deletions Library/Homebrew/test/cask/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

describe Cask::Utils do
let(:command) { NeverSudoSystemCommand }
let(:dir) { mktmpdir }
let(:path) { dir/"a/b/c" }
let(:link) { dir/"link" }

describe "::gain_permissions_mkpath" do
let(:dir) { mktmpdir }
let(:path) { dir/"a/b/c" }

it "creates a directory" do
expect(path).not_to exist
described_class.gain_permissions_mkpath path, command: command
Expand Down Expand Up @@ -37,4 +37,43 @@
end
end
end

describe "::gain_permissions_remove" do
it "removes the symlink, not the file it points to" do
path.dirname.mkpath
FileUtils.touch path
FileUtils.ln_s path, link

expect(path).to be_a_file
expect(link).to be_a_symlink
expect(link.realpath).to eq path

described_class.gain_permissions_remove link, command: command

expect(path).to be_a_file
expect(link).not_to exist

described_class.gain_permissions_remove path, command: command

expect(path).not_to exist
end

it "removes the symlink, not the directory it points to" do
path.mkpath
FileUtils.ln_s path, link

expect(path).to be_a_directory
expect(link).to be_a_symlink
expect(link.realpath).to eq path

described_class.gain_permissions_remove link, command: command

expect(path).to be_a_directory
expect(link).not_to exist

described_class.gain_permissions_remove path, command: command

expect(path).not_to exist
end
end
end

0 comments on commit d7a0e26

Please sign in to comment.