Skip to content

Instantly share code, notes, and snippets.

@robie1373
Created October 4, 2012 20:34
Show Gist options
  • Save robie1373/3836263 to your computer and use it in GitHub Desktop.
Save robie1373/3836263 to your computer and use it in GitHub Desktop.
Drive exerciser
#!/usr/bin/env ruby
require 'json'
require 'digest'
require 'tempfile'
if File.directory?(File.expand_path(ARGV[0]))
@base_dir = File.expand_path(ARGV[0])
else
@base_dir = ENV['HOME']
end
#@base_dir = "/Volumes/Untitled/"
puts "exercising #{@base_dir}. Continue? \n(Ctrl-c to quit)"
#foo = gets
@excercise_dir = @base_dir + "/**/*"
@file_cache = "#{File.basename(@base_dir)}_cache.json"
@hash_file = Tempfile.new('drive_exc_hashes')
p @hash_file.path
@iterations = 100_000
@chunk_size = 100
@file_hashes = Hash.new
def create_file_array
Dir.glob(@excercise_dir)
end
def cache_file_array(array)
File.open(@file_cache, 'w') { |f| f.write(JSON.pretty_generate(array)) }
end
def hash_the_file(filename)
if File.file?(filename)
p filename
@file_hashes[filename] = Digest::SHA256.file(filename).hexdigest
puts "hashes to: #{@file_hashes[filename]}\n\n"
end
end
def rehash_files(hash_hash)
@good = 0
@bad = 0
hash_hash.each_key do |file|
if File.file?(file)
if Digest::SHA256.file(file).hexdigest == hash_hash[file]
@good += 1
#puts "#{file}\nrehashes to: #{Digest::SHA256.file(file).hexdigest}\n\n"
else
@bad += 1
end
puts "Good: #{@good}\t Bad: #{@bad}"
end
end
p [@good, @bad]
end
if File.file?(@file_cache)
files = JSON.parse(File.read(@file_cache))
else
files = create_file_array
end
#puts "#{files.length} is the length of the list. Continue?"
#gets
begin
Dir.chdir(@base_dir)
puts "Moved to #{Dir.pwd}"
(@iterations / @chunk_size).times do
files.shuffle!
@chunk_size.times do |i|
hash_the_file(files[i])
end
File.open(@hash_file, 'w') { |f| f.write(JSON.pretty_generate(@file_hashes)) }
puts "hashes made. Time to check them"
rehash = JSON.parse(File.read(@hash_file))
p rehash
rehash_files(rehash)
end
rescue Interrupt
p "Thanks for playing"
ensure
@hash_file.close
@hash_file.unlink
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment