diff options
Diffstat (limited to 'src/file-hasher')
-rw-r--r-- | src/file-hasher/hasher.go | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/src/file-hasher/hasher.go b/src/file-hasher/hasher.go index 5f84646..5f197ab 100644 --- a/src/file-hasher/hasher.go +++ b/src/file-hasher/hasher.go @@ -37,7 +37,12 @@ import ( "golang.org/x/crypto/blake2b" ) -type FileMap map[string]string +type File struct { + Path string + Size int64 +} + +type FileMap map[string]File type Hasher struct { numThreads uint @@ -47,20 +52,22 @@ type Hasher struct { Files FileMap } -func (h *Hasher) computeHash(path string) (string, error) { +func (h *Hasher) computeHash(path string) (string, int64, error) { hash, err := h.newHash() if err != nil { - return "", err + return "", 0, err } file, err := os.Open(path) if err != nil { - return "", err + return "", 0, err } defer file.Close() - if _, err := io.Copy(hash, file); err != nil { - return "", err + + var size int64 + if size, err = io.Copy(hash, file); err != nil { + return "", 0, err } - return base64.URLEncoding.EncodeToString(hash.Sum(nil)), nil + return base64.URLEncoding.EncodeToString(hash.Sum(nil)), size, nil } func (h *Hasher) collectHashes(C <-chan string) (wg *sync.WaitGroup) { @@ -75,12 +82,12 @@ func (h *Hasher) collectHashes(C <-chan string) (wg *sync.WaitGroup) { if !ok { return } - if hash, err := h.computeHash(file); err != nil { + if hash, size, err := h.computeHash(file); err != nil { h.stdlog.Printf(" - skipping (%v): %s", err, filepath.Base(file)) } else { h.stdlog.Printf(" - hashed: %s", filepath.Base(file)) h.filesMtx.Lock() - h.Files[hash] = file + h.Files[hash] = File{file, size} h.filesMtx.Unlock() } } @@ -135,7 +142,7 @@ func NewHasher(algo string, numThreads uint, stdlog *log.Logger) (h *Hasher) { return nil } - h.Files = make(map[string]string) + h.Files = make(map[string]File) if h.numThreads < 1 { h.numThreads = 4 } |