From 72aa6be38d2f3e1025c6a2cf4f962a0786c3522e Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 21 Aug 2016 17:48:36 +0200 Subject: [PATCH] Replace fmt.Errorf() by errors.Errorf() --- src/cmds/restic/cmd_backup.go | 6 +++--- src/cmds/restic/cmd_cache.go | 2 +- src/cmds/restic/cmd_cat.go | 2 +- src/cmds/restic/cmd_check.go | 2 +- src/cmds/restic/cmd_dump.go | 2 +- src/cmds/restic/cmd_find.go | 9 +++++---- src/cmds/restic/cmd_key.go | 6 +++--- src/cmds/restic/cmd_list.go | 4 +--- src/cmds/restic/cmd_ls.go | 4 +++- src/cmds/restic/cmd_mount.go | 5 +++-- src/cmds/restic/cmd_prune.go | 4 +++- src/cmds/restic/cmd_restore.go | 4 +--- src/cmds/restic/cmd_snapshots.go | 4 +++- src/cmds/restic/global.go | 6 +++--- src/cmds/restic/integration_fuse_test.go | 5 +++-- src/cmds/restic/integration_test.go | 4 +++- src/restic/archiver.go | 4 ++-- src/restic/backend/handle.go | 2 +- src/restic/backend/local/local.go | 5 ++--- src/restic/backend/rest/rest.go | 6 +++--- src/restic/backend/sftp/config.go | 3 +-- src/restic/backend/sftp/sftp.go | 18 +++++++++--------- src/restic/cache.go | 9 ++++----- src/restic/checker/checker.go | 23 +++++++++++------------ src/restic/crypto/kdf.go | 8 ++++---- src/restic/index/index.go | 6 +++--- src/restic/node.go | 6 ++++-- src/restic/pack/pack.go | 4 ++-- src/restic/repository/index.go | 4 ++-- src/restic/repository/master_index.go | 7 ++++--- src/restic/repository/packer_manager.go | 5 +++-- src/restic/repository/repository.go | 6 +++--- src/restic/restorer.go | 5 +++-- src/restic/snapshot.go | 2 +- 34 files changed, 100 insertions(+), 92 deletions(-) diff --git a/src/cmds/restic/cmd_backup.go b/src/cmds/restic/cmd_backup.go index 9efb27203..6888611ee 100644 --- a/src/cmds/restic/cmd_backup.go +++ b/src/cmds/restic/cmd_backup.go @@ -240,7 +240,7 @@ func filterExisting(items []string) (result []string, err error) { func (cmd CmdBackup) readFromStdin(args []string) error { if len(args) != 0 { - return fmt.Errorf("when reading from stdin, no additional files can be specified") + return errors.Errorf("when reading from stdin, no additional files can be specified") } repo, err := cmd.global.OpenRepository() @@ -274,7 +274,7 @@ func (cmd CmdBackup) Execute(args []string) error { } if len(args) == 0 { - return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) + return errors.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) } target := make([]string, 0, len(args)) @@ -312,7 +312,7 @@ func (cmd CmdBackup) Execute(args []string) error { if !cmd.Force && cmd.Parent != "" { id, err := restic.FindSnapshot(repo, cmd.Parent) if err != nil { - return fmt.Errorf("invalid id %q: %v", cmd.Parent, err) + return errors.Errorf("invalid id %q: %v", cmd.Parent, err) } parentSnapshotID = &id diff --git a/src/cmds/restic/cmd_cache.go b/src/cmds/restic/cmd_cache.go index 0a7e0c5e9..989571f40 100644 --- a/src/cmds/restic/cmd_cache.go +++ b/src/cmds/restic/cmd_cache.go @@ -26,7 +26,7 @@ func (cmd CmdCache) Usage() string { func (cmd CmdCache) Execute(args []string) error { // if len(args) == 0 || len(args) > 2 { - // return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) + // return errors.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) // } repo, err := cmd.global.OpenRepository() diff --git a/src/cmds/restic/cmd_cat.go b/src/cmds/restic/cmd_cat.go index f8a96772c..2d5bbc18b 100644 --- a/src/cmds/restic/cmd_cat.go +++ b/src/cmds/restic/cmd_cat.go @@ -34,7 +34,7 @@ func (cmd CmdCat) Usage() string { func (cmd CmdCat) Execute(args []string) error { if len(args) < 1 || (args[0] != "masterkey" && args[0] != "config" && len(args) != 2) { - return fmt.Errorf("type or ID not specified, Usage: %s", cmd.Usage()) + return errors.Errorf("type or ID not specified, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() diff --git a/src/cmds/restic/cmd_check.go b/src/cmds/restic/cmd_check.go index cbb79f8c4..829996943 100644 --- a/src/cmds/restic/cmd_check.go +++ b/src/cmds/restic/cmd_check.go @@ -105,7 +105,7 @@ func (cmd CmdCheck) Execute(args []string) error { for _, err := range errs { cmd.global.Warnf("error: %v\n", err) } - return fmt.Errorf("LoadIndex returned errors") + return errors.Errorf("LoadIndex returned errors") } done := make(chan struct{}) diff --git a/src/cmds/restic/cmd_dump.go b/src/cmds/restic/cmd_dump.go index 31ed114aa..1d17e2f6a 100644 --- a/src/cmds/restic/cmd_dump.go +++ b/src/cmds/restic/cmd_dump.go @@ -204,7 +204,7 @@ func (cmd CmdDump) DumpIndexes() error { func (cmd CmdDump) Execute(args []string) error { if len(args) != 1 { - return fmt.Errorf("type not specified, Usage: %s", cmd.Usage()) + return errors.Errorf("type not specified, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() diff --git a/src/cmds/restic/cmd_find.go b/src/cmds/restic/cmd_find.go index 9f96187e1..2bd96caf6 100644 --- a/src/cmds/restic/cmd_find.go +++ b/src/cmds/restic/cmd_find.go @@ -1,10 +1,11 @@ package main import ( - "fmt" "path/filepath" "time" + "github.com/pkg/errors" + "restic" "restic/backend" "restic/debug" @@ -57,7 +58,7 @@ func parseTime(str string) (time.Time, error) { } } - return time.Time{}, fmt.Errorf("unable to parse time: %q", str) + return time.Time{}, errors.Errorf("unable to parse time: %q", str) } func (c CmdFind) findInTree(repo *repository.Repository, id backend.ID, path string) ([]findResult, error) { @@ -137,7 +138,7 @@ func (CmdFind) Usage() string { func (c CmdFind) Execute(args []string) error { if len(args) != 1 { - return fmt.Errorf("wrong number of arguments, Usage: %s", c.Usage()) + return errors.Errorf("wrong number of arguments, Usage: %s", c.Usage()) } var err error @@ -177,7 +178,7 @@ func (c CmdFind) Execute(args []string) error { if c.Snapshot != "" { snapshotID, err := restic.FindSnapshot(repo, c.Snapshot) if err != nil { - return fmt.Errorf("invalid id %q: %v", args[1], err) + return errors.Errorf("invalid id %q: %v", args[1], err) } return c.findInSnapshot(repo, snapshotID) diff --git a/src/cmds/restic/cmd_key.go b/src/cmds/restic/cmd_key.go index 42f0ee6a7..985a86081 100644 --- a/src/cmds/restic/cmd_key.go +++ b/src/cmds/restic/cmd_key.go @@ -70,7 +70,7 @@ func (cmd CmdKey) getNewPassword() string { func (cmd CmdKey) addKey(repo *repository.Repository) error { id, err := repository.AddKey(repo, cmd.getNewPassword(), repo.Key()) if err != nil { - return fmt.Errorf("creating new key failed: %v\n", err) + return errors.Errorf("creating new key failed: %v\n", err) } cmd.global.Verbosef("saved new key as %s\n", id) @@ -95,7 +95,7 @@ func (cmd CmdKey) deleteKey(repo *repository.Repository, name string) error { func (cmd CmdKey) changePassword(repo *repository.Repository) error { id, err := repository.AddKey(repo, cmd.getNewPassword(), repo.Key()) if err != nil { - return fmt.Errorf("creating new key failed: %v\n", err) + return errors.Errorf("creating new key failed: %v\n", err) } err = repo.Backend().Remove(backend.Key, repo.KeyName()) @@ -114,7 +114,7 @@ func (cmd CmdKey) Usage() string { func (cmd CmdKey) Execute(args []string) error { if len(args) < 1 || (args[0] == "rm" && len(args) != 2) { - return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) + return errors.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() diff --git a/src/cmds/restic/cmd_list.go b/src/cmds/restic/cmd_list.go index 57310b6f0..2e0df3935 100644 --- a/src/cmds/restic/cmd_list.go +++ b/src/cmds/restic/cmd_list.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - "github.com/pkg/errors" "restic/backend" @@ -28,7 +26,7 @@ func (cmd CmdList) Usage() string { func (cmd CmdList) Execute(args []string) error { if len(args) != 1 { - return fmt.Errorf("type not specified, Usage: %s", cmd.Usage()) + return errors.Errorf("type not specified, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() diff --git a/src/cmds/restic/cmd_ls.go b/src/cmds/restic/cmd_ls.go index 2f1cccf4d..aee7f77b9 100644 --- a/src/cmds/restic/cmd_ls.go +++ b/src/cmds/restic/cmd_ls.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" + "github.com/pkg/errors" + "restic" "restic/backend" "restic/repository" @@ -72,7 +74,7 @@ func (cmd CmdLs) Usage() string { func (cmd CmdLs) Execute(args []string) error { if len(args) < 1 || len(args) > 2 { - return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) + return errors.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() diff --git a/src/cmds/restic/cmd_mount.go b/src/cmds/restic/cmd_mount.go index 13643c69a..5b8186f05 100644 --- a/src/cmds/restic/cmd_mount.go +++ b/src/cmds/restic/cmd_mount.go @@ -4,9 +4,10 @@ package main import ( - "fmt" "os" + "github.com/pkg/errors" + resticfs "restic/fs" "restic/fuse" @@ -42,7 +43,7 @@ func (cmd CmdMount) Usage() string { func (cmd CmdMount) Execute(args []string) error { if len(args) == 0 { - return fmt.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) + return errors.Errorf("wrong number of parameters, Usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() diff --git a/src/cmds/restic/cmd_prune.go b/src/cmds/restic/cmd_prune.go index 7467c2bdd..0b85c76ba 100644 --- a/src/cmds/restic/cmd_prune.go +++ b/src/cmds/restic/cmd_prune.go @@ -11,6 +11,8 @@ import ( "restic/repository" "time" + "github.com/pkg/errors" + "golang.org/x/crypto/ssh/terminal" ) @@ -191,7 +193,7 @@ nextPack: removePacks.Insert(packID) if !rewritePacks.Has(packID) { - return fmt.Errorf("pack %v is unneeded, but not contained in rewritePacks", packID.Str()) + return errors.Errorf("pack %v is unneeded, but not contained in rewritePacks", packID.Str()) } rewritePacks.Delete(packID) diff --git a/src/cmds/restic/cmd_restore.go b/src/cmds/restic/cmd_restore.go index 84a9a9b08..ecbd97fa7 100644 --- a/src/cmds/restic/cmd_restore.go +++ b/src/cmds/restic/cmd_restore.go @@ -1,8 +1,6 @@ package main import ( - "fmt" - "github.com/pkg/errors" "restic" @@ -37,7 +35,7 @@ func (cmd CmdRestore) Usage() string { func (cmd CmdRestore) Execute(args []string) error { if len(args) != 1 { - return fmt.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) + return errors.Errorf("wrong number of arguments, Usage: %s", cmd.Usage()) } if cmd.Target == "" { diff --git a/src/cmds/restic/cmd_snapshots.go b/src/cmds/restic/cmd_snapshots.go index abca558f5..c55709ee3 100644 --- a/src/cmds/restic/cmd_snapshots.go +++ b/src/cmds/restic/cmd_snapshots.go @@ -8,6 +8,8 @@ import ( "sort" "strings" + "github.com/pkg/errors" + "restic" "restic/backend" ) @@ -70,7 +72,7 @@ func (cmd CmdSnapshots) Usage() string { func (cmd CmdSnapshots) Execute(args []string) error { if len(args) != 0 { - return fmt.Errorf("wrong number of arguments, usage: %s", cmd.Usage()) + return errors.Errorf("wrong number of arguments, usage: %s", cmd.Usage()) } repo, err := cmd.global.OpenRepository() diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index c16263cbb..d6ebec59b 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -263,7 +263,7 @@ func (o GlobalOptions) OpenRepository() (*repository.Repository, error) { err = s.SearchKey(o.password, maxKeys) if err != nil { - return nil, fmt.Errorf("unable to open repo: %v", err) + return nil, errors.Errorf("unable to open repo: %v", err) } return s, nil @@ -301,7 +301,7 @@ func open(s string) (backend.Backend, error) { } debug.Log("open", "invalid repository location: %v", s) - return nil, fmt.Errorf("invalid scheme %q", loc.Scheme) + return nil, errors.Errorf("invalid scheme %q", loc.Scheme) } // Create the backend specified by URI. @@ -336,5 +336,5 @@ func create(s string) (backend.Backend, error) { } debug.Log("open", "invalid repository scheme: %v", s) - return nil, fmt.Errorf("invalid scheme %q", loc.Scheme) + return nil, errors.Errorf("invalid scheme %q", loc.Scheme) } diff --git a/src/cmds/restic/integration_fuse_test.go b/src/cmds/restic/integration_fuse_test.go index 6688770e4..5df4931b6 100644 --- a/src/cmds/restic/integration_fuse_test.go +++ b/src/cmds/restic/integration_fuse_test.go @@ -4,13 +4,14 @@ package main import ( - "fmt" "io/ioutil" "os" "path/filepath" "testing" "time" + "github.com/pkg/errors" + "restic" "restic/backend" "restic/repository" @@ -50,7 +51,7 @@ func waitForMount(dir string) error { time.Sleep(mountSleep) } - return fmt.Errorf("subdir %q of dir %s never appeared", mountTestSubdir, dir) + return errors.Errorf("subdir %q of dir %s never appeared", mountTestSubdir, dir) } func cmdMount(t testing.TB, global GlobalOptions, dir string, ready, done chan struct{}) { diff --git a/src/cmds/restic/integration_test.go b/src/cmds/restic/integration_test.go index 4b99d8a32..ee1ef1893 100644 --- a/src/cmds/restic/integration_test.go +++ b/src/cmds/restic/integration_test.go @@ -15,6 +15,8 @@ import ( "testing" "time" + "github.com/pkg/errors" + "restic/backend" "restic/debug" "restic/filter" @@ -579,7 +581,7 @@ func testFileSize(filename string, size int64) error { } if fi.Size() != size { - return fmt.Errorf("wrong file size for %v: expected %v, got %v", filename, size, fi.Size()) + return errors.Errorf("wrong file size for %v: expected %v, got %v", filename, size, fi.Size()) } return nil diff --git a/src/restic/archiver.go b/src/restic/archiver.go index ae062a7f6..96a624334 100644 --- a/src/restic/archiver.go +++ b/src/restic/archiver.go @@ -178,7 +178,7 @@ func waitForResults(resultChannels [](<-chan saveResult)) ([]saveResult, error) } if len(results) != len(resultChannels) { - return nil, fmt.Errorf("chunker returned %v chunks, but only %v blobs saved", len(resultChannels), len(results)) + return nil, errors.Errorf("chunker returned %v chunks, but only %v blobs saved", len(resultChannels), len(results)) } return results, nil @@ -198,7 +198,7 @@ func updateNodeContent(node *Node, results []saveResult) error { } if bytes != node.Size { - return fmt.Errorf("errors saving node %q: saved %d bytes, wanted %d bytes", node.path, bytes, node.Size) + return errors.Errorf("errors saving node %q: saved %d bytes, wanted %d bytes", node.path, bytes, node.Size) } debug.Log("Archiver.SaveFile", "SaveFile(%q): %v blobs\n", node.path, len(results)) diff --git a/src/restic/backend/handle.go b/src/restic/backend/handle.go index 62ff00b01..09561161b 100644 --- a/src/restic/backend/handle.go +++ b/src/restic/backend/handle.go @@ -34,7 +34,7 @@ func (h Handle) Valid() error { case Index: case Config: default: - return fmt.Errorf("invalid Type %q", h.Type) + return errors.Errorf("invalid Type %q", h.Type) } if h.Type == Config { diff --git a/src/restic/backend/local/local.go b/src/restic/backend/local/local.go index d6b02a0b5..7da1cce7f 100644 --- a/src/restic/backend/local/local.go +++ b/src/restic/backend/local/local.go @@ -1,7 +1,6 @@ package local import ( - "fmt" "io" "io/ioutil" "os" @@ -36,7 +35,7 @@ func Open(dir string) (*Local, error) { // test if all necessary dirs are there for _, d := range paths(dir) { if _, err := fs.Stat(d); err != nil { - return nil, fmt.Errorf("%s does not exist", d) + return nil, errors.Errorf("%s does not exist", d) } } @@ -185,7 +184,7 @@ func (b *Local) Save(h backend.Handle, p []byte) (err error) { // test if new path already exists if _, err := fs.Stat(filename); err == nil { - return fmt.Errorf("Rename(): file %v already exists", filename) + return errors.Errorf("Rename(): file %v already exists", filename) } // create directories if necessary, ignore errors diff --git a/src/restic/backend/rest/rest.go b/src/restic/backend/rest/rest.go index 5894d75a2..635c2f4c5 100644 --- a/src/restic/backend/rest/rest.go +++ b/src/restic/backend/rest/rest.go @@ -113,7 +113,7 @@ func (b *restBackend) Load(h backend.Handle, p []byte, off int64) (n int, err er return 0, err } if resp.StatusCode != 200 && resp.StatusCode != 206 { - return 0, fmt.Errorf("unexpected HTTP response code %v", resp.StatusCode) + return 0, errors.Errorf("unexpected HTTP response code %v", resp.StatusCode) } return io.ReadFull(resp.Body, p) @@ -144,7 +144,7 @@ func (b *restBackend) Save(h backend.Handle, p []byte) (err error) { } if resp.StatusCode != 200 { - return fmt.Errorf("unexpected HTTP response code %v", resp.StatusCode) + return errors.Errorf("unexpected HTTP response code %v", resp.StatusCode) } return nil @@ -168,7 +168,7 @@ func (b *restBackend) Stat(h backend.Handle) (backend.BlobInfo, error) { } if resp.StatusCode != 200 { - return backend.BlobInfo{}, fmt.Errorf("unexpected HTTP response code %v", resp.StatusCode) + return backend.BlobInfo{}, errors.Errorf("unexpected HTTP response code %v", resp.StatusCode) } if resp.ContentLength < 0 { diff --git a/src/restic/backend/sftp/config.go b/src/restic/backend/sftp/config.go index 942efc3e1..39609e346 100644 --- a/src/restic/backend/sftp/config.go +++ b/src/restic/backend/sftp/config.go @@ -1,7 +1,6 @@ package sftp import ( - "fmt" "net/url" "path" "strings" @@ -35,7 +34,7 @@ func ParseConfig(s string) (interface{}, error) { host = url.Host dir = url.Path if dir == "" { - return nil, fmt.Errorf("invalid backend %q, no directory specified", s) + return nil, errors.Errorf("invalid backend %q, no directory specified", s) } dir = dir[1:] diff --git a/src/restic/backend/sftp/sftp.go b/src/restic/backend/sftp/sftp.go index e023bf1be..7667f3e30 100644 --- a/src/restic/backend/sftp/sftp.go +++ b/src/restic/backend/sftp/sftp.go @@ -80,7 +80,7 @@ func startClient(program string, args ...string) (*SFTP, error) { // open the SFTP session client, err := sftp.NewClientPipe(rd, wr) if err != nil { - return nil, fmt.Errorf("unable to start the sftp session, error: %v", err) + return nil, errors.Errorf("unable to start the sftp session, error: %v", err) } return &SFTP{c: client, cmd: cmd, result: ch}, nil @@ -126,7 +126,7 @@ func Open(dir string, program string, args ...string) (*SFTP, error) { // test if all necessary dirs and files are there for _, d := range paths(dir) { if _, err := sftp.c.Lstat(d); err != nil { - return nil, fmt.Errorf("%s does not exist", d) + return nil, errors.Errorf("%s does not exist", d) } } @@ -207,7 +207,7 @@ func (r *SFTP) tempFile() (string, *sftp.File, error) { buf := make([]byte, tempfileRandomSuffixLength) _, err := io.ReadFull(rand.Reader, buf) if err != nil { - return "", nil, fmt.Errorf("unable to read %d random bytes for tempfile name: %v", + return "", nil, errors.Errorf("unable to read %d random bytes for tempfile name: %v", tempfileRandomSuffixLength, err) } @@ -217,7 +217,7 @@ func (r *SFTP) tempFile() (string, *sftp.File, error) { // create file in temp dir f, err := r.c.Create(name) if err != nil { - return "", nil, fmt.Errorf("creating tempfile %q failed: %v", name, err) + return "", nil, errors.Errorf("creating tempfile %q failed: %v", name, err) } return name, f, nil @@ -231,7 +231,7 @@ func (r *SFTP) mkdirAll(dir string, mode os.FileMode) error { return nil } - return fmt.Errorf("mkdirAll(%s): entry exists but is not a directory", dir) + return errors.Errorf("mkdirAll(%s): entry exists but is not a directory", dir) } // create parent directories @@ -244,11 +244,11 @@ func (r *SFTP) mkdirAll(dir string, mode os.FileMode) error { fi, err = r.c.Lstat(dir) if err != nil { // return previous errors - return fmt.Errorf("mkdirAll(%s): unable to create directories: %v, %v", dir, errMkdirAll, errMkdir) + return errors.Errorf("mkdirAll(%s): unable to create directories: %v, %v", dir, errMkdirAll, errMkdir) } if !fi.IsDir() { - return fmt.Errorf("mkdirAll(%s): entry exists but is not a directory", dir) + return errors.Errorf("mkdirAll(%s): entry exists but is not a directory", dir) } // set mode @@ -269,7 +269,7 @@ func (r *SFTP) renameFile(oldname string, t backend.Type, name string) error { // test if new file exists if _, err := r.c.Lstat(filename); err == nil { - return fmt.Errorf("Close(): file %v already exists", filename) + return errors.Errorf("Close(): file %v already exists", filename) } err := r.c.Rename(oldname, filename) @@ -396,7 +396,7 @@ func (r *SFTP) Save(h backend.Handle, p []byte) (err error) { debug.Log("sftp.Save", "save %v: rename %v: %v", h, path.Base(filename), err) if err != nil { - return fmt.Errorf("sftp: renameFile: %v", err) + return errors.Errorf("sftp: renameFile: %v", err) } return nil diff --git a/src/restic/cache.go b/src/restic/cache.go index 1060c0a4d..32aad88d7 100644 --- a/src/restic/cache.go +++ b/src/restic/cache.go @@ -1,7 +1,6 @@ package restic import ( - "fmt" "io" "os" "path/filepath" @@ -156,7 +155,7 @@ func (c *Cache) list(t backend.Type) ([]cacheEntry, error) { case backend.Snapshot: dir = filepath.Join(c.base, "snapshots") default: - return nil, fmt.Errorf("cache not supported for type %v", t) + return nil, errors.Errorf("cache not supported for type %v", t) } fd, err := fs.Open(dir) @@ -208,7 +207,7 @@ func (c *Cache) filename(t backend.Type, subtype string, id backend.ID) (string, return filepath.Join(c.base, "snapshots", filename), nil } - return "", fmt.Errorf("cache not supported for type %v", t) + return "", errors.Errorf("cache not supported for type %v", t) } func getCacheDir() (string, error) { @@ -246,7 +245,7 @@ func getWindowsCacheDir() (string, error) { } if !fi.IsDir() { - return "", fmt.Errorf("cache dir %v is not a directory", cachedir) + return "", errors.Errorf("cache dir %v is not a directory", cachedir) } return cachedir, nil } @@ -284,7 +283,7 @@ func getXDGCacheDir() (string, error) { } if !fi.IsDir() { - return "", fmt.Errorf("cache dir %v is not a directory", cachedir) + return "", errors.Errorf("cache dir %v is not a directory", cachedir) } return cachedir, nil diff --git a/src/restic/checker/checker.go b/src/restic/checker/checker.go index 9a3a518d5..a4e998eb0 100644 --- a/src/restic/checker/checker.go +++ b/src/restic/checker/checker.go @@ -2,7 +2,6 @@ package checker import ( "bytes" - "errors" "fmt" "sync" @@ -128,7 +127,7 @@ func (c *Checker) LoadIndex() (hints []error, errs []error) { debug.Log("LoadIndex", "process index %v", res.ID) idxID, err := backend.ParseID(res.ID) if err != nil { - errs = append(errs, fmt.Errorf("unable to parse as index ID: %v", res.ID)) + errs = append(errs, errors.Errorf("unable to parse as index ID: %v", res.ID)) continue } @@ -283,7 +282,7 @@ func loadTreeFromSnapshot(repo *repository.Repository, id backend.ID) (backend.I if sn.Tree == nil { debug.Log("Checker.loadTreeFromSnapshot", "snapshot %v has no tree", id.Str()) - return backend.ID{}, fmt.Errorf("snapshot %v has no tree", id) + return backend.ID{}, errors.Errorf("snapshot %v has no tree", id) } return *sn.Tree, nil @@ -585,24 +584,24 @@ func (c *Checker) checkTree(id backend.ID, tree *restic.Tree) (errs []error) { switch node.Type { case "file": if node.Content == nil { - errs = append(errs, Error{TreeID: id, Err: fmt.Errorf("file %q has nil blob list", node.Name)}) + errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q has nil blob list", node.Name)}) } for b, blobID := range node.Content { if blobID.IsNull() { - errs = append(errs, Error{TreeID: id, Err: fmt.Errorf("file %q blob %d has null ID", node.Name, b)}) + errs = append(errs, Error{TreeID: id, Err: errors.Errorf("file %q blob %d has null ID", node.Name, b)}) continue } blobs = append(blobs, blobID) } case "dir": if node.Subtree == nil { - errs = append(errs, Error{TreeID: id, Err: fmt.Errorf("dir node %q has no subtree", node.Name)}) + errs = append(errs, Error{TreeID: id, Err: errors.Errorf("dir node %q has no subtree", node.Name)}) continue } if node.Subtree.IsNull() { - errs = append(errs, Error{TreeID: id, Err: fmt.Errorf("dir node %q subtree id is null", node.Name)}) + errs = append(errs, Error{TreeID: id, Err: errors.Errorf("dir node %q subtree id is null", node.Name)}) continue } @@ -610,7 +609,7 @@ func (c *Checker) checkTree(id backend.ID, tree *restic.Tree) (errs []error) { // nothing to check default: - errs = append(errs, Error{TreeID: id, Err: fmt.Errorf("node %q with invalid type %q", node.Name, node.Type)}) + errs = append(errs, Error{TreeID: id, Err: errors.Errorf("node %q with invalid type %q", node.Name, node.Type)}) } if node.Name == "" { @@ -672,7 +671,7 @@ func checkPack(r *repository.Repository, id backend.ID) error { hash := backend.Hash(buf) if !hash.Equal(id) { debug.Log("Checker.checkPack", "Pack ID does not match, want %v, got %v", id.Str(), hash.Str()) - return fmt.Errorf("Pack ID does not match, want %v, got %v", id.Str(), hash.Str()) + return errors.Errorf("Pack ID does not match, want %v, got %v", id.Str(), hash.Str()) } blobs, err := pack.List(r.Key(), bytes.NewReader(buf), int64(len(buf))) @@ -688,20 +687,20 @@ func checkPack(r *repository.Repository, id backend.ID) error { plainBuf, err = crypto.Decrypt(r.Key(), plainBuf, buf[blob.Offset:blob.Offset+blob.Length]) if err != nil { debug.Log("Checker.checkPack", " error decrypting blob %v: %v", blob.ID.Str(), err) - errs = append(errs, fmt.Errorf("blob %v: %v", i, err)) + errs = append(errs, errors.Errorf("blob %v: %v", i, err)) continue } hash := backend.Hash(plainBuf) if !hash.Equal(blob.ID) { debug.Log("Checker.checkPack", " Blob ID does not match, want %v, got %v", blob.ID.Str(), hash.Str()) - errs = append(errs, fmt.Errorf("Blob ID does not match, want %v, got %v", blob.ID.Str(), hash.Str())) + errs = append(errs, errors.Errorf("Blob ID does not match, want %v, got %v", blob.ID.Str(), hash.Str())) continue } } if len(errs) > 0 { - return fmt.Errorf("pack %v contains %v errors: %v", id.Str(), len(errs), errs) + return errors.Errorf("pack %v contains %v errors: %v", id.Str(), len(errs), errs) } return nil diff --git a/src/restic/crypto/kdf.go b/src/restic/crypto/kdf.go index 7fe124e44..7fec1af6c 100644 --- a/src/restic/crypto/kdf.go +++ b/src/restic/crypto/kdf.go @@ -2,10 +2,10 @@ package crypto import ( "crypto/rand" - "fmt" "time" sscrypt "github.com/elithrar/simple-scrypt" + "github.com/pkg/errors" "golang.org/x/crypto/scrypt" ) @@ -51,7 +51,7 @@ func Calibrate(timeout time.Duration, memory int) (KDFParams, error) { // using the supplied parameters N, R and P and the Salt. func KDF(p KDFParams, salt []byte, password string) (*Key, error) { if len(salt) != saltLength { - return nil, fmt.Errorf("scrypt() called with invalid salt bytes (len %d)", len(salt)) + return nil, errors.Errorf("scrypt() called with invalid salt bytes (len %d)", len(salt)) } // make sure we have valid parameters @@ -72,11 +72,11 @@ func KDF(p KDFParams, salt []byte, password string) (*Key, error) { keybytes := macKeySize + aesKeySize scryptKeys, err := scrypt.Key([]byte(password), salt, p.N, p.R, p.P, keybytes) if err != nil { - return nil, fmt.Errorf("error deriving keys from password: %v", err) + return nil, errors.Errorf("error deriving keys from password: %v", err) } if len(scryptKeys) != keybytes { - return nil, fmt.Errorf("invalid numbers of bytes expanded from scrypt(): %d", len(scryptKeys)) + return nil, errors.Errorf("invalid numbers of bytes expanded from scrypt(): %d", len(scryptKeys)) } // first 32 byte of scrypt output is the encryption key diff --git a/src/restic/index/index.go b/src/restic/index/index.go index e2fa2e216..20d2e08fe 100644 --- a/src/restic/index/index.go +++ b/src/restic/index/index.go @@ -181,7 +181,7 @@ func Load(repo types.Repository, p *restic.Progress) (*Index, error) { // error is returned. func (idx *Index) AddPack(id backend.ID, size int64, entries []pack.Blob) error { if _, ok := idx.Packs[id]; ok { - return fmt.Errorf("pack %v already present in the index", id.Str()) + return errors.Errorf("pack %v already present in the index", id.Str()) } idx.Packs[id] = Pack{Size: size, Entries: entries} @@ -204,7 +204,7 @@ func (idx *Index) AddPack(id backend.ID, size int64, entries []pack.Blob) error // RemovePack deletes a pack from the index. func (idx *Index) RemovePack(id backend.ID) error { if _, ok := idx.Packs[id]; !ok { - return fmt.Errorf("pack %v not found in the index", id.Str()) + return errors.Errorf("pack %v not found in the index", id.Str()) } for _, blob := range idx.Packs[id].Entries { @@ -279,7 +279,7 @@ func (idx *Index) FindBlob(h pack.Handle) ([]Location, error) { for packID := range blob.Packs { pack, ok := idx.Packs[packID] if !ok { - return nil, fmt.Errorf("pack %v not found in index", packID.Str()) + return nil, errors.Errorf("pack %v not found in index", packID.Str()) } for _, entry := range pack.Entries { diff --git a/src/restic/node.go b/src/restic/node.go index fd859f8a8..4a3e7f8bc 100644 --- a/src/restic/node.go +++ b/src/restic/node.go @@ -10,6 +10,8 @@ import ( "syscall" "time" + "github.com/pkg/errors" + "runtime" "restic/backend" @@ -137,7 +139,7 @@ func (node *Node) CreateAt(path string, repo *repository.Repository) error { case "socket": return nil default: - return fmt.Errorf("filetype %q not implemented!\n", node.Type) + return errors.Errorf("filetype %q not implemented!\n", node.Type) } err := node.restoreMetadata(path) @@ -485,7 +487,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error { case "fifo": case "socket": default: - err = fmt.Errorf("invalid node type %q", node.Type) + err = errors.Errorf("invalid node type %q", node.Type) } return err diff --git a/src/restic/pack/pack.go b/src/restic/pack/pack.go index d29081827..6cfde7490 100644 --- a/src/restic/pack/pack.go +++ b/src/restic/pack/pack.go @@ -182,7 +182,7 @@ func (p *Packer) writeHeader(wr io.Writer) (bytesWritten uint, err error) { case Tree: entry.Type = 1 default: - return 0, fmt.Errorf("invalid blob type %v", b.Type) + return 0, errors.Errorf("invalid blob type %v", b.Type) } err := binary.Write(wr, binary.LittleEndian, entry) @@ -316,7 +316,7 @@ func List(k *crypto.Key, rd io.ReaderAt, size int64) (entries []Blob, err error) case 1: entry.Type = Tree default: - return nil, fmt.Errorf("invalid type %d", e.Type) + return nil, errors.Errorf("invalid type %d", e.Type) } entries = append(entries, entry) diff --git a/src/restic/repository/index.go b/src/restic/repository/index.go index bdf70c351..582f84095 100644 --- a/src/restic/repository/index.go +++ b/src/restic/repository/index.go @@ -140,7 +140,7 @@ func (idx *Index) Lookup(id backend.ID, tpe pack.BlobType) (blobs []PackedBlob, } debug.Log("Index.Lookup", "id %v not found", id.Str()) - return nil, fmt.Errorf("id %v not found in index", id) + return nil, errors.Errorf("id %v not found in index", id) } // ListPack returns a list of blobs contained in a pack. @@ -327,7 +327,7 @@ func (idx *Index) generatePackList() ([]*packJSON, error) { if blob.packID.IsNull() { debug.Log("Index.generatePackList", "blob %v has no packID! (offset %v, length %v)", h, blob.offset, blob.length) - return nil, fmt.Errorf("unable to serialize index: pack for blob %v hasn't been written yet", h) + return nil, errors.Errorf("unable to serialize index: pack for blob %v hasn't been written yet", h) } // see if pack is already in map diff --git a/src/restic/repository/master_index.go b/src/restic/repository/master_index.go index c6114d055..96425f9e8 100644 --- a/src/restic/repository/master_index.go +++ b/src/restic/repository/master_index.go @@ -1,9 +1,10 @@ package repository import ( - "fmt" "sync" + "github.com/pkg/errors" + "restic/backend" "restic/debug" "restic/pack" @@ -37,7 +38,7 @@ func (mi *MasterIndex) Lookup(id backend.ID, tpe pack.BlobType) (blobs []PackedB } debug.Log("MasterIndex.Lookup", "id %v not found in any index", id.Str()) - return nil, fmt.Errorf("id %v not found in any index", id) + return nil, errors.Errorf("id %v not found in any index", id) } // LookupSize queries all known Indexes for the ID and returns the first match. @@ -52,7 +53,7 @@ func (mi *MasterIndex) LookupSize(id backend.ID, tpe pack.BlobType) (uint, error } } - return 0, fmt.Errorf("id %v not found in any index", id) + return 0, errors.Errorf("id %v not found in any index", id) } // ListPack returns the list of blobs in a pack. The first matching index is diff --git a/src/restic/repository/packer_manager.go b/src/restic/repository/packer_manager.go index cbf27e626..9683eaecc 100644 --- a/src/restic/repository/packer_manager.go +++ b/src/restic/repository/packer_manager.go @@ -1,12 +1,13 @@ package repository import ( - "fmt" "io" "io/ioutil" "os" "sync" + "github.com/pkg/errors" + "restic/backend" "restic/crypto" "restic/debug" @@ -103,7 +104,7 @@ func (r *Repository) savePacker(p *pack.Packer) error { m, err := io.ReadFull(f, data) if uint(m) != n { - return fmt.Errorf("read wrong number of bytes from %v: want %v, got %v", tmpfile.Name(), n, m) + return errors.Errorf("read wrong number of bytes from %v: want %v, got %v", tmpfile.Name(), n, m) } if err = f.Close(); err != nil { diff --git a/src/restic/repository/repository.go b/src/restic/repository/repository.go index eaa7e530c..61288d934 100644 --- a/src/restic/repository/repository.go +++ b/src/restic/repository/repository.go @@ -141,7 +141,7 @@ func (r *Repository) LoadBlob(id backend.ID, t pack.BlobType, plaintextBuf []byt return plaintextBuf, nil } - return nil, fmt.Errorf("loading blob %v from %v packs failed", id.Str(), len(blobs)) + return nil, errors.Errorf("loading blob %v from %v packs failed", id.Str(), len(blobs)) } // closeOrErr calls cl.Close() and sets err to the returned error value if @@ -238,7 +238,7 @@ func (r *Repository) SaveJSON(t pack.BlobType, item interface{}) (backend.ID, er enc := json.NewEncoder(wr) err := enc.Encode(item) if err != nil { - return backend.ID{}, fmt.Errorf("json.Encode: %v", err) + return backend.ID{}, errors.Errorf("json.Encode: %v", err) } buf = wr.Bytes() @@ -251,7 +251,7 @@ func (r *Repository) SaveJSONUnpacked(t backend.Type, item interface{}) (backend debug.Log("Repo.SaveJSONUnpacked", "save new blob %v", t) plaintext, err := json.Marshal(item) if err != nil { - return backend.ID{}, fmt.Errorf("json.Encode: %v", err) + return backend.ID{}, errors.Errorf("json.Encode: %v", err) } return r.SaveUnpacked(t, plaintext) diff --git a/src/restic/restorer.go b/src/restic/restorer.go index 2a94c24ca..1d2e7f681 100644 --- a/src/restic/restorer.go +++ b/src/restic/restorer.go @@ -1,10 +1,11 @@ package restic import ( - "fmt" "os" "path/filepath" + "github.com/pkg/errors" + "restic/backend" "restic/debug" "restic/fs" @@ -59,7 +60,7 @@ func (res *Restorer) restoreTo(dst string, dir string, treeID backend.ID) error if node.Type == "dir" { if node.Subtree == nil { - return fmt.Errorf("Dir without subtree in tree %v", treeID.Str()) + return errors.Errorf("Dir without subtree in tree %v", treeID.Str()) } subp := filepath.Join(dir, node.Name) diff --git a/src/restic/snapshot.go b/src/restic/snapshot.go index 0b2374d6c..2ce01b4d9 100644 --- a/src/restic/snapshot.go +++ b/src/restic/snapshot.go @@ -141,7 +141,7 @@ func FindLatestSnapshot(repo *repository.Repository, targets []string, source st for snapshotID := range repo.List(backend.Snapshot, make(chan struct{})) { snapshot, err := LoadSnapshot(repo, snapshotID) if err != nil { - return backend.ID{}, fmt.Errorf("Error listing snapshot: %v", err) + return backend.ID{}, errors.Errorf("Error listing snapshot: %v", err) } if snapshot.Time.After(latest) && SamePaths(snapshot.Paths, targets) && (source == "" || source == snapshot.Hostname) { latest = snapshot.Time