Sha256: d5b2c174914709edb311f20a8958f2bc58e1c0b796983980025ef2311b749fd0

Contents?: true

Size: 1.01 KB

Versions: 64

Compression:

Stored size: 1.01 KB

Contents

# This is an "interface" that should be implemented by any digest class
# passed into FileChecksum. Note that this isn't strictly enforced at
# the moment, and this class isn't directly used. It is merely here for
# documentation of structure of the class.
class DigestClass
  def update(string); end
  def hexdigest; end
end

class FileChecksum
  BUFFER_SIZE = 1024

  # Initializes an object to calculate the checksum of a file. The given
  # ``digest_klass`` should implement the ``DigestClass`` interface. Note
  # that the built-in Ruby digest classes duck type this properly:
  # Digest::MD5, Digest::SHA1, etc.
  def initialize(path, digest_klass)
    @digest_klass = digest_klass
    @path   = path
  end

  # This calculates the checksum of the file and returns it as a
  # string.
  #
  # @return [String]
  def checksum
    digest= @digest_klass.new

    File.open(@path, "r") do |f|
      while !f.eof
        buf = f.readpartial(BUFFER_SIZE)
        digest.update(buf)
      end
    end

    return digest.hexdigest
  end
end

Version data entries

64 entries across 64 versions & 11 rubygems

Version Path
tamtam-vagrant-reload-1.1.3 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/file_checksum.rb
tamtam-vagrant-reload-1.1.2 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/file_checksum.rb
tamtam-vagrant-reload-1.1.1 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/file_checksum.rb
tamtam-vagrant-reload-1.1 vendor/cache/vagrant-0ac2a8738841/lib/vagrant/util/file_checksum.rb
bmhatfield-vagrant-1.0.10 lib/vagrant/util/file_checksum.rb
bmhatfield-vagrant-1.0.9 lib/vagrant/util/file_checksum.rb
tnargav-1.3.6 lib/vagrant/util/file_checksum.rb
tnargav-1.3.3 lib/vagrant/util/file_checksum.rb
bmhatfield-vagrant-1.0.8 lib/vagrant/util/file_checksum.rb
vagrant-shell-0.2.9 demo/templates/vendor/bundle/ruby/1.9.1/gems/tnargav-1.2.2/lib/vagrant/util/file_checksum.rb
tnargav-1.2.3 lib/vagrant/util/file_checksum.rb
vagrant-shell-0.2.8 demo/templates/vendor/bundle/ruby/1.9.1/gems/tnargav-1.2.2/lib/vagrant/util/file_checksum.rb
bmhatfield-vagrant-1.0.7 lib/vagrant/util/file_checksum.rb
vagrant-shell-0.2.6 vendor/bundle/gems/tnargav-1.2.2/lib/vagrant/util/file_checksum.rb
vagrant-shell-0.2.5 vendor/bundle/gems/tnargav-1.2.2/lib/vagrant/util/file_checksum.rb
tnargav-1.2.2 lib/vagrant/util/file_checksum.rb
vagrantup-1.1.3 lib/vagrant/util/file_checksum.rb
vagrantup-1.1.2 lib/vagrant/util/file_checksum.rb
vagrantup-1.1.1 lib/vagrant/util/file_checksum.rb
vagrantup-1.1.0 lib/vagrant/util/file_checksum.rb