Sha256: 0a09e16beca6f4f1a420d4835486d7dfec2e8e1d7883bb8ebf6b6bc3fb0502c7

Contents?: true

Size: 1.51 KB

Versions: 60

Compression:

Stored size: 1.51 KB

Contents

require 'puppet/util'

module Puppet::Util::FileLocking
  module_function

  # Create a shared lock for reading
  def readlock(file)
    raise ArgumentError, "#{file} is not a file" unless !File.exists?(file) or File.file?(file)
    Puppet::Util.synchronize_on(file,Sync::SH) do
      File.open(file) { |f|
        f.lock_shared { |lf| yield lf }
      }
    end
  end

  # Create an exclusive lock for writing, and do the writing in a
  # tmp file.
  def writelock(file, mode = nil)
    raise Puppet::DevError, "Cannot create #{file}; directory #{File.dirname(file)} does not exist" unless FileTest.directory?(File.dirname(file))
    raise ArgumentError, "#{file} is not a file" unless !File.exists?(file) or File.file?(file)
    tmpfile = file + ".tmp"

    unless mode
      # It's far more likely that the file will be there than not, so it's
      # better to stat once to check for existence and mode.
      # If we can't stat, it's most likely because the file's not there,
      # but could also be because the directory isn't readable, in which case
      # we won't be able to write anyway.
      begin
        mode = File.stat(file).mode
      rescue
        mode = 0600
      end
    end

    Puppet::Util.synchronize_on(file,Sync::EX) do
      File.open(file, File::Constants::CREAT | File::Constants::WRONLY, mode) do |rf|
        rf.lock_exclusive do |lrf|
          # poor's man open(2) O_EXLOCK|O_TRUNC
          lrf.seek(0, IO::SEEK_SET)
          lrf.truncate(0)
          yield lrf
        end
      end
    end
  end
end

Version data entries

60 entries across 60 versions & 4 rubygems

Version Path
puppet-parse-0.1.4 lib/vendor/puppet/util/file_locking.rb
puppet-parse-0.1.3 lib/vendor/puppet/util/file_locking.rb
puppet-parse-0.1.2 lib/vendor/puppet/util/file_locking.rb
puppet-parse-0.1.1 lib/vendor/puppet/util/file_locking.rb
puppet-2.7.26 lib/puppet/util/file_locking.rb
puppet-2.7.25 lib/puppet/util/file_locking.rb
puppet-2.7.24 lib/puppet/util/file_locking.rb
puppet-2.7.23 lib/puppet/util/file_locking.rb
puppet-2.7.22 lib/puppet/util/file_locking.rb
puppet-parse-0.1.0 lib/vendor/puppet/util/file_locking.rb
puppet-parse-0.0.6 lib/vendor/puppet/util/file_locking.rb
puppet-2.7.21 lib/puppet/util/file_locking.rb
puppet-2.6.18 lib/puppet/util/file_locking.rb
puppet-parse-0.0.5 lib/vendor/puppet/util/file_locking.rb
puppet-parse-0.0.4 lib/vendor/puppet/util/file_locking.rb
puppet-parse-0.0.2 lib/vendor/puppet/util/file_locking.rb
puppet-2.7.20 lib/puppet/util/file_locking.rb
puppet-2.7.20.rc1 lib/puppet/util/file_locking.rb
librarian-puppet-0.9.4 vendor/gems/ruby/1.8/gems/puppet-2.7.18/lib/puppet/util/file_locking.rb
puppet-2.7.19 lib/puppet/util/file_locking.rb