aboutsummaryrefslogtreecommitdiff
path: root/internal/cmd
diff options
context:
space:
mode:
authorJoe Chen <jc@unknwon.io>2022-03-26 15:10:39 +0800
committerGitHub <noreply@github.com>2022-03-26 15:10:39 +0800
commitf37cd9672cb447cb820e2da5144a25d5d6b654d7 (patch)
tree8d2acad4a80ed308cad2cc8eb65a79d62c385532 /internal/cmd
parent9bce320160a772fb32e8cc5ea612937d8ad6a889 (diff)
restore: clean up leftover and invalid chars (#6875)
Diffstat (limited to 'internal/cmd')
-rw-r--r--internal/cmd/restore.go31
1 files changed, 19 insertions, 12 deletions
diff --git a/internal/cmd/restore.go b/internal/cmd/restore.go
index 1b6cdf2c..4c4b1661 100644
--- a/internal/cmd/restore.go
+++ b/internal/cmd/restore.go
@@ -11,13 +11,13 @@ import (
"github.com/pkg/errors"
"github.com/unknwon/cae/zip"
- "github.com/unknwon/com"
"github.com/urfave/cli"
"gopkg.in/ini.v1"
log "unknwon.dev/clog/v2"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/db"
+ "gogs.io/gogs/internal/osutil"
"gogs.io/gogs/internal/semverutil"
)
@@ -49,20 +49,27 @@ func runRestore(c *cli.Context) error {
zip.Verbose = c.Bool("verbose")
tmpDir := c.String("tempdir")
- if !com.IsExist(tmpDir) {
+ if !osutil.IsDir(tmpDir) {
log.Fatal("'--tempdir' does not exist: %s", tmpDir)
}
+ archivePath := path.Join(tmpDir, archiveRootDir)
- log.Info("Restore backup from: %s", c.String("from"))
- if err := zip.ExtractTo(c.String("from"), tmpDir); err != nil {
- log.Fatal("Failed to extract backup archive: %v", err)
+ // Make sure there was no leftover and also clean up afterwards
+ err := os.RemoveAll(archivePath)
+ if err != nil {
+ log.Fatal("Failed to clean up previous leftover in %q: %v", archivePath, err)
}
- archivePath := path.Join(tmpDir, archiveRootDir)
defer func() { _ = os.RemoveAll(archivePath) }()
+ log.Info("Restoring backup from: %s", c.String("from"))
+ err = zip.ExtractTo(c.String("from"), tmpDir)
+ if err != nil {
+ log.Fatal("Failed to extract backup archive: %v", err)
+ }
+
// Check backup version
metaFile := filepath.Join(archivePath, "metadata.ini")
- if !com.IsExist(metaFile) {
+ if !osutil.IsFile(metaFile) {
log.Fatal("File 'metadata.ini' is missing")
}
metadata, err := ini.Load(metaFile)
@@ -88,7 +95,7 @@ func runRestore(c *cli.Context) error {
var customConf string
if c.IsSet("config") {
customConf = c.String("config")
- } else if !com.IsExist(configFile) {
+ } else if !osutil.IsFile(configFile) {
log.Fatal("'--config' is not specified and custom config file is not found in backup")
} else {
customConf = configFile
@@ -113,7 +120,7 @@ func runRestore(c *cli.Context) error {
// Custom files
if !c.Bool("database-only") {
- if com.IsExist(conf.CustomDir()) {
+ if osutil.IsDir(conf.CustomDir()) {
if err = os.Rename(conf.CustomDir(), conf.CustomDir()+".bak"); err != nil {
log.Fatal("Failed to backup current 'custom': %v", err)
}
@@ -129,12 +136,12 @@ func runRestore(c *cli.Context) error {
for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
// Skip if backup archive does not have corresponding data
srcPath := filepath.Join(archivePath, "data", dir)
- if !com.IsDir(srcPath) {
+ if !osutil.IsDir(srcPath) {
continue
}
dirPath := filepath.Join(conf.Server.AppDataPath, dir)
- if com.IsExist(dirPath) {
+ if osutil.IsDir(dirPath) {
if err = os.Rename(dirPath, dirPath+".bak"); err != nil {
log.Fatal("Failed to backup current 'data': %v", err)
}
@@ -147,7 +154,7 @@ func runRestore(c *cli.Context) error {
// Repositories
reposPath := filepath.Join(archivePath, "repositories.zip")
- if !c.Bool("exclude-repos") && !c.Bool("database-only") && com.IsExist(reposPath) {
+ if !c.Bool("exclude-repos") && !c.Bool("database-only") && osutil.IsDir(reposPath) {
if err := zip.ExtractTo(reposPath, filepath.Dir(conf.Repository.Root)); err != nil {
log.Fatal("Failed to extract 'repositories.zip': %v", err)
}