Class: Puppet::FileSystem::Uniquefile

Inherits:
File
  • Object
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

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.


175
176
177
178
179
180
181
# File 'lib/puppet/file_system/uniquefile.rb', line 175

def locking(tmpname)
  lock = tmpname + '.lock'
  mkdir(lock)
  yield
ensure
  rmdir(lock) if Puppet::FileSystem.exist?(lock)
end

.mkdir(*args) ⇒ Object


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

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

Yield Parameters:

  • file (File)

    the temporary file object


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


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

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

#openObject

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

#pathObject

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

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