From ce14df303b19f6c9e9327a0d9df1388921c8e583 Mon Sep 17 00:00:00 2001 From: Andreas Olsson Date: Sun, 20 Sep 2020 12:04:08 +0200 Subject: [PATCH] Don't require `self-update --output` placeholder file This removes the requirement on `restic self-update --output` to point to a path of an existing file, to overwrite. In case the specified path does exist we still want to verify that it's a regular file, rather than a directory or a device, which gets overwritten. We also want to verify that a path to a new file exists within an existing directory. The alternative being running into that issue after the actual download, etc has completed. While at it I also replace `errors.Errorf` with the more appropriately verbose `errors.Fatalf`. Resolves #2491 --- changelog/unreleased/issue-2491 | 9 +++++++++ cmd/restic/cmd_self_update.go | 18 +++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 changelog/unreleased/issue-2491 diff --git a/changelog/unreleased/issue-2491 b/changelog/unreleased/issue-2491 new file mode 100644 index 000000000..ed1dbdac6 --- /dev/null +++ b/changelog/unreleased/issue-2491 @@ -0,0 +1,9 @@ +Bugfix: Don't require `self-update --output` placeholder file + +`restic self-update --output /path/to/new-restic` used to require that +new-restic was an existing file, to be overwritten. Now it's possible +to download an updated restic binary to a new path, without first +having to create a placeholder file. + +https://github.com/restic/restic/issues/2491 +https://github.com/restic/restic/pull/2937 diff --git a/cmd/restic/cmd_self_update.go b/cmd/restic/cmd_self_update.go index 310c5100f..f71bdf16b 100644 --- a/cmd/restic/cmd_self_update.go +++ b/cmd/restic/cmd_self_update.go @@ -4,6 +4,7 @@ package main import ( "os" + "path/filepath" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/selfupdate" @@ -56,11 +57,18 @@ func runSelfUpdate(opts SelfUpdateOptions, gopts GlobalOptions, args []string) e fi, err := os.Lstat(opts.Output) if err != nil { - return err - } - - if !fi.Mode().IsRegular() { - return errors.Errorf("output file %v is not a normal file, use --output to specify a different file", opts.Output) + dirname := filepath.Dir(opts.Output) + di, err := os.Lstat(dirname) + if err != nil { + return err + } + if !di.Mode().IsDir() { + return errors.Fatalf("output parent path %v is not a directory, use --output to specify a different file path", dirname) + } + } else { + if !fi.Mode().IsRegular() { + return errors.Fatalf("output path %v is not a normal file, use --output to specify a different file path", opts.Output) + } } Printf("writing restic to %v\n", opts.Output)