mktemp: handle dirs with restricted permissions

This commit is contained in:
Bo Anderson 2021-03-27 16:14:36 +00:00
parent a96e364b41
commit a6e7858cbc
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65

View File

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