Class: Puppet::FileSystem::Uniquefile

Inherits:
File show all
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

Instance Method Summary collapse

Methods inherited from File

exists?

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
49
50
51
# 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
    perm = 0o600
    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.



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/puppet/file_system/uniquefile.rb', line 170

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



182
183
184
# File 'lib/puppet/file_system/uniquefile.rb', line 182

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

Parameters:

  • identifier (String)

    additional part of generated pathname

Yield Parameters:

  • file (File)

    the temporary file object

Returns:

  • result of the passed block



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

.rmdir(*args) ⇒ Object



186
187
188
# File 'lib/puppet/file_system/uniquefile.rb', line 186

def rmdir(*args)
  Dir.rmdir(*args)
end

Instance Method Details

#close(unlink_now = false) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/puppet/file_system/uniquefile.rb', line 67

def close(unlink_now = false)
  if unlink_now
    close!
  else
    _close
  end
end

#close!Object



75
76
77
78
# File 'lib/puppet/file_system/uniquefile.rb', line 75

def close!
  _close
  unlink
end

#openObject

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



54
55
56
57
58
# File 'lib/puppet/file_system/uniquefile.rb', line 54

def open
  @tmpfile.close if @tmpfile
  @tmpfile = File.open(@tmpname, @mode, @opts)
  __setobj__(@tmpfile)
end

#pathObject

Returns the full path name of the temporary file. This will be nil if #unlink has been called.



96
97
98
# File 'lib/puppet/file_system/uniquefile.rb', line 96

def path
  @tmpname
end


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/puppet/file_system/uniquefile.rb', line 80

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