Class: Rdupes::Finder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFinder

Returns a new instance of Finder.



11
12
13
14
15
16
17
18
19
20
# File 'lib/rdupes.rb', line 11

def initialize
  @logger = Logger.new(STDOUT)
  @logger.level = Logger::WARN
  @reference_directories = []
  @search_directories = []
  @counters = Hash.new(0)
  @quiet = false
  @keep = false
  @dry_run = false
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/rdupes.rb', line 9

def logger
  @logger
end

Instance Method Details

#add_reference_directory(directory) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rdupes.rb', line 37

def add_reference_directory(directory)
  directory_path = File.expand_path(directory)
  @logger.debug "Adding #{directory_path} to the reference directories"
  raise "#{directory_path} does not exist" unless Dir.exist?(directory_path)
  @reference_directories << directory_path
  @reference_directories.uniq!
end

#add_search_directory(directory) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/rdupes.rb', line 45

def add_search_directory(directory)
  directory_path = File.expand_path(directory)
  @logger.debug "Adding #{directory_path} to the search directories"
  raise "#{directory_path} does not exist" unless Dir.exist?(directory_path)
  @search_directories << directory_path
  @search_directories.uniq!
end

#dry_run!Object



32
33
34
35
# File 'lib/rdupes.rb', line 32

def dry_run!
  @logger.debug "Enabling dry run mode"
  @dry_run = true
end

#keep!Object



27
28
29
30
# File 'lib/rdupes.rb', line 27

def keep!
  @logger.debug "Enabling keep mode. Will keep the fdupes output"
  @keep = true
end

#process(directories) ⇒ Object



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
# File 'lib/rdupes.rb', line 53

def process(directories)
  @search_directories.clear
  directories.each { |d| add_search_directory(d) }
  raise 'No directories provided for duplicate search' if @search_directories.empty?
  raise 'fdupes needs to be installed' unless command? 'fdupes'

  say "Processing #{@search_directories}"
  say "Reference directory: #{@reference_directories}"

  Dir.mktmpdir do |dir|
    fdupes_output = File.join(dir, 'duplicates.log')
    # Redirect to file
    cmd = "fdupes -rq #{directories_for_search.shelljoin} > #{fdupes_output}"
    @logger.debug "Executing: #{cmd}"
    r = system cmd
    raise "fdupe crashed " unless r
    process_fdupes_result(fdupes_output)

    if @keep
      fdupes_output_copy = "#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}_duplicates.log"
      say "Copying fdupes result to #{fdupes_output_copy}"
      FileUtils.cp fdupes_output, fdupes_output_copy
    end

    say "Found #{@counters[:duplicate_groups]} duplicate groups"
    say "Found #{@counters[:duplicate_entries]} duplicate entries"
    say "Flagged #{@counters[:flag_for_delete]} files for deletion"
    say "Deleted #{@counters[:deleted]} files"
  end
end

#quiet!Object



22
23
24
25
# File 'lib/rdupes.rb', line 22

def quiet!
  @logger.debug "Enabling quiet mode"
  @quiet = true
end