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 Class Methods

new(basename, *rest) click to toggle source
Calls superclass method
   # File lib/puppet/file_system/uniquefile.rb
27 def initialize(basename, *rest)
28   create_tmpname(basename, *rest) do |tmpname, n, opts|
29     mode = File::RDWR|File::CREAT|File::EXCL
30     perm = 0600
31     if opts
32       mode |= opts.delete(:mode) || 0
33       opts[:perm] = perm
34       perm = nil
35     else
36       opts = perm
37     end
38     self.class.locking(tmpname) do
39       @tmpfile = File.open(tmpname, mode, opts)
40       @tmpname = tmpname
41     end
42     @mode = mode & ~(File::CREAT|File::EXCL)
43     perm or opts.freeze
44     @opts = opts
45   end
46 
47   super(@tmpfile)
48 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
18 def self.open_tmp(identifier)
19   f = new(identifier)
20   yield f
21 ensure
22   if f
23     f.close!
24   end
25 end

Private Class Methods

locking(tmpname) { || ... } click to toggle source

yields with locking for tmpname and returns the result of the block.

    # File lib/puppet/file_system/uniquefile.rb
167 def locking(tmpname)
168   lock = tmpname + '.lock'
169   mkdir(lock)
170   yield
171 rescue Errno::ENOENT => e
172   ex = Errno::ENOENT.new("A directory component in #{lock} does not exist or is a dangling symbolic link")
173   ex.set_backtrace(e.backtrace)
174   raise ex
175 ensure
176   rmdir(lock) if Puppet::FileSystem.exist?(lock)
177 end
mkdir(*args) click to toggle source
    # File lib/puppet/file_system/uniquefile.rb
179 def mkdir(*args)
180   Dir.mkdir(*args)
181 end
rmdir(*args) click to toggle source
    # File lib/puppet/file_system/uniquefile.rb
183 def rmdir(*args)
184   Dir.rmdir(*args)
185 end

Public Instance Methods

close(unlink_now=false) click to toggle source
   # File lib/puppet/file_system/uniquefile.rb
66 def close(unlink_now=false)
67   if unlink_now
68     close!
69   else
70     _close
71   end
72 end
close!() click to toggle source
   # File lib/puppet/file_system/uniquefile.rb
74 def close!
75   _close
76   unlink
77 end
delete()
Alias for: unlink
open() click to toggle source

Opens or reopens the file with mode “r+”.

   # File lib/puppet/file_system/uniquefile.rb
51 def open
52   @tmpfile.close if @tmpfile
53   @tmpfile = File.open(@tmpname, @mode, @opts)
54   __setobj__(@tmpfile)
55 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
94 def path
95   @tmpname
96 end

Protected Instance Methods

_close() click to toggle source
   # File lib/puppet/file_system/uniquefile.rb
57 def _close
58   begin
59     @tmpfile.close if @tmpfile
60   ensure
61     @tmpfile = nil
62   end
63 end

Private Instance Methods

create_tmpname(basename, *rest) { |path, n, *opts| ... } click to toggle source
    # File lib/puppet/file_system/uniquefile.rb
117 def create_tmpname(basename, *rest)
118   opts = try_convert_to_hash(rest[-1])
119   if opts
120     opts = opts.dup if rest.pop.equal?(opts)
121     max_try = opts.delete(:max_try)
122     opts = [opts]
123   else
124     opts = []
125   end
126   tmpdir, = *rest
127   tmpdir ||= tmpdir()
128   n = nil
129   begin
130     path = File.expand_path(make_tmpname(basename, n), tmpdir)
131     yield(path, n, *opts)
132   rescue Errno::EEXIST
133     n ||= 0
134     n += 1
135     retry if !max_try or n < max_try
136     raise _("cannot generate temporary name using `%{basename}' under `%{tmpdir}'") % { basename: basename, tmpdir: tmpdir }
137   end
138   path
139 end
make_tmpname(prefix_suffix, n) click to toggle source
    # File lib/puppet/file_system/uniquefile.rb
100 def make_tmpname(prefix_suffix, n)
101   case prefix_suffix
102     when String
103       prefix = prefix_suffix
104       suffix = ""
105     when Array
106       prefix = prefix_suffix[0]
107       suffix = prefix_suffix[1]
108     else
109       raise ArgumentError, _("unexpected prefix_suffix: %{value}") % { value: prefix_suffix.inspect }
110   end
111   t = Time.now.strftime("%Y%m%d")
112   path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
113   path << "-#{n}" if n
114   path << suffix
115 end
tmpdir() click to toggle source
    # File lib/puppet/file_system/uniquefile.rb
151 def tmpdir
152   tmp = '.'
153   for dir in [ Puppet::Util.get_env('TMPDIR'), Puppet::Util.get_env('TMP'), Puppet::Util.get_env('TEMP'), @@systmpdir, '/tmp']
154     stat = File.stat(dir) if dir
155     if stat && stat.directory? && stat.writable?
156       tmp = dir
157       break
158     end rescue nil
159   end
160   File.expand_path(tmp)
161 end
try_convert_to_hash(h) click to toggle source
    # File lib/puppet/file_system/uniquefile.rb
141 def try_convert_to_hash(h)
142   begin
143     h.to_hash
144   rescue NoMethodError
145     nil
146   end
147 end