From 9e1aed257bb1231c7d3fdf2d5e8af5cf9ab6859f Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Fri, 17 Nov 2017 18:55:20 -0800 Subject: [PATCH] rhash: use io.Copy to avoid reading the full file. --- cmd/rhash/main.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/cmd/rhash/main.go b/cmd/rhash/main.go index 8c174af..3900ada 100644 --- a/cmd/rhash/main.go +++ b/cmd/rhash/main.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -15,20 +14,6 @@ import ( "github.com/kisom/goutils/lib" ) -func fetch(remote string) ([]byte, error) { - resp, err := http.Get(remote) - if err != nil { - return nil, err - } - - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } - return body, nil -} - func usage(w io.Writer) { fmt.Fprintf(w, `Usage: %s [-a algo] [-h] [-l set] urls... Compute the hash over each URL. @@ -91,13 +76,19 @@ func main() { continue } - body, err := fetch(remote) + resp, err := http.Get(remote) if err != nil { lib.Warn(err, "fetching %s", remote) continue } - sum, err := ahash.Sum(algo, body) + if err != nil { + lib.Warn(err, "fetching %s", remote) + continue + } + + sum, err := ahash.SumReader(algo, resp.Body) + resp.Body.Close() if err != nil { lib.Err(lib.ExitFailure, err, "while hashing data") }