summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-02-14 20:13:44 -0600
committerLuke Kanies <luke@madstop.com>2008-02-14 20:13:44 -0600
commit6013b2500d9e799f8aba9f614de15d4eb477860a (patch)
tree6c134299d0e491cc46208a5122b8b8183f5bbd89
parentaebd303e267a4b830592ffe3551bd80647802a20 (diff)
downloadpuppet-6013b2500d9e799f8aba9f614de15d4eb477860a.tar.gz
puppet-6013b2500d9e799f8aba9f614de15d4eb477860a.tar.xz
puppet-6013b2500d9e799f8aba9f614de15d4eb477860a.zip
Refactoring the incremental checksum generation
slightly based on the code in type/file/checksum.rb.
-rw-r--r--lib/puppet/util/checksums.rb23
-rwxr-xr-xspec/unit/util/checksums.rb30
2 files changed, 24 insertions, 29 deletions
diff --git a/lib/puppet/util/checksums.rb b/lib/puppet/util/checksums.rb
index 0d45c6d5a..598b3adfa 100644
--- a/lib/puppet/util/checksums.rb
+++ b/lib/puppet/util/checksums.rb
@@ -9,20 +9,20 @@ module Puppet::Util::Checksums
# Calculate a checksum of the first 500 chars of the content using Digest::MD5.
def md5lite(content)
- md5(content[0..499])
+ md5(content[0..511])
end
# Calculate a checksum of a file's content using Digest::MD5.
- def md5_file(filename)
+ def md5_file(filename, lite = false)
require 'digest/md5'
digest = Digest::MD5.new()
- return checksum_file(digest, filename)
+ return checksum_file(digest, filename, lite)
end
# Calculate a checksum of the first 500 chars of a file's content using Digest::MD5.
def md5lite_file(filename)
- File.open(filename, "r") { |f| return md5(f.read(500)) }
+ md5_file(filename, true)
end
# Return the :mtime timestamp of a file.
@@ -38,20 +38,20 @@ module Puppet::Util::Checksums
# Calculate a checksum of the first 500 chars of the content using Digest::SHA1.
def sha1lite(content)
- sha1(content[0..499])
+ sha1(content[0..511])
end
# Calculate a checksum of a file's content using Digest::SHA1.
- def sha1_file(filename)
+ def sha1_file(filename, lite = false)
require 'digest/sha1'
digest = Digest::SHA1.new()
- return checksum_file(digest, filename)
+ return checksum_file(digest, filename, lite)
end
# Calculate a checksum of the first 500 chars of a file's content using Digest::SHA1.
def sha1lite_file(filename)
- File.open(filename, "r") { |f| return sha1(f.read(500)) }
+ sha1_file(filename, true)
end
# Return the :ctime of a file.
@@ -62,10 +62,11 @@ module Puppet::Util::Checksums
private
# Perform an incremental checksum on a file.
- def checksum_file(digest, filename)
+ def checksum_file(digest, filename, lite = false)
File.open(filename, 'r') do |file|
- file.each_line do |line|
- digest << line
+ while content = file.read(512)
+ digest << content
+ break if lite
end
end
diff --git a/spec/unit/util/checksums.rb b/spec/unit/util/checksums.rb
index 50c213538..31cf24f5b 100755
--- a/spec/unit/util/checksums.rb
+++ b/spec/unit/util/checksums.rb
@@ -13,16 +13,6 @@ describe Puppet::Util::Checksums do
@summer.extend(Puppet::Util::Checksums)
end
- class LineYielder
- def initialize(content)
- @content = content
- end
-
- def each_line
- @content.split("\n").each { |line| yield line }
- end
- end
-
content_sums = [:md5, :md5lite, :sha1, :sha1lite]
file_only = [:timestamp, :mtime]
@@ -51,8 +41,10 @@ describe Puppet::Util::Checksums do
file = "/path/to/my/file"
- # Mocha doesn't seem to be able to mock multiple yields, yay.
- fh = LineYielder.new("firstline\nsecondline")
+ fh = mock 'filehandle'
+ fh.expects(:read).with(512).times(3).returns("firstline").then.returns("secondline").then.returns(nil)
+ #fh.expects(:read).with(512).returns("secondline")
+ #fh.expects(:read).with(512).returns(nil)
File.expects(:open).with(file, "r").yields(fh)
@@ -67,23 +59,25 @@ describe Puppet::Util::Checksums do
{:md5lite => Digest::MD5, :sha1lite => Digest::SHA1}.each do |sum, klass|
describe("when using %s" % sum) do
- it "should use #{klass} to calculate string checksums from the first 500 characters of the string" do
+ it "should use #{klass} to calculate string checksums from the first 512 characters of the string" do
content = "this is a test" * 100
- klass.expects(:hexdigest).with(content[0..499]).returns "whatever"
+ klass.expects(:hexdigest).with(content[0..511]).returns "whatever"
@summer.send(sum, content).should == "whatever"
end
- it "should use #{klass} to calculate a sum from the first 500 characters in the file" do
+ it "should use #{klass} to calculate a sum from the first 512 characters in the file" do
digest = mock 'digest'
+ klass.expects(:new).returns digest
file = "/path/to/my/file"
fh = mock 'filehandle'
- File.expects(:open).with(file, "r").yields(fh)
+ fh.expects(:read).with(512).returns('my content')
- fh.expects(:read).with(500).returns('my content')
+ File.expects(:open).with(file, "r").yields(fh)
- klass.expects(:hexdigest).with("my content").returns :mydigest
+ digest.expects(:<<).with "my content"
+ digest.expects(:hexdigest).returns :mydigest
@summer.send(sum.to_s + "_file", file).should == :mydigest
end