From 5fb5067d753fc2b0b95d1734c7fbf72f619cdc71 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sat, 28 Jan 2017 01:55:11 +0100 Subject: also add file size to file hasher output 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 } diff --git a/src/pool-import/file-hasher.go b/src/pool-import/file-hasher.go index df58a8e..1160aba 100644 --- a/src/pool-import/file-hasher.go +++ b/src/pool-import/file-hasher.go @@ -30,7 +30,12 @@ import ( "time" ) -type FileMap map[string]string +type File struct { + Path string + Size int64 +} + +type FileMap map[string]File func openGroupConfig(group string) (*os.File, error) { cwd, err := filepath.Abs(".") diff --git a/src/pool-import/main.go b/src/pool-import/main.go index cd52c2f..025c76f 100644 --- a/src/pool-import/main.go +++ b/src/pool-import/main.go @@ -103,8 +103,8 @@ func main() { stdlog.Println("") stdlog.Printf(" %d files", len(files)) stdlog.Println("****************************") - for hash, path := range files { - stdlog.Printf("%s: %s", hash, path) + for hash, file := range files { + stdlog.Printf("%s: %s (%d bytes)", hash, file.Path, file.Size) } stdlog.Println("****************************") -- cgit v0.10.2