Ejemplo n.º 1
0
// reads file in 512KB chunks
func FileCRC64(fname string) (uint64, error) {
	var checksum uint64
	var bufSize = 1024 * 512
	fp, err := os.Open(fname)
	if err != nil {
		return 0, err
		//fmt.Printf("!Err-> can't read %s\n", fname)
		//log.Panicf("!Err-> can't read %s %s\n", fname, err)
	}
	defer fp.Close()
	bufr := make([]byte, bufSize)
	checksum = 0
	for {
		n, err := io.ReadFull(fp, bufr)
		if err != nil {
			if n > 0 {
				// fmt.Printf("not full block but read %d bytes\n",n)
			}
		}
		if err == io.EOF {
			if n != 0 {
				return 0, err
			} else {
				break // normal end of file reached
			}
		}
		// fmt.Printf(" read %d bytes\n",n)
		checksum = crc64.Update(checksum, G_crcTable, bufr[0:n])
		// checksum = crc64.Checksum(bufr[0:n],crcTable)  if entire string in bufr
	}
	return checksum, nil
}
Ejemplo n.º 2
0
// TestContentEqual can be extremely slow (dozens of seconds) if
// GOMAXPROCS==1.
func TestContentEqual(t *testing.T) {
	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(runtime.NumCPU()))
	nConsumers := 10
	done := make(chan uint64, nConsumers)
	tab := crc64.MakeTable(crc64.ECMA)
	sm := NewSourceMap()
	defer sm.Close()
	for i := 0; i < nConsumers; i++ {
		go func() {
			rdr := sm.NewReader("/dev/urandom", &Config{
				SourceBuffer: 32,
				FrameBytes:   65536,
				CloseIdle:    true,
				Reopen:       false,
			})
			defer rdr.Close()
			var hash uint64
			defer func() { done <- hash }()
			var frame = make([]byte, 65536)
			for f := 0; f < 130; f++ {
				var err error
				if _, err = rdr.Read(frame); err != nil && err != io.EOF {
					t.Fatalf("rdr.Read(frame) at frame %d: %s", rdr.nextFrame, err)
				}
				hash = crc64.Update(hash, tab, frame)
			}
		}()
	}
	h0 := <-done
	for i := 1; i < nConsumers; i++ {
		hi := <-done
		if h0 == 0 {
			h0 = hi
		} else if hi != h0 {
			t.Errorf("hash mismatch: h0=%x, h%d=%x", h0, i, hi)
		}
	}
}