Sha256: 74dd73ab204ed3500085d766981407f8897f3604fe8031f8d9d9db1e23694e89

Contents?: true

Size: 1.97 KB

Versions: 16

Compression:

Stored size: 1.97 KB

Contents

# A simple class that tells us when a file has changed and thus whether we
# should reload it

require 'puppet'

module Puppet
    class NoSuchFile < Puppet::Error; end
    class Util::LoadedFile
        attr_reader :file, :statted

        # Provide a hook for setting the timestamp during testing, so we don't
        # have to depend on the granularity of the filesystem.
        attr_writer :tstamp

        # Determine whether the file has changed and thus whether it should
        # be reparsed.
        def changed?
            # Allow the timeout to be disabled entirely.
            if Puppet[:filetimeout] < 0
                return true
            end
            tmp = stamp()

            # We use a different internal variable than the stamp method
            # because it doesn't keep historical state and we do -- that is,
            # we will always be comparing two timestamps, whereas
            # stamp() just always wants the latest one.
            if tmp == @tstamp
                return false
            else
                @tstamp = tmp
                return @tstamp
            end
        end

        # Create the file.  Must be passed the file path.
        def initialize(file)
            @file = file
            unless FileTest.exists?(@file)
                raise Puppet::NoSuchFile,
                    "Can not use a non-existent file for parsing"
            end
            @statted = 0
            @stamp = nil
            @tstamp = stamp()
        end

        # Retrieve the filestamp, but only refresh it if we're beyond our
        # filetimeout
        def stamp
            if @stamp.nil? or (Time.now.to_i - @statted >= Puppet[:filetimeout])
                @statted = Time.now.to_i
                begin
                    @stamp = File.stat(@file).ctime
                rescue Errno::ENOENT
                    @stamp = Time.now
                end
            end
            return @stamp
        end

        def to_s
            @file
        end
    end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
puppet-0.25.5 lib/puppet/util/loadedfile.rb
puppet-0.25.4 lib/puppet/util/loadedfile.rb
puppet-0.25.3 lib/puppet/util/loadedfile.rb
puppet-0.24.9 lib/puppet/util/loadedfile.rb
puppet-0.25.2 lib/puppet/util/loadedfile.rb
puppet-0.25.1 lib/puppet/util/loadedfile.rb
puppet-0.25.0 lib/puppet/util/loadedfile.rb
puppet-0.24.0 lib/puppet/util/loadedfile.rb
puppet-0.24.3 lib/puppet/util/loadedfile.rb
puppet-0.24.4 lib/puppet/util/loadedfile.rb
puppet-0.24.1 lib/puppet/util/loadedfile.rb
puppet-0.24.2 lib/puppet/util/loadedfile.rb
puppet-0.24.7 lib/puppet/util/loadedfile.rb
puppet-0.24.6 lib/puppet/util/loadedfile.rb
puppet-0.24.5 lib/puppet/util/loadedfile.rb
puppet-0.24.8 lib/puppet/util/loadedfile.rb