@@ -7,6 +7,8 @@ package cmd
77import (
88 "context"
99 "errors"
10+ "fmt"
11+ "os"
1012 "strings"
1113
1214 "code.gitea.io/gitea/modules/convert"
@@ -15,6 +17,7 @@ import (
1517 base "code.gitea.io/gitea/modules/migration"
1618 "code.gitea.io/gitea/modules/setting"
1719 "code.gitea.io/gitea/modules/structs"
20+ "code.gitea.io/gitea/modules/util"
1821 "code.gitea.io/gitea/services/migrations"
1922
2023 "github.com/urfave/cli"
@@ -159,9 +162,23 @@ func runDumpRepository(ctx *cli.Context) error {
159162 }
160163 }
161164
165+ // the repo_dir will be removed if error occurs in DumpRepository
166+ // make sure the directory doesn't exist or is empty, prevent from deleting user files
167+ repoDir := ctx .String ("repo_dir" )
168+ if exists , err := util .IsExist (repoDir ); err != nil {
169+ return fmt .Errorf ("unable to stat repo_dir %q: %v" , repoDir , err )
170+ } else if exists {
171+ if isDir , _ := util .IsDir (repoDir ); ! isDir {
172+ return fmt .Errorf ("repo_dir %q already exists but it's not a directory" , repoDir )
173+ }
174+ if dir , _ := os .ReadDir (repoDir ); len (dir ) > 0 {
175+ return fmt .Errorf ("repo_dir %q is not empty" , repoDir )
176+ }
177+ }
178+
162179 if err := migrations .DumpRepository (
163180 context .Background (),
164- ctx . String ( "repo_dir" ) ,
181+ repoDir ,
165182 ctx .String ("owner_name" ),
166183 opts ,
167184 ); err != nil {
0 commit comments