Use sudo for symlinks if necessary.

This commit is contained in:
Markus Reiter 2023-05-13 02:05:02 +02:00
parent a1dd95d6ee
commit 2c71d9dcd8
No known key found for this signature in database
GPG Key ID: 245293B51702655B
3 changed files with 23 additions and 15 deletions

View File

@ -82,13 +82,7 @@ module Cask
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?

View File

@ -43,7 +43,7 @@ module Cask
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} " \
@ -57,26 +57,29 @@ module Cask
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(**)
def unlink(command: nil, **)
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

View File

@ -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|