From 74bd9691c685942798f2761607731697498ceeae Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 11 Aug 2020 21:05:34 +0100 Subject: Re-attempt to delete temporary upload if the file is locked by another process (#12447) Replace all calls to os.Remove/os.RemoveAll by retrying util.Remove/util.RemoveAll and remove circular dependencies from util. Fix #12339 Signed-off-by: Andrew Thornton Co-authored-by: silverwind --- modules/repository/create.go | 4 ++-- modules/repository/generate.go | 7 ++++--- modules/repository/hooks.go | 5 +++-- modules/repository/init.go | 8 ++++++-- modules/repository/repo.go | 8 ++++---- 5 files changed, 19 insertions(+), 13 deletions(-) (limited to 'modules/repository') diff --git a/modules/repository/create.go b/modules/repository/create.go index 2f7d10f0d1..945f4a8cea 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -6,13 +6,13 @@ package repository import ( "fmt" - "os" "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" ) // CreateRepository creates a repository for the user/organization. @@ -48,7 +48,7 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (_ *m if !opts.IsMirror { repoPath := models.RepoPath(u.Name, repo.Name) if err = initRepository(ctx, repoPath, doer, repo, opts); err != nil { - if err2 := os.RemoveAll(repoPath); err2 != nil { + if err2 := util.RemoveAll(repoPath); err2 != nil { log.Error("initRepository: %v", err) return fmt.Errorf( "delete repo directory %s/%s failed(2): %v", u.Name, repo.Name, err2) diff --git a/modules/repository/generate.go b/modules/repository/generate.go index 6d80488de7..c5fb0af383 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/util" "github.com/huandu/xstrings" ) @@ -119,7 +120,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp return fmt.Errorf("git clone: %v", err) } - if err := os.RemoveAll(path.Join(tmpDir, ".git")); err != nil { + if err := util.RemoveAll(path.Join(tmpDir, ".git")); err != nil { return fmt.Errorf("remove git dir: %v", err) } @@ -130,7 +131,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp } if gt != nil { - if err := os.Remove(gt.Path); err != nil { + if err := util.Remove(gt.Path); err != nil { return fmt.Errorf("remove .giteatemplate: %v", err) } @@ -191,7 +192,7 @@ func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo * } defer func() { - if err := os.RemoveAll(tmpDir); err != nil { + if err := util.RemoveAll(tmpDir); err != nil { log.Error("RemoveAll: %v", err) } }() diff --git a/modules/repository/hooks.go b/modules/repository/hooks.go index 6050f21f7f..2cefd56069 100644 --- a/modules/repository/hooks.go +++ b/modules/repository/hooks.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "github.com/unknwon/com" "xorm.io/builder" @@ -53,7 +54,7 @@ func createDelegateHooks(repoPath string) (err error) { } // WARNING: This will override all old server-side hooks - if err = os.Remove(oldHookPath); err != nil && !os.IsNotExist(err) { + if err = util.Remove(oldHookPath); err != nil && !os.IsNotExist(err) { return fmt.Errorf("unable to pre-remove old hook file '%s' prior to rewriting: %v ", oldHookPath, err) } if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil { @@ -64,7 +65,7 @@ func createDelegateHooks(repoPath string) (err error) { return fmt.Errorf("Unable to set %s executable. Error %v", oldHookPath, err) } - if err = os.Remove(newHookPath); err != nil && !os.IsNotExist(err) { + if err = util.Remove(newHookPath); err != nil && !os.IsNotExist(err) { return fmt.Errorf("unable to pre-remove new hook file '%s' prior to rewriting: %v", newHookPath, err) } if err = ioutil.WriteFile(newHookPath, []byte(giteaHookTpls[i]), 0777); err != nil { diff --git a/modules/repository/init.go b/modules/repository/init.go index 5bdfa7490d..8c8827c511 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "github.com/mcuadros/go-version" "github.com/unknwon/com" @@ -188,8 +189,11 @@ func initRepository(ctx models.DBContext, repoPath string, u *models.User, repo if err != nil { return fmt.Errorf("Failed to create temp dir for repository %s: %v", repo.RepoPath(), err) } - - defer os.RemoveAll(tmpDir) + defer func() { + if err := util.RemoveAll(tmpDir); err != nil { + log.Warn("Unable to remove temporary directory: %s: Error: %v", tmpDir, err) + } + }() if err = prepareRepoCommit(ctx, repo, tmpDir, repoPath, opts); err != nil { return fmt.Errorf("prepareRepoCommit: %v", err) diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 01d4701c71..2d5551d987 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -6,7 +6,6 @@ package repository import ( "fmt" - "os" "path" "strings" "time" @@ -17,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "gopkg.in/ini.v1" ) @@ -57,7 +57,7 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt migrateTimeout := time.Duration(setting.Git.Timeout.Migrate) * time.Second var err error - if err = os.RemoveAll(repoPath); err != nil { + if err = util.RemoveAll(repoPath); err != nil { return repo, fmt.Errorf("Failed to remove %s: %v", repoPath, err) } @@ -73,7 +73,7 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt wikiPath := models.WikiPath(u.Name, opts.RepoName) wikiRemotePath := WikiRemoteURL(opts.CloneAddr) if len(wikiRemotePath) > 0 { - if err := os.RemoveAll(wikiPath); err != nil { + if err := util.RemoveAll(wikiPath); err != nil { return repo, fmt.Errorf("Failed to remove %s: %v", wikiPath, err) } @@ -84,7 +84,7 @@ func MigrateRepositoryGitData(doer, u *models.User, repo *models.Repository, opt Branch: "master", }); err != nil { log.Warn("Clone wiki: %v", err) - if err := os.RemoveAll(wikiPath); err != nil { + if err := util.RemoveAll(wikiPath); err != nil { return repo, fmt.Errorf("Failed to remove %s: %v", wikiPath, err) } } -- cgit v1.2.3