Use sudo for rmdir if necessary.
This commit is contained in:
parent
b0dc84b117
commit
935c5efaf8
@ -482,32 +482,48 @@ module Cask
|
|||||||
end
|
end
|
||||||
|
|
||||||
def recursive_rmdir(*directories, command: nil, **_)
|
def recursive_rmdir(*directories, command: nil, **_)
|
||||||
success = T.let(true, T::Boolean)
|
directories.all? do |resolved_path|
|
||||||
each_resolved_path(:rmdir, directories) do |_path, resolved_paths|
|
|
||||||
resolved_paths.select(&method(:all_dirs?)).each do |resolved_path|
|
|
||||||
puts resolved_path.sub(Dir.home, "~")
|
puts resolved_path.sub(Dir.home, "~")
|
||||||
|
|
||||||
if (ds_store = resolved_path.join(".DS_Store")).exist?
|
if resolved_path.readable?
|
||||||
command.run!("/bin/rm", args: ["-f", "--", ds_store], sudo: true, print_stderr: false)
|
children = resolved_path.children
|
||||||
|
|
||||||
|
next false unless children.all? { |child| child.directory? || child.basename.to_s == ".DS_Store" }
|
||||||
|
else
|
||||||
|
lines = command.run!("/bin/ls", args: ["-A", "-F", "--", resolved_path], sudo: true, print_stderr: false)
|
||||||
|
.stdout.lines.map(&:chomp)
|
||||||
|
.flat_map(&:chomp)
|
||||||
|
|
||||||
|
# Using `-F` above outputs directories ending with `/`.
|
||||||
|
next false unless lines.all? { |l| l.end_with?("/") || l == ".DS_Store" }
|
||||||
|
|
||||||
|
children = lines.map { |l| resolved_path/l.delete_suffix("/") }
|
||||||
end
|
end
|
||||||
|
|
||||||
unless recursive_rmdir(*resolved_path.children, command: command)
|
# Directory counts as empty if it only contains a `.DS_Store`.
|
||||||
success = false
|
if children.include?(ds_store = resolved_path/".DS_Store")
|
||||||
next
|
Utils.gain_permissions_remove(ds_store, command: command)
|
||||||
|
children.delete(ds_store)
|
||||||
end
|
end
|
||||||
|
|
||||||
status = command.run("/bin/rmdir", args: ["--", resolved_path], sudo: true, print_stderr: false).success?
|
next false unless recursive_rmdir(*children, command: command)
|
||||||
success &= status
|
|
||||||
|
Utils.gain_permissions_rmdir(resolved_path, command: command)
|
||||||
|
|
||||||
|
true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
success
|
|
||||||
end
|
|
||||||
|
|
||||||
def uninstall_rmdir(*args, **kwargs)
|
def uninstall_rmdir(*directories, **kwargs)
|
||||||
return if args.empty?
|
return if directories.empty?
|
||||||
|
|
||||||
ohai "Removing directories if empty:"
|
ohai "Removing directories if empty:"
|
||||||
recursive_rmdir(*args, **kwargs)
|
|
||||||
|
each_resolved_path(:rmdir, directories) do |_path, resolved_paths|
|
||||||
|
next unless resolved_paths.all?(&:directory?)
|
||||||
|
|
||||||
|
recursive_rmdir(*resolved_paths, **kwargs)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -18,7 +18,17 @@ module Cask
|
|||||||
if dir.writable?
|
if dir.writable?
|
||||||
path.mkpath
|
path.mkpath
|
||||||
else
|
else
|
||||||
command.run!("/bin/mkdir", args: ["-p", "--", path], sudo: true)
|
command.run!("/bin/mkdir", args: ["-p", "--", path], sudo: true, print_stderr: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.gain_permissions_rmdir(path, command: SystemCommand)
|
||||||
|
gain_permissions(path, [], command) do |p|
|
||||||
|
if p.parent.writable?
|
||||||
|
FileUtils.rmdir p
|
||||||
|
else
|
||||||
|
command.run!("/bin/rmdir", args: ["--", p], sudo: true, print_stderr: false)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -45,9 +55,7 @@ module Cask
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
recursive_flag = directory ? ["-R"] : []
|
recursive_flag = directory ? ["-R"] : []
|
||||||
command.run!("/bin/rm",
|
command.run!("/bin/rm", args: recursive_flag + ["-f", "--", p], sudo: true, print_stderr: false)
|
||||||
args: recursive_flag + ["-f", "--", p],
|
|
||||||
sudo: true)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user