Skip to content

Commit

Permalink
Use sudo for symlinks if necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed May 13, 2023
1 parent 8ea287c commit 1d4192a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
8 changes: 1 addition & 7 deletions Library/Homebrew/cask/artifact/moved.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,7 @@ def move(adopt: false, force: false, verbose: false, predecessor: nil, reinstall

ohai "Moving #{self.class.english_name} '#{source.basename}' to '#{target}'"

unless target.dirname.exist?
if target.dirname.ascend.find(&:directory?).writable?
target.dirname.mkpath
else
command.run!("/bin/mkdir", args: ["-p", target.dirname], sudo: true)
end
end
Utils.gain_permissions_mkpath(target.dirname, command: command) unless target.dirname.exist?

if target.directory?
if target.writable?
Expand Down
17 changes: 10 additions & 7 deletions Library/Homebrew/cask/artifact/symlinked.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def summarize_installed

private

def link(force: false, **options)
def link(force: false, command: nil, **_options)
unless source.exist?
raise CaskError,
"It seems the #{self.class.link_type_english_name.downcase} " \
Expand All @@ -57,26 +57,29 @@ def link(force: false, **options)
if force && target.symlink? &&
(target.realpath == source.realpath || target.realpath.to_s.start_with?("#{cask.caskroom_path}/"))
opoo "#{message}; overwriting."
target.delete
Utils.gain_permissions_remove(target, command: command)
else
raise CaskError, "#{message}."
end
end

ohai "Linking #{self.class.english_name} '#{source.basename}' to '#{target}'"
create_filesystem_link(**options)
create_filesystem_link(command: command)
end

def unlink(**)
return unless target.symlink?

ohai "Unlinking #{self.class.english_name} '#{target}'"
target.delete
Utils.gain_permissions_remove(target, command: command)
end

def create_filesystem_link(command: nil, **_)
target.dirname.mkpath
command.run!("/bin/ln", args: ["-h", "-f", "-s", "--", source, target])
def create_filesystem_link(command: nil)
Utils.gain_permissions_mkpath(target.dirname, command: command)

command.run! "/bin/ln", args: ["-h", "-f", "-s", "--", source, target],
sudo: !target.dirname.writable?

add_altname_metadata(source, target.basename, command: command)
end
end
Expand Down
11 changes: 11 additions & 0 deletions Library/Homebrew/cask/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ module Cask
#
# @api private
module Utils
def self.gain_permissions_mkpath(path, command: SystemCommand)
dir = path.ascend.find(&:directory?)
return if path == dir

if dir.writable?
path.mkpath
else
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|
Expand Down

0 comments on commit 1d4192a

Please sign in to comment.