diff options
author | Lauris BH <lauris@nix.lv> | 2019-11-05 14:54:47 +0200 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2019-11-05 20:54:47 +0800 |
commit | 45f219239e420f707a2452b99b27a68f45d5d4ca (patch) | |
tree | 53baed1d8d94f3e790fdf002b3b5a463ccd05926 /modules | |
parent | 79fd422a9218a2cbf628e5fb4aa0ff1caaef46d6 (diff) | |
download | gitea-45f219239e420f707a2452b99b27a68f45d5d4ca.tar.gz gitea-45f219239e420f707a2452b99b27a68f45d5d4ca.zip |
Fix to close opened io resources as soon as not needed (#8839)
* Fix to close opened io resources as soon as not needed
* Remove unneeded err checks
Diffstat (limited to 'modules')
-rw-r--r-- | modules/migrations/gitea.go | 72 |
1 files changed, 39 insertions, 33 deletions
diff --git a/modules/migrations/gitea.go b/modules/migrations/gitea.go index a095751c6b..676667b426 100644 --- a/modules/migrations/gitea.go +++ b/modules/migrations/gitea.go @@ -252,27 +252,30 @@ func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error { } // download attachment - resp, err := http.Get(asset.URL) - if err != nil { - return err - } - defer resp.Body.Close() + err = func() error { + resp, err := http.Get(asset.URL) + if err != nil { + return err + } + defer resp.Body.Close() - localPath := attach.LocalPath() - if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil { - return fmt.Errorf("MkdirAll: %v", err) - } + localPath := attach.LocalPath() + if err = os.MkdirAll(path.Dir(localPath), os.ModePerm); err != nil { + return fmt.Errorf("MkdirAll: %v", err) + } - fw, err := os.Create(localPath) - if err != nil { - return fmt.Errorf("Create: %v", err) - } - defer fw.Close() + fw, err := os.Create(localPath) + if err != nil { + return fmt.Errorf("Create: %v", err) + } + defer fw.Close() - if _, err := io.Copy(fw, resp.Body); err != nil { + _, err = io.Copy(fw, resp.Body) + return err + }() + if err != nil { return err } - rel.Attachments = append(rel.Attachments, &attach) } @@ -468,21 +471,24 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR } // download patch file - resp, err := http.Get(pr.PatchURL) - if err != nil { - return nil, err - } - defer resp.Body.Close() - pullDir := filepath.Join(g.repo.RepoPath(), "pulls") - if err = os.MkdirAll(pullDir, os.ModePerm); err != nil { - return nil, err - } - f, err := os.Create(filepath.Join(pullDir, fmt.Sprintf("%d.patch", pr.Number))) - if err != nil { - return nil, err - } - defer f.Close() - _, err = io.Copy(f, resp.Body) + err := func() error { + resp, err := http.Get(pr.PatchURL) + if err != nil { + return err + } + defer resp.Body.Close() + pullDir := filepath.Join(g.repo.RepoPath(), "pulls") + if err = os.MkdirAll(pullDir, os.ModePerm); err != nil { + return err + } + f, err := os.Create(filepath.Join(pullDir, fmt.Sprintf("%d.patch", pr.Number))) + if err != nil { + return err + } + defer f.Close() + _, err = io.Copy(f, resp.Body) + return err + }() if err != nil { return nil, err } @@ -496,8 +502,8 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR if err != nil { return nil, err } - defer p.Close() _, err = p.WriteString(pr.Head.SHA) + p.Close() if err != nil { return nil, err } @@ -531,8 +537,8 @@ func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullR if err != nil { return nil, err } - defer b.Close() _, err = b.WriteString(pr.Head.SHA) + b.Close() if err != nil { return nil, err } |