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 28 def initialize(basename, *rest) 29 create_tmpname(basename, *rest) do |tmpname, n, opts| 30 mode = File::RDWR|File::CREAT|File::EXCL 31 perm = 0600 32 if opts 33 mode |= opts.delete(:mode) || 0 34 opts[:perm] = perm 35 perm = nil 36 else 37 opts = perm 38 end 39 self.class.locking(tmpname) do 40 @tmpfile = File.open(tmpname, mode, opts) 41 @tmpname = tmpname 42 end 43 @mode = mode & ~(File::CREAT|File::EXCL) 44 perm or opts.freeze 45 @opts = opts 46 end 47 48 super(@tmpfile) 49 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 19 def self.open_tmp(identifier) 20 f = new(identifier) 21 yield f 22 ensure 23 if f 24 f.close! 25 end 26 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 168 def locking(tmpname) 169 lock = tmpname + '.lock' 170 mkdir(lock) 171 yield 172 rescue Errno::ENOENT => e 173 ex = Errno::ENOENT.new("A directory component in #{lock} does not exist or is a dangling symbolic link") 174 ex.set_backtrace(e.backtrace) 175 raise ex 176 ensure 177 rmdir(lock) if Puppet::FileSystem.exist?(lock) 178 end
mkdir(*args)
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 180 def mkdir(*args) 181 Dir.mkdir(*args) 182 end
rmdir(*args)
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 184 def rmdir(*args) 185 Dir.rmdir(*args) 186 end
Public Instance Methods
close(unlink_now=false)
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 67 def close(unlink_now=false) 68 if unlink_now 69 close! 70 else 71 _close 72 end 73 end
close!()
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 75 def close! 76 _close 77 unlink 78 end
open()
click to toggle source
Opens or reopens the file with mode “r+”.
# File lib/puppet/file_system/uniquefile.rb 52 def open 53 @tmpfile.close if @tmpfile 54 @tmpfile = File.open(@tmpname, @mode, @opts) 55 __setobj__(@tmpfile) 56 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 95 def path 96 @tmpname 97 end
unlink()
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 80 def unlink 81 return unless @tmpname 82 begin 83 File.unlink(@tmpname) 84 rescue Errno::ENOENT 85 rescue Errno::EACCES 86 # may not be able to unlink on Windows; just ignore 87 return 88 end 89 @tmpname = nil 90 end
Also aliased as: delete
Protected Instance Methods
_close()
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 58 def _close 59 begin 60 @tmpfile.close if @tmpfile 61 ensure 62 @tmpfile = nil 63 end 64 end
Private Instance Methods
create_tmpname(basename, *rest) { |path, n, *opts| ... }
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 118 def create_tmpname(basename, *rest) 119 opts = try_convert_to_hash(rest[-1]) 120 if opts 121 opts = opts.dup if rest.pop.equal?(opts) 122 max_try = opts.delete(:max_try) 123 opts = [opts] 124 else 125 opts = [] 126 end 127 tmpdir, = *rest 128 tmpdir ||= tmpdir() 129 n = nil 130 begin 131 path = File.join(tmpdir, make_tmpname(basename, n)) 132 yield(path, n, *opts) 133 rescue Errno::EEXIST 134 n ||= 0 135 n += 1 136 retry if !max_try or n < max_try 137 raise _("cannot generate temporary name using `%{basename}' under `%{tmpdir}'") % { basename: basename, tmpdir: tmpdir } 138 end 139 path 140 end
make_tmpname(prefix_suffix, n)
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 101 def make_tmpname(prefix_suffix, n) 102 case prefix_suffix 103 when String 104 prefix = prefix_suffix 105 suffix = "" 106 when Array 107 prefix = prefix_suffix[0] 108 suffix = prefix_suffix[1] 109 else 110 raise ArgumentError, _("unexpected prefix_suffix: %{value}") % { value: prefix_suffix.inspect } 111 end 112 t = Time.now.strftime("%Y%m%d") 113 path = "#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}" 114 path << "-#{n}" if n 115 path << suffix 116 end
tmpdir()
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 152 def tmpdir 153 tmp = '.' 154 for dir in [ ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp'] 155 stat = File.stat(dir) if dir 156 if stat && stat.directory? && stat.writable? 157 tmp = dir 158 break 159 end rescue nil 160 end 161 File.expand_path(tmp) 162 end
try_convert_to_hash(h)
click to toggle source
# File lib/puppet/file_system/uniquefile.rb 142 def try_convert_to_hash(h) 143 begin 144 h.to_hash 145 rescue NoMethodError 146 nil 147 end 148 end