diff options
author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-08 13:57:31 -0700 |
---|---|---|
committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-08 15:17:55 -0700 |
commit | f9a2ffd53054b67924790b0386cf74435497c3f1 (patch) | |
tree | d7b705a2d5ba4e5a24f09cc70cfd0bef2f91d49e /spec/lib/puppet_spec | |
parent | 78cb48cea0b14eabdfd728c2f9fc183f587faded (diff) | |
download | puppet-f9a2ffd53054b67924790b0386cf74435497c3f1.tar.gz puppet-f9a2ffd53054b67924790b0386cf74435497c3f1.tar.xz puppet-f9a2ffd53054b67924790b0386cf74435497c3f1.zip |
maint: use FileUtil to remove files, not exec
We used to shell out to chmod and rm to clean up temporary files; this lead to
the cleanup method here being one of the largest consumers of walltime.
Replacing that with FileUtil calls is as, or more, secure, and performs
sufficiently well that we can just delegate.
Reviewed-By: Matt Robinson <matt@puppetlabs.com>
Diffstat (limited to 'spec/lib/puppet_spec')
-rw-r--r-- | spec/lib/puppet_spec/files.rb | 56 |
1 files changed, 33 insertions, 23 deletions
diff --git a/spec/lib/puppet_spec/files.rb b/spec/lib/puppet_spec/files.rb index 38c51a572..30fb4fc42 100644 --- a/spec/lib/puppet_spec/files.rb +++ b/spec/lib/puppet_spec/files.rb @@ -3,41 +3,51 @@ require 'tempfile' # A support module for testing files. module PuppetSpec::Files + # This code exists only to support tests that run as root, pretty much. + # Once they have finally been eliminated this can all go... --daniel 2011-04-08 + if Puppet.features.posix? then + def self.in_tmp(path) + path =~ /^\/tmp/ or path =~ /^\/var\/folders/ + end + elsif Puppet.features.microsoft_windows? + def self.in_tmp(path) + tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp")) + path =~ /^#{tempdir}/ + end + else + fail "Help! Can't find in_tmp for this platform" + end + def self.cleanup - if defined?($tmpfiles) - $tmpfiles.each do |file| - file = File.expand_path(file) - if Puppet.features.posix? and file !~ /^\/tmp/ and file !~ /^\/var\/folders/ - puts "Not deleting tmpfile #{file} outside of /tmp or /var/folders" - next - elsif Puppet.features.microsoft_windows? - tempdir = File.expand_path(File.join(Dir::LOCAL_APPDATA, "Temp")) - if file !~ /^#{tempdir}/ - puts "Not deleting tmpfile #{file} outside of #{tempdir}" - next - end - end - if FileTest.exist?(file) - system("chmod -R 755 '#{file}'") - system("rm -rf '#{file}'") - end + $global_tempfiles ||= [] + while path = $global_tempfiles.pop do + fail "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path) + + begin + FileUtils.rm_r path, :secure => true + rescue Errno::ENOENT + # nothing to do end - $tmpfiles.clear end end def tmpfile(name) + # Generate a temporary file, just for the name... source = Tempfile.new(name) path = source.path source.close! - $tmpfiles ||= [] - $tmpfiles << path + + # ...record it for cleanup, + $global_tempfiles ||= [] + $global_tempfiles << File.expand_path(path) + + # ...and bam. path end def tmpdir(name) - file = tmpfile(name) - FileUtils.mkdir_p(file) - file + path = tmpfile(name) + FileUtils.mkdir_p(path) + path end end |