sftp: Annotate a few errors

This commit is contained in:
Alexander Neumann 2015-05-03 21:06:20 +02:00
parent 13a42ec5ec
commit 250080db9c

View file

@ -4,7 +4,6 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -15,6 +14,7 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/juju/errors"
"github.com/pkg/sftp" "github.com/pkg/sftp"
"github.com/restic/restic/backend" "github.com/restic/restic/backend"
) )
@ -245,13 +245,11 @@ func (r *SFTP) Location() string {
func (r *SFTP) tempFile() (string, *sftp.File, error) { func (r *SFTP) tempFile() (string, *sftp.File, error) {
// choose random suffix // choose random suffix
buf := make([]byte, tempfileRandomSuffixLength) buf := make([]byte, tempfileRandomSuffixLength)
n, err := io.ReadFull(rand.Reader, buf) _, err := io.ReadFull(rand.Reader, buf)
if err != nil { if err != nil {
return "", nil, err return "", nil, errors.Annotatef(err,
} "unable to read %d random bytes for tempfile name",
tempfileRandomSuffixLength)
if n != len(buf) {
return "", nil, errors.New("unable to generate enough random bytes for temp file")
} }
// construct tempfile name // construct tempfile name
@ -260,7 +258,7 @@ func (r *SFTP) tempFile() (string, *sftp.File, error) {
// create file in temp dir // create file in temp dir
f, err := r.c.Create(name) f, err := r.c.Create(name)
if err != nil { if err != nil {
return "", nil, err return "", nil, errors.Annotatef(err, "creating tempfile %q failed", name)
} }
return name, f, nil return name, f, nil
@ -375,7 +373,7 @@ func (r *SFTP) Create() (backend.Blob, error) {
// create tempfile in backend // create tempfile in backend
filename, file, err := r.tempFile() filename, file, err := r.tempFile()
if err != nil { if err != nil {
return nil, err return nil, errors.Annotate(err, "create tempfile")
} }
blob := sftpBlob{ blob := sftpBlob{