|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +require 'logger' |
| 4 | +require 'sqlite3' |
| 5 | +require 'zlib' |
| 6 | + |
| 7 | +if ARGV.length < 3 || (ARGV[2] != "text" && ARGV[2] != "db") then |
| 8 | + puts "Usage: listup_aria.rb [avs_file] [list_file] [ text | db ]" |
| 9 | + exit 1 |
| 10 | +end |
| 11 | + |
| 12 | +print "Start: " |
| 13 | +p Time.now |
| 14 | + |
| 15 | +log = Logger.new("listup_aria.log") |
| 16 | + |
| 17 | +prev_offset = 0 |
| 18 | +count = 0 |
| 19 | + |
| 20 | +if ARGV[2] == "text" then |
| 21 | + op = File.open(ARGV[1] + ".lst", "w") |
| 22 | +elsif ARGV[2] == "db" then |
| 23 | + db = SQLite3::Database.new(ARGV[1] + ".db") |
| 24 | + sql = <<SQL |
| 25 | +create table if not exists keys ( |
| 26 | + key varchar(256), |
| 27 | + clock integer, |
| 28 | + del integer, |
| 29 | + flag integer |
| 30 | +); |
| 31 | +SQL |
| 32 | + db.execute(sql) |
| 33 | + db.transaction |
| 34 | +end |
| 35 | +io = File.open(ARGV[0], "rb") |
| 36 | + |
| 37 | +io.read(111) |
| 38 | + |
| 39 | +while ! io.eof? do |
| 40 | + crc32 = io.read(4).unpack("H*")[0].hex.to_i |
| 41 | + |
| 42 | + header = io.read(29) |
| 43 | + ksize = header[0, 1].unpack("H*")[0].hex.to_i |
| 44 | + dsize = header[1, 4].unpack("H*")[0].hex.to_i |
| 45 | + offset = header[9, 4].unpack("H*")[0].hex.to_i |
| 46 | + vnode = header[13, 4].unpack("H*")[0].hex.to_i |
| 47 | + vclock = header[17, 4].unpack("H*")[0].hex.to_i |
| 48 | + del = header[28, 1].unpack("H*")[0].hex.to_i |
| 49 | + key = io.read(ksize) |
| 50 | + body = io.read(dsize) |
| 51 | + bin = header[0, 5] + header[13, 8] + key + body |
| 52 | + |
| 53 | + ret_crc32 = Zlib.crc32(bin, 0) |
| 54 | + |
| 55 | + if crc32 == ret_crc32 && io.read(8) == "\0\0\0\0\0\0\0\0" then |
| 56 | + prev_offset = io.pos |
| 57 | + if ARGV[2] == "text" then |
| 58 | + if key + ",0,0,0\n" != ",0,0,0\n" then |
| 59 | + op.write(key + ",0,0,0\n") |
| 60 | + else |
| 61 | + log.error("key is not exists. #{offset}") |
| 62 | + end |
| 63 | + count = count + 1 |
| 64 | + elsif ARGV[2] == "db" then |
| 65 | + sql = "select key, clock from keys where key = ?" |
| 66 | + ret = db.execute(sql, key) |
| 67 | + if ret.length == 0 then |
| 68 | + sql = "insert into keys values (?,?,?,?)" |
| 69 | + db.execute(sql, key, vclock, del, 0) |
| 70 | + count = count + 1 |
| 71 | + elsif ret[0][1].to_i < vclock then |
| 72 | + sql = "update keys set clock = ?, del = ? where key = ?" |
| 73 | + db.execute(sql, vclock, del, key) |
| 74 | + end |
| 75 | + end |
| 76 | + log.info("#{prev_offset} #{key}") |
| 77 | + else |
| 78 | + prefix = 1 |
| 79 | + io.seek(prev_offset, IO::SEEK_SET) |
| 80 | + while io.read(8) != "\0\0\0\0\0\0\0\0" do |
| 81 | + io.seek(prev_offset + prefix, IO::SEEK_SET) |
| 82 | + prefix = prefix + 1 |
| 83 | + end |
| 84 | + prev_offset = io.pos |
| 85 | + log.error("#{prev_offset} #{key} src_crc32: #{crc32} dst_crc32: #{ret_crc32}") |
| 86 | + end |
| 87 | +end |
| 88 | + |
| 89 | +io.close() |
| 90 | +if ARGV[2] == "text" then |
| 91 | + op.close() |
| 92 | +elsif ARGV[2] == "db" then |
| 93 | + db.commit |
| 94 | + db.close() |
| 95 | +end |
| 96 | + |
| 97 | +print "End : " |
| 98 | +p Time.now |
| 99 | +puts "Total: " + count.to_s + " Offset: " + prev_offset.to_s |
| 100 | +exit 0 |
0 commit comments