Exemple #1
0
func TestRef(t *testing.T) {
	for _, elem := range data {

		var h32 hash.Hash32 = New32()
		h32.Write([]byte(elem.s))
		if v := h32.Sum32(); v != elem.h32 {
			t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
		}

		var h32_byte hash.Hash32 = New32()
		h32_byte.Write([]byte(elem.s))
		target := fmt.Sprintf("%08x", elem.h32)
		if p := fmt.Sprintf("%x", h32_byte.Sum(nil)); p != target {
			t.Errorf("'%s': %s (want %s)", elem.s, p, target)
		}

		if v := Sum32([]byte(elem.s)); v != elem.h32 {
			t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
		}

		var h64 hash.Hash64 = New64()
		h64.Write([]byte(elem.s))
		if v := h64.Sum64(); v != elem.h64_1 {
			t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1)
		}

		var h64_byte hash.Hash64 = New64()
		h64_byte.Write([]byte(elem.s))
		target = fmt.Sprintf("%016x", elem.h64_1)
		if p := fmt.Sprintf("%x", h64_byte.Sum(nil)); p != target {
			t.Errorf("Sum64: '%s': %s (want %s)", elem.s, p, target)
		}

		if v := Sum64([]byte(elem.s)); v != elem.h64_1 {
			t.Errorf("Sum64: '%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1)
		}

		var h128 Hash128 = New128()
		h128.Write([]byte(elem.s))
		if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
			t.Errorf("New128: '%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
		}

		var h128_byte Hash128 = New128()
		h128_byte.Write([]byte(elem.s))
		target = fmt.Sprintf("%016x%016x", elem.h64_1, elem.h64_2)
		if p := fmt.Sprintf("%x", h128_byte.Sum(nil)); p != target {
			t.Errorf("New128: '%s': %s (want %s)", elem.s, p, target)
		}

		if v1, v2 := Sum128([]byte(elem.s)); v1 != elem.h64_1 || v2 != elem.h64_2 {
			t.Errorf("Sum128: '%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
		}
	}
}
Exemple #2
0
func (ds *Directory) Get(key string, opts store.GetOptions) ([]byte, store.Stat, error) {
	ds.mu.RLock()
	defer ds.mu.RUnlock()

	select {
	case <-opts.Cancel:
		return nil, store.Stat{}, store.ErrCancelled
	default:
	}

	fh, path, err := ds.findAndOpen(key)
	if err != nil {
		return nil, store.Stat{}, err
	}
	if fh == nil {
		return nil, store.Stat{}, store.ErrNotFound
	}
	defer fh.Close()

	var expectedFNV [8]byte
	_, err = io.ReadFull(fh, expectedFNV[:])
	if err != nil {
		return nil, store.Stat{}, err
	}

	var rdr io.Reader
	var fnver hash.Hash64
	if opts.NoVerify {
		rdr = fh
	} else {
		fnver = fnv.New64a()
		rdr = io.TeeReader(fh, fnver)
	}

	var expectedSHA256 [32]byte
	_, err = io.ReadFull(rdr, expectedSHA256[:])
	if err != nil {
		return nil, store.Stat{}, err
	}

	fi, err := fh.Stat()
	if err != nil {
		return nil, store.Stat{}, err
	}

	size := fi.Size() - 40

	if int64(int(size)) != size {
		return nil, store.Stat{}, errors.New("file is too big")
	}

	writeTime := fi.ModTime().Unix()

	data := make([]byte, int(size))
	at := 0
	for at < len(data) {
		readInto := data[at:]
		if len(readInto) > 1024*1024 {
			readInto = readInto[:1024*1024]
		}

		n, err := rdr.Read(readInto)
		at += n
		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, store.Stat{}, err
		}

		select {
		case <-opts.Cancel:
			return nil, store.Stat{}, store.ErrCancelled
		default:
		}
	}

	if at != len(data) {
		return nil, store.Stat{}, errors.New("file was shortened during read")
	}

	if !opts.NoVerify {
		actualFNV := fnver.Sum(nil)

		if !bytes.Equal(actualFNV, expectedFNV[:]) {
			fh.Close()

			// TODO: this relocking is f****d and racy
			ds.mu.RUnlock()
			ds.mu.Lock()
			ds.quarantine(key, path)
			ds.mu.Unlock()
			ds.mu.RLock()
			return nil, store.Stat{}, ErrCorruptObject
		}
	}

	return data, store.Stat{
		SHA256:    expectedSHA256,
		Size:      size,
		WriteTime: writeTime,
	}, nil
}
Exemple #3
0
// hashKernel returns the upper and lower base hash values from which the k
// hashes are derived.
func hashKernel(data []byte, hash hash.Hash64) (uint32, uint32) {
	hash.Write(data)
	sum := hash.Sum(nil)
	hash.Reset()
	return binary.BigEndian.Uint32(sum[4:8]), binary.BigEndian.Uint32(sum[0:4])
}