# HG changeset patch # User edogawaconan # Date 1403973018 -32400 # Node ID a0f4a3e15322383691049b0bd7c620ca6467ace3 # Parent fb2d37acca81bcd8b4b9493a95c6a49eeb7ea455 Extend cek.rb to match main script. diff -r fb2d37acca81 -r a0f4a3e15322 bin/cek.rb --- 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 + /(\[|\()(?\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