diff options
Diffstat (limited to 'modules')
25 files changed, 127 insertions, 101 deletions
diff --git a/modules/git/commit_info_test.go b/modules/git/commit_info_test.go index ef40e4a3ab..8bdf1a769b 100644 --- a/modules/git/commit_info_test.go +++ b/modules/git/commit_info_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -87,7 +88,7 @@ func TestEntries_GetCommitsInfo(t *testing.T) { clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestEntries_GetCommitsInfo") assert.NoError(t, err) - defer os.RemoveAll(clonedPath) + defer util.RemoveAll(clonedPath) clonedRepo1, err := OpenRepository(clonedPath) assert.NoError(t, err) defer clonedRepo1.Close() diff --git a/modules/git/hook.go b/modules/git/hook.go index 245c6b92ce..7b75405901 100644 --- a/modules/git/hook.go +++ b/modules/git/hook.go @@ -12,6 +12,7 @@ import ( "path/filepath" "strings" + "code.gitea.io/gitea/modules/util" "github.com/unknwon/com" ) @@ -82,7 +83,7 @@ func (h *Hook) Name() string { func (h *Hook) Update() error { if len(strings.TrimSpace(h.Content)) == 0 { if isExist(h.path) { - err := os.Remove(h.path) + err := util.Remove(h.path) if err != nil { return err } @@ -129,7 +130,7 @@ func SetUpdateHook(repoPath, content string) (err error) { log("Setting update hook: %s", repoPath) hookPath := path.Join(repoPath, HookPathUpdate) if com.IsExist(hookPath) { - err = os.Remove(hookPath) + err = util.Remove(hookPath) } else { err = os.MkdirAll(path.Dir(hookPath), os.ModePerm) } diff --git a/modules/git/repo_compare_test.go b/modules/git/repo_compare_test.go index bf4631d853..f0e20838f4 100644 --- a/modules/git/repo_compare_test.go +++ b/modules/git/repo_compare_test.go @@ -7,10 +7,10 @@ package git import ( "bytes" "io/ioutil" - "os" "path/filepath" "testing" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -18,7 +18,7 @@ func TestGetFormatPatch(t *testing.T) { bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestGetFormatPatch") assert.NoError(t, err) - defer os.RemoveAll(clonedPath) + defer util.RemoveAll(clonedPath) repo, err := OpenRepository(clonedPath) assert.NoError(t, err) defer repo.Close() diff --git a/modules/git/repo_tag_test.go b/modules/git/repo_tag_test.go index 7a644e39db..1dee29ba57 100644 --- a/modules/git/repo_tag_test.go +++ b/modules/git/repo_tag_test.go @@ -5,10 +5,10 @@ package git import ( - "os" "path/filepath" "testing" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -31,7 +31,7 @@ func TestRepository_GetTag(t *testing.T) { clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestRepository_GetTag") assert.NoError(t, err) - defer os.RemoveAll(clonedPath) + defer util.RemoveAll(clonedPath) bareRepo1, err := OpenRepository(clonedPath) assert.NoError(t, err) @@ -81,7 +81,7 @@ func TestRepository_GetAnnotatedTag(t *testing.T) { clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestRepository_GetTag") assert.NoError(t, err) - defer os.RemoveAll(clonedPath) + defer util.RemoveAll(clonedPath) bareRepo1, err := OpenRepository(clonedPath) assert.NoError(t, err) diff --git a/modules/graceful/net_unix.go b/modules/graceful/net_unix.go index 1e496e9d91..2dc714955e 100644 --- a/modules/graceful/net_unix.go +++ b/modules/graceful/net_unix.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" ) const ( @@ -173,7 +174,7 @@ func GetListenerUnix(network string, address *net.UnixAddr) (*net.UnixListener, } // make a fresh listener - if err := os.Remove(address.Name); err != nil && !os.IsNotExist(err) { + if err := util.Remove(address.Name); err != nil && !os.IsNotExist(err) { return nil, fmt.Errorf("Failed to remove unix socket %s: %v", address.Name, err) } diff --git a/modules/indexer/code/bleve.go b/modules/indexer/code/bleve.go index 576cc3e5e4..6502259ba4 100644 --- a/modules/indexer/code/bleve.go +++ b/modules/indexer/code/bleve.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "github.com/blevesearch/bleve" analyzer_custom "github.com/blevesearch/bleve/analysis/analyzer/custom" @@ -75,14 +76,14 @@ func openIndexer(path string, latestVersion int) (bleve.Index, error) { if metadata.Version < latestVersion { // the indexer is using a previous version, so we should delete it and // re-populate - return nil, os.RemoveAll(path) + return nil, util.RemoveAll(path) } index, err := bleve.Open(path) if err != nil && err == upsidedown.IncompatibleVersion { // the indexer was built with a previous version of bleve, so we should // delete it and re-populate - return nil, os.RemoveAll(path) + return nil, util.RemoveAll(path) } else if err != nil { return nil, err } diff --git a/modules/indexer/code/bleve_test.go b/modules/indexer/code/bleve_test.go index 89cfceea2d..2b3128ac88 100644 --- a/modules/indexer/code/bleve_test.go +++ b/modules/indexer/code/bleve_test.go @@ -6,12 +6,12 @@ package code import ( "io/ioutil" - "os" "path/filepath" "testing" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -29,7 +29,7 @@ func TestIndexAndSearch(t *testing.T) { assert.Fail(t, "Unable to create temporary directory") return } - defer os.RemoveAll(dir) + defer util.RemoveAll(dir) setting.Indexer.RepoIndexerEnabled = true idx, _, err := NewBleveIndexer(dir) diff --git a/modules/indexer/issues/bleve.go b/modules/indexer/issues/bleve.go index 655bc7dd17..a1f51dba50 100644 --- a/modules/indexer/issues/bleve.go +++ b/modules/indexer/issues/bleve.go @@ -10,6 +10,7 @@ import ( "strconv" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/util" "github.com/blevesearch/bleve" "github.com/blevesearch/bleve/analysis/analyzer/custom" "github.com/blevesearch/bleve/analysis/token/lowercase" @@ -86,14 +87,14 @@ func openIndexer(path string, latestVersion int) (bleve.Index, error) { if metadata.Version < latestVersion { // the indexer is using a previous version, so we should delete it and // re-populate - return nil, os.RemoveAll(path) + return nil, util.RemoveAll(path) } index, err := bleve.Open(path) if err != nil && err == upsidedown.IncompatibleVersion { // the indexer was built with a previous version of bleve, so we should // delete it and re-populate - return nil, os.RemoveAll(path) + return nil, util.RemoveAll(path) } else if err != nil { return nil, err } diff --git a/modules/indexer/issues/bleve_test.go b/modules/indexer/issues/bleve_test.go index 234da076cd..a8b6e8138b 100644 --- a/modules/indexer/issues/bleve_test.go +++ b/modules/indexer/issues/bleve_test.go @@ -6,9 +6,9 @@ package issues import ( "io/ioutil" - "os" "testing" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -19,7 +19,7 @@ func TestBleveIndexAndSearch(t *testing.T) { assert.Fail(t, "Unable to create temporary directory") return } - defer os.RemoveAll(dir) + defer util.RemoveAll(dir) indexer := NewBleveIndexer(dir) defer indexer.Close() diff --git a/modules/indexer/issues/indexer_test.go b/modules/indexer/issues/indexer_test.go index 8a54c200ff..95007d8faf 100644 --- a/modules/indexer/issues/indexer_test.go +++ b/modules/indexer/issues/indexer_test.go @@ -6,7 +6,6 @@ package issues import ( "io/ioutil" - "os" "path" "path/filepath" "testing" @@ -14,6 +13,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "gopkg.in/ini.v1" @@ -40,7 +40,7 @@ func TestBleveSearchIssues(t *testing.T) { defer func() { setting.Indexer.IssueQueueDir = oldQueueDir setting.Indexer.IssuePath = oldIssuePath - os.RemoveAll(tmpIndexerDir) + util.RemoveAll(tmpIndexerDir) }() setting.Indexer.IssueType = "bleve" diff --git a/modules/lfs/content_store.go b/modules/lfs/content_store.go index 6ad565178c..b0fa77e255 100644 --- a/modules/lfs/content_store.go +++ b/modules/lfs/content_store.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/util" ) var ( @@ -61,7 +62,11 @@ func (s *ContentStore) Put(meta *models.LFSMetaObject, r io.Reader) error { log.Error("Whilst putting LFS OID[%s]: Unable to open temporary file for writing: %s Error: %v", tmpPath, err) return err } - defer os.Remove(tmpPath) + defer func() { + if err := util.Remove(tmpPath); err != nil { + log.Warn("Unable to remove temporary path: %s: Error: %v", tmpPath, err) + } + }() hash := sha256.New() hw := io.MultiWriter(hash, file) diff --git a/modules/log/file.go b/modules/log/file.go index 925d83f2b7..c9b5d47c0b 100644 --- a/modules/log/file.go +++ b/modules/log/file.go @@ -15,6 +15,8 @@ import ( "strings" "sync" "time" + + "code.gitea.io/gitea/modules/util" ) // FileLogger implements LoggerProvider. @@ -214,11 +216,11 @@ func compressOldLogFile(fname string, compressionLevel int) error { if err != nil { zw.Close() fw.Close() - os.Remove(fname + ".gz") + util.Remove(fname + ".gz") return err } reader.Close() - return os.Remove(fname) + return util.Remove(fname) } func (log *FileLogger) deleteOldLog() { @@ -233,7 +235,7 @@ func (log *FileLogger) deleteOldLog() { if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*log.Maxdays) { if strings.HasPrefix(filepath.Base(path), filepath.Base(log.Filename)) { - if err := os.Remove(path); err != nil { + if err := util.Remove(path); err != nil { returnErr = fmt.Errorf("Failed to remove %s: %v", path, err) } } diff --git a/modules/log/file_test.go b/modules/log/file_test.go index 38279315ab..af6fbcb29d 100644 --- a/modules/log/file_test.go +++ b/modules/log/file_test.go @@ -14,13 +14,14 @@ import ( "testing" "time" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) func TestFileLoggerFails(t *testing.T) { tmpDir, err := ioutil.TempDir("", "TestFileLogger") assert.NoError(t, err) - defer os.RemoveAll(tmpDir) + defer util.RemoveAll(tmpDir) prefix := "TestPrefix " level := INFO @@ -48,7 +49,7 @@ func TestFileLoggerFails(t *testing.T) { func TestFileLogger(t *testing.T) { tmpDir, err := ioutil.TempDir("", "TestFileLogger") assert.NoError(t, err) - defer os.RemoveAll(tmpDir) + defer util.RemoveAll(tmpDir) prefix := "TestPrefix " level := INFO @@ -151,7 +152,7 @@ func TestFileLogger(t *testing.T) { func TestCompressFileLogger(t *testing.T) { tmpDir, err := ioutil.TempDir("", "TestFileLogger") assert.NoError(t, err) - defer os.RemoveAll(tmpDir) + defer util.RemoveAll(tmpDir) prefix := "TestPrefix " level := INFO @@ -211,7 +212,7 @@ func TestCompressFileLogger(t *testing.T) { func TestCompressOldFile(t *testing.T) { tmpDir, err := ioutil.TempDir("", "TestFileLogger") assert.NoError(t, err) - defer os.RemoveAll(tmpDir) + defer util.RemoveAll(tmpDir) fname := filepath.Join(tmpDir, "test") nonGzip := filepath.Join(tmpDir, "test-nonGzip") diff --git a/modules/markup/external/external.go b/modules/markup/external/external.go index 02cf61be16..7acf936171 100644 --- a/modules/markup/external/external.go +++ b/modules/markup/external/external.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" ) // RegisterParsers registers all supported third part parsers according settings @@ -70,7 +71,12 @@ func (p *Parser) Render(rawBytes []byte, urlPrefix string, metas map[string]stri log.Error("%s create temp file when rendering %s failed: %v", p.Name(), p.Command, err) return []byte("") } - defer os.Remove(f.Name()) + tmpPath := f.Name() + defer func() { + if err := util.Remove(tmpPath); err != nil { + log.Warn("Unable to remove temporary file: %s: Error: %v", tmpPath, err) + } + }() _, err = io.Copy(f, rd) if err != nil { diff --git a/modules/queue/queue_disk_channel_test.go b/modules/queue/queue_disk_channel_test.go index 4ef68961c6..5049fb58d0 100644 --- a/modules/queue/queue_disk_channel_test.go +++ b/modules/queue/queue_disk_channel_test.go @@ -7,10 +7,10 @@ package queue import ( "context" "io/ioutil" - "os" "testing" "time" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -29,7 +29,7 @@ func TestPersistableChannelQueue(t *testing.T) { tmpDir, err := ioutil.TempDir("", "persistable-channel-queue-test-data") assert.NoError(t, err) - defer os.RemoveAll(tmpDir) + defer util.RemoveAll(tmpDir) queue, err := NewPersistableChannelQueue(handle, PersistableChannelQueueConfiguration{ DataDir: tmpDir, diff --git a/modules/queue/queue_disk_test.go b/modules/queue/queue_disk_test.go index c7d3eb160b..789b6c3e64 100644 --- a/modules/queue/queue_disk_test.go +++ b/modules/queue/queue_disk_test.go @@ -7,11 +7,11 @@ package queue import ( "context" "io/ioutil" - "os" "sync" "testing" "time" + "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) @@ -31,7 +31,7 @@ func TestLevelQueue(t *testing.T) { tmpDir, err := ioutil.TempDir("", "level-queue-test-data") assert.NoError(t, err) - defer os.RemoveAll(tmpDir) + defer util.RemoveAll(tmpDir) queue, err := NewLevelQueue(handle, LevelQueueConfiguration{ ByteFIFOQueueConfiguration: ByteFIFOQueueConfiguration{ 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) } } diff --git a/modules/util/remove.go b/modules/util/remove.go new file mode 100644 index 0000000000..f2bbbc30b9 --- /dev/null +++ b/modules/util/remove.go @@ -0,0 +1,57 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package util + +import ( + "os" + "syscall" + "time" +) + +// Remove removes the named file or (empty) directory with at most 5 attempts. +func Remove(name string) error { + var err error + for i := 0; i < 5; i++ { + err = os.Remove(name) + if err == nil { + break + } + unwrapped := err.(*os.PathError).Err + if unwrapped == syscall.EBUSY || unwrapped == syscall.ENOTEMPTY || unwrapped == syscall.EPERM || unwrapped == syscall.EMFILE || unwrapped == syscall.ENFILE { + // try again + <-time.After(100 * time.Millisecond) + continue + } + + if unwrapped == syscall.ENOENT { + // it's already gone + return nil + } + } + return err +} + +// RemoveAll removes the named file or (empty) directory with at most 5 attempts.Remove +func RemoveAll(name string) error { + var err error + for i := 0; i < 5; i++ { + err = os.RemoveAll(name) + if err == nil { + break + } + unwrapped := err.(*os.PathError).Err + if unwrapped == syscall.EBUSY || unwrapped == syscall.ENOTEMPTY || unwrapped == syscall.EPERM || unwrapped == syscall.EMFILE || unwrapped == syscall.ENFILE { + // try again + <-time.After(100 * time.Millisecond) + continue + } + + if unwrapped == syscall.ENOENT { + // it's already gone + return nil + } + } + return err +} diff --git a/modules/util/sanitize.go b/modules/util/sanitize.go index d04e1dee77..b1c17b29cf 100644 --- a/modules/util/sanitize.go +++ b/modules/util/sanitize.go @@ -7,8 +7,6 @@ package util import ( "net/url" "strings" - - "code.gitea.io/gitea/modules/log" ) // urlSafeError wraps an error whose message may contain a sensitive URL @@ -38,7 +36,6 @@ func SanitizeMessage(message, unsanitizedURL string) string { func SanitizeURLCredentials(unsanitizedURL string, usePlaceholder bool) string { u, err := url.Parse(unsanitizedURL) if err != nil { - log.Error("parse url %s failed: %v", unsanitizedURL, err) // don't log the error, since it might contain unsanitized URL. return "(unparsable url)" } diff --git a/modules/util/url.go b/modules/util/url.go index 263255fcd3..96c66df92b 100644 --- a/modules/util/url.go +++ b/modules/util/url.go @@ -8,9 +8,6 @@ import ( "net/url" "path" "strings" - - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/setting" ) // PathEscapeSegments escapes segments of a path while not escaping forward slash @@ -30,13 +27,11 @@ func URLJoin(base string, elems ...string) string { } baseURL, err := url.Parse(base) if err != nil { - log.Error("URLJoin: Invalid base URL %s", base) return "" } joinedPath := path.Join(elems...) argURL, err := url.Parse(joinedPath) if err != nil { - log.Error("URLJoin: Invalid arg %s", joinedPath) return "" } joinedURL := baseURL.ResolveReference(argURL).String() @@ -45,16 +40,3 @@ func URLJoin(base string, elems ...string) string { } return joinedURL } - -// IsExternalURL checks if rawURL points to an external URL like http://example.com -func IsExternalURL(rawURL string) bool { - parsed, err := url.Parse(rawURL) - if err != nil { - return true - } - appURL, _ := url.Parse(setting.AppURL) - if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(appURL.Host, "www.", "", 1) { - return true - } - return false -} diff --git a/modules/util/util_test.go b/modules/util/util_test.go index 04ab42f292..1d4f23de90 100644 --- a/modules/util/util_test.go +++ b/modules/util/util_test.go @@ -8,8 +8,6 @@ import ( "strings" "testing" - "code.gitea.io/gitea/modules/setting" - "github.com/stretchr/testify/assert" ) @@ -46,39 +44,6 @@ func TestURLJoin(t *testing.T) { } } -func TestIsExternalURL(t *testing.T) { - setting.AppURL = "https://try.gitea.io" - type test struct { - Expected bool - RawURL string - } - newTest := func(expected bool, rawURL string) test { - return test{Expected: expected, RawURL: rawURL} - } - for _, test := range []test{ - newTest(false, - "https://try.gitea.io"), - newTest(true, - "https://example.com/"), - newTest(true, - "//example.com"), - newTest(true, - "http://example.com"), - newTest(false, - "a/"), - newTest(false, - "https://try.gitea.io/test?param=false"), - newTest(false, - "test?param=false"), - newTest(false, - "//try.gitea.io/test?param=false"), - newTest(false, - "/hey/hey/hey#3244"), - } { - assert.Equal(t, test.Expected, IsExternalURL(test.RawURL)) - } -} - func TestIsEmptyString(t *testing.T) { cases := []struct { |