Exemplo n.º 1
0
Arquivo: hash.go Projeto: mildred/doc
func symlinkHash(path string) (mh.Multihash, error) {
	link, err := os.Readlink(path)
	if err != nil {
		return nil, err
	}

	hasher := sha1.New()
	_, err = hasher.Write([]byte(link))
	if err != nil {
		panic(err)
	}

	return mh.Encode(hasher.Sum(nil), mh.SHA1)
}
Exemplo n.º 2
0
func dummyHash(t *testing.T, seed byte) *Hash {
	data := make([]byte, multihash.DefaultLengths[goipfsutil.DefaultIpfsHash])
	for idx := range data {
		data[idx] = seed
	}

	hash, err := multihash.Encode(data, goipfsutil.DefaultIpfsHash)

	if err != nil {
		t.Fatalf("Failed to create dummy hash: %v", err)
		return nil
	}

	return &Hash{hash}
}
Exemplo n.º 3
0
func Init(dir string) error {
	newpath := filepath.Join(dir, Doccommit)

	// Rename the doccommit file in a single atomic operation

	f, err := os.OpenFile(newpath, os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		return err
	}
	f.Close()

	digestBin := sha1.Sum([]byte{})
	digest, err := mh.Encode(digestBin[:], mh.SHA1)
	if err != nil {
		panic(err)
	}

	return commitDircommit(newpath, digest)
}
Exemplo n.º 4
0
func writeDoccommitFile(newpath, prefix string, entries []Entry) error {
	var data []byte

	for _, e := range entries {
		data = append(data, []byte(entryToLine(prefix, e))...)
	}

	digestBin := sha1.Sum(data)
	digest, err := mh.Encode(digestBin[:], mh.SHA1)
	if err != nil {
		return err
	}

	f, err := ioutil.TempFile(filepath.Dir(newpath), filepath.Base(newpath))
	if err != nil {
		return err
	}
	defer func() {
		if f != nil {
			fn := f.Name()
			f.Close()
			os.Remove(fn)
		}
	}()

	_, err = f.Write(data)
	if err != nil {
		return err
	}

	// Rename the doccommit file in a single atomic operation
	err = os.Rename(f.Name(), newpath)
	if err != nil {
		return err
	}

	f.Close()
	f = nil

	return commitDircommit(newpath, digest)
}
Exemplo n.º 5
0
Arquivo: hash.go Projeto: mildred/doc
func HashFile(path string, info os.FileInfo) (mh.Multihash, error) {
	if info.Mode()&os.ModeSymlink != 0 {
		return symlinkHash(path)
	}

	f, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	hasher := sha1.New()
	_, err = io.Copy(hasher, f)
	if err != nil {
		return nil, err
	}

	digest, err := mh.Encode(hasher.Sum(nil), mh.SHA1)
	if err != nil {
		panic(err)
	}

	return digest, nil
}