Skip to content

Commit

Permalink
HttpGetter.GetFile: defer close the file so that we don't leak a fd i…
Browse files Browse the repository at this point in the history
…n case there's an error
  • Loading branch information
azr committed Feb 5, 2019
1 parent cac00f6 commit f645fc4
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions get_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (g *HttpGetter) Get(dst string, u *url.URL) error {
return g.getSubdir(ctx, dst, source, subDir)
}

func (g *HttpGetter) GetFile(dst string, src *url.URL) error {
func (g *HttpGetter) GetFile(dst string, src *url.URL) (err error) {
ctx := g.Context()
if g.Netrc {
// Add auth from netrc if we can
Expand All @@ -146,6 +146,12 @@ func (g *HttpGetter) GetFile(dst string, src *url.URL) error {
if err != nil {
return err
}
defer func() {
cerr := f.Close()
if err == nil {
err = cerr
}
}()

if g.Client == nil {
g.Client = httpClient
Expand Down Expand Up @@ -211,9 +217,6 @@ func (g *HttpGetter) GetFile(dst string, src *url.URL) error {
if err == nil && n < resp.ContentLength {
err = io.ErrShortWrite
}
if err1 := f.Close(); err == nil {
err = err1
}
return err
}

Expand Down

0 comments on commit f645fc4

Please sign in to comment.