Module: Omnibus::Digestable

Included in:
Builder, Fetcher, Package, Packager::Base, Project, Software
Defined in:
lib/omnibus/digestable.rb

Instance Method Summary collapse

Instance Method Details

#digest(path, type = :md5) ⇒ String

Calculate the digest of the file at the given path. Files are read in binary chunks to prevent Ruby from exploding.


34
35
36
37
38
39
# File 'lib/omnibus/digestable.rb', line 34

def digest(path, type = :md5)
  digest = digest_from_type(type)

  update_with_file_contents(digest, path)
  digest.hexdigest
end

#digest_directory(path, type = :md5) ⇒ String

Calculate the digest of a directory at the given path. Each file in the directory is read in binary chunks to prevent excess memory usage. Filesystem entries of all types are included in the digest, including directories, links, and sockets. The contents of non-file entries are represented as:

$type $path

while the contents of regular files are represented as:

file $path

and then appended by the binary contents of the file/


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/omnibus/digestable.rb', line 64

def digest_directory(path, type = :md5)
  digest = digest_from_type(type)

  FileSyncer.glob("#{path}/**/*").each do |filename|
    # Calculate the filename relative to the given path. Since directories
    # are SHAed according to their filepath, two difference directories on
    # disk would have different SHAs even if they had the same content.
    relative = Pathname.new(filename).relative_path_from(Pathname.new(path))

    case ftype = File.ftype(filename)
    when 'file'
      update_with_string(digest, "#{ftype} #{relative}")
      update_with_file_contents(digest, filename)
    else
      update_with_string(digest, "#{ftype} #{relative}")
    end
  end

  digest.hexdigest
end