class AFI

Constants

MAX_RANDOM_NUMBER

Public Class Methods

new() click to toggle source

Start counters

# File lib/afinus.rb, line 9
def initialize
  @c_file = 0
  @c_dir  = 0
  @c_err  = 0
  @c_byte = 0
  @dirs   = []
end

Public Instance Methods

clean!(recursive: false) click to toggle source

Clean all files!

# File lib/afinus.rb, line 55
def clean!(recursive: false)
  collect(recursive && :recursive).each do |file|
    if File.directory?(file)
      @c_dir += 1
      printer(@c_dir.to_s.yellow.bold, file, 'Directory Found!'.white)
      Dir.rmdir(file) if Dir.empty?(file)
      @dirs << file
    else
      begin
        rewrite(file) if File.writable?(file)
      rescue
        puts '>> FOLDER DO NOT EXIST OR PERMISSION DENIED <<'.red.bold
      end
    end

    puts "\n[#{@c_dir}] directories counted".white
  end
end
enter(dir) click to toggle source

Enter start directory or exit

# File lib/afinus.rb, line 32
def enter(dir)
  Dir.chdir(dir)
  print_working_dir
rescue
  puts "\n >> FOLDER DO NOT EXIST OR PERMISSION DENIED <<\n".red.bold
  exit(1)
end
execute!(directory, opts = {}) click to toggle source
# File lib/afinus.rb, line 17
def execute!(directory, opts = {})
  recursive   = opts[:recursive].is_a?(TrueClass)
  @start_time = Time.now
  enter(directory)
  fill_empty_space!(512_000) # Fill empty space before recursive clean!
  clean!(recursive: recursive)
  remove_directories!(recursive: recursive)

  @end_time = Time.now

  print_info
  print_thank_you
end
fill_empty_space!(bytes) click to toggle source

Fill empty space on partition

# File lib/afinus.rb, line 43
def fill_empty_space!(bytes)
  @start_time = Time.now
  create_random_files(bytes)
rescue
  @end_time = Time.now
  puts "\n[#{@c_byte}] files created in [#{(@end_time - @start_time).round(3)}] seconds".white
  print_delimiter
end
remove_directories!(recursive: false) click to toggle source

Remove Directories

# File lib/afinus.rb, line 77
def remove_directories!(recursive: false)
  collect(recursive && :recursive).select { |file| File.directory?(file) }
    .each { |dir| Dir.rmdir(dir) }
end
rewrite(file) click to toggle source

Overwrite and Null file

# File lib/afinus.rb, line 83
def rewrite(file)
  truncate_file(file)
  File.delete(file)
  @c_file += 1
  printer(@c_file.to_s.yellow, file.white, 'Overwritten, Nulled, Removed!'.yellow)
rescue
  @c_err += 1
  printer(@c_err.to_s.red, file.white.bold, 'NOT PROCESSED => PERMISSION PROBLEM?'.red.bold)
end
truncate_file(file, bytes_array = [50, 100, 50]) click to toggle source

end of rewrite

# File lib/afinus.rb, line 95
def truncate_file(file, bytes_array = [50, 100, 50])
  bytes_array = Array(bytes_array)
  bytes_array.each do |bytes|
    File.write(file, Random.new.bytes(bytes).to_s)
    File.truncate(file, 0)
  end
end

Private Instance Methods

collect(mode = nil) click to toggle source

Collect files to clean

# File lib/afinus.rb, line 145
def collect(mode = nil)
  # binding.pry
  case mode.to_sym
  when :recursive
    Dir.glob(File.join('**', '*'))
  when :fill_empty
    Dir.glob('*.fillfile')
  else
    Dir.glob('*').select { |file| File.file?(file) }
  end
end
create_random_files(bytes) click to toggle source

Create random-byte file

# File lib/afinus.rb, line 133
def create_random_files(bytes)
  loop do
    a_rand  = "#{rand(MAX_RANDOM_NUMBER)}.fillfile"
    @c_byte += 1
    File.write(a_rand.to_s, Random.new.bytes(bytes).to_s)
    printer(@c_byte.to_s.yellow.bold, "Created #{a_rand}".white, "#{bytes} bytes\n".white)
  end
end
print_delimiter() click to toggle source
print_info() click to toggle source

Print file counter and time info

print_thank_you() click to toggle source

Print “thank you” note

print_working_dir() click to toggle source

Print working directory

printer(counter, name, note) click to toggle source

Define printer for processed files

# File lib/afinus.rb, line 128
def printer(counter, name, note)
  puts "[#{counter}] - #{name} - #{note}"
end