From b06845c5454437d5b686f859c71d91e6d2e32239 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 29 Aug 2016 19:18:57 +0200 Subject: [PATCH] Always use errors.Cause() for testing error values --- src/cmds/restic/cmd_backup.go | 4 +++- src/cmds/restic/cmd_mount.go | 4 +++- src/cmds/restic/global.go | 3 ++- src/cmds/restic/integration_fuse_test.go | 4 +++- src/cmds/restic/integration_test.go | 22 +++++++++++--------- src/restic/archive_reader.go | 3 ++- src/restic/archiver.go | 2 +- src/restic/archiver_test.go | 7 ++++--- src/restic/backend/local/local.go | 2 +- src/restic/backend/s3/s3.go | 4 ++-- src/restic/backend/sftp/sftp.go | 2 +- src/restic/backend/sftp/sftp_backend_test.go | 4 +++- src/restic/backend/test/tests.go | 10 +++++---- src/restic/backend/utils.go | 8 +++++-- src/restic/cache.go | 10 ++++----- src/restic/checker/checker.go | 2 +- src/restic/debug/debug.go | 4 +++- src/restic/fs/file_linux.go | 4 +++- src/restic/pack/pack.go | 2 +- src/restic/repository/key.go | 2 +- src/restic/repository/repack.go | 4 +++- src/restic/repository/repository.go | 2 +- src/restic/restorer.go | 2 +- src/restic/testing.go | 3 ++- 24 files changed, 70 insertions(+), 44 deletions(-) diff --git a/src/cmds/restic/cmd_backup.go b/src/cmds/restic/cmd_backup.go index 57628ac59..7a7640249 100644 --- a/src/cmds/restic/cmd_backup.go +++ b/src/cmds/restic/cmd_backup.go @@ -13,6 +13,8 @@ import ( "strings" "time" + "github.com/pkg/errors" + "golang.org/x/crypto/ssh/terminal" ) @@ -222,7 +224,7 @@ func (cmd CmdBackup) newArchiveStdinProgress() *restic.Progress { func filterExisting(items []string) (result []string, err error) { for _, item := range items { _, err := fs.Lstat(item) - if err != nil && os.IsNotExist(err) { + if err != nil && os.IsNotExist(errors.Cause(err)) { continue } diff --git a/src/cmds/restic/cmd_mount.go b/src/cmds/restic/cmd_mount.go index e8bf4f272..36a2ce997 100644 --- a/src/cmds/restic/cmd_mount.go +++ b/src/cmds/restic/cmd_mount.go @@ -7,6 +7,8 @@ import ( "os" "restic" + "github.com/pkg/errors" + resticfs "restic/fs" "restic/fuse" @@ -56,7 +58,7 @@ func (cmd CmdMount) Execute(args []string) error { } mountpoint := args[0] - if _, err := resticfs.Stat(mountpoint); os.IsNotExist(err) { + if _, err := resticfs.Stat(mountpoint); os.IsNotExist(errors.Cause(err)) { cmd.global.Verbosef("Mountpoint %s doesn't exist, creating it\n", mountpoint) err = resticfs.Mkdir(mountpoint, os.ModeDir|0700) if err != nil { diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index 9f3a014ed..2aedb026e 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -19,6 +19,7 @@ import ( "restic/repository" "github.com/jessevdk/go-flags" + "github.com/pkg/errors" "golang.org/x/crypto/ssh/terminal" ) @@ -183,7 +184,7 @@ func readPassword(in io.Reader) (password string, err error) { n, err := io.ReadFull(in, buf) buf = buf[:n] - if err != nil && err != io.ErrUnexpectedEOF { + if err != nil && errors.Cause(err) != io.ErrUnexpectedEOF { return "", err } diff --git a/src/cmds/restic/integration_fuse_test.go b/src/cmds/restic/integration_fuse_test.go index 39e24668e..25f4b7e84 100644 --- a/src/cmds/restic/integration_fuse_test.go +++ b/src/cmds/restic/integration_fuse_test.go @@ -10,6 +10,8 @@ import ( "testing" "time" + "github.com/pkg/errors" + "restic" "restic/backend" "restic/repository" @@ -125,7 +127,7 @@ func TestMount(t *testing.T) { datafile := filepath.Join("testdata", "backup-data.tar.gz") fd, err := os.Open(datafile) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { t.Skipf("unable to find data file %q, skipping", datafile) return } diff --git a/src/cmds/restic/integration_test.go b/src/cmds/restic/integration_test.go index 755f5ec9a..fff765bef 100644 --- a/src/cmds/restic/integration_test.go +++ b/src/cmds/restic/integration_test.go @@ -16,6 +16,8 @@ import ( "testing" "time" + "github.com/pkg/errors" + "restic/backend" "restic/debug" "restic/filter" @@ -142,7 +144,7 @@ func TestBackup(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz") fd, err := os.Open(datafile) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { t.Skipf("unable to find data file %q, skipping", datafile) return } @@ -204,7 +206,7 @@ func TestBackupNonExistingFile(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz") fd, err := os.Open(datafile) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { t.Skipf("unable to find data file %q, skipping", datafile) return } @@ -232,7 +234,7 @@ func TestBackupMissingFile1(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz") fd, err := os.Open(datafile) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { t.Skipf("unable to find data file %q, skipping", datafile) return } @@ -270,7 +272,7 @@ func TestBackupMissingFile2(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz") fd, err := os.Open(datafile) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { t.Skipf("unable to find data file %q, skipping", datafile) return } @@ -308,7 +310,7 @@ func TestBackupDirectoryError(t *testing.T) { withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) { datafile := filepath.Join("testdata", "backup-data.tar.gz") fd, err := os.Open(datafile) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { t.Skipf("unable to find data file %q, skipping", datafile) return } @@ -625,7 +627,7 @@ func TestRestoreFilter(t *testing.T) { if ok, _ := filter.Match(pat, filepath.Base(test.name)); !ok { OK(t, err) } else { - Assert(t, os.IsNotExist(err), + Assert(t, os.IsNotExist(errors.Cause(err)), "expected %v to not exist in restore step %v, but it exists, err %v", test.name, i+1, err) } } @@ -673,15 +675,15 @@ func TestRestoreLatest(t *testing.T) { cmdRestoreLatest(t, global, filepath.Join(env.base, "restore1"), []string{filepath.Dir(p1)}, "") OK(t, testFileSize(p1rAbs, int64(102))) - if _, err := os.Stat(p2rAbs); os.IsNotExist(err) { - Assert(t, os.IsNotExist(err), + if _, err := os.Stat(p2rAbs); os.IsNotExist(errors.Cause(err)) { + Assert(t, os.IsNotExist(errors.Cause(err)), "expected %v to not exist in restore, but it exists, err %v", p2rAbs, err) } cmdRestoreLatest(t, global, filepath.Join(env.base, "restore2"), []string{filepath.Dir(p2)}, "") OK(t, testFileSize(p2rAbs, int64(103))) - if _, err := os.Stat(p1rAbs); os.IsNotExist(err) { - Assert(t, os.IsNotExist(err), + if _, err := os.Stat(p1rAbs); os.IsNotExist(errors.Cause(err)) { + Assert(t, os.IsNotExist(errors.Cause(err)), "expected %v to not exist in restore, but it exists, err %v", p1rAbs, err) } diff --git a/src/restic/archive_reader.go b/src/restic/archive_reader.go index ad6bded8c..f62f68537 100644 --- a/src/restic/archive_reader.go +++ b/src/restic/archive_reader.go @@ -9,6 +9,7 @@ import ( "restic/repository" "time" + "github.com/pkg/errors" "github.com/restic/chunker" ) @@ -48,7 +49,7 @@ func ArchiveReader(repo *repository.Repository, p *Progress, rd io.Reader, name for { chunk, err := chnker.Next(getBuf()) - if err == io.EOF { + if errors.Cause(err) == io.EOF { break } diff --git a/src/restic/archiver.go b/src/restic/archiver.go index 96a624334..aea3c1bc1 100644 --- a/src/restic/archiver.go +++ b/src/restic/archiver.go @@ -225,7 +225,7 @@ func (arch *Archiver) SaveFile(p *Progress, node *Node) error { for { chunk, err := chnker.Next(getBuf()) - if err == io.EOF { + if errors.Cause(err) == io.EOF { break } diff --git a/src/restic/archiver_test.go b/src/restic/archiver_test.go index e42151a27..47b2210fa 100644 --- a/src/restic/archiver_test.go +++ b/src/restic/archiver_test.go @@ -14,6 +14,7 @@ import ( "restic/repository" . "restic/test" + "github.com/pkg/errors" "github.com/restic/chunker" ) @@ -31,7 +32,7 @@ func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.K for { chunk, err := ch.Next(buf) - if err == io.EOF { + if errors.Cause(err) == io.EOF { break } @@ -69,7 +70,7 @@ func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key) for { chunk, err := ch.Next(buf) - if err == io.EOF { + if errors.Cause(err) == io.EOF { break } @@ -292,7 +293,7 @@ func getRandomData(seed int, size int) []chunker.Chunk { for { c, err := chunker.Next(nil) - if err == io.EOF { + if errors.Cause(err) == io.EOF { break } chunks = append(chunks, c) diff --git a/src/restic/backend/local/local.go b/src/restic/backend/local/local.go index 7da1cce7f..078f21ad5 100644 --- a/src/restic/backend/local/local.go +++ b/src/restic/backend/local/local.go @@ -232,7 +232,7 @@ func (b *Local) Test(t backend.Type, name string) (bool, error) { debug.Log("backend.local.Test", "Test %v %v", t, name) _, err := fs.Stat(filename(b.p, t, name)) if err != nil { - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { return false, nil } return false, err diff --git a/src/restic/backend/s3/s3.go b/src/restic/backend/s3/s3.go index ff3ff627d..6da9cac47 100644 --- a/src/restic/backend/s3/s3.go +++ b/src/restic/backend/s3/s3.go @@ -125,7 +125,7 @@ func (be s3) Load(h backend.Handle, p []byte, off int64) (n int, err error) { // return an error if the offset is beyond the end of the file if off > info.Size { - return 0, io.EOF + return 0, errors.Wrap(io.EOF, "") } var nextError error @@ -141,7 +141,7 @@ func (be s3) Load(h backend.Handle, p []byte, off int64) (n int, err error) { } n, err = obj.ReadAt(p, off) - if int64(n) == info.Size-off && err == io.EOF { + if int64(n) == info.Size-off && errors.Cause(err) == io.EOF { err = nil } diff --git a/src/restic/backend/sftp/sftp.go b/src/restic/backend/sftp/sftp.go index 7667f3e30..43806d24c 100644 --- a/src/restic/backend/sftp/sftp.go +++ b/src/restic/backend/sftp/sftp.go @@ -429,7 +429,7 @@ func (r *SFTP) Test(t backend.Type, name string) (bool, error) { } _, err := r.c.Lstat(r.filename(t, name)) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { return false, nil } diff --git a/src/restic/backend/sftp/sftp_backend_test.go b/src/restic/backend/sftp/sftp_backend_test.go index 52b50f6f2..2d8e609ca 100644 --- a/src/restic/backend/sftp/sftp_backend_test.go +++ b/src/restic/backend/sftp/sftp_backend_test.go @@ -6,6 +6,8 @@ import ( "path/filepath" "strings" + "github.com/pkg/errors" + "restic/backend" "restic/backend/sftp" "restic/backend/test" @@ -37,7 +39,7 @@ func init() { for _, dir := range strings.Split(TestSFTPPath, ":") { testpath := filepath.Join(dir, "sftp-server") _, err := os.Stat(testpath) - if !os.IsNotExist(err) { + if !os.IsNotExist(errors.Cause(err)) { sftpserver = testpath break } diff --git a/src/restic/backend/test/tests.go b/src/restic/backend/test/tests.go index 77896499e..a9b18e361 100644 --- a/src/restic/backend/test/tests.go +++ b/src/restic/backend/test/tests.go @@ -10,6 +10,8 @@ import ( "sort" "testing" + "github.com/pkg/errors" + "restic/backend" . "restic/test" ) @@ -223,7 +225,7 @@ func TestLoad(t testing.TB) { // if we requested data beyond the end of the file, require // ErrUnexpectedEOF error if l > len(d) { - if err != io.ErrUnexpectedEOF { + if errors.Cause(err) != io.ErrUnexpectedEOF { t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), int64(o)) } err = nil @@ -270,7 +272,7 @@ func TestLoad(t testing.TB) { // if we requested data beyond the end of the file, require // ErrUnexpectedEOF error if l > len(d) { - if err != io.ErrUnexpectedEOF { + if errors.Cause(err) != io.ErrUnexpectedEOF { t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), int64(o)) continue } @@ -303,7 +305,7 @@ func TestLoad(t testing.TB) { t.Errorf("wrong length for larger buffer returned, want %d, got %d", length, n) } - if err != io.ErrUnexpectedEOF { + if errors.Cause(err) != io.ErrUnexpectedEOF { t.Errorf("wrong error returned for larger buffer: want io.ErrUnexpectedEOF, got %#v", err) } @@ -337,7 +339,7 @@ func TestLoadNegativeOffset(t testing.TB) { // if we requested data beyond the end of the file, require // ErrUnexpectedEOF error if len(buf) > -o { - if err != io.ErrUnexpectedEOF { + if errors.Cause(err) != io.ErrUnexpectedEOF { t.Errorf("Load(%d, %d) did not return io.ErrUnexpectedEOF", len(buf), o) continue } diff --git a/src/restic/backend/utils.go b/src/restic/backend/utils.go index e1c36236b..01446a78c 100644 --- a/src/restic/backend/utils.go +++ b/src/restic/backend/utils.go @@ -1,6 +1,10 @@ package backend -import "io" +import ( + "io" + + "github.com/pkg/errors" +) // LoadAll reads all data stored in the backend for the handle. The buffer buf // is resized to accomodate all data in the blob. Errors returned by be.Load() @@ -17,7 +21,7 @@ func LoadAll(be Backend, h Handle, buf []byte) ([]byte, error) { } n, err := be.Load(h, buf, 0) - if err == io.ErrUnexpectedEOF { + if errors.Cause(err) == io.ErrUnexpectedEOF { err = nil } buf = buf[:n] diff --git a/src/restic/cache.go b/src/restic/cache.go index 32aad88d7..da6106c47 100644 --- a/src/restic/cache.go +++ b/src/restic/cache.go @@ -48,7 +48,7 @@ func (c *Cache) Has(t backend.Type, subtype string, id backend.ID) (bool, error) defer fd.Close() if err != nil { - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { debug.Log("Cache.Has", "test for file %v: not cached", filename) return false, nil } @@ -106,7 +106,7 @@ func (c *Cache) purge(t backend.Type, subtype string, id backend.ID) error { err = fs.Remove(filename) debug.Log("Cache.purge", "Remove file %v: %v", filename, err) - if err != nil && os.IsNotExist(err) { + if err != nil && os.IsNotExist(errors.Cause(err)) { return nil } @@ -160,7 +160,7 @@ func (c *Cache) list(t backend.Type) ([]cacheEntry, error) { fd, err := fs.Open(dir) if err != nil { - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { return []cacheEntry{}, nil } return nil, err @@ -231,7 +231,7 @@ func getWindowsCacheDir() (string, error) { cachedir = filepath.Join(cachedir, "restic") fi, err := fs.Stat(cachedir) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { err = fs.MkdirAll(cachedir, 0700) if err != nil { return "", err @@ -268,7 +268,7 @@ func getXDGCacheDir() (string, error) { } fi, err := fs.Stat(cachedir) - if os.IsNotExist(err) { + if os.IsNotExist(errors.Cause(err)) { err = fs.MkdirAll(cachedir, 0700) if err != nil { return "", err diff --git a/src/restic/checker/checker.go b/src/restic/checker/checker.go index a4e998eb0..1755bd3ac 100644 --- a/src/restic/checker/checker.go +++ b/src/restic/checker/checker.go @@ -85,7 +85,7 @@ func (c *Checker) LoadIndex() (hints []error, errs []error) { worker := func(id backend.ID, done <-chan struct{}) error { debug.Log("LoadIndex", "worker got index %v", id) idx, err := repository.LoadIndexWithDecoder(c.repo, id, repository.DecodeIndex) - if err == repository.ErrOldIndexFormat { + if errors.Cause(err) == repository.ErrOldIndexFormat { debug.Log("LoadIndex", "index %v has old format", id.Str()) hints = append(hints, ErrOldIndexFormat{id}) diff --git a/src/restic/debug/debug.go b/src/restic/debug/debug.go index 12b8c490a..aeae376cd 100644 --- a/src/restic/debug/debug.go +++ b/src/restic/debug/debug.go @@ -14,6 +14,8 @@ import ( "strings" "sync" "time" + + "github.com/pkg/errors" ) type process struct { @@ -59,7 +61,7 @@ func initDebugLogger() { } } - if err != nil && os.IsNotExist(err) { + if err != nil && os.IsNotExist(errors.Cause(err)) { f, err = fs.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE, 0600) } diff --git a/src/restic/fs/file_linux.go b/src/restic/fs/file_linux.go index 4e0831b76..e3cdf9600 100644 --- a/src/restic/fs/file_linux.go +++ b/src/restic/fs/file_linux.go @@ -6,13 +6,15 @@ import ( "os" "syscall" + "github.com/pkg/errors" + "golang.org/x/sys/unix" ) // Open opens a file for reading, without updating the atime and without caching data on read. func Open(name string) (File, error) { file, err := os.OpenFile(name, os.O_RDONLY|syscall.O_NOATIME, 0) - if os.IsPermission(err) { + if os.IsPermission(errors.Cause(err)) { file, err = os.OpenFile(name, os.O_RDONLY, 0) } return &nonCachingFile{File: file}, err diff --git a/src/restic/pack/pack.go b/src/restic/pack/pack.go index 6cfde7490..aa126a642 100644 --- a/src/restic/pack/pack.go +++ b/src/restic/pack/pack.go @@ -296,7 +296,7 @@ func List(k *crypto.Key, rd io.ReaderAt, size int64) (entries []Blob, err error) for { e := headerEntry{} err = binary.Read(hdrRd, binary.LittleEndian, &e) - if err == io.EOF { + if errors.Cause(err) == io.EOF { break } diff --git a/src/restic/repository/key.go b/src/restic/repository/key.go index 3ef78e6b2..895230bbf 100644 --- a/src/restic/repository/key.go +++ b/src/restic/repository/key.go @@ -126,7 +126,7 @@ func SearchKey(s *Repository, password string, maxKeys int) (*Key, error) { debug.Log("SearchKey", "key %v returned error %v", name[:12], err) // ErrUnauthenticated means the password is wrong, try the next key - if err == crypto.ErrUnauthenticated { + if errors.Cause(err) == crypto.ErrUnauthenticated { continue } diff --git a/src/restic/repository/repack.go b/src/restic/repository/repack.go index bf8d7a3a7..2c61705da 100644 --- a/src/restic/repository/repack.go +++ b/src/restic/repository/repack.go @@ -7,6 +7,8 @@ import ( "restic/crypto" "restic/debug" "restic/pack" + + "github.com/pkg/errors" ) // Repack takes a list of packs together with a list of blobs contained in @@ -22,7 +24,7 @@ func Repack(repo *Repository, packs backend.IDSet, keepBlobs pack.BlobSet) (err h := backend.Handle{Type: backend.Data, Name: packID.String()} l, err := repo.Backend().Load(h, buf[:cap(buf)], 0) - if err == io.ErrUnexpectedEOF { + if errors.Cause(err) == io.ErrUnexpectedEOF { err = nil buf = buf[:l] } diff --git a/src/restic/repository/repository.go b/src/restic/repository/repository.go index 92419c7ba..88c010b8a 100644 --- a/src/restic/repository/repository.go +++ b/src/restic/repository/repository.go @@ -403,7 +403,7 @@ func LoadIndex(repo *Repository, id backend.ID) (*Index, error) { return idx, nil } - if err == ErrOldIndexFormat { + if errors.Cause(err) == ErrOldIndexFormat { fmt.Fprintf(os.Stderr, "index %v has old format\n", id.Str()) return LoadIndexWithDecoder(repo, id, DecodeOldIndex) } diff --git a/src/restic/restorer.go b/src/restic/restorer.go index 1d2e7f681..b6aa376b7 100644 --- a/src/restic/restorer.go +++ b/src/restic/restorer.go @@ -95,7 +95,7 @@ func (res *Restorer) restoreNodeTo(node *Node, dir string, dst string) error { } // Did it fail because of ENOENT? - if err != nil && os.IsNotExist(err) { + if err != nil && os.IsNotExist(errors.Cause(err)) { debug.Log("Restorer.restoreNodeTo", "create intermediate paths") // Create parent directories and retry diff --git a/src/restic/testing.go b/src/restic/testing.go index e1d6bf610..78783ee44 100644 --- a/src/restic/testing.go +++ b/src/restic/testing.go @@ -11,6 +11,7 @@ import ( "testing" "time" + "github.com/pkg/errors" "github.com/restic/chunker" ) @@ -34,7 +35,7 @@ func (fs fakeFileSystem) saveFile(rd io.Reader) (blobs backend.IDs) { for { chunk, err := ch.Next(getBuf()) - if err == io.EOF { + if errors.Cause(err) == io.EOF { break }