summaryrefslogtreecommitdiffstats
path: root/lib/puppet/file_serving
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-10-22 22:33:06 -0500
committerLuke Kanies <luke@madstop.com>2007-10-22 22:33:06 -0500
commit7fa99b08f9aa3777fba82f24eb5bb391f3758f48 (patch)
treea9dc23c0cdf3ecbc4a177086a5c699ad4f3b00f2 /lib/puppet/file_serving
parent688fcdf11a685dfda297beff50de8d4c751494d9 (diff)
downloadpuppet-7fa99b08f9aa3777fba82f24eb5bb391f3758f48.tar.gz
puppet-7fa99b08f9aa3777fba82f24eb5bb391f3758f48.tar.xz
puppet-7fa99b08f9aa3777fba82f24eb5bb391f3758f48.zip
Link handling is now in the file serving classes.
This was done by putting all of the functionality in the Content and Metadata class (actually, in a new base class for them). There are still some issues, and there need to be integration tests between the :local (soon to be renamed :file) termini for these classes.
Diffstat (limited to 'lib/puppet/file_serving')
-rw-r--r--lib/puppet/file_serving/content.rb18
-rw-r--r--lib/puppet/file_serving/file_base.rb46
-rw-r--r--lib/puppet/file_serving/metadata.rb35
3 files changed, 74 insertions, 25 deletions
diff --git a/lib/puppet/file_serving/content.rb b/lib/puppet/file_serving/content.rb
index 38ca80fb0..3cb428e63 100644
--- a/lib/puppet/file_serving/content.rb
+++ b/lib/puppet/file_serving/content.rb
@@ -4,30 +4,28 @@
require 'puppet/indirector'
require 'puppet/file_serving'
+require 'puppet/file_serving/file_base'
require 'puppet/file_serving/terminus_selector'
# A class that handles retrieving file contents.
# It only reads the file when its content is specifically
# asked for.
-class Puppet::FileServing::Content
+class Puppet::FileServing::Content < Puppet::FileServing::FileBase
extend Puppet::Indirector
indirects :file_content, :extend => Puppet::FileServing::TerminusSelector
attr_reader :path
- def content
- ::File.read(@path)
- end
-
- def initialize(path)
- raise ArgumentError.new("Files must be fully qualified") unless path =~ /^#{::File::SEPARATOR}/
- raise ArgumentError.new("Files must exist") unless FileTest.exists?(path)
+ # Read the content of our file in.
+ def content(base = nil)
+ # This stat can raise an exception, too.
+ raise(ArgumentError, "Cannot read the contents of links unless following links") if stat(base).ftype == "symlink"
- @path = path
+ ::File.read(full_path(base))
end
# Just return the file contents as the yaml. This allows us to
- # avoid escaping or any such thing. LAK:FIXME Not really sure how
+ # avoid escaping or any such thing. LAK:NOTE Not really sure how
# this will behave if the file contains yaml... I think the far
# side needs to understand that it's a plain string.
def to_yaml
diff --git a/lib/puppet/file_serving/file_base.rb b/lib/puppet/file_serving/file_base.rb
new file mode 100644
index 000000000..b2e9a0656
--- /dev/null
+++ b/lib/puppet/file_serving/file_base.rb
@@ -0,0 +1,46 @@
+#
+# Created by Luke Kanies on 2007-10-22.
+# Copyright (c) 2007. All rights reserved.
+
+require 'puppet/file_serving'
+
+# The base class for Content and Metadata; provides common
+# functionality like the behaviour around links.
+class Puppet::FileServing::FileBase
+ attr_accessor :path, :base_path
+
+ def full_path(base = nil)
+ base ||= base_path || raise(ArgumentError, "You must set or provide a base path")
+
+ full = File.join(base, self.path)
+ end
+
+ def initialize(path, options = {})
+ raise ArgumentError.new("Files must not be fully qualified") if path =~ /^#{::File::SEPARATOR}/
+
+ @path = path
+ @links = :manage
+
+ options.each do |param, value|
+ begin
+ send param.to_s + "=", value
+ rescue NoMethodError
+ raise ArgumentError, "Invalid option %s for %s" % [param, self.class]
+ end
+ end
+ end
+
+ attr_reader :links
+ def links=(value)
+ raise(ArgumentError, ":links can only be set to :manage or :follow") unless [:manage, :follow].include?(value)
+ @links = value
+ end
+
+ # Stat our file, using the appropriate link-sensitive method.
+ def stat(base = nil)
+ unless defined?(@stat_method)
+ @stat_method = self.links == :manage ? :lstat : :stat
+ end
+ File.send(@stat_method, full_path(base))
+ end
+end
diff --git a/lib/puppet/file_serving/metadata.rb b/lib/puppet/file_serving/metadata.rb
index 7adb66981..62ebccca9 100644
--- a/lib/puppet/file_serving/metadata.rb
+++ b/lib/puppet/file_serving/metadata.rb
@@ -5,17 +5,18 @@
require 'puppet'
require 'puppet/indirector'
require 'puppet/file_serving'
+require 'puppet/file_serving/file_base'
require 'puppet/util/checksums'
require 'puppet/file_serving/terminus_selector'
# A class that handles retrieving file metadata.
-class Puppet::FileServing::Metadata
+class Puppet::FileServing::Metadata < Puppet::FileServing::FileBase
include Puppet::Util::Checksums
extend Puppet::Indirector
indirects :file_metadata, :extend => Puppet::FileServing::TerminusSelector
- attr_reader :path, :owner, :group, :mode, :checksum_type, :checksum
+ attr_reader :path, :owner, :group, :mode, :checksum_type, :checksum, :ftype, :destination
def checksum_type=(type)
raise(ArgumentError, "Unsupported checksum type %s" % type) unless respond_to?("%s_file" % type)
@@ -23,32 +24,36 @@ class Puppet::FileServing::Metadata
@checksum_type = type
end
- def get_attributes
- stat = File.stat(path)
+ # Retrieve the attributes for this file, relative to a base directory.
+ # Note that File.stat raises Errno::ENOENT if the file is absent and this
+ # method does not catch that exception.
+ def collect_attributes(base = nil)
+ real_path = full_path(base)
+ stat = stat(base)
@owner = stat.uid
@group = stat.gid
+ @ftype = stat.ftype
+
# Set the octal mode, but as a string.
@mode = "%o" % (stat.mode & 007777)
- @checksum = get_checksum
- end
-
- def initialize(path = nil)
- if path
- raise ArgumentError.new("Files must be fully qualified") unless path =~ /^#{::File::SEPARATOR}/
- raise ArgumentError.new("Files must exist") unless FileTest.exists?(path)
-
- @path = path
+ if stat.ftype == "symlink"
+ @destination = File.readlink(real_path)
+ else
+ @checksum = get_checksum(real_path)
end
+ end
+ def initialize(*args)
@checksum_type = "md5"
+ super
end
private
# Retrieve our checksum.
- def get_checksum
- ("{%s}" % @checksum_type) + send("%s_file" % @checksum_type, @path)
+ def get_checksum(path)
+ ("{%s}" % @checksum_type) + send("%s_file" % @checksum_type, path)
end
end