summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parsedfile.rb
blob: 1b531b1a0eebf8dc063239832f2887ba901f257d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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 ParsedFile
        attr_reader :file

        # 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

        Puppet.config.setdefaults(:puppet,
            :filetimeout => [ 15,
                "The minimum time to wait between checking for updates in
                configuration files."
            ]
        )

        # Determine whether the file has changed and thus whether it should
        # be reparsed
        def changed?
            # Don't actually stat the file more often than filetimeout.
            if Time.now - @statted >= Puppet[:filetimeout]
                tmp = stamp()

                if tmp == @tstamp
                    return false
                else
                    @tstamp = tmp
                    return true
                end
            else
                return false
            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
            @tstamp = stamp()
        end

        private

        def stamp
            @statted = Time.now
            return File.stat(@file).ctime
        end
    end
end

# $Id$