Sha256: caa31d2d5af18fac21f166009225b11ccc08e78ea10f28b38c044c49d1df91a3

Contents?: true

Size: 1.37 KB

Versions: 9

Compression:

Stored size: 1.37 KB

Contents

require 'puppet/util'

module Puppet::Util::FileLocking
    module_function

    # Create a shared lock for reading
    def readlock(file)
        Puppet::Util.sync(file).synchronize(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)
        unless FileTest.directory?(File.dirname(file))
            raise Puppet::DevError, "Cannot create %s; directory %s does not exist" % [file, File.dirname(file)]
        end
        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.sync(file).synchronize(Sync::EX) do
            File.open(file, "w", mode) do |rf|
                rf.lock_exclusive do |lrf|
                    yield lrf
                end
            end
        end
    end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
puppet-0.25.5 lib/puppet/util/file_locking.rb
puppet-0.25.4 lib/puppet/util/file_locking.rb
puppet-0.25.3 lib/puppet/util/file_locking.rb
puppet-0.24.9 lib/puppet/util/file_locking.rb
puppet-0.25.2 lib/puppet/util/file_locking.rb
puppet-0.25.1 lib/puppet/util/file_locking.rb
puppet-0.25.0 lib/puppet/util/file_locking.rb
puppet-0.24.7 lib/puppet/util/file_locking.rb
puppet-0.24.8 lib/puppet/util/file_locking.rb