File: files.go

package info (click to toggle)
golang-github-biogo-biogo 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 5,332 kB
  • sloc: sh: 282; makefile: 2
file content (41 lines) | stat: -rw-r--r-- 857 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright ©2011-2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package util

import (
	"errors"
	"hash"
	"io"
	"os"
)

const (
	bufferLen = 1 << 15
)

var buffer = make([]byte, bufferLen)

// Hash returns the h hash sum of file f and any error. The hash is not reset on return,
// so if individual files are to be hashed with the same h, it should be reset.
func Hash(h hash.Hash, f *os.File) (sum []byte, err error) {
	fi, err := f.Stat()
	if err != nil || fi.IsDir() {
		return nil, errors.New("util: file is a directory")
	}

	s := io.NewSectionReader(f, 0, fi.Size())

	for n, buffer := 0, make([]byte, bufferLen); err == nil; {
		n, err = s.Read(buffer)
		h.Write(buffer[:n])
	}
	if err == io.EOF {
		err = nil
	}

	sum = h.Sum(nil)

	return
}