From b78cbcbb99afa756faef3f1f131a02e29a4926e4 Mon Sep 17 00:00:00 2001 From: Phi Date: Wed, 8 Feb 2023 11:33:48 +0100 Subject: [PATCH 1/2] Error if backup file exists Error out if backup file already exists --- cli/backup.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cli/backup.go b/cli/backup.go index 83234a42392..e939ec0dfb6 100644 --- a/cli/backup.go +++ b/cli/backup.go @@ -61,6 +61,10 @@ func BackupCmd(repoFlag string, rt repo.RepoType, getApi BackupApiFn) *cli.Comma return xerrors.Errorf("expanding file path: %w", err) } + if _, err := os.Stat(fpath); !os.IsNotExist(err) { + return xerrors.Errorf("backup file %s already exists. Overwriting it will corrupt the file, please specify another file name", fpath) + } + out, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { return xerrors.Errorf("opening backup file %s: %w", fpath, err) @@ -87,7 +91,12 @@ func BackupCmd(repoFlag string, rt repo.RepoType, getApi BackupApiFn) *cli.Comma } defer closer() - err = api.CreateBackup(ReqContext(cctx), cctx.Args().First()) + backupPath := cctx.Args().First() + if _, err := os.Stat(backupPath); err == nil { + return xerrors.Errorf("backup file %s already exists. Overwriting it will corrupt the file, please specify another file name", backupPath) + } + + err = api.CreateBackup(ReqContext(cctx), backupPath) if err != nil { return err } From 0e7cf61888257d18a0f3b1efbd0adf2125cc048d Mon Sep 17 00:00:00 2001 From: Phi-rjan Date: Thu, 9 Feb 2023 16:50:15 +0100 Subject: [PATCH 2/2] Update cli/backup.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ɓukasz Magiera --- cli/backup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/backup.go b/cli/backup.go index e939ec0dfb6..d2d8f25ff89 100644 --- a/cli/backup.go +++ b/cli/backup.go @@ -92,7 +92,7 @@ func BackupCmd(repoFlag string, rt repo.RepoType, getApi BackupApiFn) *cli.Comma defer closer() backupPath := cctx.Args().First() - if _, err := os.Stat(backupPath); err == nil { + if _, err := os.Stat(backupPath); !os.IsNotExist(err) { return xerrors.Errorf("backup file %s already exists. Overwriting it will corrupt the file, please specify another file name", backupPath) }