Module: Puppet::Util::Checksums

Included in:
FileBucketFile::File, FileServing::Metadata
Defined in:
lib/vendor/puppet/util/checksums.rb

Overview

A stand-alone module for calculating checksums in a generic way.

Defined Under Namespace

Classes: FakeChecksum

Instance Method Summary collapse

Instance Method Details

#checksum?(string) ⇒ Boolean

Is the provided string a checksum?

Returns:

  • (Boolean)


14
15
16
# File 'lib/vendor/puppet/util/checksums.rb', line 14

def checksum?(string)
  string =~ /^\{(\w{3,5})\}\S+/
end

#ctime(content) ⇒ Object



110
111
112
# File 'lib/vendor/puppet/util/checksums.rb', line 110

def ctime(content)
  ""
end

#ctime_file(filename) ⇒ Object

Return the :ctime of a file.



104
105
106
# File 'lib/vendor/puppet/util/checksums.rb', line 104

def ctime_file(filename)
  File.stat(filename).send(:ctime)
end

#md5(content) ⇒ Object

Calculate a checksum using Digest::MD5.



29
30
31
# File 'lib/vendor/puppet/util/checksums.rb', line 29

def md5(content)
  Digest::MD5.hexdigest(content)
end

#md5_file(filename, lite = false) ⇒ Object

Calculate a checksum of a file’s content using Digest::MD5.



39
40
41
42
# File 'lib/vendor/puppet/util/checksums.rb', line 39

def md5_file(filename, lite = false)
  digest = Digest::MD5.new
  checksum_file(digest, filename,  lite)
end

#md5_stream {|digest| ... } ⇒ Object Also known as: md5lite_stream

Yields:

  • (digest)


49
50
51
52
53
# File 'lib/vendor/puppet/util/checksums.rb', line 49

def md5_stream(&block)
  digest = Digest::MD5.new
  yield digest
  digest.hexdigest
end

#md5lite(content) ⇒ Object

Calculate a checksum of the first 500 chars of the content using Digest::MD5.



34
35
36
# File 'lib/vendor/puppet/util/checksums.rb', line 34

def md5lite(content)
  md5(content[0..511])
end

#md5lite_file(filename) ⇒ Object

Calculate a checksum of the first 500 chars of a file’s content using Digest::MD5.



45
46
47
# File 'lib/vendor/puppet/util/checksums.rb', line 45

def md5lite_file(filename)
  md5_file(filename, true)
end

#mtime(content) ⇒ Object



70
71
72
# File 'lib/vendor/puppet/util/checksums.rb', line 70

def mtime(content)
  ""
end

#mtime_file(filename) ⇒ Object

Return the :mtime timestamp of a file.



58
59
60
# File 'lib/vendor/puppet/util/checksums.rb', line 58

def mtime_file(filename)
  File.stat(filename).send(:mtime)
end

#mtime_stream {|noop_digest| ... } ⇒ Object Also known as: ctime_stream

by definition this doesn’t exist but we still need to execute the block given

Yields:

  • (noop_digest)


64
65
66
67
68
# File 'lib/vendor/puppet/util/checksums.rb', line 64

def mtime_stream
  noop_digest = FakeChecksum.new
  yield noop_digest
  nil
end

#none(content) ⇒ Object



125
126
127
# File 'lib/vendor/puppet/util/checksums.rb', line 125

def none(content)
  ""
end

#none_file(filename) ⇒ Object

Return a “no checksum”



115
116
117
# File 'lib/vendor/puppet/util/checksums.rb', line 115

def none_file(filename)
  ""
end

#none_stream {|noop_digest| ... } ⇒ Object

Yields:

  • (noop_digest)


119
120
121
122
123
# File 'lib/vendor/puppet/util/checksums.rb', line 119

def none_stream
  noop_digest = FakeChecksum.new
  yield noop_digest
  ""
end

#sha1(content) ⇒ Object

Calculate a checksum using Digest::SHA1.



75
76
77
# File 'lib/vendor/puppet/util/checksums.rb', line 75

def sha1(content)
  Digest::SHA1.hexdigest(content)
end

#sha1_file(filename, lite = false) ⇒ Object

Calculate a checksum of a file’s content using Digest::SHA1.



85
86
87
88
# File 'lib/vendor/puppet/util/checksums.rb', line 85

def sha1_file(filename, lite = false)
  digest = Digest::SHA1.new
  checksum_file(digest, filename, lite)
end

#sha1_stream {|digest| ... } ⇒ Object Also known as: sha1lite_stream

Yields:

  • (digest)


95
96
97
98
99
# File 'lib/vendor/puppet/util/checksums.rb', line 95

def sha1_stream
  digest = Digest::SHA1.new
  yield digest
  digest.hexdigest
end

#sha1lite(content) ⇒ Object

Calculate a checksum of the first 500 chars of the content using Digest::SHA1.



80
81
82
# File 'lib/vendor/puppet/util/checksums.rb', line 80

def sha1lite(content)
  sha1(content[0..511])
end

#sha1lite_file(filename) ⇒ Object

Calculate a checksum of the first 500 chars of a file’s content using Digest::SHA1.



91
92
93
# File 'lib/vendor/puppet/util/checksums.rb', line 91

def sha1lite_file(filename)
  sha1_file(filename, true)
end

#sumdata(checksum) ⇒ Object

Strip the checksum type from an existing checksum



19
20
21
# File 'lib/vendor/puppet/util/checksums.rb', line 19

def sumdata(checksum)
  checksum =~ /^\{(\w+)\}(.+)/ ? $2 : nil
end

#sumtype(checksum) ⇒ Object

Strip the checksum type from an existing checksum



24
25
26
# File 'lib/vendor/puppet/util/checksums.rb', line 24

def sumtype(checksum)
  checksum =~ /^\{(\w+)\}/ ? $1 : nil
end