738: def readwritelock(default, *args, &bloc)
739: file = value(get_config_file_default(default).name)
740: tmpfile = file + ".tmp"
741: sync = Sync.new
742: raise Puppet::DevError, "Cannot create #{file}; directory #{File.dirname(file)} does not exist" unless FileTest.directory?(File.dirname(tmpfile))
743:
744: sync.synchronize(Sync::EX) do
745: File.open(file, ::File::CREAT|::File::RDWR, 0600) do |rf|
746: rf.lock_exclusive do
747: if File.exist?(tmpfile)
748: raise Puppet::Error, ".tmp file already exists for #{file}; Aborting locked write. Check the .tmp file and delete if appropriate"
749: end
750:
751:
752: begin
753: writesub(default, tmpfile, *args, &bloc)
754: rescue
755: File.unlink(tmpfile) if FileTest.exist?(tmpfile)
756: raise
757: end
758:
759: begin
760: File.rename(tmpfile, file)
761: rescue => detail
762: Puppet.err "Could not rename #{file} to #{tmpfile}: #{detail}"
763: File.unlink(tmpfile) if FileTest.exist?(tmpfile)
764: end
765: end
766: end
767: end
768: end