2017-03-29 21:58:25 +00:00
|
|
|
package swift
|
|
|
|
|
|
|
|
import (
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2017-05-01 08:13:03 +00:00
|
|
|
"fmt"
|
2017-03-29 21:58:25 +00:00
|
|
|
"io"
|
2017-05-01 08:13:03 +00:00
|
|
|
"net/http"
|
2017-03-29 21:58:25 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
2017-03-29 21:58:25 +00:00
|
|
|
"github.com/ncw/swift"
|
|
|
|
)
|
|
|
|
|
|
|
|
const connLimit = 10
|
|
|
|
|
|
|
|
// beSwift is a backend which stores the data on a swift endpoint.
|
|
|
|
type beSwift struct {
|
|
|
|
conn *swift.Connection
|
2017-06-05 22:33:25 +00:00
|
|
|
sem *backend.Semaphore
|
2017-03-29 21:58:25 +00:00
|
|
|
container string // Container name
|
|
|
|
prefix string // Prefix of object names in the container
|
2017-05-01 08:13:03 +00:00
|
|
|
backend.Layout
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 15:39:57 +00:00
|
|
|
// ensure statically that *beSwift implements restic.Backend.
|
|
|
|
var _ restic.Backend = &beSwift{}
|
|
|
|
|
2017-03-29 21:58:25 +00:00
|
|
|
// Open opens the swift backend at a container in region. The container is
|
|
|
|
// created if it does not exist yet.
|
2017-09-24 18:04:23 +00:00
|
|
|
func Open(cfg Config, rt http.RoundTripper) (restic.Backend, error) {
|
2017-05-01 08:13:03 +00:00
|
|
|
debug.Log("config %#v", cfg)
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2017-06-05 22:33:25 +00:00
|
|
|
sem, err := backend.NewSemaphore(cfg.Connections)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-29 21:58:25 +00:00
|
|
|
be := &beSwift{
|
|
|
|
conn: &swift.Connection{
|
|
|
|
UserName: cfg.UserName,
|
|
|
|
Domain: cfg.Domain,
|
|
|
|
ApiKey: cfg.APIKey,
|
|
|
|
AuthUrl: cfg.AuthURL,
|
|
|
|
Region: cfg.Region,
|
|
|
|
Tenant: cfg.Tenant,
|
|
|
|
TenantId: cfg.TenantID,
|
|
|
|
TenantDomain: cfg.TenantDomain,
|
|
|
|
TrustId: cfg.TrustID,
|
|
|
|
StorageUrl: cfg.StorageURL,
|
|
|
|
AuthToken: cfg.AuthToken,
|
|
|
|
ConnectTimeout: time.Minute,
|
|
|
|
Timeout: time.Minute,
|
2017-05-01 08:13:03 +00:00
|
|
|
|
2017-09-24 18:04:23 +00:00
|
|
|
Transport: rt,
|
2017-03-29 21:58:25 +00:00
|
|
|
},
|
2017-06-05 22:33:25 +00:00
|
|
|
sem: sem,
|
2017-03-29 21:58:25 +00:00
|
|
|
container: cfg.Container,
|
|
|
|
prefix: cfg.Prefix,
|
2017-05-01 08:13:03 +00:00
|
|
|
Layout: &backend.DefaultLayout{
|
|
|
|
Path: cfg.Prefix,
|
|
|
|
Join: path.Join,
|
|
|
|
},
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Authenticate if needed
|
|
|
|
if !be.conn.Authenticated() {
|
|
|
|
if err := be.conn.Authenticate(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "conn.Authenticate")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure container exists
|
|
|
|
switch _, _, err := be.conn.Container(be.container); err {
|
|
|
|
case nil:
|
|
|
|
// Container exists
|
|
|
|
|
|
|
|
case swift.ContainerNotFound:
|
|
|
|
err = be.createContainer(cfg.DefaultContainerPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "beSwift.createContainer")
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Wrap(err, "conn.Container")
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
return be, nil
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (be *beSwift) createContainer(policy string) error {
|
|
|
|
var h swift.Headers
|
|
|
|
if policy != "" {
|
|
|
|
h = swift.Headers{
|
|
|
|
"X-Storage-Policy": policy,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return be.conn.ContainerCreate(be.container, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Location returns this backend's location (the container name).
|
|
|
|
func (be *beSwift) Location() string {
|
|
|
|
return be.container
|
|
|
|
}
|
|
|
|
|
2018-01-17 04:59:16 +00:00
|
|
|
// Load runs fn with a reader that yields the contents of the file at h at the
|
|
|
|
// given offset.
|
|
|
|
func (be *beSwift) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
|
|
|
|
return backend.DefaultLoad(ctx, h, length, offset, be.openReader, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (be *beSwift) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2017-03-29 21:58:25 +00:00
|
|
|
debug.Log("Load %v, length %v, offset %v", h, length, offset)
|
|
|
|
if err := h.Valid(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, errors.New("offset is negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
if length < 0 {
|
|
|
|
return nil, errors.Errorf("invalid length %d", length)
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
objName := be.Filename(h)
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
headers := swift.Headers{}
|
|
|
|
if offset > 0 {
|
|
|
|
headers["Range"] = fmt.Sprintf("bytes=%d-", offset)
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
if length > 0 {
|
|
|
|
headers["Range"] = fmt.Sprintf("bytes=%d-%d", offset, offset+int64(length)-1)
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
if _, ok := headers["Range"]; ok {
|
|
|
|
debug.Log("Load(%v) send range %v", h, headers["Range"])
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 16:38:17 +00:00
|
|
|
be.sem.GetToken()
|
2017-05-01 08:13:03 +00:00
|
|
|
obj, _, err := be.conn.ObjectOpen(be.container, objName, false, headers)
|
2017-03-29 21:58:25 +00:00
|
|
|
if err != nil {
|
2017-05-01 08:13:03 +00:00
|
|
|
debug.Log(" err %v", err)
|
2017-11-02 16:38:17 +00:00
|
|
|
be.sem.ReleaseToken()
|
2017-05-01 08:13:03 +00:00
|
|
|
return nil, errors.Wrap(err, "conn.ObjectOpen")
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 16:38:17 +00:00
|
|
|
return be.sem.ReleaseTokenOnClose(obj, nil), nil
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save stores data in the backend at the handle.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *beSwift) Save(ctx context.Context, h restic.Handle, rd io.Reader) (err error) {
|
2017-03-29 21:58:25 +00:00
|
|
|
if err = h.Valid(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
objName := be.Filename(h)
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
debug.Log("Save %v at %v", h, objName)
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2017-11-02 22:29:32 +00:00
|
|
|
be.sem.GetToken()
|
|
|
|
defer be.sem.ReleaseToken()
|
|
|
|
|
2017-03-29 21:58:25 +00:00
|
|
|
encoding := "binary/octet-stream"
|
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
debug.Log("PutObject(%v, %v, %v)", be.container, objName, encoding)
|
2017-03-29 21:58:25 +00:00
|
|
|
_, err = be.conn.ObjectPut(be.container, objName, rd, true, "", encoding, nil)
|
|
|
|
debug.Log("%v, err %#v", objName, err)
|
|
|
|
|
|
|
|
return errors.Wrap(err, "client.PutObject")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stat returns information about a blob.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *beSwift) Stat(ctx context.Context, h restic.Handle) (bi restic.FileInfo, err error) {
|
2017-03-29 21:58:25 +00:00
|
|
|
debug.Log("%v", h)
|
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
objName := be.Filename(h)
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2017-11-02 22:29:32 +00:00
|
|
|
be.sem.GetToken()
|
|
|
|
defer be.sem.ReleaseToken()
|
|
|
|
|
2017-03-29 21:58:25 +00:00
|
|
|
obj, _, err := be.conn.Object(be.container, objName)
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("Object() err %v", err)
|
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "conn.Object")
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
return restic.FileInfo{Size: obj.Bytes, Name: h.Name}, nil
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test returns true if a blob of the given type and name exists in the backend.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *beSwift) Test(ctx context.Context, h restic.Handle) (bool, error) {
|
2017-05-01 08:13:03 +00:00
|
|
|
objName := be.Filename(h)
|
2017-11-02 22:29:32 +00:00
|
|
|
|
|
|
|
be.sem.GetToken()
|
|
|
|
defer be.sem.ReleaseToken()
|
|
|
|
|
2017-03-29 21:58:25 +00:00
|
|
|
switch _, _, err := be.conn.Object(be.container, objName); err {
|
|
|
|
case nil:
|
|
|
|
return true, nil
|
|
|
|
|
|
|
|
case swift.ObjectNotFound:
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false, errors.Wrap(err, "conn.Object")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the blob with the given name and type.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *beSwift) Remove(ctx context.Context, h restic.Handle) error {
|
2017-05-01 08:13:03 +00:00
|
|
|
objName := be.Filename(h)
|
2017-11-02 22:29:32 +00:00
|
|
|
|
|
|
|
be.sem.GetToken()
|
|
|
|
defer be.sem.ReleaseToken()
|
|
|
|
|
2017-03-29 21:58:25 +00:00
|
|
|
err := be.conn.ObjectDelete(be.container, objName)
|
|
|
|
debug.Log("Remove(%v) -> err %v", h, err)
|
|
|
|
return errors.Wrap(err, "conn.ObjectDelete")
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
// List runs fn for each file in the backend which has the type t. When an
|
|
|
|
// error occurs (or fn returns an error), List stops and returns it.
|
|
|
|
func (be *beSwift) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
2017-03-29 21:58:25 +00:00
|
|
|
debug.Log("listing %v", t)
|
|
|
|
|
2017-12-14 18:13:01 +00:00
|
|
|
prefix, _ := be.Basedir(t)
|
|
|
|
prefix += "/"
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
err := be.conn.ObjectsWalk(be.container, &swift.ObjectsOpts{Prefix: prefix},
|
|
|
|
func(opts *swift.ObjectsOpts) (interface{}, error) {
|
|
|
|
be.sem.GetToken()
|
|
|
|
newObjects, err := be.conn.Objects(be.container, opts)
|
|
|
|
be.sem.ReleaseToken()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "conn.ObjectNames")
|
|
|
|
}
|
|
|
|
for _, obj := range newObjects {
|
|
|
|
m := path.Base(strings.TrimPrefix(obj.Name, prefix))
|
|
|
|
if m == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fi := restic.FileInfo{
|
|
|
|
Name: m,
|
|
|
|
Size: obj.Bytes,
|
|
|
|
}
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2017-11-02 22:29:32 +00:00
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
err := fn(fi)
|
2017-03-29 21:58:25 +00:00
|
|
|
if err != nil {
|
2018-01-20 18:34:38 +00:00
|
|
|
return nil, err
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
2018-01-20 18:34:38 +00:00
|
|
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
2018-01-20 18:34:38 +00:00
|
|
|
}
|
|
|
|
return newObjects, nil
|
|
|
|
})
|
2017-05-01 08:13:03 +00:00
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-29 21:58:25 +00:00
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
return ctx.Err()
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove keys for a specified backend type.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *beSwift) removeKeys(ctx context.Context, t restic.FileType) error {
|
2018-01-20 18:34:38 +00:00
|
|
|
return be.List(ctx, t, func(fi restic.FileInfo) error {
|
|
|
|
return be.Remove(ctx, restic.Handle{Type: t, Name: fi.Name})
|
|
|
|
})
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 08:13:03 +00:00
|
|
|
// IsNotExist returns true if the error is caused by a not existing file.
|
|
|
|
func (be *beSwift) IsNotExist(err error) bool {
|
|
|
|
if e, ok := errors.Cause(err).(*swift.Error); ok {
|
|
|
|
return e.StatusCode == http.StatusNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-03-29 21:58:25 +00:00
|
|
|
// Delete removes all restic objects in the container.
|
|
|
|
// It will not remove the container itself.
|
2017-06-03 15:39:57 +00:00
|
|
|
func (be *beSwift) Delete(ctx context.Context) error {
|
2017-03-29 21:58:25 +00:00
|
|
|
alltypes := []restic.FileType{
|
|
|
|
restic.DataFile,
|
|
|
|
restic.KeyFile,
|
|
|
|
restic.LockFile,
|
|
|
|
restic.SnapshotFile,
|
|
|
|
restic.IndexFile}
|
|
|
|
|
|
|
|
for _, t := range alltypes {
|
2017-06-03 15:39:57 +00:00
|
|
|
err := be.removeKeys(ctx, t)
|
2017-03-29 21:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 15:39:57 +00:00
|
|
|
err := be.Remove(ctx, restic.Handle{Type: restic.ConfigFile})
|
2017-05-01 08:13:03 +00:00
|
|
|
if err != nil && !be.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-03-29 21:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close does nothing
|
|
|
|
func (be *beSwift) Close() error { return nil }
|