2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00

Merge pull request #2980 from greatroar/error-wrapping

Remove repetitive error wrapping from internal/cache
This commit is contained in:
MichaelEischer 2020-10-06 15:19:00 +02:00 committed by GitHub
commit 09d39e260d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 20 deletions

View File

@ -33,12 +33,12 @@ func readVersion(dir string) (v uint, err error) {
} }
if err != nil { if err != nil {
return 0, errors.Wrap(err, "ReadFile") return 0, errors.Wrap(err, "readVersion")
} }
ver, err := strconv.ParseUint(string(buf), 10, 32) ver, err := strconv.ParseUint(string(buf), 10, 32)
if err != nil { if err != nil {
return 0, errors.Wrap(err, "ParseUint") return 0, errors.Wrap(err, "readVersion")
} }
return uint(ver), nil return uint(ver), nil
@ -56,13 +56,13 @@ const cachedirTagSignature = "Signature: 8a477f597d28d172789f06886806bc55\n"
func writeCachedirTag(dir string) error { func writeCachedirTag(dir string) error {
if err := fs.MkdirAll(dir, dirMode); err != nil { if err := fs.MkdirAll(dir, dirMode); err != nil {
return err return errors.WithStack(err)
} }
tagfile := filepath.Join(dir, "CACHEDIR.TAG") tagfile := filepath.Join(dir, "CACHEDIR.TAG")
_, err := fs.Lstat(tagfile) _, err := fs.Lstat(tagfile)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return errors.Wrap(err, "Lstat") return errors.WithStack(err)
} }
f, err := fs.OpenFile(tagfile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, fileMode) f, err := fs.OpenFile(tagfile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, fileMode)
@ -71,16 +71,16 @@ func writeCachedirTag(dir string) error {
return nil return nil
} }
return errors.Wrap(err, "OpenFile") return errors.WithStack(err)
} }
debug.Log("Create CACHEDIR.TAG at %v", dir) debug.Log("Create CACHEDIR.TAG at %v", dir)
if _, err := f.Write([]byte(cachedirTagSignature)); err != nil { if _, err := f.Write([]byte(cachedirTagSignature)); err != nil {
_ = f.Close() _ = f.Close()
return errors.Wrap(err, "Write") return errors.WithStack(err)
} }
return f.Close() return errors.WithStack(f.Close())
} }
// New returns a new cache for the repo ID at basedir. If basedir is the empty // New returns a new cache for the repo ID at basedir. If basedir is the empty
@ -98,7 +98,7 @@ func New(id string, basedir string) (c *Cache, err error) {
err = fs.MkdirAll(basedir, 0700) err = fs.MkdirAll(basedir, 0700)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
// create base dir and tag it as a cache directory // create base dir and tag it as a cache directory
@ -124,7 +124,7 @@ func New(id string, basedir string) (c *Cache, err error) {
if os.IsNotExist(err) { if os.IsNotExist(err) {
err = fs.MkdirAll(cachedir, dirMode) err = fs.MkdirAll(cachedir, dirMode)
if err != nil { if err != nil {
return nil, err return nil, errors.WithStack(err)
} }
created = true created = true
} }
@ -138,13 +138,13 @@ func New(id string, basedir string) (c *Cache, err error) {
if v < cacheVersion { if v < cacheVersion {
err = ioutil.WriteFile(filepath.Join(cachedir, "version"), []byte(fmt.Sprintf("%d", cacheVersion)), fileMode) err = ioutil.WriteFile(filepath.Join(cachedir, "version"), []byte(fmt.Sprintf("%d", cacheVersion)), fileMode)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "WriteFile") return nil, errors.WithStack(err)
} }
} }
for _, p := range cacheLayoutPaths { for _, p := range cacheLayoutPaths {
if err = fs.MkdirAll(filepath.Join(cachedir, p), dirMode); err != nil { if err = fs.MkdirAll(filepath.Join(cachedir, p), dirMode); err != nil {
return nil, err return nil, errors.WithStack(err)
} }
} }

View File

@ -48,13 +48,13 @@ func (c *Cache) load(h restic.Handle, length int, offset int64) (io.ReadCloser,
f, err := fs.Open(c.filename(h)) f, err := fs.Open(c.filename(h))
if err != nil { if err != nil {
return nil, errors.Wrap(err, "Open") return nil, errors.WithStack(err)
} }
fi, err := f.Stat() fi, err := f.Stat()
if err != nil { if err != nil {
_ = f.Close() _ = f.Close()
return nil, errors.Wrap(err, "Stat") return nil, errors.WithStack(err)
} }
if fi.Size() <= crypto.Extension { if fi.Size() <= crypto.Extension {
@ -94,15 +94,11 @@ func (c *Cache) saveWriter(h restic.Handle) (io.WriteCloser, error) {
p := c.filename(h) p := c.filename(h)
err := fs.MkdirAll(filepath.Dir(p), 0700) err := fs.MkdirAll(filepath.Dir(p), 0700)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "MkdirAll") return nil, errors.WithStack(err)
} }
f, err := fs.OpenFile(p, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0400) f, err := fs.OpenFile(p, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0400)
if err != nil { return f, errors.WithStack(err)
return nil, errors.Wrap(err, "Create")
}
return f, err
} }
// Save saves a file in the cache. // Save saves a file in the cache.
@ -133,7 +129,7 @@ func (c *Cache) Save(h restic.Handle, rd io.Reader) error {
if err = f.Close(); err != nil { if err = f.Close(); err != nil {
_ = c.remove(h) _ = c.remove(h)
return errors.Wrap(err, "Close") return errors.WithStack(err)
} }
return nil return nil