diff --git a/Library/Homebrew/cask/artifact/moved.rb b/Library/Homebrew/cask/artifact/moved.rb index e12d0160f77c73..392c6673bacecb 100644 --- a/Library/Homebrew/cask/artifact/moved.rb +++ b/Library/Homebrew/cask/artifact/moved.rb @@ -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? diff --git a/Library/Homebrew/cask/artifact/symlinked.rb b/Library/Homebrew/cask/artifact/symlinked.rb index 3d920c1ede19ef..316f30c3753270 100644 --- a/Library/Homebrew/cask/artifact/symlinked.rb +++ b/Library/Homebrew/cask/artifact/symlinked.rb @@ -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} " \ @@ -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 diff --git a/Library/Homebrew/cask/utils.rb b/Library/Homebrew/cask/utils.rb index dd5ba92ca0acb2..73d069fac59b42 100644 --- a/Library/Homebrew/cask/utils.rb +++ b/Library/Homebrew/cask/utils.rb @@ -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|