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