Use sudo if parent path of target is not writable.

This commit is contained in:
Markus Reiter 2017-04-01 01:53:29 +02:00
parent e30eeb3d1b
commit c3baf55527
2 changed files with 21 additions and 7 deletions

View File

@ -31,20 +31,26 @@ module Hbc
ohai "Moving #{self.class.artifact_english_name} '#{source.basename}' to '#{target}'." ohai "Moving #{self.class.artifact_english_name} '#{source.basename}' to '#{target}'."
target.dirname.mkpath target.dirname.mkpath
FileUtils.move(source, target)
if target.parent.writable?
FileUtils.move(source, target)
else
SystemCommand.run("/bin/mv", args: [source, target], sudo: true)
end
add_altname_metadata target, source.basename.to_s add_altname_metadata target, source.basename.to_s
end end
def delete def delete
ohai "Removing #{self.class.artifact_english_name} '#{target}'." ohai "Removing #{self.class.artifact_english_name} '#{target}'."
return unless Utils.path_occupied?(target)
raise CaskError, "Cannot remove undeletable #{self.class.artifact_english_name}." if MacOS.undeletable?(target) raise CaskError, "Cannot remove undeletable #{self.class.artifact_english_name}." if MacOS.undeletable?(target)
if force return unless Utils.path_occupied?(target)
Utils.gain_permissions_remove(target, command: @command)
else if target.parent.writable? && !force
target.rmtree target.rmtree
else
Utils.gain_permissions_remove(target, command: @command)
end end
end end

View File

@ -39,7 +39,15 @@ module Hbc
module Utils module Utils
def self.gain_permissions_remove(path, command: SystemCommand) def self.gain_permissions_remove(path, command: SystemCommand)
if path.respond_to?(:rmtree) && path.exist? if path.respond_to?(:rmtree) && path.exist?
gain_permissions(path, ["-R"], command, &:rmtree) gain_permissions(path, ["-R"], command) do |p|
if p.parent.writable?
p.rmtree
else
command.run("/bin/rm",
args: command_args + ["-r", "-f", "--", p],
sudo: true)
end
end
elsif File.symlink?(path) elsif File.symlink?(path)
gain_permissions(path, ["-h"], command, &FileUtils.method(:rm_f)) gain_permissions(path, ["-h"], command, &FileUtils.method(:rm_f))
end end