Mercurial > ec-dotfiles
changeset 522:a0f4a3e15322
Extend cek.rb to match main script.
author | edogawaconan <me@myconan.net> |
---|---|
date | Sun, 29 Jun 2014 01:30:18 +0900 |
parents | fb2d37acca81 |
children | b88f621b6bd8 |
files | bin/cek.rb |
diffstat | 1 files changed, 66 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/bin/cek.rb Wed Jun 18 16:08:22 2014 +0900 +++ b/bin/cek.rb Sun Jun 29 01:30:18 2014 +0900 @@ -1,14 +1,71 @@ #!/usr/bin/env ruby require 'zlib' -block = 1048576 -ARGV.each do |filename| - crc = 0 - file = File.open(filename, 'rb') - currentbyte = 0 - while (line = file.read(block)) do - crc = Zlib.crc32(line,crc) +class Cek + CRC32_BLOCK = 2 ** 20 + + attr_accessor :files + + def crc32(filepath) + file = File.open filepath, 'rb' + crc32 = 0 + while (line = file.read(CRC32_BLOCK)) do + crc32 = Zlib.crc32(line, crc32) + end + file.close + + format "%08X", crc32.to_i end - file.close - printf("%s %08X\n", filename, crc.to_i) + + def print_result + results = Hash.new(0) + files.each do |f| + time_start = Time.now + if File.readable?(f) && File.file?(f) then + hash = crc32 f + size = File.size f + /(\[|\()(?<filename_hash>\p{XDigit}{8})(\]|\))/ =~ f + result = + if filename_hash + if filename_hash == hash + :ok + else + :fail + end + else + :missing_hash + end + output = hash + case result + when :ok then output << " - OK!" + when :fail then output << " - ERROR - should be #{filename_hash}" + end + else + result = :unreadable + output = "not a file or unreadable" + end + process_time = Time.now - time_start + size ||= 0 + size_mb = size.to_f / 1_000_000 + speed = (size_mb.to_f / process_time) + results[result] += 1 + puts "#{f}: #{output} (#{format "%.2f", size_mb} MB / #{format "%.2f", process_time} s / #{format "%.2f", speed} MB/s)" + end + puts ("-" * 50) + puts "Files ok: #{results[:ok]}" + puts "Files broken: #{results[:fail]}" + puts "Files without crc information: #{results[:missing_hash]}" + puts "Files unreadable or not a file: #{results[:unreadable]}" + end + + def initialize(*files) + self.files = files.flatten + @stats = {} + end + + def self.run(*files) + new(files).print_result + end end + +Cek.run(ARGV) if __FILE__ == $0