# File lib/puppet/util/file_locking.rb, line 17
17:     def writelock(file, mode = nil)
18:         unless FileTest.directory?(File.dirname(file))
19:             raise Puppet::DevError, "Cannot create %s; directory %s does not exist" % [file, File.dirname(file)]
20:         end
21:         tmpfile = file + ".tmp"
22: 
23:         unless mode
24:             # It's far more likely that the file will be there than not, so it's
25:             # better to stat once to check for existence and mode.
26:             # If we can't stat, it's most likely because the file's not there,
27:             # but could also be because the directory isn't readable, in which case
28:             # we won't be able to write anyway.
29:             begin
30:                 mode = File.stat(file).mode
31:             rescue
32:                 mode = 0600
33:             end
34:         end
35: 
36:         Puppet::Util.sync(file).synchronize(Sync::EX) do
37:             File.open(file, "w", mode) do |rf|
38:                 rf.lock_exclusive do |lrf|
39:                     yield lrf
40:                 end
41:             end
42:         end
43:     end