summaryrefslogtreecommitdiffstats
path: root/lib/puppet/file_bucket
diff options
context:
space:
mode:
authorPaul Berry <paul@puppetlabs.com>2011-01-11 15:24:10 -0800
committerPaul Berry <paul@puppetlabs.com>2011-01-12 16:29:15 -0800
commit89f56920f26544f7c5aa97785567b193034db151 (patch)
treee8b3697de660c34bb353bc40fac0e9338a2f1cfc /lib/puppet/file_bucket
parent9cfd3d58699b0d0c3ab53cb37226cade84d7ec64 (diff)
downloadpuppet-89f56920f26544f7c5aa97785567b193034db151.tar.gz
puppet-89f56920f26544f7c5aa97785567b193034db151.tar.xz
puppet-89f56920f26544f7c5aa97785567b193034db151.zip
(#5838) Implemented the "head" method for FileBucketFile::File terminus.
In order to do this it was necessary to refactor FileBucketFile to untangle responsibilities for computing paths, reading files, etc. In the process, removed speculative generalizations and unused functionality. Paired-with: Jesse Wolfe <jesse@puppetlabs.com>
Diffstat (limited to 'lib/puppet/file_bucket')
-rw-r--r--lib/puppet/file_bucket/dipper.rb2
-rw-r--r--lib/puppet/file_bucket/file.rb108
2 files changed, 15 insertions, 95 deletions
diff --git a/lib/puppet/file_bucket/dipper.rb b/lib/puppet/file_bucket/dipper.rb
index dbfcdcd43..b012a8681 100644
--- a/lib/puppet/file_bucket/dipper.rb
+++ b/lib/puppet/file_bucket/dipper.rb
@@ -33,7 +33,7 @@ class Puppet::FileBucket::Dipper
raise(ArgumentError, "File #{file} does not exist") unless ::File.exist?(file)
contents = ::File.read(file)
begin
- file_bucket_file = Puppet::FileBucket::File.new(contents, :bucket_path => @local_path, :path => absolutize_path(file) )
+ file_bucket_file = Puppet::FileBucket::File.new(contents, :bucket_path => @local_path)
dest_path = "#{@rest_path}#{file_bucket_file.name}"
file_bucket_file.save(dest_path)
diff --git a/lib/puppet/file_bucket/file.rb b/lib/puppet/file_bucket/file.rb
index 96fd8e225..08c0329f1 100644
--- a/lib/puppet/file_bucket/file.rb
+++ b/lib/puppet/file_bucket/file.rb
@@ -1,10 +1,9 @@
require 'puppet/file_bucket'
require 'puppet/indirector'
require 'puppet/util/checksums'
+require 'digest/md5'
class Puppet::FileBucket::File
- include Puppet::Util::Checksums
-
# This class handles the abstract notion of a file in a filebucket.
# There are mechanisms to save and load this file locally and remotely in puppet/indirector/filebucketfile/*
# There is a compatibility class that emulates pre-indirector filebuckets in Puppet::FileBucket::Dipper
@@ -12,71 +11,27 @@ class Puppet::FileBucket::File
require 'puppet/file_bucket/file/indirection_hooks'
indirects :file_bucket_file, :terminus_class => :file, :extend => Puppet::FileBucket::File::IndirectionHooks
- attr :path, true
- attr :paths, true
- attr :contents, true
- attr :checksum_type
- attr :bucket_path, true
-
- def self.default_checksum_type
- "md5"
- end
+ attr :contents
+ attr :bucket_path
def initialize( contents, options = {} )
- @bucket_path = options[:bucket_path]
- @path = options[:path]
- @paths = options[:paths] || []
-
- @checksum = options[:checksum]
- @checksum_type = options[:checksum_type]
-
- self.contents = contents
-
- yield(self) if block_given?
-
- validate!
- end
+ raise ArgumentError if !contents.is_a?(String)
+ @contents = contents
- def validate!
- validate_checksum_type!(checksum_type)
- validate_checksum!(checksum) if checksum
+ @bucket_path = options.delete(:bucket_path)
+ raise ArgumentError if options != {}
end
- def contents=(str)
- raise "You may not change the contents of a FileBucket File" if @contents
- validate_content!(str)
- @contents = str
+ def checksum_type
+ 'md5'
end
def checksum
- return @checksum if @checksum
- @checksum = calculate_checksum if contents
- @checksum
- end
-
- def checksum=(checksum)
- validate_checksum!(checksum)
- @checksum = checksum
- end
-
- def checksum_type=( new_checksum_type )
- @checksum = nil
- @checksum_type = new_checksum_type
- end
-
- def checksum_type
- unless @checksum_type
- if @checksum
- @checksum_type = sumtype(checksum)
- else
- @checksum_type = self.class.default_checksum_type
- end
- end
- @checksum_type
+ "{#{checksum_type}}#{checksum_data}"
end
def checksum_data
- sumdata(checksum)
+ @checksum_data ||= Digest::MD5.hexdigest(contents)
end
def to_s
@@ -84,18 +39,7 @@ class Puppet::FileBucket::File
end
def name
- [checksum_type, checksum_data, path].compact.join('/')
- end
-
- def name=(name)
- data = name.split('/',3)
- self.path = data.pop
- @checksum_type = nil
- self.checksum = "{#{data[0]}}#{data[1]}"
- end
-
- def conflict_check?
- true
+ "#{checksum_type}/#{checksum_data}"
end
def self.from_s( contents )
@@ -103,34 +47,10 @@ class Puppet::FileBucket::File
end
def to_pson
- hash = { "contents" => contents }
- hash["path"] = @path if @path
- hash.to_pson
+ { "contents" => contents }.to_pson
end
def self.from_pson( pson )
- self.new( pson["contents"], :path => pson["path"] )
- end
-
- private
-
- def calculate_checksum
- "{#{checksum_type}}" + send(checksum_type, contents)
- end
-
- def validate_content!(content)
- raise ArgumentError, "Contents must be a string" if content and ! content.is_a?(String)
- end
-
- def validate_checksum!(new_checksum)
- newtype = sumtype(new_checksum)
-
- unless sumdata(new_checksum) == (calc_sum = send(newtype, contents))
- raise Puppet::Error, "Checksum #{new_checksum} does not match contents #{calc_sum}"
- end
- end
-
- def validate_checksum_type!(type)
- raise ArgumentError, "Invalid checksum type #{type}" unless respond_to?(type)
+ self.new( pson["contents"] )
end
end