Module: Omnibus::Digestable

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other) ⇒ Object



24
25
26
# File 'lib/omnibus/digestable.rb', line 24

def self.included(other)
  other.send(:include, Logging)
end

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.

Parameters:

  • path (String)

    the path of the file to digest

  • type (Symbol) (defaults to: :md5)

    the type of digest to use

Returns:

  • (String)

    the hexdigest of the file at the path



40
41
42
43
44
45
# File 'lib/omnibus/digestable.rb', line 40

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

  update_with_file_contents(digest, path)
  digest.hexdigest
end

#digest_directory(path, type = :md5, options = {}) ⇒ 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/

Parameters:

  • path (String)

    the path of the directory to digest

  • type (Symbol) (defaults to: :md5)

    the type of digest to use

  • options (Hash) (defaults to: {})

    options to pass through to the FileSyncer when scanning for files

Returns:

  • (String)

    the hexdigest of the directory



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omnibus/digestable.rb', line 72

def digest_directory(path, type = :md5, options = {})
  digest = digest_from_type(type)
  log.info(log_key) { "Digesting #{path} with #{type}" }
  FileSyncer.all_files_under(path, options).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