A class that provides `Tempfile`-like capabilities, but does not attempt to manage the deletion of the file for you. API is identical to the normal `Tempfile` class.
@api public
# File lib/puppet/file_system/uniquefile.rb, line 66 def close(unlink_now=false) if unlink_now close! else _close end end
# File lib/puppet/file_system/uniquefile.rb, line 74 def close! _close unlink end
Opens or reopens the file with mode “r+”.
# File lib/puppet/file_system/uniquefile.rb, line 51 def open @tmpfile.close if @tmpfile @tmpfile = File.open(@tmpname, @mode, @opts) __setobj__(@tmpfile) end
Returns the full path name of the temporary file. This will be nil if unlink has been called.
# File lib/puppet/file_system/uniquefile.rb, line 94 def path @tmpname end
# File lib/puppet/file_system/uniquefile.rb, line 79 def unlink return unless @tmpname begin File.unlink(@tmpname) rescue Errno::ENOENT rescue Errno::EACCES # may not be able to unlink on Windows; just ignore return end @tmpname = nil end
# File lib/puppet/file_system/uniquefile.rb, line 57 def _close begin @tmpfile.close if @tmpfile ensure @tmpfile = nil end end
# File lib/puppet/file_system/uniquefile.rb, line 27 def initialize(basename, *rest) create_tmpname(basename, *rest) do |tmpname, n, opts| mode = File::RDWR|File::CREAT|File::EXCL perm = 0600 if opts mode |= opts.delete(:mode) || 0 opts[:perm] = perm perm = nil else opts = perm end self.class.locking(tmpname) do @tmpfile = File.open(tmpname, mode, opts) @tmpname = tmpname end @mode = mode & ~(File::CREAT|File::EXCL) perm or opts.freeze @opts = opts end super(@tmpfile) end
Convenience method which ensures that the file is closed and unlinked before returning
@param identifier [String] additional part of generated pathname @yieldparam file [File] the temporary file object @return result of the passed block @api private
# File lib/puppet/file_system/uniquefile.rb, line 18 def self.open_tmp(identifier) f = new(identifier) yield f ensure if f f.close! end end