forked from TrueCloudLab/restic
migrations: Fix S3 backend detection
This commit is contained in:
parent
c934c99d41
commit
6042df075f
8 changed files with 63 additions and 14 deletions
|
@ -46,6 +46,8 @@ func (r rateLimitedBackend) Load(ctx context.Context, h restic.Handle, length in
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r rateLimitedBackend) Unwrap() restic.Backend { return r.Backend }
|
||||||
|
|
||||||
type limitedReader struct {
|
type limitedReader struct {
|
||||||
io.Reader
|
io.Reader
|
||||||
writerTo io.WriterTo
|
writerTo io.WriterTo
|
||||||
|
|
|
@ -75,3 +75,5 @@ func (be *Backend) Close() error {
|
||||||
debug.Log(" close err %v", err)
|
debug.Log(" close err %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (be *Backend) Unwrap() restic.Backend { return be.Backend }
|
||||||
|
|
|
@ -191,3 +191,7 @@ func (be *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.F
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (be *Backend) Unwrap() restic.Backend {
|
||||||
|
return be.Backend
|
||||||
|
}
|
||||||
|
|
|
@ -85,3 +85,7 @@ func (be *SemaphoreBackend) Remove(ctx context.Context, h restic.Handle) error {
|
||||||
|
|
||||||
return be.Backend.Remove(ctx, h)
|
return be.Backend.Remove(ctx, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (be *SemaphoreBackend) Unwrap() restic.Backend {
|
||||||
|
return be.Backend
|
||||||
|
}
|
||||||
|
|
4
internal/cache/backend.go
vendored
4
internal/cache/backend.go
vendored
|
@ -211,3 +211,7 @@ func (b *Backend) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, e
|
||||||
func (b *Backend) IsNotExist(err error) bool {
|
func (b *Backend) IsNotExist(err error) bool {
|
||||||
return b.Backend.IsNotExist(err)
|
return b.Backend.IsNotExist(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Backend) Unwrap() restic.Backend {
|
||||||
|
return b.Backend
|
||||||
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
|
|
||||||
"github.com/restic/restic/internal/backend/layout"
|
"github.com/restic/restic/internal/backend/layout"
|
||||||
"github.com/restic/restic/internal/backend/s3"
|
"github.com/restic/restic/internal/backend/s3"
|
||||||
"github.com/restic/restic/internal/cache"
|
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
"github.com/restic/restic/internal/errors"
|
"github.com/restic/restic/internal/errors"
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
|
@ -22,24 +21,26 @@ func init() {
|
||||||
// "default" layout.
|
// "default" layout.
|
||||||
type S3Layout struct{}
|
type S3Layout struct{}
|
||||||
|
|
||||||
func toS3Backend(repo restic.Repository) *s3.Backend {
|
func toS3Backend(b restic.Backend) *s3.Backend {
|
||||||
b := repo.Backend()
|
for b != nil {
|
||||||
// unwrap cache
|
if be, ok := b.(*s3.Backend); ok {
|
||||||
if be, ok := b.(*cache.Backend); ok {
|
return be
|
||||||
b = be.Backend
|
}
|
||||||
}
|
|
||||||
|
|
||||||
be, ok := b.(*s3.Backend)
|
if be, ok := b.(restic.BackendUnwrapper); ok {
|
||||||
if !ok {
|
b = be.Unwrap()
|
||||||
debug.Log("backend is not s3")
|
} else {
|
||||||
return nil
|
// not the backend we're looking for
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return be
|
debug.Log("backend is not s3")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check tests whether the migration can be applied.
|
// Check tests whether the migration can be applied.
|
||||||
func (m *S3Layout) Check(ctx context.Context, repo restic.Repository) (bool, string, error) {
|
func (m *S3Layout) Check(ctx context.Context, repo restic.Repository) (bool, string, error) {
|
||||||
be := toS3Backend(repo)
|
be := toS3Backend(repo.Backend())
|
||||||
if be == nil {
|
if be == nil {
|
||||||
debug.Log("backend is not s3")
|
debug.Log("backend is not s3")
|
||||||
return false, "backend is not s3", nil
|
return false, "backend is not s3", nil
|
||||||
|
@ -91,7 +92,7 @@ func (m *S3Layout) moveFiles(ctx context.Context, be *s3.Backend, l layout.Layou
|
||||||
|
|
||||||
// Apply runs the migration.
|
// Apply runs the migration.
|
||||||
func (m *S3Layout) Apply(ctx context.Context, repo restic.Repository) error {
|
func (m *S3Layout) Apply(ctx context.Context, repo restic.Repository) error {
|
||||||
be := toS3Backend(repo)
|
be := toS3Backend(repo.Backend())
|
||||||
if be == nil {
|
if be == nil {
|
||||||
debug.Log("backend is not s3")
|
debug.Log("backend is not s3")
|
||||||
return errors.New("backend is not s3")
|
return errors.New("backend is not s3")
|
||||||
|
|
27
internal/migrations/s3_layout_test.go
Normal file
27
internal/migrations/s3_layout_test.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/restic/restic/internal/backend/mock"
|
||||||
|
"github.com/restic/restic/internal/backend/s3"
|
||||||
|
"github.com/restic/restic/internal/cache"
|
||||||
|
"github.com/restic/restic/internal/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestS3UnwrapBackend(t *testing.T) {
|
||||||
|
// toS3Backend(b restic.Backend) *s3.Backend
|
||||||
|
|
||||||
|
m := mock.NewBackend()
|
||||||
|
test.Assert(t, toS3Backend(m) == nil, "mock backend is not an s3 backend")
|
||||||
|
|
||||||
|
// uninitialized fake backend for testing
|
||||||
|
s3 := &s3.Backend{}
|
||||||
|
test.Assert(t, toS3Backend(s3) == s3, "s3 was not returned")
|
||||||
|
|
||||||
|
c := &cache.Backend{Backend: s3}
|
||||||
|
test.Assert(t, toS3Backend(c) == s3, "failed to unwrap s3 backend")
|
||||||
|
|
||||||
|
c.Backend = m
|
||||||
|
test.Assert(t, toS3Backend(c) == nil, "a wrapped mock backend is not an s3 backend")
|
||||||
|
}
|
|
@ -70,6 +70,11 @@ type Backend interface {
|
||||||
Delete(ctx context.Context) error
|
Delete(ctx context.Context) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BackendUnwrapper interface {
|
||||||
|
// Unwrap returns the underlying backend or nil if there is none.
|
||||||
|
Unwrap() Backend
|
||||||
|
}
|
||||||
|
|
||||||
// FileInfo is contains information about a file in the backend.
|
// FileInfo is contains information about a file in the backend.
|
||||||
type FileInfo struct {
|
type FileInfo struct {
|
||||||
Size int64
|
Size int64
|
||||||
|
|
Loading…
Reference in a new issue