@@ -7,6 +7,8 @@ package cmd
7
7
import (
8
8
"context"
9
9
"errors"
10
+ "fmt"
11
+ "os"
10
12
"strings"
11
13
12
14
"code.gitea.io/gitea/modules/convert"
@@ -15,6 +17,7 @@ import (
15
17
base "code.gitea.io/gitea/modules/migration"
16
18
"code.gitea.io/gitea/modules/setting"
17
19
"code.gitea.io/gitea/modules/structs"
20
+ "code.gitea.io/gitea/modules/util"
18
21
"code.gitea.io/gitea/services/migrations"
19
22
20
23
"github.com/urfave/cli"
@@ -159,9 +162,23 @@ func runDumpRepository(ctx *cli.Context) error {
159
162
}
160
163
}
161
164
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
+
162
179
if err := migrations .DumpRepository (
163
180
context .Background (),
164
- ctx . String ( "repo_dir" ) ,
181
+ repoDir ,
165
182
ctx .String ("owner_name" ),
166
183
opts ,
167
184
); err != nil {
0 commit comments