# File lib/puppet/util/file_locking.rb, line 18
18:   def writelock(file, mode = nil)
19:     raise Puppet::DevError, "Cannot create #{file}; directory #{File.dirname(file)} does not exist" unless FileTest.directory?(File.dirname(file))
20:     raise ArgumentError, "#{file} is not a file" unless !File.exists?(file) or File.file?(file)
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.synchronize_on(file,Sync::EX) do
37:       File.open(file, File::Constants::CREAT | File::Constants::WRONLY, mode) do |rf|
38:         rf.lock_exclusive do |lrf|
39:           # poor's man open(2) O_EXLOCK|O_TRUNC
40:           lrf.seek(0, IO::SEEK_SET)
41:           lrf.truncate(0)
42:           yield lrf
43:         end
44:       end
45:     end
46:   end