class Puppet::FileServing::Metadata

A class that handles retrieving file metadata.

Constants

PARAM_ORDER

Attributes

checksum[R]
checksum_type[R]
destination[R]
ftype[R]
group[R]
mode[R]
owner[R]
path[R]

Public Instance Methods

checksum_type=(type) click to toggle source
# File lib/puppet/file_serving/metadata.rb, line 19
def checksum_type=(type)
  raise(ArgumentError, "Unsupported checksum type #{type}") unless respond_to?("#{type}_file")

  @checksum_type = type
end
collect(source_permissions = nil) click to toggle source

Retrieve the attributes for this file, relative to a base directory. Note that Puppet::FileSystem.stat raises Errno::ENOENT if the file is absent and this method does not catch that exception.

# File lib/puppet/file_serving/metadata.rb, line 119
def collect(source_permissions = nil)
  real_path = full_path

  stat = collect_stat(real_path, source_permissions)
  @owner = stat.owner
  @group = stat.group
  @ftype = stat.ftype

  # We have to mask the mode, yay.
  @mode = stat.mode & 007777

  case stat.ftype
  when "file"
    @checksum = ("{#{@checksum_type}}") + send("#{@checksum_type}_file", real_path).to_s
  when "directory" # Always just timestamp the directory.
    @checksum_type = "ctime"
    @checksum = ("{#{@checksum_type}}") + send("#{@checksum_type}_file", path).to_s
  when "link"
    @destination = Puppet::FileSystem.readlink(real_path)
    @checksum = ("{#{@checksum_type}}") + send("#{@checksum_type}_file", real_path).to_s rescue nil
  else
    raise ArgumentError, "Cannot manage files of type #{stat.ftype}"
  end
end
collect_stat(path, source_permissions) click to toggle source
# File lib/puppet/file_serving/metadata.rb, line 106
def collect_stat(path, source_permissions)
  stat = stat()

  if Puppet.features.microsoft_windows?
    WindowsStat.new(stat, path, source_permissions)
  else
    MetaStat.new(stat, source_permissions)
  end
end
to_data_hash() click to toggle source
# File lib/puppet/file_serving/metadata.rb, line 158
def to_data_hash
  super.update(
    {
      'owner'        => owner,
      'group'        => group,
      'mode'         => mode,
      'checksum'     => {
        'type'   => checksum_type,
        'value'  => checksum
      },
      'type'         => ftype,
      'destination'  => destination,

    }
  )
end
to_pson(*args) click to toggle source
# File lib/puppet/file_serving/metadata.rb, line 190
def to_pson(*args)
  to_pson_data_hash.to_pson(*args)
end
to_pson_data_hash() click to toggle source
# File lib/puppet/file_serving/metadata.rb, line 180
def to_pson_data_hash
  {
    'document_type' => 'FileMetadata',
    'data'          => to_data_hash,
    'metadata'      => {
      'api_version' => 1
      }
  }
end

Public Class Methods

from_data_hash(data) click to toggle source
# File lib/puppet/file_serving/metadata.rb, line 175
def self.from_data_hash(data)
  new(data.delete('path'), data)
end
from_pson(data) click to toggle source
# File lib/puppet/file_serving/metadata.rb, line 194
def self.from_pson(data)
  Puppet.deprecation_warning("from_pson is being removed in favour of from_data_hash.")
  self.from_data_hash(data)
end
new(path,data={}) click to toggle source
# File lib/puppet/file_serving/metadata.rb, line 144
def initialize(path,data={})
  @owner       = data.delete('owner')
  @group       = data.delete('group')
  @mode        = data.delete('mode')
  if checksum = data.delete('checksum')
    @checksum_type = checksum['type']
    @checksum      = checksum['value']
  end
  @checksum_type ||= Puppet[:digest_algorithm]
  @ftype       = data.delete('type')
  @destination = data.delete('destination')
  super(path,data)
end