Sha256: ff01d169272aa189c65eaa9489a60d1e5c99bcd3b8af336c5b15323b4d0617ed

Contents?: true

Size: 1.21 KB

Versions: 46

Compression:

Stored size: 1.21 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 * 8

  # 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
    buf = ''

    File.open(@path, "rb") do |f|
      while !f.eof
        begin
          f.readpartial(BUFFER_SIZE, buf)
          digest.update(buf)
        rescue EOFError
          # Although we check for EOF earlier, this seems to happen
          # sometimes anyways [GH-2716].
          break
        end
      end
    end

    return digest.hexdigest
  end
end

Version data entries

46 entries across 39 versions & 6 rubygems

Version Path
vagrant-unbundled-2.2.5.0 lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.2.4.0 lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.2.3.0 lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.2.2.0 lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.2.0.0 lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.1.4.0 lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.1.2.0 lib/vagrant/util/file_checksum.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/file_checksum.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/file_checksum.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/file_checksum.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/file_checksum.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/file_checksum.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.1.1.0 lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.0.4.0 lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.0.3.0 lib/vagrant/util/file_checksum.rb
vagrant-aws-detiber-0.7.2.pre.4 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/lib/vagrant/util/file_checksum.rb
vagrant-aws-detiber-0.7.2.pre.3 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/lib/vagrant/util/file_checksum.rb
vagrant-aws-detiber-0.7.2.pre.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/lib/vagrant/util/file_checksum.rb
vagrant-unbundled-2.0.2.0 lib/vagrant/util/file_checksum.rb