2015-05-11 16:11:47 +00:00
|
|
|
// Package swift provides a storagedriver.StorageDriver implementation to
|
|
|
|
// store blobs in Openstack Swift object storage.
|
|
|
|
//
|
|
|
|
// This package leverages the ncw/swift client library for interfacing with
|
|
|
|
// Swift.
|
|
|
|
//
|
2015-06-30 12:17:12 +00:00
|
|
|
// It supports both TempAuth authentication and Keystone authentication
|
|
|
|
// (up to version 3).
|
|
|
|
//
|
|
|
|
// As Swift has a limit on the size of a single uploaded object (by default
|
|
|
|
// this is 5GB), the driver makes use of the Swift Large Object Support
|
|
|
|
// (http://docs.openstack.org/developer/swift/overview_large_objects.html).
|
|
|
|
// Only one container is used for both manifests and data objects. Manifests
|
|
|
|
// are stored in the 'files' pseudo directory, data objects are stored under
|
|
|
|
// 'segments'.
|
2015-05-11 16:11:47 +00:00
|
|
|
package swift
|
|
|
|
|
|
|
|
import (
|
2016-03-08 23:57:12 +00:00
|
|
|
"bufio"
|
2015-05-11 16:11:47 +00:00
|
|
|
"bytes"
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-07-08 10:59:29 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha1"
|
2015-06-08 08:37:11 +00:00
|
|
|
"crypto/tls"
|
2015-07-08 10:59:29 +00:00
|
|
|
"encoding/hex"
|
2015-05-11 16:11:47 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2015-11-03 08:59:50 +00:00
|
|
|
"net/url"
|
2015-05-11 16:11:47 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-05-29 14:12:58 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-05-23 07:22:41 +00:00
|
|
|
"github.com/ncw/swift"
|
2015-05-11 16:11:47 +00:00
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/base"
|
|
|
|
"github.com/distribution/distribution/v3/registry/storage/driver/factory"
|
|
|
|
"github.com/distribution/distribution/v3/version"
|
2015-05-11 16:11:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const driverName = "swift"
|
|
|
|
|
2015-06-30 12:17:12 +00:00
|
|
|
// defaultChunkSize defines the default size of a segment
|
2015-06-19 13:55:34 +00:00
|
|
|
const defaultChunkSize = 20 * 1024 * 1024
|
2015-05-11 16:11:47 +00:00
|
|
|
|
2015-06-30 12:17:12 +00:00
|
|
|
// minChunkSize defines the minimum size of a segment
|
2015-05-29 14:12:58 +00:00
|
|
|
const minChunkSize = 1 << 20
|
|
|
|
|
2016-03-08 23:57:12 +00:00
|
|
|
// contentType defines the Content-Type header associated with stored segments
|
|
|
|
const contentType = "application/octet-stream"
|
|
|
|
|
2015-10-29 11:24:56 +00:00
|
|
|
// readAfterWriteTimeout defines the time we wait before an object appears after having been uploaded
|
|
|
|
var readAfterWriteTimeout = 15 * time.Second
|
|
|
|
|
|
|
|
// readAfterWriteWait defines the time to sleep between two retries
|
|
|
|
var readAfterWriteWait = 200 * time.Millisecond
|
|
|
|
|
2015-06-30 12:22:41 +00:00
|
|
|
// Parameters A struct that encapsulates all of the driver parameters after all values have been set
|
|
|
|
type Parameters struct {
|
2015-11-03 08:59:50 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
AuthURL string
|
|
|
|
Tenant string
|
|
|
|
TenantID string
|
|
|
|
Domain string
|
|
|
|
DomainID string
|
2016-08-15 09:21:16 +00:00
|
|
|
TenantDomain string
|
|
|
|
TenantDomainID string
|
2015-11-03 08:59:50 +00:00
|
|
|
TrustID string
|
|
|
|
Region string
|
2016-04-13 15:32:10 +00:00
|
|
|
AuthVersion int
|
2015-11-03 08:59:50 +00:00
|
|
|
Container string
|
|
|
|
Prefix string
|
2016-03-07 21:41:20 +00:00
|
|
|
EndpointType string
|
2015-11-03 08:59:50 +00:00
|
|
|
InsecureSkipVerify bool
|
|
|
|
ChunkSize int
|
|
|
|
SecretKey string
|
|
|
|
AccessKey string
|
|
|
|
TempURLContainerKey bool
|
|
|
|
TempURLMethods []string
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 08:59:50 +00:00
|
|
|
// swiftInfo maps the JSON structure returned by Swift /info endpoint
|
|
|
|
type swiftInfo struct {
|
|
|
|
Swift struct {
|
|
|
|
Version string `mapstructure:"version"`
|
|
|
|
}
|
|
|
|
Tempurl struct {
|
|
|
|
Methods []string `mapstructure:"methods"`
|
|
|
|
}
|
2016-08-24 17:09:25 +00:00
|
|
|
BulkDelete struct {
|
|
|
|
MaxDeletesPerRequest int `mapstructure:"max_deletes_per_request"`
|
|
|
|
} `mapstructure:"bulk_delete"`
|
2015-11-03 08:59:50 +00:00
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
factory.Register(driverName, &swiftDriverFactory{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// swiftDriverFactory implements the factory.StorageDriverFactory interface
|
|
|
|
type swiftDriverFactory struct{}
|
|
|
|
|
|
|
|
func (factory *swiftDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
|
|
|
|
return FromParameters(parameters)
|
|
|
|
}
|
|
|
|
|
|
|
|
type driver struct {
|
2016-10-31 18:41:53 +00:00
|
|
|
Conn *swift.Connection
|
2016-08-24 17:09:25 +00:00
|
|
|
Container string
|
|
|
|
Prefix string
|
|
|
|
BulkDeleteSupport bool
|
|
|
|
BulkDeleteMaxDeletes int
|
|
|
|
ChunkSize int
|
|
|
|
SecretKey string
|
|
|
|
AccessKey string
|
|
|
|
TempURLContainerKey bool
|
|
|
|
TempURLMethods []string
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type baseEmbed struct {
|
|
|
|
base.Base
|
|
|
|
}
|
|
|
|
|
2015-06-30 12:17:12 +00:00
|
|
|
// Driver is a storagedriver.StorageDriver implementation backed by Openstack Swift
|
|
|
|
// Objects are stored at absolute keys in the provided container.
|
2015-05-11 16:11:47 +00:00
|
|
|
type Driver struct {
|
|
|
|
baseEmbed
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromParameters constructs a new Driver with a given parameters map
|
|
|
|
// Required parameters:
|
|
|
|
// - username
|
|
|
|
// - password
|
|
|
|
// - authurl
|
|
|
|
// - container
|
|
|
|
func FromParameters(parameters map[string]interface{}) (*Driver, error) {
|
2015-06-30 12:22:41 +00:00
|
|
|
params := Parameters{
|
2015-06-08 08:37:11 +00:00
|
|
|
ChunkSize: defaultChunkSize,
|
|
|
|
InsecureSkipVerify: false,
|
2015-05-29 14:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-12-17 17:26:09 +00:00
|
|
|
// Sanitize some entries before trying to decode parameters with mapstructure
|
|
|
|
// TenantID and Tenant when integers only and passed as ENV variables
|
|
|
|
// are considered as integer and not string. The parser fails in this
|
|
|
|
// case.
|
|
|
|
_, ok := parameters["tenant"]
|
|
|
|
if ok {
|
|
|
|
parameters["tenant"] = fmt.Sprint(parameters["tenant"])
|
|
|
|
}
|
|
|
|
_, ok = parameters["tenantid"]
|
|
|
|
if ok {
|
|
|
|
parameters["tenantid"] = fmt.Sprint(parameters["tenantid"])
|
|
|
|
}
|
|
|
|
|
2015-05-29 14:12:58 +00:00
|
|
|
if err := mapstructure.Decode(parameters, ¶ms); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Username == "" {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("no username parameter provided")
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2015-05-29 14:12:58 +00:00
|
|
|
|
|
|
|
if params.Password == "" {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("no password parameter provided")
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2015-05-29 14:12:58 +00:00
|
|
|
|
|
|
|
if params.AuthURL == "" {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("no authurl parameter provided")
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2015-05-29 14:12:58 +00:00
|
|
|
|
|
|
|
if params.Container == "" {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("no container parameter provided")
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 14:12:58 +00:00
|
|
|
if params.ChunkSize < minChunkSize {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("the chunksize %#v parameter should be a number that is larger than or equal to %d", params.ChunkSize, minChunkSize)
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return New(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a new Driver with the given Openstack Swift credentials and container name
|
2015-06-30 12:22:41 +00:00
|
|
|
func New(params Parameters) (*Driver, error) {
|
2015-06-08 08:37:11 +00:00
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
MaxIdleConnsPerHost: 2048,
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: params.InsecureSkipVerify},
|
|
|
|
}
|
|
|
|
|
2016-10-31 18:41:53 +00:00
|
|
|
ct := &swift.Connection{
|
2015-05-11 16:11:47 +00:00
|
|
|
UserName: params.Username,
|
|
|
|
ApiKey: params.Password,
|
|
|
|
AuthUrl: params.AuthURL,
|
|
|
|
Region: params.Region,
|
2016-04-13 15:32:10 +00:00
|
|
|
AuthVersion: params.AuthVersion,
|
2015-06-30 21:39:04 +00:00
|
|
|
UserAgent: "distribution/" + version.Version,
|
2015-05-11 16:11:47 +00:00
|
|
|
Tenant: params.Tenant,
|
2015-06-08 08:37:11 +00:00
|
|
|
TenantId: params.TenantID,
|
|
|
|
Domain: params.Domain,
|
|
|
|
DomainId: params.DomainID,
|
2016-08-15 09:21:16 +00:00
|
|
|
TenantDomain: params.TenantDomain,
|
|
|
|
TenantDomainId: params.TenantDomainID,
|
2015-08-24 15:08:33 +00:00
|
|
|
TrustId: params.TrustID,
|
2016-03-07 21:41:20 +00:00
|
|
|
EndpointType: swift.EndpointType(params.EndpointType),
|
2015-06-08 08:37:11 +00:00
|
|
|
Transport: transport,
|
2015-05-11 16:11:47 +00:00
|
|
|
ConnectTimeout: 60 * time.Second,
|
|
|
|
Timeout: 15 * 60 * time.Second,
|
|
|
|
}
|
|
|
|
err := ct.Authenticate()
|
|
|
|
if err != nil {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("swift authentication failed: %s", err)
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 01:31:47 +00:00
|
|
|
if _, _, err := ct.Container(params.Container); err == swift.ContainerNotFound {
|
|
|
|
if err := ct.ContainerCreate(params.Container, nil); err != nil {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("failed to create container %s (%s)", params.Container, err)
|
2015-09-03 01:31:47 +00:00
|
|
|
}
|
|
|
|
} else if err != nil {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("failed to retrieve info about container %s (%s)", params.Container, err)
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d := &driver{
|
2015-11-03 08:59:50 +00:00
|
|
|
Conn: ct,
|
|
|
|
Container: params.Container,
|
|
|
|
Prefix: params.Prefix,
|
|
|
|
ChunkSize: params.ChunkSize,
|
|
|
|
TempURLMethods: make([]string, 0),
|
|
|
|
AccessKey: params.AccessKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
info := swiftInfo{}
|
|
|
|
if config, err := d.Conn.QueryInfo(); err == nil {
|
|
|
|
_, d.BulkDeleteSupport = config["bulk_delete"]
|
|
|
|
|
|
|
|
if err := mapstructure.Decode(config, &info); err == nil {
|
|
|
|
d.TempURLContainerKey = info.Swift.Version >= "2.3.0"
|
|
|
|
d.TempURLMethods = info.Tempurl.Methods
|
2016-08-24 17:09:25 +00:00
|
|
|
if d.BulkDeleteSupport {
|
|
|
|
d.BulkDeleteMaxDeletes = info.BulkDelete.MaxDeletesPerRequest
|
|
|
|
}
|
2015-11-03 08:59:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d.TempURLContainerKey = params.TempURLContainerKey
|
|
|
|
d.TempURLMethods = params.TempURLMethods
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(d.TempURLMethods) > 0 {
|
|
|
|
secretKey := params.SecretKey
|
|
|
|
if secretKey == "" {
|
|
|
|
secretKey, _ = generateSecret()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since Swift 2.2.2, we can now set secret keys on containers
|
|
|
|
// in addition to the account secret keys. Use them in preference.
|
|
|
|
if d.TempURLContainerKey {
|
|
|
|
_, containerHeaders, err := d.Conn.Container(d.Container)
|
|
|
|
if err != nil {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("failed to fetch container info %s (%s)", d.Container, err)
|
2015-11-03 08:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SecretKey = containerHeaders["X-Container-Meta-Temp-Url-Key"]
|
|
|
|
if d.SecretKey == "" || (params.SecretKey != "" && d.SecretKey != params.SecretKey) {
|
|
|
|
m := swift.Metadata{}
|
|
|
|
m["temp-url-key"] = secretKey
|
|
|
|
if d.Conn.ContainerUpdate(d.Container, m.ContainerHeaders()); err == nil {
|
|
|
|
d.SecretKey = secretKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use the account secret key
|
|
|
|
_, accountHeaders, err := d.Conn.Account()
|
|
|
|
if err != nil {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("failed to fetch account info (%s)", err)
|
2015-11-03 08:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SecretKey = accountHeaders["X-Account-Meta-Temp-Url-Key"]
|
|
|
|
if d.SecretKey == "" || (params.SecretKey != "" && d.SecretKey != params.SecretKey) {
|
|
|
|
m := swift.Metadata{}
|
|
|
|
m["temp-url-key"] = secretKey
|
|
|
|
if err := d.Conn.AccountUpdate(m.AccountHeaders()); err == nil {
|
|
|
|
d.SecretKey = secretKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Driver{
|
|
|
|
baseEmbed: baseEmbed{
|
|
|
|
Base: base.Base{
|
|
|
|
StorageDriver: d,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement the storagedriver.StorageDriver interface
|
|
|
|
|
|
|
|
func (d *driver) Name() string {
|
|
|
|
return driverName
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
|
|
|
func (d *driver) GetContent(ctx context.Context, path string) ([]byte, error) {
|
|
|
|
content, err := d.Conn.ObjectGetBytes(d.Container, d.swiftPath(path))
|
2015-07-08 11:01:34 +00:00
|
|
|
if err == swift.ObjectNotFound {
|
2015-07-03 10:29:54 +00:00
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2016-03-30 14:04:01 +00:00
|
|
|
return content, err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
|
|
|
func (d *driver) PutContent(ctx context.Context, path string, contents []byte) error {
|
2016-03-08 23:57:12 +00:00
|
|
|
err := d.Conn.ObjectPutBytes(d.Container, d.swiftPath(path), contents, contentType)
|
2015-07-08 11:01:34 +00:00
|
|
|
if err == swift.ObjectNotFound {
|
2015-07-03 10:29:54 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 23:57:12 +00:00
|
|
|
// Reader retrieves an io.ReadCloser for the content stored at "path" with a
|
2015-05-11 16:11:47 +00:00
|
|
|
// given byte offset.
|
2016-03-08 23:57:12 +00:00
|
|
|
func (d *driver) Reader(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
|
2015-05-11 16:11:47 +00:00
|
|
|
headers := make(swift.Headers)
|
|
|
|
headers["Range"] = "bytes=" + strconv.FormatInt(offset, 10) + "-"
|
|
|
|
|
2016-05-06 11:14:21 +00:00
|
|
|
waitingTime := readAfterWriteWait
|
|
|
|
endTime := time.Now().Add(readAfterWriteTimeout)
|
|
|
|
|
|
|
|
for {
|
|
|
|
file, headers, err := d.Conn.ObjectOpen(d.Container, d.swiftPath(path), false, headers)
|
|
|
|
if err != nil {
|
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
if swiftErr, ok := err.(*swift.Error); ok && swiftErr.StatusCode == http.StatusRequestedRangeNotSatisfiable {
|
2022-11-02 21:55:22 +00:00
|
|
|
return io.NopCloser(bytes.NewReader(nil)), nil
|
2016-05-06 11:14:21 +00:00
|
|
|
}
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
// if this is a DLO and it is clear that segments are still missing,
|
|
|
|
// wait until they show up
|
2016-05-06 11:14:21 +00:00
|
|
|
_, isDLO := headers["X-Object-Manifest"]
|
|
|
|
size, err := file.Length()
|
|
|
|
if err != nil {
|
|
|
|
return file, err
|
|
|
|
}
|
|
|
|
if isDLO && size == 0 {
|
|
|
|
if time.Now().Add(waitingTime).After(endTime) {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("timeout expired while waiting for segments of %s to show up", path)
|
2016-05-06 11:14:21 +00:00
|
|
|
}
|
|
|
|
time.Sleep(waitingTime)
|
|
|
|
waitingTime *= 2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
// if not, then this reader will be fine
|
2016-05-06 11:14:21 +00:00
|
|
|
return file, nil
|
2015-07-03 10:29:54 +00:00
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 23:57:12 +00:00
|
|
|
// Writer returns a FileWriter which will store the content written to it
|
|
|
|
// at the location designated by "path" after the call to Commit.
|
|
|
|
func (d *driver) Writer(ctx context.Context, path string, append bool) (storagedriver.FileWriter, error) {
|
2015-05-11 16:11:47 +00:00
|
|
|
var (
|
2016-03-08 23:57:12 +00:00
|
|
|
segments []swift.Object
|
|
|
|
segmentsPath string
|
|
|
|
err error
|
2015-05-11 16:11:47 +00:00
|
|
|
)
|
|
|
|
|
2016-03-08 23:57:12 +00:00
|
|
|
if !append {
|
|
|
|
segmentsPath, err = d.swiftSegmentPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-08 23:57:12 +00:00
|
|
|
info, headers, err := d.Conn.Object(d.Container, d.swiftPath(path))
|
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2016-03-08 23:57:12 +00:00
|
|
|
manifest, ok := headers["X-Object-Manifest"]
|
|
|
|
if !ok {
|
|
|
|
segmentsPath, err = d.swiftSegmentPath(path)
|
2015-07-03 10:29:54 +00:00
|
|
|
if err != nil {
|
2016-03-08 23:57:12 +00:00
|
|
|
return nil, err
|
2015-07-03 10:29:54 +00:00
|
|
|
}
|
2016-04-05 14:46:39 +00:00
|
|
|
if err := d.Conn.ObjectMove(d.Container, d.swiftPath(path), d.Container, getSegmentPath(segmentsPath, len(segments))); err != nil {
|
2016-03-08 23:57:12 +00:00
|
|
|
return nil, err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2016-03-08 23:57:12 +00:00
|
|
|
segments = []swift.Object{info}
|
|
|
|
} else {
|
|
|
|
_, segmentsPath = parseManifest(manifest)
|
|
|
|
if segments, err = d.getAllSegments(segmentsPath); err != nil {
|
|
|
|
return nil, err
|
2015-10-29 11:24:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 23:57:12 +00:00
|
|
|
return d.newWriter(path, segmentsPath, segments), nil
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat retrieves the FileInfo for the given path, including the current size
|
|
|
|
// in bytes and the creation time.
|
|
|
|
func (d *driver) Stat(ctx context.Context, path string) (storagedriver.FileInfo, error) {
|
2015-07-02 16:25:27 +00:00
|
|
|
swiftPath := d.swiftPath(path)
|
|
|
|
opts := &swift.ObjectsOpts{
|
|
|
|
Prefix: swiftPath,
|
|
|
|
Delimiter: '/',
|
|
|
|
}
|
|
|
|
|
|
|
|
objects, err := d.Conn.ObjectsAll(d.Container, opts)
|
2015-05-11 16:11:47 +00:00
|
|
|
if err != nil {
|
2015-07-03 10:29:54 +00:00
|
|
|
if err == swift.ContainerNotFound {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
2015-07-02 16:25:27 +00:00
|
|
|
return nil, err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fi := storagedriver.FileInfoFields{
|
2015-07-02 16:25:27 +00:00
|
|
|
Path: strings.TrimPrefix(strings.TrimSuffix(swiftPath, "/"), d.swiftPath("/")),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, obj := range objects {
|
|
|
|
if obj.PseudoDirectory && obj.Name == swiftPath+"/" {
|
|
|
|
fi.IsDir = true
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
|
|
|
} else if obj.Name == swiftPath {
|
2016-04-05 14:46:39 +00:00
|
|
|
// The file exists. But on Swift 1.12, the 'bytes' field is always 0 so
|
|
|
|
// we need to do a separate HEAD request.
|
|
|
|
break
|
2015-07-02 16:25:27 +00:00
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
// Don't trust an empty `objects` slice. A container listing can be
|
|
|
|
// outdated. For files, we can make a HEAD request on the object which
|
|
|
|
// reports existence (at least) much more reliably.
|
2016-05-06 11:14:21 +00:00
|
|
|
waitingTime := readAfterWriteWait
|
|
|
|
endTime := time.Now().Add(readAfterWriteTimeout)
|
|
|
|
|
|
|
|
for {
|
|
|
|
info, headers, err := d.Conn.Object(d.Container, swiftPath)
|
|
|
|
if err != nil {
|
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return nil, err
|
2016-04-05 14:46:39 +00:00
|
|
|
}
|
2016-05-06 11:14:21 +00:00
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
// if this is a DLO and it is clear that segments are still missing,
|
|
|
|
// wait until they show up
|
2016-05-06 11:14:21 +00:00
|
|
|
_, isDLO := headers["X-Object-Manifest"]
|
|
|
|
if isDLO && info.Bytes == 0 {
|
|
|
|
if time.Now().Add(waitingTime).After(endTime) {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("timeout expired while waiting for segments of %s to show up", path)
|
2016-05-06 11:14:21 +00:00
|
|
|
}
|
|
|
|
time.Sleep(waitingTime)
|
|
|
|
waitingTime *= 2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
// otherwise, accept the result
|
2016-05-06 11:14:21 +00:00
|
|
|
fi.IsDir = false
|
|
|
|
fi.Size = info.Bytes
|
|
|
|
fi.ModTime = info.LastModified
|
|
|
|
return storagedriver.FileInfoInternal{FileInfoFields: fi}, nil
|
2016-04-05 14:46:39 +00:00
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List returns a list of the objects that are direct descendants of the given path.
|
|
|
|
func (d *driver) List(ctx context.Context, path string) ([]string, error) {
|
2015-06-04 08:11:19 +00:00
|
|
|
var files []string
|
|
|
|
|
2015-05-11 16:11:47 +00:00
|
|
|
prefix := d.swiftPath(path)
|
|
|
|
if prefix != "" {
|
|
|
|
prefix += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &swift.ObjectsOpts{
|
2015-06-04 08:11:19 +00:00
|
|
|
Prefix: prefix,
|
2015-05-11 16:11:47 +00:00
|
|
|
Delimiter: '/',
|
|
|
|
}
|
|
|
|
|
2015-07-02 16:27:13 +00:00
|
|
|
objects, err := d.Conn.ObjectsAll(d.Container, opts)
|
2015-06-04 08:11:19 +00:00
|
|
|
for _, obj := range objects {
|
2015-07-02 16:25:27 +00:00
|
|
|
files = append(files, strings.TrimPrefix(strings.TrimSuffix(obj.Name, "/"), d.swiftPath("/")))
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 02:30:14 +00:00
|
|
|
if err == swift.ContainerNotFound || (len(objects) == 0 && path != "/") {
|
2015-07-03 10:29:54 +00:00
|
|
|
return files, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return files, err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the original
|
|
|
|
// object.
|
|
|
|
func (d *driver) Move(ctx context.Context, sourcePath string, destPath string) error {
|
2015-07-08 10:59:29 +00:00
|
|
|
_, headers, err := d.Conn.Object(d.Container, d.swiftPath(sourcePath))
|
|
|
|
if err == nil {
|
|
|
|
if manifest, ok := headers["X-Object-Manifest"]; ok {
|
|
|
|
if err = d.createManifest(destPath, manifest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = d.Conn.ObjectDelete(d.Container, d.swiftPath(sourcePath))
|
|
|
|
} else {
|
|
|
|
err = d.Conn.ObjectMove(d.Container, d.swiftPath(sourcePath), d.Container, d.swiftPath(destPath))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == swift.ObjectNotFound {
|
2015-07-03 10:29:54 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: sourcePath}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2015-07-03 10:29:54 +00:00
|
|
|
return err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
|
|
|
func (d *driver) Delete(ctx context.Context, path string) error {
|
|
|
|
opts := swift.ObjectsOpts{
|
2015-07-02 08:59:14 +00:00
|
|
|
Prefix: d.swiftPath(path) + "/",
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 16:27:13 +00:00
|
|
|
objects, err := d.Conn.ObjectsAll(d.Container, &opts)
|
2015-05-11 16:11:47 +00:00
|
|
|
if err != nil {
|
2015-07-03 10:29:54 +00:00
|
|
|
if err == swift.ContainerNotFound {
|
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 08:59:14 +00:00
|
|
|
for _, obj := range objects {
|
|
|
|
if obj.PseudoDirectory {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if _, headers, err := d.Conn.Object(d.Container, obj.Name); err == nil {
|
2015-06-19 14:44:55 +00:00
|
|
|
manifest, ok := headers["X-Object-Manifest"]
|
|
|
|
if ok {
|
2015-10-30 16:08:56 +00:00
|
|
|
_, prefix := parseManifest(manifest)
|
2015-07-08 10:59:29 +00:00
|
|
|
segments, err := d.getAllSegments(prefix)
|
2015-06-19 14:44:55 +00:00
|
|
|
if err != nil {
|
2015-07-03 10:29:54 +00:00
|
|
|
return err
|
2015-06-19 14:44:55 +00:00
|
|
|
}
|
2015-10-30 16:08:56 +00:00
|
|
|
objects = append(objects, segments...)
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2015-06-19 14:44:55 +00:00
|
|
|
} else {
|
2015-07-08 11:01:34 +00:00
|
|
|
if err == swift.ObjectNotFound {
|
2015-07-03 10:29:54 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: obj.Name}
|
|
|
|
}
|
|
|
|
return err
|
2015-06-19 14:44:55 +00:00
|
|
|
}
|
2015-10-30 16:08:56 +00:00
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
|
2016-08-24 17:09:25 +00:00
|
|
|
if d.BulkDeleteSupport && len(objects) > 0 && d.BulkDeleteMaxDeletes > 0 {
|
2015-10-30 16:08:56 +00:00
|
|
|
filenames := make([]string, len(objects))
|
|
|
|
for i, obj := range objects {
|
|
|
|
filenames[i] = obj.Name
|
|
|
|
}
|
2016-08-24 17:09:25 +00:00
|
|
|
|
|
|
|
chunks, err := chunkFilenames(filenames, d.BulkDeleteMaxDeletes)
|
|
|
|
if err != nil {
|
2015-07-03 10:29:54 +00:00
|
|
|
return err
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
2016-08-24 17:09:25 +00:00
|
|
|
for _, chunk := range chunks {
|
|
|
|
_, err := d.Conn.BulkDelete(d.Container, chunk)
|
|
|
|
// Don't fail on ObjectNotFound because eventual consistency
|
|
|
|
// makes this situation normal.
|
|
|
|
if err != nil && err != swift.Forbidden && err != swift.ObjectNotFound {
|
|
|
|
if err == swift.ContainerNotFound {
|
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 16:08:56 +00:00
|
|
|
} else {
|
|
|
|
for _, obj := range objects {
|
|
|
|
if err := d.Conn.ObjectDelete(d.Container, obj.Name); err != nil {
|
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return storagedriver.PathNotFoundError{Path: obj.Name}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 10:29:54 +00:00
|
|
|
_, _, err = d.Conn.Object(d.Container, d.swiftPath(path))
|
|
|
|
if err == nil {
|
|
|
|
if err := d.Conn.ObjectDelete(d.Container, d.swiftPath(path)); err != nil {
|
2015-07-08 11:01:34 +00:00
|
|
|
if err == swift.ObjectNotFound {
|
2015-07-03 10:29:54 +00:00
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2015-07-08 11:01:34 +00:00
|
|
|
} else if err == swift.ObjectNotFound {
|
|
|
|
if len(objects) == 0 {
|
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
2015-07-02 08:59:14 +00:00
|
|
|
}
|
2015-05-11 16:11:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// URLFor returns a URL which may be used to retrieve the content stored at the given path.
|
|
|
|
func (d *driver) URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error) {
|
2015-11-03 08:59:50 +00:00
|
|
|
if d.SecretKey == "" {
|
2015-10-02 23:19:06 +00:00
|
|
|
return "", storagedriver.ErrUnsupportedMethod{}
|
2015-11-03 08:59:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
methodString := http.MethodGet
|
2015-11-03 08:59:50 +00:00
|
|
|
method, ok := options["method"]
|
|
|
|
if ok {
|
|
|
|
if methodString, ok = method.(string); !ok {
|
2015-10-02 23:19:06 +00:00
|
|
|
return "", storagedriver.ErrUnsupportedMethod{}
|
2015-11-03 08:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 22:31:23 +00:00
|
|
|
if methodString == http.MethodHead {
|
2015-11-03 08:59:50 +00:00
|
|
|
// A "HEAD" request on a temporary URL is allowed if the
|
|
|
|
// signature was generated with "GET", "POST" or "PUT"
|
2022-11-02 22:31:23 +00:00
|
|
|
methodString = http.MethodGet
|
2015-11-03 08:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
supported := false
|
|
|
|
for _, method := range d.TempURLMethods {
|
|
|
|
if method == methodString {
|
|
|
|
supported = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !supported {
|
2015-10-02 23:19:06 +00:00
|
|
|
return "", storagedriver.ErrUnsupportedMethod{}
|
2015-11-03 08:59:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expiresTime := time.Now().Add(20 * time.Minute)
|
|
|
|
expires, ok := options["expiry"]
|
|
|
|
if ok {
|
|
|
|
et, ok := expires.(time.Time)
|
|
|
|
if ok {
|
|
|
|
expiresTime = et
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tempURL := d.Conn.ObjectTempUrl(d.Container, d.swiftPath(path), d.SecretKey, methodString, expiresTime)
|
|
|
|
|
|
|
|
if d.AccessKey != "" {
|
|
|
|
// On HP Cloud, the signature must be in the form of tenant_id:access_key:signature
|
|
|
|
url, _ := url.Parse(tempURL)
|
|
|
|
query := url.Query()
|
|
|
|
query.Set("temp_url_sig", fmt.Sprintf("%s:%s:%s", d.Conn.TenantId, d.AccessKey, query.Get("temp_url_sig")))
|
|
|
|
url.RawQuery = query.Encode()
|
|
|
|
tempURL = url.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return tempURL, nil
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 19:17:39 +00:00
|
|
|
// Walk traverses a filesystem defined within driver, starting
|
2021-06-24 18:42:02 +00:00
|
|
|
// from the given path, calling f on each file and directory
|
2017-11-29 19:17:39 +00:00
|
|
|
func (d *driver) Walk(ctx context.Context, path string, f storagedriver.WalkFn) error {
|
|
|
|
return storagedriver.WalkFallback(ctx, d, path, f)
|
|
|
|
}
|
|
|
|
|
2015-05-11 16:11:47 +00:00
|
|
|
func (d *driver) swiftPath(path string) string {
|
2015-06-19 14:44:55 +00:00
|
|
|
return strings.TrimLeft(strings.TrimRight(d.Prefix+"/files"+path, "/"), "/")
|
|
|
|
}
|
|
|
|
|
2019-06-23 06:44:43 +00:00
|
|
|
// swiftSegmentPath returns a randomly generated path in the segments directory.
|
2015-07-08 10:59:29 +00:00
|
|
|
func (d *driver) swiftSegmentPath(path string) (string, error) {
|
|
|
|
checksum := sha1.New()
|
2019-06-23 06:44:43 +00:00
|
|
|
checksum.Write([]byte(path))
|
|
|
|
|
|
|
|
if _, err := io.CopyN(checksum, rand.Reader, 32); err != nil {
|
2015-07-08 10:59:29 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2019-06-23 06:44:43 +00:00
|
|
|
|
|
|
|
path = hex.EncodeToString(checksum.Sum(nil))
|
2015-07-08 10:59:29 +00:00
|
|
|
return strings.TrimLeft(strings.TrimRight(d.Prefix+"/segments/"+path[0:3]+"/"+path[3:], "/"), "/"), nil
|
2015-05-11 16:11:47 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 14:44:55 +00:00
|
|
|
func (d *driver) getAllSegments(path string) ([]swift.Object, error) {
|
2022-11-02 21:05:45 +00:00
|
|
|
// a simple container listing works 99.9% of the time
|
2015-07-08 10:59:29 +00:00
|
|
|
segments, err := d.Conn.ObjectsAll(d.Container, &swift.ObjectsOpts{Prefix: path})
|
2016-04-05 14:46:39 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == swift.ContainerNotFound {
|
|
|
|
return nil, storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
// build a lookup table by object name
|
2016-04-05 14:46:39 +00:00
|
|
|
hasObjectName := make(map[string]struct{})
|
|
|
|
for _, segment := range segments {
|
|
|
|
hasObjectName[segment.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
// The container listing might be outdated (i.e. not contain all existing
|
|
|
|
// segment objects yet) because of temporary inconsistency (Swift is only
|
|
|
|
// eventually consistent!). Check its completeness.
|
2016-04-05 14:46:39 +00:00
|
|
|
segmentNumber := 0
|
|
|
|
for {
|
|
|
|
segmentNumber++
|
|
|
|
segmentPath := getSegmentPath(path, segmentNumber)
|
|
|
|
|
|
|
|
if _, seen := hasObjectName[segmentPath]; seen {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-02 21:05:45 +00:00
|
|
|
// This segment is missing in the container listing. Use a more reliable
|
|
|
|
// request to check its existence. (HEAD requests on segments are
|
|
|
|
// guaranteed to return the correct metadata, except for the pathological
|
|
|
|
// case of an outage of large parts of the Swift cluster or its network,
|
|
|
|
// since every segment is only written once.)
|
2016-04-05 14:46:39 +00:00
|
|
|
segment, _, err := d.Conn.Object(d.Container, segmentPath)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2022-11-02 21:05:45 +00:00
|
|
|
// found new segment -> keep going, more might be missing
|
2016-04-05 14:46:39 +00:00
|
|
|
segments = append(segments, segment)
|
|
|
|
continue
|
|
|
|
case swift.ObjectNotFound:
|
2022-11-02 21:05:45 +00:00
|
|
|
// This segment is missing. Since we upload segments sequentially,
|
|
|
|
// there won't be any more segments after it.
|
2016-04-05 14:46:39 +00:00
|
|
|
return segments, nil
|
|
|
|
default:
|
2022-11-02 21:05:45 +00:00
|
|
|
return nil, err // unexpected error
|
2016-04-05 14:46:39 +00:00
|
|
|
}
|
2015-07-03 10:29:54 +00:00
|
|
|
}
|
2015-05-29 14:09:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 10:59:29 +00:00
|
|
|
func (d *driver) createManifest(path string, segments string) error {
|
2015-05-29 14:09:05 +00:00
|
|
|
headers := make(swift.Headers)
|
2015-07-08 10:59:29 +00:00
|
|
|
headers["X-Object-Manifest"] = segments
|
2016-03-08 23:57:12 +00:00
|
|
|
manifest, err := d.Conn.ObjectCreate(d.Container, d.swiftPath(path), false, "", contentType, headers)
|
2015-07-08 10:59:29 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return err
|
2015-07-03 10:29:54 +00:00
|
|
|
}
|
2015-07-08 10:59:29 +00:00
|
|
|
if err := manifest.Close(); err != nil {
|
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return storagedriver.PathNotFoundError{Path: path}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-05-29 14:09:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 17:09:25 +00:00
|
|
|
func chunkFilenames(slice []string, maxSize int) (chunks [][]string, err error) {
|
|
|
|
if maxSize > 0 {
|
|
|
|
for offset := 0; offset < len(slice); offset += maxSize {
|
|
|
|
chunkSize := maxSize
|
|
|
|
if offset+chunkSize > len(slice) {
|
|
|
|
chunkSize = len(slice) - offset
|
|
|
|
}
|
|
|
|
chunks = append(chunks, slice[offset:offset+chunkSize])
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-05 00:01:04 +00:00
|
|
|
return nil, fmt.Errorf("max chunk size must be > 0")
|
2016-08-24 17:09:25 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-07-08 10:59:29 +00:00
|
|
|
func parseManifest(manifest string) (container string, prefix string) {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
container, prefix, _ = strings.Cut(manifest, "/")
|
2015-07-08 10:59:29 +00:00
|
|
|
return container, prefix
|
|
|
|
}
|
2015-11-03 08:59:50 +00:00
|
|
|
|
|
|
|
func generateSecret() (string, error) {
|
|
|
|
var secretBytes [32]byte
|
|
|
|
if _, err := rand.Read(secretBytes[:]); err != nil {
|
|
|
|
return "", fmt.Errorf("could not generate random bytes for Swift secret key: %v", err)
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(secretBytes[:]), nil
|
|
|
|
}
|
2016-03-08 23:57:12 +00:00
|
|
|
|
2016-04-05 14:46:39 +00:00
|
|
|
func getSegmentPath(segmentsPath string, partNumber int) string {
|
2016-03-08 23:57:12 +00:00
|
|
|
return fmt.Sprintf("%s/%016d", segmentsPath, partNumber)
|
|
|
|
}
|
|
|
|
|
|
|
|
type writer struct {
|
|
|
|
driver *driver
|
|
|
|
path string
|
|
|
|
segmentsPath string
|
|
|
|
size int64
|
|
|
|
bw *bufio.Writer
|
|
|
|
closed bool
|
|
|
|
committed bool
|
|
|
|
cancelled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driver) newWriter(path, segmentsPath string, segments []swift.Object) storagedriver.FileWriter {
|
|
|
|
var size int64
|
|
|
|
for _, segment := range segments {
|
|
|
|
size += segment.Bytes
|
|
|
|
}
|
|
|
|
return &writer{
|
|
|
|
driver: d,
|
|
|
|
path: path,
|
|
|
|
segmentsPath: segmentsPath,
|
|
|
|
size: size,
|
|
|
|
bw: bufio.NewWriterSize(&segmentWriter{
|
|
|
|
conn: d.Conn,
|
|
|
|
container: d.Container,
|
|
|
|
segmentsPath: segmentsPath,
|
|
|
|
segmentNumber: len(segments) + 1,
|
|
|
|
maxChunkSize: d.ChunkSize,
|
|
|
|
}, d.ChunkSize),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Write(p []byte) (int, error) {
|
|
|
|
if w.closed {
|
|
|
|
return 0, fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return 0, fmt.Errorf("already committed")
|
|
|
|
} else if w.cancelled {
|
|
|
|
return 0, fmt.Errorf("already cancelled")
|
|
|
|
}
|
|
|
|
|
|
|
|
n, err := w.bw.Write(p)
|
|
|
|
w.size += int64(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Size() int64 {
|
|
|
|
return w.size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Close() error {
|
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.bw.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !w.committed && !w.cancelled {
|
|
|
|
if err := w.driver.createManifest(w.path, w.driver.Container+"/"+w.segmentsPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-19 11:48:08 +00:00
|
|
|
if err := w.waitForSegmentsToShowUp(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-08 23:57:12 +00:00
|
|
|
}
|
|
|
|
w.closed = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Cancel() error {
|
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return fmt.Errorf("already committed")
|
|
|
|
}
|
|
|
|
w.cancelled = true
|
|
|
|
return w.driver.Delete(context.Background(), w.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *writer) Commit() error {
|
|
|
|
if w.closed {
|
|
|
|
return fmt.Errorf("already closed")
|
|
|
|
} else if w.committed {
|
|
|
|
return fmt.Errorf("already committed")
|
|
|
|
} else if w.cancelled {
|
|
|
|
return fmt.Errorf("already cancelled")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.bw.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.driver.createManifest(w.path, w.driver.Container+"/"+w.segmentsPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.committed = true
|
2016-04-19 11:48:08 +00:00
|
|
|
return w.waitForSegmentsToShowUp()
|
|
|
|
}
|
2016-03-08 23:57:12 +00:00
|
|
|
|
2016-04-19 11:48:08 +00:00
|
|
|
func (w *writer) waitForSegmentsToShowUp() error {
|
2016-03-08 23:57:12 +00:00
|
|
|
var err error
|
|
|
|
waitingTime := readAfterWriteWait
|
|
|
|
endTime := time.Now().Add(readAfterWriteTimeout)
|
2016-04-19 11:48:08 +00:00
|
|
|
|
2016-03-08 23:57:12 +00:00
|
|
|
for {
|
|
|
|
var info swift.Object
|
|
|
|
if info, _, err = w.driver.Conn.Object(w.driver.Container, w.driver.swiftPath(w.path)); err == nil {
|
|
|
|
if info.Bytes == w.size {
|
|
|
|
break
|
|
|
|
}
|
2019-02-05 00:01:04 +00:00
|
|
|
err = fmt.Errorf("timeout expired while waiting for segments of %s to show up", w.path)
|
2016-03-08 23:57:12 +00:00
|
|
|
}
|
|
|
|
if time.Now().Add(waitingTime).After(endTime) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(waitingTime)
|
|
|
|
waitingTime *= 2
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type segmentWriter struct {
|
2016-10-31 18:41:53 +00:00
|
|
|
conn *swift.Connection
|
2016-03-08 23:57:12 +00:00
|
|
|
container string
|
|
|
|
segmentsPath string
|
|
|
|
segmentNumber int
|
|
|
|
maxChunkSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sw *segmentWriter) Write(p []byte) (int, error) {
|
|
|
|
n := 0
|
|
|
|
for offset := 0; offset < len(p); offset += sw.maxChunkSize {
|
|
|
|
chunkSize := sw.maxChunkSize
|
|
|
|
if offset+chunkSize > len(p) {
|
|
|
|
chunkSize = len(p) - offset
|
|
|
|
}
|
2016-04-05 14:46:39 +00:00
|
|
|
_, err := sw.conn.ObjectPut(sw.container, getSegmentPath(sw.segmentsPath, sw.segmentNumber), bytes.NewReader(p[offset:offset+chunkSize]), false, "", contentType, nil)
|
2016-03-08 23:57:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sw.segmentNumber++
|
|
|
|
n += chunkSize
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
}
|