Class: Puppet::FileSystem::Uniquefile
- Defined in:
- lib/puppet/file_system/uniquefile.rb
Overview
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.
Class Method Summary collapse
-
.locking(tmpname) ⇒ Object
yields with locking for
tmpname
and returns the result of the block. - .mkdir(*args) ⇒ Object
-
.open_tmp(identifier) {|file| ... } ⇒ Object
private
Convenience method which ensures that the file is closed and unlinked before returning.
- .rmdir(*args) ⇒ Object
Instance Method Summary collapse
- #close(unlink_now = false) ⇒ Object
- #close! ⇒ Object
-
#initialize(basename, *rest) ⇒ Uniquefile
constructor
A new instance of Uniquefile.
-
#open ⇒ Object
Opens or reopens the file with mode “r+”.
-
#path ⇒ Object
Returns the full path name of the temporary file.
- #unlink ⇒ Object (also: #delete)
Constructor Details
#initialize(basename, *rest) ⇒ Uniquefile
Returns a new instance of Uniquefile.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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 |
Class Method Details
.locking(tmpname) ⇒ Object
yields with locking for tmpname
and returns the result of the block.
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/puppet/file_system/uniquefile.rb', line 167 def locking(tmpname) lock = tmpname + '.lock' mkdir(lock) yield rescue Errno::ENOENT => e ex = Errno::ENOENT.new("A directory component in #{lock} does not exist or is a dangling symbolic link") ex.set_backtrace(e.backtrace) raise ex ensure rmdir(lock) if Puppet::FileSystem.exist?(lock) end |
.mkdir(*args) ⇒ Object
179 180 181 |
# File 'lib/puppet/file_system/uniquefile.rb', line 179 def mkdir(*args) Dir.mkdir(*args) end |
.open_tmp(identifier) {|file| ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convenience method which ensures that the file is closed and unlinked before returning
18 19 20 21 22 23 24 25 |
# 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 |
.rmdir(*args) ⇒ Object
183 184 185 |
# File 'lib/puppet/file_system/uniquefile.rb', line 183 def rmdir(*args) Dir.rmdir(*args) end |
Instance Method Details
#close(unlink_now = false) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/puppet/file_system/uniquefile.rb', line 66 def close(unlink_now=false) if unlink_now close! else _close end end |
#close! ⇒ Object
74 75 76 77 |
# File 'lib/puppet/file_system/uniquefile.rb', line 74 def close! _close unlink end |
#open ⇒ Object
Opens or reopens the file with mode “r+”.
51 52 53 54 55 |
# 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 ⇒ Object
Returns the full path name of the temporary file. This will be nil if #unlink has been called.
94 95 96 |
# File 'lib/puppet/file_system/uniquefile.rb', line 94 def path @tmpname end |
#unlink ⇒ Object Also known as: delete
79 80 81 82 83 84 85 86 87 88 89 |
# 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 |