From a6e7858cbc95b2526ac481248ac72e8bdc2dc786 Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Sat, 27 Mar 2021 16:14:36 +0000 Subject: [PATCH] mktemp: handle dirs with restricted permissions --- Library/Homebrew/mktemp.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/mktemp.rb b/Library/Homebrew/mktemp.rb index fb1eeb5397..fbfc7ef627 100644 --- a/Library/Homebrew/mktemp.rb +++ b/Library/Homebrew/mktemp.rb @@ -62,9 +62,23 @@ class Mktemp begin Dir.chdir(tmpdir) { yield self } ensure - ignore_interrupts { rm_rf(tmpdir) } unless retain? + ignore_interrupts { chmod_rm_rf(tmpdir) } unless retain? end ensure ohai "Temporary files retained at:", @tmpdir.to_s if retain? && !@tmpdir.nil? && !@quiet end + + private + + def chmod_rm_rf(path) + if path.directory? && !path.symlink? + chmod("u+rw", path) if path.owned? # Need permissions in order to see the contents + path.children.each { |child| chmod_rm_rf(child) } + rmdir(path) + else + rm_f(path) + end + rescue + nil # Just skip this directory. + end end