2015-05-10 15:20:58 +00:00
|
|
|
package s3
|
|
|
|
|
|
|
|
import (
|
2017-06-03 15:39:57 +00:00
|
|
|
"context"
|
2020-01-03 19:37:43 +00:00
|
|
|
"fmt"
|
2020-12-19 11:39:48 +00:00
|
|
|
"hash"
|
2015-05-10 15:20:58 +00:00
|
|
|
"io"
|
2017-09-24 18:04:23 +00:00
|
|
|
"net/http"
|
2017-05-13 21:55:22 +00:00
|
|
|
"os"
|
2016-02-15 17:47:10 +00:00
|
|
|
"path"
|
2015-05-10 15:20:58 +00:00
|
|
|
"strings"
|
2017-05-15 21:37:02 +00:00
|
|
|
"time"
|
2015-05-10 15:20:58 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2022-10-15 14:23:39 +00:00
|
|
|
"github.com/restic/restic/internal/backend/layout"
|
2022-06-12 15:45:34 +00:00
|
|
|
"github.com/restic/restic/internal/backend/sema"
|
2020-12-17 11:47:53 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2020-12-17 11:47:53 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2020-09-19 19:57:02 +00:00
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
2015-05-10 15:20:58 +00:00
|
|
|
)
|
|
|
|
|
2017-06-07 19:59:01 +00:00
|
|
|
// Backend stores data on an S3 endpoint.
|
|
|
|
type Backend struct {
|
2017-06-15 14:41:09 +00:00
|
|
|
client *minio.Client
|
2022-06-12 15:45:34 +00:00
|
|
|
sem sema.Semaphore
|
2017-06-15 14:41:09 +00:00
|
|
|
cfg Config
|
2022-10-15 14:23:39 +00:00
|
|
|
layout.Layout
|
2015-05-15 22:29:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 19:59:01 +00:00
|
|
|
// make sure that *Backend implements backend.Backend
|
|
|
|
var _ restic.Backend = &Backend{}
|
2017-06-03 15:39:57 +00:00
|
|
|
|
2017-06-07 21:08:20 +00:00
|
|
|
const defaultLayout = "default"
|
2017-05-15 21:37:02 +00:00
|
|
|
|
2020-09-19 20:01:32 +00:00
|
|
|
func open(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("open, config %#v", cfg)
|
2015-05-10 15:20:58 +00:00
|
|
|
|
2017-06-12 19:09:26 +00:00
|
|
|
if cfg.MaxRetries > 0 {
|
|
|
|
minio.MaxRetry = int(cfg.MaxRetries)
|
|
|
|
}
|
|
|
|
|
2018-05-16 23:39:25 +00:00
|
|
|
// Chains all credential types, in the following order:
|
|
|
|
// - Static credentials provided by user
|
|
|
|
// - AWS env vars (i.e. AWS_ACCESS_KEY_ID)
|
|
|
|
// - Minio env vars (i.e. MINIO_ACCESS_KEY)
|
|
|
|
// - AWS creds file (i.e. AWS_SHARED_CREDENTIALS_FILE or ~/.aws/credentials)
|
|
|
|
// - Minio creds file (i.e. MINIO_SHARED_CREDENTIALS_FILE or ~/.mc/config.json)
|
|
|
|
// - IAM profile based credentials. (performs an HTTP
|
|
|
|
// call to a pre-defined endpoint, only valid inside
|
|
|
|
// configured ec2 instances)
|
2017-12-09 09:56:49 +00:00
|
|
|
creds := credentials.NewChainCredentials([]credentials.Provider{
|
2018-07-12 13:18:19 +00:00
|
|
|
&credentials.EnvAWS{},
|
2017-10-09 19:48:42 +00:00
|
|
|
&credentials.Static{
|
|
|
|
Value: credentials.Value{
|
|
|
|
AccessKeyID: cfg.KeyID,
|
2021-08-04 20:56:18 +00:00
|
|
|
SecretAccessKey: cfg.Secret.Unwrap(),
|
2017-10-09 19:48:42 +00:00
|
|
|
},
|
|
|
|
},
|
2018-05-16 23:39:25 +00:00
|
|
|
&credentials.EnvMinio{},
|
2018-05-16 23:35:14 +00:00
|
|
|
&credentials.FileAWSCredentials{},
|
|
|
|
&credentials.FileMinioClient{},
|
2017-10-09 19:48:42 +00:00
|
|
|
&credentials.IAM{
|
|
|
|
Client: &http.Client{
|
|
|
|
Transport: http.DefaultTransport,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2020-01-03 19:37:43 +00:00
|
|
|
|
2021-09-30 18:45:31 +00:00
|
|
|
c, err := creds.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "creds.Get")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SignerType == credentials.SignatureAnonymous {
|
|
|
|
debug.Log("using anonymous access for %#v", cfg.Endpoint)
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:37:43 +00:00
|
|
|
options := &minio.Options{
|
2020-09-19 19:57:02 +00:00
|
|
|
Creds: creds,
|
|
|
|
Secure: !cfg.UseHTTP,
|
|
|
|
Region: cfg.Region,
|
|
|
|
Transport: rt,
|
2020-01-03 19:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(cfg.BucketLookup) {
|
|
|
|
case "", "auto":
|
|
|
|
options.BucketLookup = minio.BucketLookupAuto
|
|
|
|
case "dns":
|
|
|
|
options.BucketLookup = minio.BucketLookupDNS
|
|
|
|
case "path":
|
|
|
|
options.BucketLookup = minio.BucketLookupPath
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(`bad bucket-lookup style %q must be "auto", "path" or "dns"`, cfg.BucketLookup)
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := minio.New(cfg.Endpoint, options)
|
2017-10-09 19:48:42 +00:00
|
|
|
if err != nil {
|
2020-09-19 19:57:02 +00:00
|
|
|
return nil, errors.Wrap(err, "minio.New")
|
2015-08-26 11:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 15:45:34 +00:00
|
|
|
sem, err := sema.New(cfg.Connections)
|
2017-06-05 22:17:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:59:01 +00:00
|
|
|
be := &Backend{
|
2017-06-15 14:41:09 +00:00
|
|
|
client: client,
|
|
|
|
sem: sem,
|
|
|
|
cfg: cfg,
|
2017-03-14 22:05:51 +00:00
|
|
|
}
|
2017-02-10 18:24:54 +00:00
|
|
|
|
2022-10-15 14:23:39 +00:00
|
|
|
l, err := layout.ParseLayout(ctx, be, cfg.Layout, defaultLayout, cfg.Prefix)
|
2017-05-15 21:37:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
be.Layout = l
|
|
|
|
|
2017-06-17 20:15:58 +00:00
|
|
|
return be, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens the S3 backend at bucket and region. The bucket is created if it
|
|
|
|
// does not exist yet.
|
2020-09-19 20:01:32 +00:00
|
|
|
func Open(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backend, error) {
|
|
|
|
return open(ctx, cfg, rt)
|
2017-06-17 20:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create opens the S3 backend at bucket and region and creates the bucket if
|
|
|
|
// it does not exist yet.
|
2020-09-19 19:57:02 +00:00
|
|
|
func Create(ctx context.Context, cfg Config, rt http.RoundTripper) (restic.Backend, error) {
|
2020-09-19 20:01:32 +00:00
|
|
|
be, err := open(ctx, cfg, rt)
|
2017-07-17 08:33:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "open")
|
|
|
|
}
|
2020-09-19 19:57:02 +00:00
|
|
|
found, err := be.client.BucketExists(ctx, cfg.Bucket)
|
2018-03-02 08:47:20 +00:00
|
|
|
|
2022-06-13 18:35:37 +00:00
|
|
|
if err != nil && isAccessDenied(err) {
|
2018-03-02 08:47:20 +00:00
|
|
|
err = nil
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
2016-08-21 14:14:58 +00:00
|
|
|
if err != nil {
|
2016-12-05 22:12:30 +00:00
|
|
|
debug.Log("BucketExists(%v) returned err %v", cfg.Bucket, err)
|
2016-08-29 19:54:50 +00:00
|
|
|
return nil, errors.Wrap(err, "client.BucketExists")
|
2016-08-21 14:14:58 +00:00
|
|
|
}
|
2015-12-28 17:55:15 +00:00
|
|
|
|
2016-12-05 22:12:30 +00:00
|
|
|
if !found {
|
2016-01-03 20:46:07 +00:00
|
|
|
// create new bucket with default ACL in default region
|
2020-09-19 19:57:02 +00:00
|
|
|
err = be.client.MakeBucket(ctx, cfg.Bucket, minio.MakeBucketOptions{})
|
2016-01-03 20:46:07 +00:00
|
|
|
if err != nil {
|
2016-08-29 19:54:50 +00:00
|
|
|
return nil, errors.Wrap(err, "client.MakeBucket")
|
2016-01-03 20:46:07 +00:00
|
|
|
}
|
2015-11-06 21:31:59 +00:00
|
|
|
}
|
|
|
|
|
2015-12-06 22:21:48 +00:00
|
|
|
return be, nil
|
|
|
|
}
|
|
|
|
|
2022-06-13 18:35:37 +00:00
|
|
|
// isAccessDenied returns true if the error is caused by Access Denied.
|
|
|
|
func isAccessDenied(err error) bool {
|
|
|
|
debug.Log("isAccessDenied(%T, %#v)", err, err)
|
2018-03-02 08:47:20 +00:00
|
|
|
|
2022-06-13 18:35:37 +00:00
|
|
|
var e minio.ErrorResponse
|
|
|
|
return errors.As(err, &e) && e.Code == "Access Denied"
|
2018-03-02 08:47:20 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 21:37:02 +00:00
|
|
|
// IsNotExist returns true if the error is caused by a not existing file.
|
2017-06-07 19:59:01 +00:00
|
|
|
func (be *Backend) IsNotExist(err error) bool {
|
2017-05-15 21:37:02 +00:00
|
|
|
debug.Log("IsNotExist(%T, %#v)", err, err)
|
2017-06-16 08:54:46 +00:00
|
|
|
|
2022-06-13 18:35:37 +00:00
|
|
|
var e minio.ErrorResponse
|
|
|
|
return errors.As(err, &e) && e.Code == "NoSuchKey"
|
2017-05-15 21:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Join combines path components with slashes.
|
2017-06-07 19:59:01 +00:00
|
|
|
func (be *Backend) Join(p ...string) string {
|
2017-05-15 21:37:02 +00:00
|
|
|
return path.Join(p...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileInfo struct {
|
|
|
|
name string
|
|
|
|
size int64
|
|
|
|
mode os.FileMode
|
|
|
|
modTime time.Time
|
|
|
|
isDir bool
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:45:45 +00:00
|
|
|
func (fi *fileInfo) Name() string { return fi.name } // base name of the file
|
|
|
|
func (fi *fileInfo) Size() int64 { return fi.size } // length in bytes for regular files; system-dependent for others
|
|
|
|
func (fi *fileInfo) Mode() os.FileMode { return fi.mode } // file mode bits
|
|
|
|
func (fi *fileInfo) ModTime() time.Time { return fi.modTime } // modification time
|
|
|
|
func (fi *fileInfo) IsDir() bool { return fi.isDir } // abbreviation for Mode().IsDir()
|
|
|
|
func (fi *fileInfo) Sys() interface{} { return nil } // underlying data source (can return nil)
|
2017-05-15 21:37:02 +00:00
|
|
|
|
|
|
|
// ReadDir returns the entries for a directory.
|
2020-09-19 20:01:32 +00:00
|
|
|
func (be *Backend) ReadDir(ctx context.Context, dir string) (list []os.FileInfo, err error) {
|
2017-05-15 21:37:02 +00:00
|
|
|
debug.Log("ReadDir(%v)", dir)
|
|
|
|
|
|
|
|
// make sure dir ends with a slash
|
|
|
|
if dir[len(dir)-1] != '/' {
|
|
|
|
dir += "/"
|
|
|
|
}
|
|
|
|
|
2020-09-19 20:01:32 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2020-09-19 19:57:02 +00:00
|
|
|
defer cancel()
|
2017-05-15 21:37:02 +00:00
|
|
|
|
2020-11-11 10:49:20 +00:00
|
|
|
debug.Log("using ListObjectsV1(%v)", be.cfg.ListObjectsV1)
|
|
|
|
|
2020-09-19 19:57:02 +00:00
|
|
|
for obj := range be.client.ListObjects(ctx, be.cfg.Bucket, minio.ListObjectsOptions{
|
|
|
|
Prefix: dir,
|
|
|
|
Recursive: false,
|
2020-11-11 10:49:20 +00:00
|
|
|
UseV1: be.cfg.ListObjectsV1,
|
2020-09-19 19:57:02 +00:00
|
|
|
}) {
|
2018-06-01 20:15:23 +00:00
|
|
|
if obj.Err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:37:02 +00:00
|
|
|
if obj.Key == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := strings.TrimPrefix(obj.Key, dir)
|
2017-10-18 20:45:31 +00:00
|
|
|
// Sometimes s3 returns an entry for the dir itself. Ignore it.
|
2017-05-15 21:37:02 +00:00
|
|
|
if name == "" {
|
2017-10-18 20:45:31 +00:00
|
|
|
continue
|
2017-05-15 21:37:02 +00:00
|
|
|
}
|
2021-06-07 17:45:45 +00:00
|
|
|
entry := &fileInfo{
|
2017-05-15 21:37:02 +00:00
|
|
|
name: name,
|
|
|
|
size: obj.Size,
|
|
|
|
modTime: obj.LastModified,
|
|
|
|
}
|
|
|
|
|
|
|
|
if name[len(name)-1] == '/' {
|
|
|
|
entry.isDir = true
|
|
|
|
entry.mode = os.ModeDir | 0755
|
|
|
|
entry.name = name[:len(name)-1]
|
|
|
|
} else {
|
|
|
|
entry.mode = 0644
|
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:20:49 +00:00
|
|
|
func (be *Backend) Connections() uint {
|
|
|
|
return be.cfg.Connections
|
|
|
|
}
|
|
|
|
|
2015-05-13 17:48:52 +00:00
|
|
|
// Location returns this backend's location (the bucket name).
|
2017-06-07 19:59:01 +00:00
|
|
|
func (be *Backend) Location() string {
|
2017-06-15 14:41:09 +00:00
|
|
|
return be.Join(be.cfg.Bucket, be.cfg.Prefix)
|
2017-06-07 20:54:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 11:39:48 +00:00
|
|
|
// Hasher may return a hash function for calculating a content hash for the backend
|
|
|
|
func (be *Backend) Hasher() hash.Hash {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:07:29 +00:00
|
|
|
// HasAtomicReplace returns whether Save() can atomically replace files
|
|
|
|
func (be *Backend) HasAtomicReplace() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-06-07 20:54:37 +00:00
|
|
|
// Path returns the path in the bucket that is used for this backend.
|
|
|
|
func (be *Backend) Path() string {
|
2017-06-15 14:41:09 +00:00
|
|
|
return be.cfg.Prefix
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 00:15:35 +00:00
|
|
|
// Save stores data in the backend at the handle.
|
2018-03-03 13:20:54 +00:00
|
|
|
func (be *Backend) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
|
2017-06-11 09:15:15 +00:00
|
|
|
debug.Log("Save %v", h)
|
|
|
|
|
2016-01-24 00:15:35 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return backoff.Permanent(err)
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 20:04:18 +00:00
|
|
|
objName := be.Filename(h)
|
2017-04-17 17:18:47 +00:00
|
|
|
|
2017-11-01 13:40:54 +00:00
|
|
|
be.sem.GetToken()
|
|
|
|
defer be.sem.ReleaseToken()
|
|
|
|
|
2019-03-26 15:37:07 +00:00
|
|
|
opts := minio.PutObjectOptions{StorageClass: be.cfg.StorageClass}
|
2017-12-08 20:52:50 +00:00
|
|
|
opts.ContentType = "application/octet-stream"
|
2020-12-19 11:29:02 +00:00
|
|
|
// the only option with the high-level api is to let the library handle the checksum computation
|
|
|
|
opts.SendContentMd5 = true
|
2022-07-13 16:00:17 +00:00
|
|
|
// only use multipart uploads for very large files
|
|
|
|
opts.PartSize = 200 * 1024 * 1024
|
2017-12-08 20:52:50 +00:00
|
|
|
|
2018-03-03 13:20:54 +00:00
|
|
|
debug.Log("PutObject(%v, %v, %v)", be.cfg.Bucket, objName, rd.Length())
|
2022-12-02 18:36:43 +00:00
|
|
|
info, err := be.client.PutObject(ctx, be.cfg.Bucket, objName, io.NopCloser(rd), int64(rd.Length()), opts)
|
2017-06-11 09:15:15 +00:00
|
|
|
|
2020-12-18 22:41:29 +00:00
|
|
|
debug.Log("%v -> %v bytes, err %#v: %v", objName, info.Size, err, err)
|
|
|
|
|
|
|
|
// sanity check
|
2021-03-08 19:23:57 +00:00
|
|
|
if err == nil && info.Size != rd.Length() {
|
2020-12-18 22:41:29 +00:00
|
|
|
return errors.Errorf("wrote %d bytes instead of the expected %d bytes", info.Size, rd.Length())
|
|
|
|
}
|
2016-01-24 00:15:35 +00:00
|
|
|
|
2016-08-29 19:54:50 +00:00
|
|
|
return errors.Wrap(err, "client.PutObject")
|
2016-01-24 00:15:35 +00:00
|
|
|
}
|
|
|
|
|
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 *Backend) 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 *Backend) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
|
2017-04-17 17:18:47 +00:00
|
|
|
debug.Log("Load %v, length %v, offset %v from %v", h, length, offset, be.Filename(h))
|
2017-01-22 21:01:12 +00:00
|
|
|
if err := h.Valid(); err != nil {
|
2020-12-17 11:47:53 +00:00
|
|
|
return nil, backoff.Permanent(err)
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, errors.New("offset is negative")
|
|
|
|
}
|
|
|
|
|
|
|
|
if length < 0 {
|
|
|
|
return nil, errors.Errorf("invalid length %d", length)
|
|
|
|
}
|
|
|
|
|
2017-04-11 20:04:18 +00:00
|
|
|
objName := be.Filename(h)
|
2017-12-08 20:52:50 +00:00
|
|
|
opts := minio.GetObjectOptions{}
|
2017-01-22 21:01:12 +00:00
|
|
|
|
2017-12-08 20:52:50 +00:00
|
|
|
var err error
|
2017-05-13 19:18:14 +00:00
|
|
|
if length > 0 {
|
2017-12-08 20:52:50 +00:00
|
|
|
debug.Log("range: %v-%v", offset, offset+int64(length)-1)
|
|
|
|
err = opts.SetRange(offset, offset+int64(length)-1)
|
|
|
|
} else if offset > 0 {
|
|
|
|
debug.Log("range: %v-", offset)
|
|
|
|
err = opts.SetRange(offset, 0)
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
2017-06-11 12:30:56 +00:00
|
|
|
|
2017-12-08 20:52:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "SetRange")
|
|
|
|
}
|
2017-01-22 21:01:12 +00:00
|
|
|
|
2017-12-08 20:52:50 +00:00
|
|
|
be.sem.GetToken()
|
2021-08-07 17:45:52 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
2017-06-05 22:17:39 +00:00
|
|
|
coreClient := minio.Core{Client: be.client}
|
2020-09-19 19:57:02 +00:00
|
|
|
rd, _, _, err := coreClient.GetObject(ctx, be.cfg.Bucket, objName, opts)
|
2017-05-13 22:09:59 +00:00
|
|
|
if err != nil {
|
2021-08-07 17:45:52 +00:00
|
|
|
cancel()
|
2017-06-05 22:17:39 +00:00
|
|
|
be.sem.ReleaseToken()
|
2017-05-13 22:09:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-22 21:01:12 +00:00
|
|
|
|
2021-08-07 17:45:52 +00:00
|
|
|
return be.sem.ReleaseTokenOnClose(rd, cancel), err
|
2017-01-22 21:01:12 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 22:27:58 +00:00
|
|
|
// Stat returns information about a blob.
|
2017-06-07 19:59:01 +00:00
|
|
|
func (be *Backend) Stat(ctx context.Context, h restic.Handle) (bi restic.FileInfo, err error) {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("%v", h)
|
2016-08-21 14:15:24 +00:00
|
|
|
|
2017-04-11 20:04:18 +00:00
|
|
|
objName := be.Filename(h)
|
2016-08-21 14:15:24 +00:00
|
|
|
var obj *minio.Object
|
|
|
|
|
2017-12-08 20:52:50 +00:00
|
|
|
opts := minio.GetObjectOptions{}
|
|
|
|
|
2017-11-01 13:40:54 +00:00
|
|
|
be.sem.GetToken()
|
2020-09-19 19:57:02 +00:00
|
|
|
obj, err = be.client.GetObject(ctx, be.cfg.Bucket, objName, opts)
|
2016-01-23 22:27:58 +00:00
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("GetObject() err %v", err)
|
2017-11-01 13:40:54 +00:00
|
|
|
be.sem.ReleaseToken()
|
2016-08-31 20:39:36 +00:00
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "client.GetObject")
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-21 14:15:24 +00:00
|
|
|
// make sure that the object is closed properly.
|
|
|
|
defer func() {
|
|
|
|
e := obj.Close()
|
2017-11-01 13:40:54 +00:00
|
|
|
be.sem.ReleaseToken()
|
2016-08-21 14:15:24 +00:00
|
|
|
if err == nil {
|
2016-08-29 19:54:50 +00:00
|
|
|
err = errors.Wrap(e, "Close")
|
2016-08-21 14:15:24 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-01-23 22:27:58 +00:00
|
|
|
fi, err := obj.Stat()
|
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Stat() err %v", err)
|
2016-08-31 20:39:36 +00:00
|
|
|
return restic.FileInfo{}, errors.Wrap(err, "Stat")
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
return restic.FileInfo{Size: fi.Size, Name: h.Name}, nil
|
2016-01-23 22:27:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-10 15:20:58 +00:00
|
|
|
// Remove removes the blob with the given name and type.
|
2017-06-07 19:59:01 +00:00
|
|
|
func (be *Backend) Remove(ctx context.Context, h restic.Handle) error {
|
2017-04-11 20:04:18 +00:00
|
|
|
objName := be.Filename(h)
|
2017-11-01 13:40:54 +00:00
|
|
|
|
|
|
|
be.sem.GetToken()
|
2020-09-19 19:57:02 +00:00
|
|
|
err := be.client.RemoveObject(ctx, be.cfg.Bucket, objName, minio.RemoveObjectOptions{})
|
2017-11-01 13:40:54 +00:00
|
|
|
be.sem.ReleaseToken()
|
|
|
|
|
2017-04-17 17:18:47 +00:00
|
|
|
debug.Log("Remove(%v) at %v -> err %v", h, objName, err)
|
2017-06-15 14:06:47 +00:00
|
|
|
|
|
|
|
if be.IsNotExist(err) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
2016-08-29 19:54:50 +00:00
|
|
|
return errors.Wrap(err, "client.RemoveObject")
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
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 *Backend) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("listing %v", t)
|
2015-05-10 15:20:58 +00:00
|
|
|
|
2017-12-14 18:13:01 +00:00
|
|
|
prefix, recursive := be.Basedir(t)
|
2015-05-10 15:20:58 +00:00
|
|
|
|
2017-05-15 21:37:02 +00:00
|
|
|
// make sure prefix ends with a slash
|
2018-01-20 18:34:38 +00:00
|
|
|
if !strings.HasSuffix(prefix, "/") {
|
2017-05-15 21:37:02 +00:00
|
|
|
prefix += "/"
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-11-11 10:49:20 +00:00
|
|
|
debug.Log("using ListObjectsV1(%v)", be.cfg.ListObjectsV1)
|
|
|
|
|
2017-11-02 22:29:32 +00:00
|
|
|
// NB: unfortunately we can't protect this with be.sem.GetToken() here.
|
|
|
|
// Doing so would enable a deadlock situation (gh-1399), as ListObjects()
|
|
|
|
// starts its own goroutine and returns results via a channel.
|
2020-09-19 19:57:02 +00:00
|
|
|
listresp := be.client.ListObjects(ctx, be.cfg.Bucket, minio.ListObjectsOptions{
|
|
|
|
Prefix: prefix,
|
|
|
|
Recursive: recursive,
|
2020-11-11 10:49:20 +00:00
|
|
|
UseV1: be.cfg.ListObjectsV1,
|
2020-09-19 19:57:02 +00:00
|
|
|
})
|
2015-05-13 17:48:52 +00:00
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
for obj := range listresp {
|
2018-06-01 20:15:23 +00:00
|
|
|
if obj.Err != nil {
|
|
|
|
return obj.Err
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
m := strings.TrimPrefix(obj.Key, prefix)
|
|
|
|
if m == "" {
|
|
|
|
continue
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
fi := restic.FileInfo{
|
|
|
|
Name: path.Base(m),
|
|
|
|
Size: obj.Size,
|
|
|
|
}
|
2015-05-10 15:20:58 +00:00
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
err := fn(fi)
|
2015-12-06 22:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-20 18:34:38 +00:00
|
|
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2015-06-14 13:02:29 +00:00
|
|
|
}
|
2015-12-06 22:21:48 +00:00
|
|
|
|
2018-01-20 18:34:38 +00:00
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove keys for a specified backend type.
|
|
|
|
func (be *Backend) removeKeys(ctx context.Context, t restic.FileType) error {
|
2020-08-16 09:16:38 +00:00
|
|
|
return be.List(ctx, restic.PackFile, func(fi restic.FileInfo) error {
|
2018-01-20 18:34:38 +00:00
|
|
|
return be.Remove(ctx, restic.Handle{Type: t, Name: fi.Name})
|
|
|
|
})
|
2015-06-14 13:02:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-19 12:23:05 +00:00
|
|
|
// Delete removes all restic keys in the bucket. It will not remove the bucket itself.
|
2017-06-07 19:59:01 +00:00
|
|
|
func (be *Backend) Delete(ctx context.Context) error {
|
2016-08-31 20:39:36 +00:00
|
|
|
alltypes := []restic.FileType{
|
2020-08-16 09:16:38 +00:00
|
|
|
restic.PackFile,
|
2016-08-31 20:39:36 +00:00
|
|
|
restic.KeyFile,
|
|
|
|
restic.LockFile,
|
|
|
|
restic.SnapshotFile,
|
|
|
|
restic.IndexFile}
|
2015-12-06 22:21:48 +00:00
|
|
|
|
|
|
|
for _, t := range alltypes {
|
2017-06-03 15:39:57 +00:00
|
|
|
err := be.removeKeys(ctx, t)
|
2015-12-06 22:21:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 15:39:57 +00:00
|
|
|
return be.Remove(ctx, restic.Handle{Type: restic.ConfigFile})
|
2015-05-10 15:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close does nothing
|
2017-06-07 19:59:01 +00:00
|
|
|
func (be *Backend) Close() error { return nil }
|
2017-06-07 20:54:37 +00:00
|
|
|
|
|
|
|
// Rename moves a file based on the new layout l.
|
2022-10-15 14:23:39 +00:00
|
|
|
func (be *Backend) Rename(ctx context.Context, h restic.Handle, l layout.Layout) error {
|
2017-06-07 20:54:37 +00:00
|
|
|
debug.Log("Rename %v to %v", h, l)
|
|
|
|
oldname := be.Filename(h)
|
|
|
|
newname := l.Filename(h)
|
|
|
|
|
2017-07-02 08:29:41 +00:00
|
|
|
if oldname == newname {
|
|
|
|
debug.Log(" %v is already renamed", newname)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-07 20:54:37 +00:00
|
|
|
debug.Log(" %v -> %v", oldname, newname)
|
|
|
|
|
2020-09-19 19:57:02 +00:00
|
|
|
src := minio.CopySrcOptions{
|
|
|
|
Bucket: be.cfg.Bucket,
|
|
|
|
Object: oldname,
|
|
|
|
}
|
2017-07-17 18:43:45 +00:00
|
|
|
|
2020-09-19 19:57:02 +00:00
|
|
|
dst := minio.CopyDestOptions{
|
|
|
|
Bucket: be.cfg.Bucket,
|
|
|
|
Object: newname,
|
2017-07-17 18:43:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 19:57:02 +00:00
|
|
|
_, err := be.client.CopyObject(ctx, dst, src)
|
2017-07-02 08:47:50 +00:00
|
|
|
if err != nil && be.IsNotExist(err) {
|
|
|
|
debug.Log("copy failed: %v, seems to already have been renamed", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-07 20:54:37 +00:00
|
|
|
if err != nil {
|
|
|
|
debug.Log("copy failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-19 19:57:02 +00:00
|
|
|
return be.client.RemoveObject(ctx, be.cfg.Bucket, oldname, minio.RemoveObjectOptions{})
|
2017-06-07 20:54:37 +00:00
|
|
|
}
|