Commit 89583030 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 339f3e1c
...@@ -33,16 +33,32 @@ type hasher struct { ...@@ -33,16 +33,32 @@ type hasher struct {
name string name string
} }
// hasher that discards data
type nullHasher struct {}
func (h nullHasher) Write(b []byte) (int, error) { return len(b), nil }
func (h nullHasher) Sum(b []byte) []byte { return []byte{0} }
func (h nullHasher) Reset() {}
func (h nullHasher) Size() int { return 1 }
func (h nullHasher) BlockSize() int { return 1 }
func main() { func main() {
defer log.Flush() defer log.Flush()
fadler32 := flag.Bool("adler32", false, "compute Adler32 checksum") fnull := flag.Bool("null", false, "don't compute hash - just read data")
fadler32 := flag.Bool("adler32",false, "compute Adler32 checksum")
fcrc32 := flag.Bool("crc32", false, "compute CRC32 checksum") fcrc32 := flag.Bool("crc32", false, "compute CRC32 checksum")
fsha1 := flag.Bool("sha1", false, "compute SHA1 cryptographic hash") fsha1 := flag.Bool("sha1", false, "compute SHA1 cryptographic hash")
fsha256 := flag.Bool("sha256", false, "compute SHA256 cryptographic hash") fsha256 := flag.Bool("sha256", false, "compute SHA256 cryptographic hash")
fsha512 := flag.Bool("sha512", false, "compute SHA512 cryptographic hash") fsha512 := flag.Bool("sha512", false, "compute SHA512 cryptographic hash")
useprefetch := flag.Bool("useprefetch", false, "prefetch loaded objects") useprefetch := flag.Bool("useprefetch", false, "prefetch loaded objects")
flag.Parse() flag.Parse()
url := flag.Args()[0] // XXX dirty
if flag.NArg() != 1 {
flag.Usage()
// XXX exit
}
url := flag.Arg(0)
ctx := context.Background() ctx := context.Background()
...@@ -57,6 +73,7 @@ func main() { ...@@ -57,6 +73,7 @@ func main() {
} }
switch { switch {
case *fnull: inith("null", func() hash.Hash { return nullHasher{} })
case *fadler32: inith("adler32", func() hash.Hash { return adler32.New() }) case *fadler32: inith("adler32", func() hash.Hash { return adler32.New() })
case *fcrc32: inith("crc32", func() hash.Hash { return crc32.NewIEEE() }) case *fcrc32: inith("crc32", func() hash.Hash { return crc32.NewIEEE() })
case *fsha1: inith("sha1", sha1.New) case *fsha1: inith("sha1", sha1.New)
...@@ -99,7 +116,7 @@ func zhash(ctx context.Context, url string, h hasher, useprefetch bool) (err err ...@@ -99,7 +116,7 @@ func zhash(ctx context.Context, url string, h hasher, useprefetch bool) (err err
} }
} }
load := func(ctx context.Context, xid zodb.Xid) ([]byte, zodb.Tid, error) { load := func(ctx context.Context, xid zodb.Xid) (*zodb.Buf, zodb.Tid, error) {
if cache != nil { if cache != nil {
return cache.Load(ctx, xid) return cache.Load(ctx, xid)
} else { } else {
...@@ -158,7 +175,7 @@ loop: ...@@ -158,7 +175,7 @@ loop:
if xid.Oid % 8 == 0 { if xid.Oid % 8 == 0 {
prefetchBlk(ctx, xid) prefetchBlk(ctx, xid)
} }
data, _, err := load(ctx, xid) buf, _, err := load(ctx, xid)
switch err.(type) { switch err.(type) {
case nil: case nil:
// ok // ok
...@@ -168,13 +185,15 @@ loop: ...@@ -168,13 +185,15 @@ loop:
return err return err
} }
h.Write(data) h.Write(buf.Data)
//fmt.Fprintf(os.Stderr, "%d @%s\tsha1: %x\n", uint(oid), serial, h.Sum(nil)) //fmt.Fprintf(os.Stderr, "%d @%s\tsha1: %x\n", uint(oid), serial, h.Sum(nil))
//fmt.Fprintf(os.Stderr, "\tdata: %x\n", data) //fmt.Fprintf(os.Stderr, "\tdata: %x\n", buf.Data)
nread += len(data) nread += len(buf.Data)
oid += 1 oid += 1
buf.Free()
} }
tend := time.Now() tend := time.Now()
......
...@@ -14,6 +14,16 @@ import sys ...@@ -14,6 +14,16 @@ import sys
from time import time from time import time
from getopt import getopt, GetoptError from getopt import getopt, GetoptError
# hasher that discards data
class NullHasher:
name = "null"
def update(self, data):
pass
def hexdigest(self):
return "00"
# adler32 in hashlib interface # adler32 in hashlib interface
class Adler32Hasher: class Adler32Hasher:
name = "adler32" name = "adler32"
...@@ -42,6 +52,7 @@ class CRC32Hasher: ...@@ -42,6 +52,7 @@ class CRC32Hasher:
# {} name -> hasher # {} name -> hasher
hashRegistry = { hashRegistry = {
"null": NullHasher,
"adler32": Adler32Hasher, "adler32": Adler32Hasher,
"crc32": CRC32Hasher, "crc32": CRC32Hasher,
"sha1": hashlib.sha1, "sha1": hashlib.sha1,
...@@ -55,6 +66,7 @@ def usage(w): ...@@ -55,6 +66,7 @@ def usage(w):
options: options:
--null don't compute hash - just read data
--adler32 compute Adler32 checksum --adler32 compute Adler32 checksum
--crc32 compute CRC32 checksum --crc32 compute CRC32 checksum
--sha1 compute SHA1 cryptographic hash --sha1 compute SHA1 cryptographic hash
...@@ -72,6 +84,7 @@ def main(): ...@@ -72,6 +84,7 @@ def main():
for opt, _ in optv: for opt, _ in optv:
if opt in ("-h", "--help"): if opt in ("-h", "--help"):
print(__doc__)
usage(sys.stdout) usage(sys.stdout)
sys.exit() sys.exit()
...@@ -80,6 +93,7 @@ def main(): ...@@ -80,6 +93,7 @@ def main():
h = hctor() h = hctor()
if len(argv) != 1: if len(argv) != 1:
print(__doc__)
usage(sys.stderr) usage(sys.stderr)
sys.exit(1) sys.exit(1)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment