Skip to content

Commit

Permalink
add a verify mode where it can scan a hard disk:
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrh committed Nov 21, 2018
1 parent 4da8d3f commit db474a1
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
4 changes: 4 additions & 0 deletions c/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func main() {
if arg == "-d" {
decompress = true
}
if arg == "-dirtree" {
recursiveVerify(os.Args[index+1])
return
}
if arg == "-cat" {
toCat = append(toCat, os.Args[index+1:]...)
break
Expand Down
123 changes: 123 additions & 0 deletions c/go/verify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"fmt"
"bytes"

"github.com/dropbox/rust-brotli/c/go/brotli"
"io"
"os"
"path/filepath"
insecure_random "math/rand"
)


func makeOptions() brotli.CompressionOptions {
ret := brotli.CompressionOptions{
NumThreads: insecure_random.Intn(15) + 1,
Quality: float32(insecure_random.Intn(9) + 3),
Catable: true,
Appendable: true,
Magic: true,
}
if ret.Quality > 11 {
ret.Quality = 9.5
}
return ret
}
func verifyReader(path string) {
f, err := os.Open(path)
if err != nil {
return
}
defer func() { _ = f.Close()}()
origFile := bytes.NewBuffer(nil)
options := makeOptions()
reader := brotli.NewMultiCompressionReader(
io.TeeReader(f, origFile),
options,
)
compressed := bytes.NewBuffer(nil)
rtReader := brotli.NewDecompressionReader(io.TeeReader(reader, compressed))
rt := bytes.NewBuffer(nil)
io.Copy(rt, rtReader)
if string(rt.Bytes()) != string(origFile.Bytes()) {
fi, err := os.Create("/tmp/IN.orig")
if err != nil {
defer func() { _ = fi.Close()}()
_, _ = fi.Write(origFile.Bytes())
}
fc, err := os.Create("/tmp/IN.br")
if err != nil {
defer func() { _ = fc.Close()}()
_, _ = fc.Write(compressed.Bytes())
}
fr, err := os.Create("/tmp/IN.rt")
if err != nil {
defer func() { _ = fr.Close()}()
_, _ = fr.Write(rt.Bytes())
}
panic(fmt.Sprintf("%v Bytes mismatch %d != %d\n", options, len(rt.Bytes()), len(origFile.Bytes())))
}
}

func verifyWriter(path string) {
f, err := os.Open(path)
if err != nil {
return
}
options := makeOptions()
defer func() { _ = f.Close()}()
compressed := bytes.NewBuffer(nil)
rt := bytes.NewBuffer(nil)
origFile := bytes.NewBuffer(nil)
dwriter := brotli.NewDecompressionWriter(rt)
writer := brotli.NewMultiCompressionWriter(
io.MultiWriter(compressed, dwriter),
options,
)
_, err = io.Copy(io.MultiWriter(writer, origFile), f)
if err != nil {
panic(err)
}
err = writer.Close()
if err != nil {
panic(err)
}
err = dwriter.Close()
if err != nil {
panic(err)
}
if string(rt.Bytes()) != string(origFile.Bytes()) {
fi, err := os.Create("/tmp/INW.orig")
if err != nil {
defer func() { _ = fi.Close()}()
_, _ = fi.Write(origFile.Bytes())
}
fc, err := os.Create("/tmp/INW.br")
if err != nil {
defer func() { _ = fc.Close()}()
_, _ = fc.Write(compressed.Bytes())
}
fr, err := os.Create("/tmp/INW.rt")
if err != nil {
defer func() { _ = fr.Close()}()
_, _ = fr.Write(rt.Bytes())
}
panic(fmt.Sprintf("%v Bytes mismatch %d != %d\n", options, len(rt.Bytes()), len(origFile.Bytes())))
} else {
fmt.Fprintf(os.Stderr, "Processing %s %v/%v = %f\n",
path, len(rt.Bytes()), len(compressed.Bytes()), float32(len(compressed.Bytes()))/float32(len(rt.Bytes()) ))
}
}

func recursiveVerify(root string) {
filepath.Walk(root, filepath.WalkFunc(func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
fmt.Fprintf(os.Stderr, "Processing %v", path)
verifyReader(path);
verifyWriter(path);
}
return nil
}))
}

0 comments on commit db474a1

Please sign in to comment.