class Relinker::Discoverer

Attributes

cache_file[R]
options[R]

Public Class Methods

new(cache_file, options = {}) click to toggle source
# File lib/relinker/discoverer.rb, line 8
def initialize(cache_file, options = {})
  @cache_file = cache_file
  @options = options
end

Public Instance Methods

collect(base_dir) click to toggle source
# File lib/relinker/discoverer.rb, line 13
def collect(base_dir)
  File.open(cache_file, "a+") do |cache|
    discover(base_dir) do |file, checksum|
      cache.write("#{checksum}$$$\t#{file}\n")
    end
  end
  resort_cache
end
discover(base_dir) { |file, checksum(file)| ... } click to toggle source
# File lib/relinker/discoverer.rb, line 27
def discover(base_dir)
  children = Pathname.new(base_dir).children
  children.each do |child|
    next if child.symlink?
    if child.directory?
      discover(child) { |file| yield(file, checksum(file)) if regular_file?(file) }
    else
      yield(child, checksum(child)) if regular_file?(child)
    end
  end
end
list(base_dir) click to toggle source
# File lib/relinker/discoverer.rb, line 39
def list(base_dir)
  files = []
  discover(base_dir) do |file, check|
    files << { path: file.to_s, checksum: check }
  end
  files
end
resort_cache() click to toggle source
# File lib/relinker/discoverer.rb, line 22
def resort_cache
  system("cat #{cache_file} | sort | uniq | cat > #{cache_file}.tmp \
          && mv #{cache_file}.tmp #{cache_file}")
end

Private Instance Methods

checksum(file) click to toggle source
# File lib/relinker/discoverer.rb, line 64
def checksum(file)
  Digest::MD5.file(file).hexdigest
end
default_options() click to toggle source
# File lib/relinker/discoverer.rb, line 60
def default_options
  { cache_dir: "/tmp" }
end
merge_options(options = {}) click to toggle source
# File lib/relinker/discoverer.rb, line 55
def merge_options(options = {})
  options = options.map { |key, val| [key.to_sym, val] }.to_h
  default_options.merge(options)
end
regular_file?(path) click to toggle source
# File lib/relinker/discoverer.rb, line 49
def regular_file?(path)
  special_types = %i[symlink chardev blockdev socket pipe]
  special_type = special_types.find { |test| path.send("#{test}?") }
  path.file? && !special_type && !path.to_s.match(/:|pid$|swap$|lock$/)
end