rhash: use io.Copy to avoid reading the full file.

This commit is contained in:
Kyle Isom 2017-11-17 18:55:20 -08:00
parent 411907c0ad
commit 9e1aed257b
1 changed files with 8 additions and 17 deletions

View File

@ -4,7 +4,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -15,20 +14,6 @@ import (
"github.com/kisom/goutils/lib" "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) { func usage(w io.Writer) {
fmt.Fprintf(w, `Usage: %s [-a algo] [-h] [-l set] urls... fmt.Fprintf(w, `Usage: %s [-a algo] [-h] [-l set] urls...
Compute the hash over each URL. Compute the hash over each URL.
@ -91,13 +76,19 @@ func main() {
continue continue
} }
body, err := fetch(remote) resp, err := http.Get(remote)
if err != nil { if err != nil {
lib.Warn(err, "fetching %s", remote) lib.Warn(err, "fetching %s", remote)
continue 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 { if err != nil {
lib.Err(lib.ExitFailure, err, "while hashing data") lib.Err(lib.ExitFailure, err, "while hashing data")
} }