Skip to content

Commit

Permalink
Bump restic
Browse files Browse the repository at this point in the history
Upstream ref: 8efb874f4887721b2df98eae2f6a831e09c0cc6a
  • Loading branch information
rubiojr committed Dec 23, 2020
1 parent 2b65f02 commit 688532d
Show file tree
Hide file tree
Showing 27 changed files with 594 additions and 272 deletions.
8 changes: 5 additions & 3 deletions backend/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"path"
"strings"

"github.com/Azure/azure-sdk-for-go/storage"
"github.com/rubiojr/rapi/backend"
"github.com/rubiojr/rapi/internal/debug"
"github.com/rubiojr/rapi/internal/errors"
"github.com/rubiojr/rapi/restic"

"github.com/Azure/azure-sdk-for-go/storage"
"github.com/cenkalti/backoff/v4"
)

// Backend stores data on an azure endpoint.
Expand Down Expand Up @@ -119,7 +121,7 @@ func (be *Backend) Path() string {
// Save stores data in the backend at the handle.
func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
if err := h.Valid(); err != nil {
return err
return backoff.Permanent(err)
}

objName := be.Filename(h)
Expand Down Expand Up @@ -219,7 +221,7 @@ func (be *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
func (be *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h))
if err := h.Valid(); err != nil {
return nil, err
return nil, backoff.Permanent(err)
}

if offset < 0 {
Expand Down
8 changes: 2 additions & 6 deletions backend/azure/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ func TestUploadLargeFile(t *testing.T) {

azcfg, err := azure.ParseConfig(os.Getenv("RESTIC_TEST_AZURE_REPOSITORY"))
if err != nil {
if err != nil {
t.Fatal(err)
}
t.Fatal(err)
}

cfg := azcfg.(azure.Config)
Expand All @@ -158,9 +156,7 @@ func TestUploadLargeFile(t *testing.T) {

be, err := azure.Create(cfg, tr)
if err != nil {
if err != nil {
t.Fatal(err)
}
t.Fatal(err)
}

defer func() {
Expand Down
5 changes: 3 additions & 2 deletions backend/b2/b2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/rubiojr/rapi/internal/errors"
"github.com/rubiojr/rapi/restic"

"github.com/cenkalti/backoff/v4"
"github.com/kurin/blazer/b2"
)

Expand Down Expand Up @@ -150,7 +151,7 @@ func (be *b2Backend) Load(ctx context.Context, h restic.Handle, length int, offs
func (be *b2Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h))
if err := h.Valid(); err != nil {
return nil, err
return nil, backoff.Permanent(err)
}

if offset < 0 {
Expand Down Expand Up @@ -189,7 +190,7 @@ func (be *b2Backend) Save(ctx context.Context, h restic.Handle, rd restic.Rewind
defer cancel()

if err := h.Valid(); err != nil {
return err
return backoff.Permanent(err)
}

be.sem.GetToken()
Expand Down
24 changes: 18 additions & 6 deletions backend/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/rubiojr/rapi/backend"
"github.com/rubiojr/rapi/internal/debug"
"github.com/rubiojr/rapi/internal/fs"

"github.com/cenkalti/backoff/v4"
)

// Local is a backend in a local directory.
Expand Down Expand Up @@ -80,16 +82,24 @@ func (b *Local) IsNotExist(err error) bool {
}

// Save stores data in the backend at the handle.
func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) (err error) {
debug.Log("Save %v", h)
if err := h.Valid(); err != nil {
return err
return backoff.Permanent(err)
}

filename := b.Filename(h)

defer func() {
// Mark non-retriable errors as such (currently only
// "no space left on device").
if errors.Is(err, syscall.ENOSPC) {
err = backoff.Permanent(err)
}
}()

// create new file
f, err := fs.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, backend.Modes.File)
f, err := openFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, backend.Modes.File)

if b.IsNotExist(err) {
debug.Log("error %v: creating dir", err)
Expand All @@ -100,7 +110,7 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
debug.Log("error creating dir %v: %v", filepath.Dir(filename), mkdirErr)
} else {
// try again
f, err = fs.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, backend.Modes.File)
f, err = openFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, backend.Modes.File)
}
}

Expand Down Expand Up @@ -141,6 +151,8 @@ func (b *Local) Save(ctx context.Context, h restic.Handle, rd restic.RewindReade
return nil
}

var openFile = fs.OpenFile // Overridden by test.

// Load runs fn with a reader that yields the contents of the file at h at the
// given offset.
func (b *Local) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
Expand All @@ -150,7 +162,7 @@ func (b *Local) Load(ctx context.Context, h restic.Handle, length int, offset in
func (b *Local) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
debug.Log("Load %v, length %v, offset %v", h, length, offset)
if err := h.Valid(); err != nil {
return nil, err
return nil, backoff.Permanent(err)
}

if offset < 0 {
Expand Down Expand Up @@ -181,7 +193,7 @@ func (b *Local) openReader(ctx context.Context, h restic.Handle, length int, off
func (b *Local) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
debug.Log("Stat %v", h)
if err := h.Valid(); err != nil {
return restic.FileInfo{}, err
return restic.FileInfo{}, backoff.Permanent(err)
}

fi, err := fs.Stat(b.Filename(h))
Expand Down
42 changes: 42 additions & 0 deletions backend/local/local_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package local

import (
"context"
"errors"
"os"
"syscall"
"testing"

"github.com/rubiojr/rapi/restic"
rtest "github.com/rubiojr/rapi/internal/test"

"github.com/cenkalti/backoff/v4"
)

func TestNoSpacePermanent(t *testing.T) {
oldOpenFile := openFile
defer func() {
openFile = oldOpenFile
}()

openFile = func(name string, flags int, mode os.FileMode) (*os.File, error) {
// The actual error from os.OpenFile is *os.PathError.
// Other functions called inside Save may return *os.SyscallError.
return nil, os.NewSyscallError("open", syscall.ENOSPC)
}

dir, cleanup := rtest.TempDir(t)
defer cleanup()

be, err := Open(context.Background(), Config{Path: dir})
rtest.OK(t, err)
defer be.Close()

h := restic.Handle{Type: restic.ConfigFile}
err = be.Save(context.Background(), h, nil)
_, ok := err.(*backoff.PermanentError)
rtest.Assert(t, ok,
"error type should be backoff.PermanentError, got %T", err)
rtest.Assert(t, errors.Is(err, syscall.ENOSPC),
"could not recover original ENOSPC error")
}
9 changes: 5 additions & 4 deletions backend/mem/mem_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"sync"

"github.com/rubiojr/rapi/backend"
"github.com/rubiojr/rapi/internal/debug"
"github.com/rubiojr/rapi/internal/errors"
"github.com/rubiojr/rapi/restic"

"github.com/rubiojr/rapi/internal/debug"
"github.com/cenkalti/backoff/v4"
)

type memMap map[restic.Handle][]byte
Expand Down Expand Up @@ -61,7 +62,7 @@ func (be *MemoryBackend) IsNotExist(err error) bool {
// Save adds new Data to the backend.
func (be *MemoryBackend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
if err := h.Valid(); err != nil {
return err
return backoff.Permanent(err)
}

be.m.Lock()
Expand Down Expand Up @@ -94,7 +95,7 @@ func (be *MemoryBackend) Load(ctx context.Context, h restic.Handle, length int,

func (be *MemoryBackend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
if err := h.Valid(); err != nil {
return nil, err
return nil, backoff.Permanent(err)
}

be.m.Lock()
Expand Down Expand Up @@ -133,7 +134,7 @@ func (be *MemoryBackend) Stat(ctx context.Context, h restic.Handle) (restic.File
defer be.m.Unlock()

if err := h.Valid(); err != nil {
return restic.FileInfo{}, err
return restic.FileInfo{}, backoff.Permanent(err)
}

if h.Type == restic.ConfigFile {
Expand Down
11 changes: 6 additions & 5 deletions backend/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (

"golang.org/x/net/context/ctxhttp"

"github.com/rubiojr/rapi/backend"
"github.com/rubiojr/rapi/internal/debug"
"github.com/rubiojr/rapi/internal/errors"
"github.com/rubiojr/rapi/restic"

"github.com/rubiojr/rapi/backend"
"github.com/cenkalti/backoff/v4"
)

// make sure the rest backend implements restic.Backend
Expand Down Expand Up @@ -109,7 +110,7 @@ func (b *Backend) Location() string {
// Save stores data in the backend at the handle.
func (b *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
if err := h.Valid(); err != nil {
return err
return backoff.Permanent(err)
}

ctx, cancel := context.WithCancel(ctx)
Expand Down Expand Up @@ -204,7 +205,7 @@ func (b *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
debug.Log("Load %v, length %v, offset %v", h, length, offset)
if err := h.Valid(); err != nil {
return nil, err
return nil, backoff.Permanent(err)
}

if offset < 0 {
Expand Down Expand Up @@ -256,7 +257,7 @@ func (b *Backend) openReader(ctx context.Context, h restic.Handle, length int, o
// Stat returns information about a blob.
func (b *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
if err := h.Valid(); err != nil {
return restic.FileInfo{}, err
return restic.FileInfo{}, backoff.Permanent(err)
}

req, err := http.NewRequest(http.MethodHead, b.Filename(h), nil)
Expand Down Expand Up @@ -311,7 +312,7 @@ func (b *Backend) Test(ctx context.Context, h restic.Handle) (bool, error) {
// Remove removes the blob with the given name and type.
func (b *Backend) Remove(ctx context.Context, h restic.Handle) error {
if err := h.Valid(); err != nil {
return err
return backoff.Permanent(err)
}

req, err := http.NewRequest("DELETE", b.Filename(h), nil)
Expand Down
8 changes: 4 additions & 4 deletions backend/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
"time"

"github.com/rubiojr/rapi/backend"
"github.com/rubiojr/rapi/internal/debug"
"github.com/rubiojr/rapi/internal/errors"
"github.com/rubiojr/rapi/restic"

"github.com/cenkalti/backoff/v4"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"

"github.com/rubiojr/rapi/internal/debug"
)

// Backend stores data on an S3 endpoint.
Expand Down Expand Up @@ -260,7 +260,7 @@ func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindRe
debug.Log("Save %v", h)

if err := h.Valid(); err != nil {
return err
return backoff.Permanent(err)
}

objName := be.Filename(h)
Expand Down Expand Up @@ -300,7 +300,7 @@ func (be *Backend) Load(ctx context.Context, h restic.Handle, length int, offset
func (be *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h))
if err := h.Valid(); err != nil {
return nil, err
return nil, backoff.Permanent(err)
}

if offset < 0 {
Expand Down
9 changes: 5 additions & 4 deletions backend/sftp/sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/rubiojr/rapi/backend"
"github.com/rubiojr/rapi/internal/debug"

"github.com/cenkalti/backoff/v4"
"github.com/pkg/sftp"
)

Expand Down Expand Up @@ -99,7 +100,7 @@ func (r *SFTP) clientError() error {
select {
case err := <-r.result:
debug.Log("client has exited with err %v", err)
return err
return backoff.Permanent(err)
default:
}

Expand Down Expand Up @@ -263,7 +264,7 @@ func (r *SFTP) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader
}

if err := h.Valid(); err != nil {
return err
return backoff.Permanent(err)
}

filename := r.Filename(h)
Expand Down Expand Up @@ -310,7 +311,7 @@ func (r *SFTP) Load(ctx context.Context, h restic.Handle, length int, offset int
func (r *SFTP) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
debug.Log("Load %v, length %v, offset %v", h, length, offset)
if err := h.Valid(); err != nil {
return nil, err
return nil, backoff.Permanent(err)
}

if offset < 0 {
Expand Down Expand Up @@ -345,7 +346,7 @@ func (r *SFTP) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, erro
}

if err := h.Valid(); err != nil {
return restic.FileInfo{}, err
return restic.FileInfo{}, backoff.Permanent(err)
}

fi, err := r.c.Lstat(r.Filename(h))
Expand Down
Loading

0 comments on commit 688532d

Please sign in to comment.