summaryrefslogtreecommitdiffstats
path: root/lib/puppet/file_serving/metadata.rb
blob: 29a395c6ad4a6abf3f53a2aa4598c68b1ecafaa6 (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
#
#  Created by Luke Kanies on 2007-10-16.
#  Copyright (c) 2007. All rights reserved.

require 'puppet'
require 'puppet/indirector'
require 'puppet/file_serving'
require 'puppet/util/checksums'

# A class that handles retrieving file metadata.
class Puppet::FileServing::Metadata
    include Puppet::Util::Checksums

    extend Puppet::Indirector
    indirects :file_metadata, :terminus_class => :ral

    attr_reader :path, :owner, :group, :mode, :checksum_type, :checksum

    def checksum_type=(type)
        raise(ArgumentError, "Unsupported checksum type %s" % type) unless respond_to?("%s_file" % type)

        @checksum_type = type
    end

    def initialize(path, checksum_type = "md5")
        raise ArgumentError.new("Files must be fully qualified") unless path =~ /^#{::File::SEPARATOR}/
        raise ArgumentError.new("Files must exist") unless FileTest.exists?(path)

        @path = path

        stat = File.stat(path)
        @owner = stat.uid
        @group = stat.gid

        # Set the octal mode, but as a string.
        @mode = "%o" % (stat.mode & 007777)

        @checksum_type = checksum_type
        @checksum = get_checksum
    end

    private

    # Retrieve our checksum.
    def get_checksum
        ("{%s}" % @checksum_type) + send("%s_file" % @checksum_type, @path)
    end
end