class Puppet::FileSystem::Uniquefile

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

Public Instance Methods

close(unlink_now=false) click to toggle source
# File lib/puppet/file_system/uniquefile.rb, line 66
def close(unlink_now=false)
  if unlink_now
    close!
  else
    _close
  end
end
close!() click to toggle source
# File lib/puppet/file_system/uniquefile.rb, line 74
def close!
  _close
  unlink
end
delete() click to toggle source
Alias for: unlink
open() click to toggle source

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
path() click to toggle source

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

Protected Instance Methods

_close() click to toggle source
# File lib/puppet/file_system/uniquefile.rb, line 57
def _close
  begin
    @tmpfile.close if @tmpfile
  ensure
    @tmpfile = nil
  end
end

Public Class Methods

new(basename, *rest) click to toggle source
# 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
open_tmp(identifier) { |f| ... } click to toggle source

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