Method: Puppet::Util::Settings#readwritelock

Defined in:
lib/vendor/puppet/util/settings.rb

#readwritelock(default, *args, &bloc) ⇒ Object

Raises:



729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
# File 'lib/vendor/puppet/util/settings.rb', line 729

def readwritelock(default, *args, &bloc)
  file = value(get_config_file_default(default).name)
  tmpfile = file + ".tmp"
  sync = Sync.new
  raise Puppet::DevError, "Cannot create #{file}; directory #{File.dirname(file)} does not exist" unless FileTest.directory?(File.dirname(tmpfile))

  sync.synchronize(Sync::EX) do
    File.open(file, ::File::CREAT|::File::RDWR, 0600) do |rf|
      rf.lock_exclusive do
        if File.exist?(tmpfile)
          raise Puppet::Error, ".tmp file already exists for #{file}; Aborting locked write. Check the .tmp file and delete if appropriate"
        end

        # If there's a failure, remove our tmpfile
        begin
          writesub(default, tmpfile, *args, &bloc)
        rescue
          File.unlink(tmpfile) if FileTest.exist?(tmpfile)
          raise
        end

        begin
          File.rename(tmpfile, file)
        rescue => detail
          Puppet.err "Could not rename #{file} to #{tmpfile}: #{detail}"
          File.unlink(tmpfile) if FileTest.exist?(tmpfile)
        end
      end
    end
  end
end