diff options
author | a1012112796 <1012112796@qq.com> | 2022-07-14 21:52:18 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-14 21:52:18 +0800 |
commit | f85bb6f70bb390ec04a296827ac0f2a078741a80 (patch) | |
tree | 004b7024de47d6fcb8405d6f3a99e5a5ee243f6c /cmd | |
parent | 725f9e40b36b510e734a95d83b0f1fd0a8f3ef86 (diff) | |
download | gitea-f85bb6f70bb390ec04a296827ac0f2a078741a80.tar.gz gitea-f85bb6f70bb390ec04a296827ac0f2a078741a80.zip |
Make sure `repo_dir` is an empty directory or doesn't exist before 'dump-repo' (#20205)
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/dump_repo.go | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/cmd/dump_repo.go b/cmd/dump_repo.go index c9d24c6c83..72456c61d3 100644 --- a/cmd/dump_repo.go +++ b/cmd/dump_repo.go @@ -7,6 +7,8 @@ package cmd import ( "context" "errors" + "fmt" + "os" "strings" "code.gitea.io/gitea/modules/convert" @@ -15,6 +17,7 @@ import ( base "code.gitea.io/gitea/modules/migration" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/migrations" "github.com/urfave/cli" @@ -159,9 +162,23 @@ func runDumpRepository(ctx *cli.Context) error { } } + // the repo_dir will be removed if error occurs in DumpRepository + // make sure the directory doesn't exist or is empty, prevent from deleting user files + repoDir := ctx.String("repo_dir") + if exists, err := util.IsExist(repoDir); err != nil { + return fmt.Errorf("unable to stat repo_dir %q: %v", repoDir, err) + } else if exists { + if isDir, _ := util.IsDir(repoDir); !isDir { + return fmt.Errorf("repo_dir %q already exists but it's not a directory", repoDir) + } + if dir, _ := os.ReadDir(repoDir); len(dir) > 0 { + return fmt.Errorf("repo_dir %q is not empty", repoDir) + } + } + if err := migrations.DumpRepository( context.Background(), - ctx.String("repo_dir"), + repoDir, ctx.String("owner_name"), opts, ); err != nil { |