Class: Fingerprint::Scanner
- Inherits:
-
Object
- Object
- Fingerprint::Scanner
- Defined in:
- lib/fingerprint/scanner.rb
Overview
The scanner class can scan a set of directories and produce an index.
Instance Attribute Summary collapse
-
#digests ⇒ Object
readonly
Returns the value of attribute digests.
-
#recordset ⇒ Object
readonly
Returns the value of attribute recordset.
Class Method Summary collapse
-
.scan_paths(paths, options = {}) ⇒ Object
A helper function to scan a set of directories.
Instance Method Summary collapse
-
#excluded?(path) ⇒ Boolean
Returns true if the given path should be excluded.
-
#initialize(roots, options = {}) ⇒ Scanner
constructor
Initialize the scanner to scan a given set of directories in order.
-
#scan(recordset) ⇒ Object
Run the scanning process.
- #scan_path(path) ⇒ Object
- #valid_file?(path) ⇒ Boolean
Constructor Details
#initialize(roots, options = {}) ⇒ Scanner
Initialize the scanner to scan a given set of directories in order.
options[:excludes]
-
An array of regular expressions of files to avoid indexing.
options[:output]
-
An
IO
where the results will be written.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fingerprint/scanner.rb', line 42 def initialize(roots, = {}) @roots = roots @excludes = [:excludes] || [] @options = @digests = {} @progress = nil unless @options[:checksums] and @options[:checksums].size > 0 @options[:checksums] = DEFAULT_CHECKSUMS end @options[:checksums].each do |name| @digests[name] = CHECKSUMS[name].call end @callback = nil end |
Instance Attribute Details
#digests ⇒ Object (readonly)
Returns the value of attribute digests.
64 65 66 |
# File 'lib/fingerprint/scanner.rb', line 64 def digests @digests end |
#recordset ⇒ Object (readonly)
Returns the value of attribute recordset.
63 64 65 |
# File 'lib/fingerprint/scanner.rb', line 63 def recordset @recordset end |
Class Method Details
.scan_paths(paths, options = {}) ⇒ Object
A helper function to scan a set of directories.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/fingerprint/scanner.rb', line 283 def self.scan_paths(paths, = {}) if [:output] if .key? :recordset recordset = [:recordset] else recordset = RecordSet.new end [:recordset] = RecordSetPrinter.new(recordset, [:output]) end scanner = Scanner.new(paths, ) scanner.scan([:recordset]) return [:recordset] end |
Instance Method Details
#excluded?(path) ⇒ Boolean
Returns true if the given path should be excluded.
156 157 158 159 160 161 162 163 164 |
# File 'lib/fingerprint/scanner.rb', line 156 def excluded?(path) @excludes.each do |exclusion| if path.match(exclusion) return true end end return false end |
#scan(recordset) ⇒ Object
Run the scanning process.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/fingerprint/scanner.rb', line 183 def scan(recordset) excluded_count = 0 processed_count = 0 processed_size = 0 directory_count = 0 total_count = 0 total_size = 0 # Estimate the number of files and amount of data to process.. if @options[:progress] @roots.each do |root| Dir.chdir(root) do Find.find("./") do |path| if @options[:progress] $stderr.puts "# Scanning: #{path}" end if File.directory?(path) if excluded?(path) Find.prune # Ignore this directory end else # Skip anything that isn't a valid file (e.g. pipes, sockets, symlinks). if valid_file?(path) total_count += 1 total_size += File.size(path) end end end end end end if @options[:progress] @progress = lambda do |read_size| $stderr.puts "# Progress: File #{processed_count} / #{total_count}; Byte #{processed_size + read_size} / #{total_size} = #{sprintf('%0.3f%', (processed_size + read_size).to_f / total_size.to_f * 100.0)} (#{read_size}, #{processed_size}, #{total_size})" end end @roots.each do |root| Dir.chdir(root) do recordset << header_for(root) Find.find("./") do |path| if @options[:progress] $stderr.puts "# Path: #{path}" end if File.directory?(path) if excluded?(path) excluded_count += 1 if @options[:verbose] recordset << excluded_record_for(path) end Find.prune # Ignore this directory else directory_count += 1 recordset << directory_record_for(path) end else # Skip anything that isn't a valid file (e.g. pipes, sockets, symlinks). if valid_file?(path) recordset << file_record_for(path) processed_count += 1 processed_size += File.size(path) else excluded_count += 1 if @options[:verbose] recordset << excluded_record_for(path) end end end # Print out a progress summary if requested @progress.call(0) if @progress end end end = "#{processed_count} files processed." # Output summary recordset << Record.new(:summary, , { 'summary.directories' => directory_count, 'summary.files' => processed_count, 'summary.size' => processed_size, 'summary.excluded' => excluded_count, 'summary.time.end' => Time.now }) return recordset end |
#scan_path(path) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/fingerprint/scanner.rb', line 170 def scan_path(path) @roots.each do |root| Dir.chdir(root) do if valid_file?(path) return file_record_for(path) end end end return nil end |
#valid_file?(path) ⇒ Boolean
166 167 168 |
# File 'lib/fingerprint/scanner.rb', line 166 def valid_file?(path) !(excluded?(path) || File.symlink?(path) || !File.file?(path) || !File.readable?(path)) end |