2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +00:00

Upgrade ncw/swift to v2

This commit is contained in:
greatroar 2021-04-09 11:24:28 +02:00 committed by greatroar
parent ef5672a902
commit e7ec0453b1
5 changed files with 25 additions and 31 deletions

View File

@ -693,7 +693,7 @@ func open(s string, gopts GlobalOptions, opts options.Options) (restic.Backend,
case "azure":
be, err = azure.Open(cfg.(azure.Config), rt)
case "swift":
be, err = swift.Open(cfg.(swift.Config), rt)
be, err = swift.Open(globalOptions.ctx, cfg.(swift.Config), rt)
case "b2":
be, err = b2.Open(globalOptions.ctx, cfg.(b2.Config), rt)
case "rest":
@ -769,7 +769,7 @@ func create(s string, opts options.Options) (restic.Backend, error) {
case "azure":
return azure.Create(cfg.(azure.Config), rt)
case "swift":
return swift.Open(cfg.(swift.Config), rt)
return swift.Open(globalOptions.ctx, cfg.(swift.Config), rt)
case "b2":
return b2.Create(globalOptions.ctx, cfg.(b2.Config), rt)
case "rest":

2
go.mod
View File

@ -19,7 +19,7 @@ require (
github.com/kurin/blazer v0.5.3
github.com/minio/minio-go/v7 v7.0.12
github.com/minio/sha256-simd v1.0.0
github.com/ncw/swift v1.0.53
github.com/ncw/swift/v2 v2.0.0
github.com/pkg/errors v0.9.1
github.com/pkg/profile v1.6.0
github.com/pkg/sftp v1.13.2

4
go.sum
View File

@ -275,8 +275,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/ncw/swift v1.0.53 h1:luHjjTNtekIEvHg5KdAFIBaH7bWfNkefwFnpDffSIks=
github.com/ncw/swift v1.0.53/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
github.com/ncw/swift/v2 v2.0.0 h1:Q1jkMe/yhCkx7yAKq4bBZ/Th3NR+ejRcwbVK8Pi1i/0=
github.com/ncw/swift/v2 v2.0.0/go.mod h1:z0A9RVdYPjNjXVo2pDOPxZ4eu3oarO1P91fTItcb+Kg=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

View File

@ -19,7 +19,7 @@ import (
"github.com/restic/restic/internal/restic"
"github.com/cenkalti/backoff/v4"
"github.com/ncw/swift"
"github.com/ncw/swift/v2"
)
// beSwift is a backend which stores the data on a swift endpoint.
@ -36,7 +36,7 @@ var _ restic.Backend = &beSwift{}
// Open opens the swift backend at a container in region. The container is
// created if it does not exist yet.
func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) {
func Open(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backend, error) {
debug.Log("config %#v", cfg)
sem, err := backend.NewSemaphore(cfg.Connections)
@ -79,18 +79,18 @@ func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) {
// Authenticate if needed
if !be.conn.Authenticated() {
if err := be.conn.Authenticate(); err != nil {
if err := be.conn.Authenticate(ctx); err != nil {
return nil, errors.Wrap(err, "conn.Authenticate")
}
}
// Ensure container exists
switch _, _, err := be.conn.Container(be.container); err {
switch _, _, err := be.conn.Container(ctx, be.container); err {
case nil:
// Container exists
case swift.ContainerNotFound:
err = be.createContainer(cfg.DefaultContainerPolicy)
err = be.createContainer(ctx, cfg.DefaultContainerPolicy)
if err != nil {
return nil, errors.Wrap(err, "beSwift.createContainer")
}
@ -102,7 +102,7 @@ func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) {
return be, nil
}
func (be *beSwift) createContainer(policy string) error {
func (be *beSwift) createContainer(ctx context.Context, policy string) error {
var h swift.Headers
if policy != "" {
h = swift.Headers{
@ -110,7 +110,7 @@ func (be *beSwift) createContainer(policy string) error {
}
}
return be.conn.ContainerCreate(be.container, h)
return be.conn.ContainerCreate(ctx, be.container, h)
}
// Location returns this backend's location (the container name).
@ -159,7 +159,7 @@ func (be *beSwift) openReader(ctx context.Context, h restic.Handle, length int,
}
be.sem.GetToken()
obj, _, err := be.conn.ObjectOpen(be.container, objName, false, headers)
obj, _, err := be.conn.ObjectOpen(ctx, be.container, objName, false, headers)
if err != nil {
debug.Log(" err %v", err)
be.sem.ReleaseToken()
@ -186,7 +186,9 @@ func (be *beSwift) Save(ctx context.Context, h restic.Handle, rd restic.RewindRe
debug.Log("PutObject(%v, %v, %v)", be.container, objName, encoding)
hdr := swift.Headers{"Content-Length": strconv.FormatInt(rd.Length(), 10)}
_, err := be.conn.ObjectPut(be.container, objName, rd, true, hex.EncodeToString(rd.Hash()), encoding, hdr)
_, err := be.conn.ObjectPut(ctx,
be.container, objName, rd, true, hex.EncodeToString(rd.Hash()),
encoding, hdr)
// swift does not return the upload length
debug.Log("%v, err %#v", objName, err)
@ -202,7 +204,7 @@ func (be *beSwift) Stat(ctx context.Context, h restic.Handle) (bi restic.FileInf
be.sem.GetToken()
defer be.sem.ReleaseToken()
obj, _, err := be.conn.Object(be.container, objName)
obj, _, err := be.conn.Object(ctx, be.container, objName)
if err != nil {
debug.Log("Object() err %v", err)
return restic.FileInfo{}, errors.Wrap(err, "conn.Object")
@ -218,7 +220,7 @@ func (be *beSwift) Test(ctx context.Context, h restic.Handle) (bool, error) {
be.sem.GetToken()
defer be.sem.ReleaseToken()
switch _, _, err := be.conn.Object(be.container, objName); err {
switch _, _, err := be.conn.Object(ctx, be.container, objName); err {
case nil:
return true, nil
@ -237,7 +239,7 @@ func (be *beSwift) Remove(ctx context.Context, h restic.Handle) error {
be.sem.GetToken()
defer be.sem.ReleaseToken()
err := be.conn.ObjectDelete(be.container, objName)
err := be.conn.ObjectDelete(ctx, be.container, objName)
debug.Log("Remove(%v) -> err %v", h, err)
return errors.Wrap(err, "conn.ObjectDelete")
}
@ -250,10 +252,10 @@ func (be *beSwift) List(ctx context.Context, t restic.FileType, fn func(restic.F
prefix, _ := be.Basedir(t)
prefix += "/"
err := be.conn.ObjectsWalk(be.container, &swift.ObjectsOpts{Prefix: prefix},
func(opts *swift.ObjectsOpts) (interface{}, error) {
err := be.conn.ObjectsWalk(ctx, be.container, &swift.ObjectsOpts{Prefix: prefix},
func(ctx context.Context, opts *swift.ObjectsOpts) (interface{}, error) {
be.sem.GetToken()
newObjects, err := be.conn.Objects(be.container, opts)
newObjects, err := be.conn.Objects(ctx, be.container, opts)
be.sem.ReleaseToken()
if err != nil {
@ -270,18 +272,10 @@ func (be *beSwift) List(ctx context.Context, t restic.FileType, fn func(restic.F
Size: obj.Bytes,
}
if ctx.Err() != nil {
return nil, ctx.Err()
}
err := fn(fi)
if err != nil {
return nil, err
}
if ctx.Err() != nil {
return nil, ctx.Err()
}
}
return newObjects, nil
})

View File

@ -61,7 +61,7 @@ func newSwiftTestSuite(t testing.TB) *test.Suite {
Create: func(config interface{}) (restic.Backend, error) {
cfg := config.(swift.Config)
be, err := swift.Open(cfg, tr)
be, err := swift.Open(context.TODO(), cfg, tr)
if err != nil {
return nil, err
}
@ -81,14 +81,14 @@ func newSwiftTestSuite(t testing.TB) *test.Suite {
// OpenFn is a function that opens a previously created temporary repository.
Open: func(config interface{}) (restic.Backend, error) {
cfg := config.(swift.Config)
return swift.Open(cfg, tr)
return swift.Open(context.TODO(), cfg, tr)
},
// CleanupFn removes data created during the tests.
Cleanup: func(config interface{}) error {
cfg := config.(swift.Config)
be, err := swift.Open(cfg, tr)
be, err := swift.Open(context.TODO(), cfg, tr)
if err != nil {
return err
}