Class: RunLoop::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/run_loop/directory.rb

Overview

Class for performing operations on directories.

Class Method Summary collapse

Class Method Details

.directory_digest(path) ⇒ Object

Computes the digest of directory.

Parameters:

  • path

    A path to a directory.

Raises:

  • ArgumentError When ‘path` is not a directory or path does not exist.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/run_loop/directory.rb', line 26

def self.directory_digest(path)

  unless File.exist?(path)
    raise ArgumentError, "Expected '#{path}' to exist"
  end

  unless File.directory?(path)
    raise ArgumentError, "Expected '#{path}' to be a directory"
  end

  entries = self.recursive_glob_for_entries(path)

  if entries.empty?
    raise ArgumentError, "Expected a non-empty dir at '#{path}' found '#{entries}'"
  end

  debug = RunLoop::Environment.debug?

  sha = OpenSSL::Digest::SHA256.new
  self.recursive_glob_for_entries(path).each do |file|
    if File.directory?(file)
      # skip directories
    elsif !Pathname.new(file).exist?
      # skip broken symlinks
    else
      case File.ftype(file)
        when 'fifo'
          RunLoop.log_warn("SHA1 SKIPPING FIFO #{file}") if debug
        when 'socket'
          RunLoop.log_warn("SHA1 SKIPPING SOCKET #{file}") if debug
        when 'characterSpecial'
          RunLoop.log_warn("SHA1 SKIPPING CHAR SPECIAL #{file}") if debug
        when 'blockSpecial'
          RunLoop.log_warn("SHA1 SKIPPING BLOCK SPECIAL #{file}") if debug
        else
          sha << File.read(file)
      end
    end
  end
  sha.hexdigest
end

.recursive_glob_for_entries(base_dir) ⇒ Object

Dir.glob ignores files that start with ‘.’, but we often need to find dotted files and directories.

Ruby 2.* does the right thing by ignoring ‘..’ and ‘.’.

Ruby < 2.0 includes ‘..’ and ‘.’ in results which causes problems for some of run-loop’s internal methods. In particular ‘reset_app_sandbox`.



16
17
18
19
20
# File 'lib/run_loop/directory.rb', line 16

def self.recursive_glob_for_entries(base_dir)
  Dir.glob("#{base_dir}/{**/.*,**/*}").select do |entry|
    !(entry.end_with?('..') || entry.end_with?('.'))
  end
end