Wrap errors

This commit is contained in:
Alexander Neumann 2016-08-29 21:38:34 +02:00
parent b06845c545
commit b53679a24d
10 changed files with 47 additions and 38 deletions

View file

@ -17,7 +17,7 @@ import (
func saveTreeJSON(repo *repository.Repository, item interface{}) (backend.ID, error) {
data, err := json.Marshal(item)
if err != nil {
return backend.ID{}, err
return backend.ID{}, errors.Wrap(err, "")
}
data = append(data, '\n')
@ -54,7 +54,7 @@ func ArchiveReader(repo *repository.Repository, p *Progress, rd io.Reader, name
}
if err != nil {
return nil, backend.ID{}, err
return nil, backend.ID{}, errors.Wrap(err, "chunker.Next()")
}
id := backend.Hash(chunk.Data)

View file

@ -48,6 +48,9 @@ func checkSavedFile(t *testing.T, repo *repository.Repository, treeID backend.ID
buf2 = buf2[:len(buf)]
_, err = io.ReadFull(rd, buf2)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, buf2) {
t.Fatalf("blob %d (%v) is wrong", i, id.Str())

View file

@ -113,7 +113,7 @@ func (arch *Archiver) Save(t pack.BlobType, data []byte, id backend.ID) error {
func (arch *Archiver) SaveTreeJSON(item interface{}) (backend.ID, error) {
data, err := json.Marshal(item)
if err != nil {
return backend.ID{}, err
return backend.ID{}, errors.Wrap(err, "Marshal")
}
data = append(data, '\n')
@ -129,7 +129,7 @@ func (arch *Archiver) SaveTreeJSON(item interface{}) (backend.ID, error) {
func (arch *Archiver) reloadFileIfChanged(node *Node, file fs.File) (*Node, error) {
fi, err := file.Stat()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "Stat")
}
if fi.ModTime() == node.ModTime {
@ -212,7 +212,7 @@ func (arch *Archiver) SaveFile(p *Progress, node *Node) error {
file, err := fs.Open(node.path)
defer file.Close()
if err != nil {
return err
return errors.Wrap(err, "Open")
}
node, err = arch.reloadFileIfChanged(node, file)
@ -230,7 +230,7 @@ func (arch *Archiver) SaveFile(p *Progress, node *Node) error {
}
if err != nil {
return err
return errors.Wrap(err, "chunker.Next")
}
resCh := make(chan saveResult, 1)
@ -819,7 +819,7 @@ func Scan(dirs []string, filter pipe.SelectFunc, p *Progress) (Stat, error) {
debug.Log("Scan", "Done for %v, err: %v", dir, err)
if err != nil {
return Stat{}, err
return Stat{}, errors.Wrap(err, "fs.Walk")
}
}

View file

@ -54,7 +54,7 @@ func (c *Cache) Has(t backend.Type, subtype string, id backend.ID) (bool, error)
}
debug.Log("Cache.Has", "test for file %v: error %v", filename, err)
return false, err
return false, errors.Wrap(err, "Open")
}
debug.Log("Cache.Has", "test for file %v: is cached", filename)
@ -73,13 +73,13 @@ func (c *Cache) Store(t backend.Type, subtype string, id backend.ID) (io.WriteCl
dirname := filepath.Dir(filename)
err = fs.MkdirAll(dirname, 0700)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "MkdirAll")
}
file, err := fs.Create(filename)
if err != nil {
debug.Log("Cache.Store", "error creating file %v: %v", filename, err)
return nil, err
return nil, errors.Wrap(err, "Create")
}
debug.Log("Cache.Store", "created file %v", filename)
@ -110,7 +110,7 @@ func (c *Cache) purge(t backend.Type, subtype string, id backend.ID) error {
return nil
}
return err
return errors.Wrap(err, "Remove")
}
// Clear removes information from the cache that isn't present in the repository any more.
@ -163,13 +163,13 @@ func (c *Cache) list(t backend.Type) ([]cacheEntry, error) {
if os.IsNotExist(errors.Cause(err)) {
return []cacheEntry{}, nil
}
return nil, err
return nil, errors.Wrap(err, "Open")
}
defer fd.Close()
fis, err := fd.Readdir(-1)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "Readdir")
}
entries := make([]cacheEntry, 0, len(fis))
@ -234,14 +234,14 @@ func getWindowsCacheDir() (string, error) {
if os.IsNotExist(errors.Cause(err)) {
err = fs.MkdirAll(cachedir, 0700)
if err != nil {
return "", err
return "", errors.Wrap(err, "MkdirAll")
}
return cachedir, nil
}
if err != nil {
return "", err
return "", errors.Wrap(err, "Stat")
}
if !fi.IsDir() {
@ -271,7 +271,7 @@ func getXDGCacheDir() (string, error) {
if os.IsNotExist(errors.Cause(err)) {
err = fs.MkdirAll(cachedir, 0700)
if err != nil {
return "", err
return "", errors.Wrap(err, "MkdirAll")
}
fi, err = fs.Stat(cachedir)
@ -279,7 +279,7 @@ func getXDGCacheDir() (string, error) {
}
if err != nil {
return "", err
return "", errors.Wrap(err, "Stat")
}
if !fi.IsDir() {

View file

@ -18,6 +18,9 @@ func TestCache(t *testing.T) {
// archive some files, this should automatically cache all blobs from the snapshot
_, _, err = arch.Snapshot(nil, []string{BenchArchiveDirectory}, nil)
if err != nil {
t.Fatal(err)
}
// TODO: test caching index
}

View file

@ -9,6 +9,8 @@ import (
"syscall"
"time"
"github.com/pkg/errors"
"restic/backend"
"restic/debug"
"restic/repository"
@ -47,7 +49,7 @@ func (e ErrAlreadyLocked) Error() string {
// IsAlreadyLocked returns true iff err is an instance of ErrAlreadyLocked.
func IsAlreadyLocked(err error) bool {
if _, ok := err.(ErrAlreadyLocked); ok {
if _, ok := errors.Cause(err).(ErrAlreadyLocked); ok {
return true
}
@ -189,7 +191,7 @@ var staleTimeout = 30 * time.Minute
// process isn't alive any more.
func (l *Lock) Stale() bool {
debug.Log("Lock.Stale", "testing if lock %v for process %d is stale", l, l.PID)
if time.Now().Sub(l.Time) > staleTimeout {
if time.Since(l.Time) > staleTimeout {
debug.Log("Lock.Stale", "lock is stale, timestamp is too old: %v\n", l.Time)
return true
}

View file

@ -8,6 +8,8 @@ import (
"strconv"
"syscall"
"github.com/pkg/errors"
"restic/debug"
)
@ -16,11 +18,11 @@ func uidGidInt(u user.User) (uid, gid uint32, err error) {
var ui, gi int64
ui, err = strconv.ParseInt(u.Uid, 10, 32)
if err != nil {
return
return uid, gid, errors.Wrap(err, "ParseInt")
}
gi, err = strconv.ParseInt(u.Gid, 10, 32)
if err != nil {
return
return uid, gid, errors.Wrap(err, "ParseInt")
}
uid = uint32(ui)
gid = uint32(gi)

View file

@ -155,13 +155,13 @@ func (node Node) restoreMetadata(path string) error {
err = lchown(path, int(node.UID), int(node.GID))
if err != nil {
return err
return errors.Wrap(err, "Lchown")
}
if node.Type != "symlink" {
err = fs.Chmod(path, node.Mode)
if err != nil {
return err
return errors.Wrap(err, "Chmod")
}
}
@ -183,15 +183,11 @@ func (node Node) RestoreTimestamps(path string) error {
}
if node.Type == "symlink" {
if err := node.restoreSymlinkTimestamps(path, utimes); err != nil {
return err
}
return nil
return node.restoreSymlinkTimestamps(path, utimes)
}
if err := syscall.UtimesNano(path, utimes[:]); err != nil {
return err
return errors.Wrap(err, "UtimesNano")
}
return nil
@ -200,7 +196,7 @@ func (node Node) RestoreTimestamps(path string) error {
func (node Node) createDirAt(path string) error {
err := fs.Mkdir(path, node.Mode)
if err != nil {
return err
return errors.Wrap(err, "Mkdir")
}
return nil
@ -211,7 +207,7 @@ func (node Node) createFileAt(path string, repo *repository.Repository) error {
defer f.Close()
if err != nil {
return err
return errors.Wrap(err, "OpenFile")
}
var buf []byte
@ -233,7 +229,7 @@ func (node Node) createFileAt(path string, repo *repository.Repository) error {
_, err = f.Write(buf)
if err != nil {
return err
return errors.Wrap(err, "Write")
}
}
@ -247,7 +243,7 @@ func (node Node) createSymlinkAt(path string) error {
}
err := fs.Symlink(node.LinkTarget, path)
if err != nil {
return err
return errors.Wrap(err, "Symlink")
}
return nil
@ -280,11 +276,11 @@ func (node *Node) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, nj)
if err != nil {
return err
return errors.Wrap(err, "Unmarshal")
}
nj.Name, err = strconv.Unquote(`"` + nj.Name + `"`)
return err
return errors.Wrap(err, "Unquote")
}
func (node Node) Equals(other Node) bool {
@ -480,6 +476,7 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
case "dir":
case "symlink":
node.LinkTarget, err = fs.Readlink(path)
err = errors.Wrap(err, "Readlink")
case "dev":
node.Device = uint64(stat.rdev())
case "chardev":

View file

@ -5,6 +5,8 @@ import (
"syscall"
"unsafe"
"github.com/pkg/errors"
"restic/fs"
)
@ -12,13 +14,13 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe
dir, err := fs.Open(filepath.Dir(path))
defer dir.Close()
if err != nil {
return err
return errors.Wrap(err, "Open")
}
err = utimesNanoAt(int(dir.Fd()), filepath.Base(path), utimes, AT_SYMLINK_NOFOLLOW)
if err != nil {
return err
return errors.Wrap(err, "UtimesNanoAt")
}
return nil

View file

@ -100,7 +100,7 @@ func (res *Restorer) restoreNodeTo(node *Node, dir string, dst string) error {
// Create parent directories and retry
err = fs.MkdirAll(filepath.Dir(dstPath), 0700)
if err == nil || err == os.ErrExist {
if err == nil || os.IsExist(errors.Cause(err)) {
err = node.CreateAt(dstPath, res.repo)
}
}