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
tmpnameand 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)
Methods inherited from File
Constructor Details
#initialize(basename, *rest) ⇒ Uniquefile
Returns a new instance of Uniquefile.
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 30 def initialize(basename, *rest) create_tmpname(basename, *rest) do |tmpname, _n, opts| mode = File::RDWR | File::CREAT | File::EXCL if opts mode |= opts.delete(:mode) || 0 opts.delete(:perm) else opts = {} end self.class.locking(tmpname) do @tmpfile = File.open(tmpname, mode, 0o600, **opts) @tmpname = tmpname end @mode = mode & ~(File::CREAT | File::EXCL) @opts = opts.freeze 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
21 22 23 24 25 26 27 28 |
# File 'lib/puppet/file_system/uniquefile.rb', line 21 def self.open_tmp(identifier) f = new(identifier) yield f ensure if f f.close! end end |
Instance Method Details
#close(unlink_now = false) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/puppet/file_system/uniquefile.rb', line 64 def close(unlink_now = false) if unlink_now close! else _close end end |
#close! ⇒ Object
72 73 74 75 |
# File 'lib/puppet/file_system/uniquefile.rb', line 72 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, 0o600, **@opts) __setobj__(@tmpfile) end |
#path ⇒ Object
Returns the full path name of the temporary file. This will be nil if #unlink has been called.
93 94 95 |
# File 'lib/puppet/file_system/uniquefile.rb', line 93 def path @tmpname end |
#unlink ⇒ Object Also known as: delete
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/puppet/file_system/uniquefile.rb', line 77 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 |