2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-28 15:56:37 +00:00

Add more error handling

This commit is contained in:
Alexander Neumann 2021-01-30 19:35:46 +01:00
parent aef3658a5f
commit 0858fbf6aa
23 changed files with 91 additions and 36 deletions

View File

@ -237,7 +237,9 @@ func preCheckChangelogVersion() {
if err != nil { if err != nil {
die("unable to open CHANGELOG.md: %v", err) die("unable to open CHANGELOG.md: %v", err)
} }
defer f.Close() defer func() {
_ = f.Close()
}()
sc := bufio.NewScanner(f) sc := bufio.NewScanner(f)
for sc.Scan() { for sc.Scan() {

View File

@ -62,11 +62,11 @@ func TestBackendListRetry(t *testing.T) {
// fail during first retry, succeed during second // fail during first retry, succeed during second
retry++ retry++
if retry == 1 { if retry == 1 {
fn(restic.FileInfo{Name: ID1}) _ = fn(restic.FileInfo{Name: ID1})
return errors.New("test list error") return errors.New("test list error")
} }
fn(restic.FileInfo{Name: ID1}) _ = fn(restic.FileInfo{Name: ID1})
fn(restic.FileInfo{Name: ID2}) _ = fn(restic.FileInfo{Name: ID2})
return nil return nil
}, },
} }

View File

@ -265,9 +265,15 @@ func visitDirs(ctx context.Context, dir string, fn func(restic.FileInfo) error)
if err != nil { if err != nil {
return err return err
} }
defer d.Close()
sub, err := d.Readdirnames(-1) sub, err := d.Readdirnames(-1)
if err != nil {
// ignore subsequent errors
_ = d.Close()
return err
}
err = d.Close()
if err != nil { if err != nil {
return err return err
} }
@ -286,9 +292,15 @@ func visitFiles(ctx context.Context, dir string, fn func(restic.FileInfo) error)
if err != nil { if err != nil {
return err return err
} }
defer d.Close()
sub, err := d.Readdir(-1) sub, err := d.Readdir(-1)
if err != nil {
// ignore subsequent errors
_ = d.Close()
return err
}
err = d.Close()
if err != nil { if err != nil {
return err return err
} }

View File

@ -30,7 +30,9 @@ func TestNoSpacePermanent(t *testing.T) {
be, err := Open(context.Background(), Config{Path: dir}) be, err := Open(context.Background(), Config{Path: dir})
rtest.OK(t, err) rtest.OK(t, err)
defer be.Close() defer func() {
rtest.OK(t, be.Close())
}()
h := restic.Handle{Type: restic.ConfigFile} h := restic.Handle{Type: restic.ConfigFile}
err = be.Save(context.Background(), h, nil) err = be.Save(context.Background(), h, nil)

View File

@ -52,7 +52,7 @@ func DefaultLoad(ctx context.Context, h restic.Handle, length int, offset int64,
} }
err = fn(rd) err = fn(rd)
if err != nil { if err != nil {
rd.Close() // ignore secondary errors closing the reader _ = rd.Close() // ignore secondary errors closing the reader
return err return err
} }
return rd.Close() return rd.Close()

View File

@ -166,7 +166,7 @@ func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
if err == nil { if err == nil {
err = consumer(rd) err = consumer(rd)
if err != nil { if err != nil {
rd.Close() // ignore secondary errors _ = rd.Close() // ignore secondary errors
return err return err
} }
return rd.Close() return rd.Close()

View File

@ -17,7 +17,12 @@ func TestCacheDirEnv(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Unsetenv("RESTIC_CACHE_DIR") defer func() {
err := os.Unsetenv("RESTIC_CACHE_DIR")
if err != nil {
t.Fatal(err)
}
}()
} }
dir, err := DefaultDir() dir, err := DefaultDir()

View File

@ -101,12 +101,23 @@ func (a *acl) decode(xattr []byte) {
func (a *acl) encode() []byte { func (a *acl) encode() []byte {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
ae := new(aclElem) ae := new(aclElem)
binary.Write(buf, binary.LittleEndian, &a.Version)
err := binary.Write(buf, binary.LittleEndian, &a.Version)
// write to a bytes.Buffer always returns a nil error
if err != nil {
panic(err)
}
for _, elem := range a.List { for _, elem := range a.List {
ae.Tag = uint16(elem.getType()) ae.Tag = uint16(elem.getType())
ae.Perm = elem.Perm ae.Perm = elem.Perm
ae.ID = elem.getID() ae.ID = elem.getID()
binary.Write(buf, binary.LittleEndian, ae)
err := binary.Write(buf, binary.LittleEndian, ae)
// write to a bytes.Buffer always returns a nil error
if err != nil {
panic(err)
}
} }
return buf.Bytes() return buf.Bytes()
} }

View File

@ -25,7 +25,8 @@ func writeDump(ctx context.Context, repo restic.Repository, tree *restic.Tree, r
rootNode.Path = rootPath rootNode.Path = rootPath
err := dumpTree(ctx, repo, rootNode, rootPath, dmp) err := dumpTree(ctx, repo, rootNode, rootPath, dmp)
if err != nil { if err != nil {
dmp.Close() // ignore subsequent errors
_ = dmp.Close()
return err return err
} }

View File

@ -23,10 +23,16 @@ func readZipFile(f *zip.File) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rc.Close()
b := &bytes.Buffer{} b := &bytes.Buffer{}
_, err = b.ReadFrom(rc) _, err = b.ReadFrom(rc)
if err != nil {
// ignore subsequent errors
_ = rc.Close()
return nil, err
}
err = rc.Close()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -50,7 +50,7 @@ func (fs *LocalVss) DeleteSnapshots() {
for volumeName, snapshot := range fs.snapshots { for volumeName, snapshot := range fs.snapshots {
if err := snapshot.Delete(); err != nil { if err := snapshot.Delete(); err != nil {
fs.msgError(volumeName, errors.Errorf("failed to delete VSS snapshot: %s", err)) _ = fs.msgError(volumeName, errors.Errorf("failed to delete VSS snapshot: %s", err))
activeSnapshots[volumeName] = snapshot activeSnapshots[volumeName] = snapshot
} }
} }
@ -117,7 +117,7 @@ func (fs *LocalVss) snapshotPath(path string) string {
fs.msgMessage("creating VSS snapshot for [%s]\n", vssVolume) fs.msgMessage("creating VSS snapshot for [%s]\n", vssVolume)
if snapshot, err := NewVssSnapshot(vssVolume, 120, fs.msgError); err != nil { if snapshot, err := NewVssSnapshot(vssVolume, 120, fs.msgError); err != nil {
fs.msgError(vssVolume, errors.Errorf("failed to create snapshot for [%s]: %s\n", _ = fs.msgError(vssVolume, errors.Errorf("failed to create snapshot for [%s]: %s\n",
vssVolume, err)) vssVolume, err))
fs.failedSnapshots[volumeNameLower] = struct{}{} fs.failedSnapshots[volumeNameLower] = struct{}{}
} else { } else {

View File

@ -73,7 +73,7 @@ func (m *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
} }
err = fn(rd) err = fn(rd)
if err != nil { if err != nil {
rd.Close() // ignore secondary errors closing the reader _ = rd.Close() // ignore secondary errors closing the reader
return err return err
} }
return rd.Close() return rd.Close()

View File

@ -61,9 +61,9 @@ func TestReadHeaderEagerLoad(t *testing.T) {
expectedHeader := rtest.Random(0, entryCount*int(EntrySize)+crypto.Extension) expectedHeader := rtest.Random(0, entryCount*int(EntrySize)+crypto.Extension)
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
buf.Write(rtest.Random(0, dataSize)) // pack blobs data buf.Write(rtest.Random(0, dataSize)) // pack blobs data
buf.Write(expectedHeader) // pack header buf.Write(expectedHeader) // pack header
binary.Write(buf, binary.LittleEndian, uint32(len(expectedHeader))) // pack header length rtest.OK(t, binary.Write(buf, binary.LittleEndian, uint32(len(expectedHeader)))) // pack header length
rd := &countingReaderAt{delegate: bytes.NewReader(buf.Bytes())} rd := &countingReaderAt{delegate: bytes.NewReader(buf.Bytes())}
@ -104,9 +104,9 @@ func TestReadRecords(t *testing.T) {
expectedHeader := totalHeader[off:] expectedHeader := totalHeader[off:]
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
buf.Write(rtest.Random(0, dataSize)) // pack blobs data buf.Write(rtest.Random(0, dataSize)) // pack blobs data
buf.Write(totalHeader) // pack header buf.Write(totalHeader) // pack header
binary.Write(buf, binary.LittleEndian, uint32(len(totalHeader))) // pack header length rtest.OK(t, binary.Write(buf, binary.LittleEndian, uint32(len(totalHeader)))) // pack header length
rd := bytes.NewReader(buf.Bytes()) rd := bytes.NewReader(buf.Bytes())

View File

@ -39,7 +39,8 @@ func newPack(t testing.TB, k *crypto.Key, lengths []int) ([]Buf, []byte, uint) {
var buf bytes.Buffer var buf bytes.Buffer
p := pack.NewPacker(k, &buf) p := pack.NewPacker(k, &buf)
for _, b := range bufs { for _, b := range bufs {
p.Add(restic.TreeBlob, b.id, b.data) _, err := p.Add(restic.TreeBlob, b.id, b.data)
rtest.OK(t, err)
} }
_, err := p.Finalize() _, err := p.Finalize()

View File

@ -38,7 +38,9 @@ func (l Lock) processExists() bool {
debug.Log("error searching for process %d: %v\n", l.PID, err) debug.Log("error searching for process %d: %v\n", l.PID, err)
return false return false
} }
defer proc.Release() defer func() {
_ = proc.Release()
}()
debug.Log("sending SIGHUP to process %d\n", l.PID) debug.Log("sending SIGHUP to process %d\n", l.PID)
err = proc.Signal(syscall.SIGHUP) err = proc.Signal(syscall.SIGHUP)

View File

@ -15,7 +15,6 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe
if err != nil { if err != nil {
return errors.Wrap(err, "Open") return errors.Wrap(err, "Open")
} }
defer dir.Close()
times := []unix.Timespec{ times := []unix.Timespec{
{Sec: utimes[0].Sec, Nsec: utimes[0].Nsec}, {Sec: utimes[0].Sec, Nsec: utimes[0].Nsec},
@ -25,10 +24,12 @@ func (node Node) restoreSymlinkTimestamps(path string, utimes [2]syscall.Timespe
err = unix.UtimesNanoAt(int(dir.Fd()), filepath.Base(path), times, unix.AT_SYMLINK_NOFOLLOW) err = unix.UtimesNanoAt(int(dir.Fd()), filepath.Base(path), times, unix.AT_SYMLINK_NOFOLLOW)
if err != nil { if err != nil {
// ignore subsequent errors
_ = dir.Close()
return errors.Wrap(err, "UtimesNanoAt") return errors.Wrap(err, "UtimesNanoAt")
} }
return nil return dir.Close()
} }
func (node Node) device() int { func (node Node) device() int {

View File

@ -29,7 +29,8 @@ func BenchmarkNodeFillUser(t *testing.B) {
t.ResetTimer() t.ResetTimer()
for i := 0; i < t.N; i++ { for i := 0; i < t.N; i++ {
restic.NodeFromFileInfo(path, fi) _, err := restic.NodeFromFileInfo(path, fi)
rtest.OK(t, err)
} }
rtest.OK(t, tempfile.Close()) rtest.OK(t, tempfile.Close())

View File

@ -97,7 +97,8 @@ func (w *filesWriter) writeToFile(path string, blob []byte, offset int64, create
_, err = wr.WriteAt(blob, offset) _, err = wr.WriteAt(blob, offset)
if err != nil { if err != nil {
releaseWriter(wr) // ignore subsequent errors
_ = releaseWriter(wr)
return err return err
} }

View File

@ -19,7 +19,9 @@ func TestPreallocate(t *testing.T) {
flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY flags := os.O_CREATE | os.O_TRUNC | os.O_WRONLY
wr, err := os.OpenFile(path.Join(dirpath, "test"), flags, 0600) wr, err := os.OpenFile(path.Join(dirpath, "test"), flags, 0600)
test.OK(t, err) test.OK(t, err)
defer wr.Close() defer func() {
test.OK(t, wr.Close())
}()
err = preallocateFile(wr, i) err = preallocateFile(wr, i)
test.OK(t, err) test.OK(t, err)

View File

@ -74,7 +74,7 @@ func saveDir(t testing.TB, repo restic.Repository, nodes map[string]Node, inode
if mode == 0 { if mode == 0 {
mode = 0644 mode = 0644
} }
tree.Insert(&restic.Node{ err := tree.Insert(&restic.Node{
Type: "file", Type: "file",
Mode: mode, Mode: mode,
ModTime: node.ModTime, ModTime: node.ModTime,
@ -86,6 +86,7 @@ func saveDir(t testing.TB, repo restic.Repository, nodes map[string]Node, inode
Inode: fi, Inode: fi,
Links: lc, Links: lc,
}) })
rtest.OK(t, err)
case Dir: case Dir:
id := saveDir(t, repo, node.Nodes, inode) id := saveDir(t, repo, node.Nodes, inode)
@ -94,7 +95,7 @@ func saveDir(t testing.TB, repo restic.Repository, nodes map[string]Node, inode
mode = 0755 mode = 0755
} }
tree.Insert(&restic.Node{ err := tree.Insert(&restic.Node{
Type: "dir", Type: "dir",
Mode: mode, Mode: mode,
ModTime: node.ModTime, ModTime: node.ModTime,
@ -103,6 +104,7 @@ func saveDir(t testing.TB, repo restic.Repository, nodes map[string]Node, inode
GID: uint32(os.Getgid()), GID: uint32(os.Getgid()),
Subtree: &id, Subtree: &id,
}) })
rtest.OK(t, err)
default: default:
t.Fatalf("unknown node type %T", node) t.Fatalf("unknown node type %T", node)
} }

View File

@ -95,7 +95,9 @@ func Random(seed, count int) []byte {
func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) { func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) {
input, err := os.Open(tarFile) input, err := os.Open(tarFile)
OK(t, err) OK(t, err)
defer input.Close() defer func() {
OK(t, input.Close())
}()
var rd io.Reader var rd io.Reader
switch filepath.Ext(tarFile) { switch filepath.Ext(tarFile) {
@ -103,7 +105,9 @@ func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) {
r, err := gzip.NewReader(input) r, err := gzip.NewReader(input)
OK(t, err) OK(t, err)
defer r.Close() defer func() {
OK(t, r.Close())
}()
rd = r rd = r
case ".bzip2": case ".bzip2":
rd = bzip2.NewReader(input) rd = bzip2.NewReader(input)

View File

@ -25,7 +25,8 @@ func writeTempfile(t testing.TB, data []byte) (string, func()) {
err = closeErr err = closeErr
} }
if err != nil { if err != nil {
os.Remove(name) // ignore subsequent errors
_ = os.Remove(name)
t.Fatal(err) t.Fatal(err)
} }
}() }()

View File

@ -12,8 +12,9 @@ func TestIsProcessBackground(t *testing.T) {
if err != nil { if err != nil {
t.Skipf("can't open terminal: %v", err) t.Skipf("can't open terminal: %v", err)
} }
defer tty.Close()
_, err = isProcessBackground(tty.Fd()) _, err = isProcessBackground(tty.Fd())
rtest.OK(t, err) rtest.OK(t, err)
_ = tty.Close()
} }