Class: RunLoop::Directory
- Inherits:
-
Object
- Object
- RunLoop::Directory
- Defined in:
- lib/run_loop/directory.rb
Overview
Class for performing operations on directories.
Class Method Summary collapse
-
.directory_digest(path, options = {}) ⇒ Object
Computes the digest of directory.
- .iterate_for_size(entries) ⇒ Object
-
.recursive_glob_for_entries(base_dir) ⇒ Object
Dir.glob ignores files that start with ‘.’, but we often need to find dotted files and directories.
- .size(path, format) ⇒ Object
- .skip_file?(file, task, debug) ⇒ Boolean
Class Method Details
.directory_digest(path, options = {}) ⇒ Object
Computes the digest of directory.
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/run_loop/directory.rb', line 33 def self.directory_digest(path, ={}) = { :handle_errors_by => :raising } = .merge() handle_errors_by = [:handle_errors_by] unless [:raising, :logging, :ignoring].include?(handle_errors_by) raise ArgumentError, %Q{Expected :handle_errors_by to be :raising, :logging, or :ignoring; found '#{handle_errors_by}' } end 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).sort if entries.empty? raise ArgumentError, "Expected a non-empty dir at '#{path}' found '#{entries}'" end debug = RunLoop::Environment.debug? file_shas = [] cumulative = OpenSSL::Digest::SHA256.new entries.each do |path| if !self.skip_file?(path, "SHA256", debug) begin file_sha = OpenSSL::Digest::SHA256.new contents = File.read(path, {mode: "rb"}) file_sha << contents cumulative << contents file_shas << [file_sha.hexdigest] rescue => e case handle_errors_by when :logging = %Q{RunLoop::Directory.directory_digest raised an error: #{e} while trying to find the SHA of this file: #{path} This is not a fatal error; it can be ignored. } RunLoop.log_debug() when :raising raise e.class, e. when :ignoring # nop else # nop end end end end digest_of_digests = OpenSSL::Digest::SHA256.new digest_of_digests << file_shas.join("\n") # We have at least one example where the cumulative digest has an # unexpected value when computing the digest of an installed .app on an # iOS Simulator. I want return the cumulative.hexdigest in case there is # a client (end user) who is using this method. return digest_of_digests.hexdigest, cumulative.hexdigest end |
.iterate_for_size(entries) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/run_loop/directory.rb', line 184 def self.iterate_for_size(entries) debug = RunLoop::Environment.debug? size = 0 entries.each do |file| unless self.skip_file?(file, "SIZE", debug) begin size = size + File.size(file) rescue => e RunLoop.log_debug("Directory.iterate_for_size? rescued an ignorable error.") RunLoop.log_debug("#{e.class}: #{e.message}") end end end size 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 |
.size(path, format) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/run_loop/directory.rb', line 107 def self.size(path, format) allowed_formats = [:bytes, :kb, :mb, :gb] unless allowed_formats.include?(format) raise ArgumentError, "Expected '#{format}' to be one of #{allowed_formats.join(', ')}" end 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 size = self.iterate_for_size(entries) case format when :bytes size when :kb size/1000.0 when :mb size/1000.0/1000.0 when :gb size/1000.0/1000.0/1000.0 else # Not expected to reach this. size end end |
.skip_file?(file, task, debug) ⇒ Boolean
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/run_loop/directory.rb', line 147 def self.skip_file?(file, task, debug) skip = false begin if File.directory?(file) # Skip directories skip = true elsif !Pathname.new(file).exist? # Skip broken symlinks skip = true elsif !File.exist?(file) # Skip files that don't exist skip = true else case File.ftype(file) when 'fifo' RunLoop.log_warn("#{task} IS SKIPPING FIFO #{file}") if debug skip = true when 'socket' RunLoop.log_warn("#{task} IS SKIPPING SOCKET #{file}") if debug skip = true when 'characterSpecial' RunLoop.log_warn("#{task} IS SKIPPING CHAR SPECIAL #{file}") if debug skip = true when 'blockSpecial' skip = true RunLoop.log_warn("#{task} SKIPPING BLOCK SPECIAL #{file}") if debug else end end rescue => e skip = true RunLoop.log_debug("Directory.skip_file? rescued an ignorable error.") RunLoop.log_debug("#{e.class}: #{e.message}") end skip end |