Skip to content

Commit f85bb6f

Browse files
authored
Make sure repo_dir is an empty directory or doesn't exist before 'dump-repo' (#20205)
1 parent 725f9e4 commit f85bb6f

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Diff for: cmd/dump_repo.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package cmd
77
import (
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

Comments
 (0)