2015-09-22 19:47:16 +02:00
|
|
|
// Package swift provides an interface to the Swift object storage system
|
2013-06-27 21:13:07 +02:00
|
|
|
package swift
|
2012-12-26 13:23:58 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-14 07:42:16 +02:00
|
|
|
"bufio"
|
2015-09-23 08:57:48 +02:00
|
|
|
"bytes"
|
2012-12-26 13:23:58 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-05-05 20:52:52 +02:00
|
|
|
"path"
|
2012-12-29 12:35:41 +01:00
|
|
|
"regexp"
|
2015-10-03 16:38:35 +02:00
|
|
|
"strconv"
|
2012-12-26 13:23:58 +01:00
|
|
|
"strings"
|
2017-06-07 15:16:50 +02:00
|
|
|
"sync"
|
2012-12-26 13:23:58 +01:00
|
|
|
"time"
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/swift"
|
2016-06-12 16:06:02 +02:00
|
|
|
"github.com/pkg/errors"
|
2015-09-23 08:57:48 +02:00
|
|
|
)
|
|
|
|
|
2015-11-07 16:17:46 +01:00
|
|
|
// Constants
|
|
|
|
const (
|
|
|
|
directoryMarkerContentType = "application/directory" // content type of directory marker objects
|
2017-06-06 16:03:52 +02:00
|
|
|
listChunks = 1000 // chunk size to read directory listings
|
2015-11-07 16:17:46 +01:00
|
|
|
)
|
|
|
|
|
2015-09-23 08:57:48 +02:00
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
chunkSize = fs.SizeSuffix(5 * 1024 * 1024 * 1024)
|
2014-03-15 17:06:11 +01:00
|
|
|
)
|
2013-06-27 21:13:07 +02:00
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2016-02-18 12:35:25 +01:00
|
|
|
fs.Register(&fs.RegInfo{
|
2016-02-15 19:11:53 +01:00
|
|
|
Name: "swift",
|
|
|
|
Description: "Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)",
|
|
|
|
NewFs: NewFs,
|
2014-03-15 17:06:11 +01:00
|
|
|
Options: []fs.Option{{
|
2017-08-10 22:38:45 +02:00
|
|
|
Name: "env_auth",
|
|
|
|
Help: "Get swift credentials from environment variables in standard OpenStack form.",
|
|
|
|
Examples: []fs.OptionExample{
|
|
|
|
{
|
|
|
|
Value: "false",
|
|
|
|
Help: "Enter swift credentials in the next step",
|
|
|
|
}, {
|
|
|
|
Value: "true",
|
|
|
|
Help: "Get swift credentials from environment vars. Leave other fields blank if using this.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, {
|
2014-03-15 17:06:11 +01:00
|
|
|
Name: "user",
|
|
|
|
Help: "User name to log in.",
|
|
|
|
}, {
|
|
|
|
Name: "key",
|
|
|
|
Help: "API key or password.",
|
|
|
|
}, {
|
|
|
|
Name: "auth",
|
|
|
|
Help: "Authentication URL for server.",
|
|
|
|
Examples: []fs.OptionExample{{
|
|
|
|
Help: "Rackspace US",
|
|
|
|
Value: "https://auth.api.rackspacecloud.com/v1.0",
|
|
|
|
}, {
|
|
|
|
Help: "Rackspace UK",
|
|
|
|
Value: "https://lon.auth.api.rackspacecloud.com/v1.0",
|
|
|
|
}, {
|
|
|
|
Help: "Rackspace v2",
|
|
|
|
Value: "https://identity.api.rackspacecloud.com/v2.0",
|
|
|
|
}, {
|
|
|
|
Help: "Memset Memstore UK",
|
|
|
|
Value: "https://auth.storage.memset.com/v1.0",
|
|
|
|
}, {
|
|
|
|
Help: "Memset Memstore UK v2",
|
|
|
|
Value: "https://auth.storage.memset.com/v2.0",
|
2016-01-07 14:15:10 +01:00
|
|
|
}, {
|
|
|
|
Help: "OVH",
|
|
|
|
Value: "https://auth.cloud.ovh.net/v2.0",
|
2014-03-15 17:06:11 +01:00
|
|
|
}},
|
2016-04-21 01:00:49 +02:00
|
|
|
}, {
|
|
|
|
Name: "domain",
|
|
|
|
Help: "User domain - optional (v3 auth)",
|
2014-11-24 13:05:10 +01:00
|
|
|
}, {
|
|
|
|
Name: "tenant",
|
2016-07-15 19:25:30 +02:00
|
|
|
Help: "Tenant name - optional for v1 auth, required otherwise",
|
2016-07-05 18:34:22 +02:00
|
|
|
}, {
|
|
|
|
Name: "tenant_domain",
|
|
|
|
Help: "Tenant domain - optional (v3 auth)",
|
2015-03-04 18:09:53 +01:00
|
|
|
}, {
|
|
|
|
Name: "region",
|
|
|
|
Help: "Region name - optional",
|
2016-02-02 20:45:05 +01:00
|
|
|
}, {
|
|
|
|
Name: "storage_url",
|
|
|
|
Help: "Storage URL - optional",
|
2016-06-03 18:52:02 +02:00
|
|
|
}, {
|
|
|
|
Name: "auth_version",
|
|
|
|
Help: "AuthVersion - optional - set to (1,2,3) if your auth URL has no version",
|
2017-08-10 22:38:45 +02:00
|
|
|
}, {
|
|
|
|
Name: "endpoint_type",
|
|
|
|
Help: "Endpoint type to choose from the service catalogue",
|
|
|
|
Examples: []fs.OptionExample{{
|
|
|
|
Help: "Public (default, choose this if not sure)",
|
|
|
|
Value: "public",
|
|
|
|
}, {
|
|
|
|
Help: "Internal (use internal service net)",
|
|
|
|
Value: "internal",
|
|
|
|
}, {
|
|
|
|
Help: "Admin",
|
|
|
|
Value: "admin",
|
|
|
|
}},
|
2014-03-15 17:06:11 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2016-12-20 16:50:46 +01:00
|
|
|
fs.VarP(&chunkSize, "swift-chunk-size", "", "Above this size files will be chunked into a _segments container.")
|
2013-06-27 21:13:07 +02:00
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// Fs represents a remote swift server
|
|
|
|
type Fs struct {
|
2016-03-23 15:15:38 +01:00
|
|
|
name string // name of this remote
|
2017-01-13 18:21:47 +01:00
|
|
|
root string // the path we are working on if any
|
|
|
|
features *fs.Features // optional features
|
2016-03-23 15:15:38 +01:00
|
|
|
c *swift.Connection // the connection to the swift server
|
|
|
|
container string // the container we are working on
|
2017-06-07 15:16:50 +02:00
|
|
|
containerOKMu sync.Mutex // mutex to protect container OK
|
|
|
|
containerOK bool // true if we have created the container
|
2016-03-23 15:15:38 +01:00
|
|
|
segmentsContainer string // container to store the segments (if any) in
|
2017-08-30 16:54:49 +02:00
|
|
|
noCheckContainer bool // don't check the container before creating it
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// Object describes a swift object
|
2012-12-26 13:23:58 +01:00
|
|
|
//
|
|
|
|
// Will definitely have info but maybe not meta
|
2015-11-07 12:14:46 +01:00
|
|
|
type Object struct {
|
|
|
|
fs *Fs // what this object is part of
|
2015-09-24 18:41:16 +02:00
|
|
|
remote string // The remote path
|
|
|
|
info swift.Object // Info from the swift object if known
|
|
|
|
headers *swift.Headers // The object headers if known
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Name of the remote (as passed into NewFs)
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Name() string {
|
2015-08-22 17:53:11 +02:00
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Root of the remote (as passed into NewFs)
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Root() string {
|
2015-09-01 21:45:27 +02:00
|
|
|
if f.root == "" {
|
|
|
|
return f.container
|
|
|
|
}
|
|
|
|
return f.container + "/" + f.root
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// String converts this Fs to a string
|
|
|
|
func (f *Fs) String() string {
|
2014-05-05 18:30:55 +02:00
|
|
|
if f.root == "" {
|
|
|
|
return fmt.Sprintf("Swift container %s", f.container)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Swift container %s path %s", f.container, f.root)
|
2012-12-31 17:40:34 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 18:21:47 +01:00
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
return f.features
|
|
|
|
}
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// Pattern to match a swift path
|
|
|
|
var matcher = regexp.MustCompile(`^([^/]*)(.*)$`)
|
|
|
|
|
2012-12-29 12:35:41 +01:00
|
|
|
// parseParse parses a swift 'url'
|
|
|
|
func parsePath(path string) (container, directory string, err error) {
|
2014-03-15 17:06:11 +01:00
|
|
|
parts := matcher.FindStringSubmatch(path)
|
|
|
|
if parts == nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
err = errors.Errorf("couldn't find container in swift path %q", path)
|
2012-12-29 12:35:41 +01:00
|
|
|
} else {
|
2014-03-15 17:06:11 +01:00
|
|
|
container, directory = parts[1], parts[2]
|
2012-12-29 12:35:41 +01:00
|
|
|
directory = strings.Trim(directory, "/")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// swiftConnection makes a connection to swift
|
2014-03-15 17:06:11 +01:00
|
|
|
func swiftConnection(name string) (*swift.Connection, error) {
|
2012-12-29 12:35:41 +01:00
|
|
|
c := &swift.Connection{
|
2017-08-10 22:38:45 +02:00
|
|
|
UserName: fs.ConfigFileGet(name, "user"),
|
|
|
|
ApiKey: fs.ConfigFileGet(name, "key"),
|
|
|
|
AuthUrl: fs.ConfigFileGet(name, "auth"),
|
2016-12-20 19:03:09 +01:00
|
|
|
AuthVersion: fs.ConfigFileGetInt(name, "auth_version", 0),
|
|
|
|
Tenant: fs.ConfigFileGet(name, "tenant"),
|
|
|
|
Region: fs.ConfigFileGet(name, "region"),
|
|
|
|
Domain: fs.ConfigFileGet(name, "domain"),
|
|
|
|
TenantDomain: fs.ConfigFileGet(name, "tenant_domain"),
|
2017-08-10 22:38:45 +02:00
|
|
|
EndpointType: swift.EndpointType(fs.ConfigFileGet(name, "endpoint_type", "public")),
|
2015-05-10 12:25:54 +02:00
|
|
|
ConnectTimeout: 10 * fs.Config.ConnectTimeout, // Use the timeouts in the transport
|
|
|
|
Timeout: 10 * fs.Config.Timeout, // Use the timeouts in the transport
|
|
|
|
Transport: fs.Config.Transport(),
|
2012-12-29 12:35:41 +01:00
|
|
|
}
|
2017-08-10 22:38:45 +02:00
|
|
|
if fs.ConfigFileGetBool(name, "env_auth", false) {
|
|
|
|
err := c.ApplyEnvironment()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read environment variables")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.UserName == "" {
|
|
|
|
return nil, errors.New("user not found")
|
|
|
|
}
|
|
|
|
if c.ApiKey == "" {
|
|
|
|
return nil, errors.New("key not found")
|
|
|
|
}
|
|
|
|
if c.AuthUrl == "" {
|
|
|
|
return nil, errors.New("auth not found")
|
|
|
|
}
|
2012-12-29 12:35:41 +01:00
|
|
|
err := c.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-11-08 16:29:58 +01:00
|
|
|
// NewFsWithConnection contstructs an Fs from the path, container:path
|
2017-08-30 16:54:49 +02:00
|
|
|
// and authenticated connection.
|
|
|
|
//
|
|
|
|
// if noCheckContainer is set then the Fs won't check the container
|
|
|
|
// exists before creating it.
|
|
|
|
func NewFsWithConnection(name, root string, c *swift.Connection, noCheckContainer bool) (fs.Fs, error) {
|
2014-05-05 20:52:52 +02:00
|
|
|
container, directory, err := parsePath(root)
|
2012-12-29 12:35:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-07 12:14:46 +01:00
|
|
|
f := &Fs{
|
2015-09-26 13:51:05 +02:00
|
|
|
name: name,
|
2016-03-23 15:15:38 +01:00
|
|
|
c: c,
|
2015-09-26 13:51:05 +02:00
|
|
|
container: container,
|
|
|
|
segmentsContainer: container + "_segments",
|
|
|
|
root: directory,
|
2017-08-30 16:54:49 +02:00
|
|
|
noCheckContainer: noCheckContainer,
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
2017-08-09 16:27:43 +02:00
|
|
|
f.features = (&fs.Features{
|
|
|
|
ReadMimeType: true,
|
|
|
|
WriteMimeType: true,
|
|
|
|
BucketBased: true,
|
|
|
|
}).Fill(f)
|
2016-02-02 20:45:05 +01:00
|
|
|
// StorageURL overloading
|
2016-12-20 19:03:09 +01:00
|
|
|
storageURL := fs.ConfigFileGet(name, "storage_url")
|
2016-02-02 20:45:05 +01:00
|
|
|
if storageURL != "" {
|
|
|
|
f.c.StorageUrl = storageURL
|
|
|
|
f.c.Auth = newAuth(f.c.Auth, storageURL)
|
|
|
|
}
|
2014-05-05 20:52:52 +02:00
|
|
|
if f.root != "" {
|
|
|
|
f.root += "/"
|
2015-11-11 18:19:57 +01:00
|
|
|
// Check to see if the object exists - ignoring directory markers
|
|
|
|
info, _, err := f.c.Object(container, directory)
|
|
|
|
if err == nil && info.ContentType != directoryMarkerContentType {
|
2014-05-05 20:52:52 +02:00
|
|
|
f.root = path.Dir(directory)
|
|
|
|
if f.root == "." {
|
|
|
|
f.root = ""
|
|
|
|
} else {
|
|
|
|
f.root += "/"
|
|
|
|
}
|
2016-06-21 19:01:53 +02:00
|
|
|
// return an error with an fs which points to the parent
|
|
|
|
return f, fs.ErrorIsFile
|
2014-05-05 20:52:52 +02:00
|
|
|
}
|
|
|
|
}
|
2012-12-29 12:35:41 +01:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2015-11-08 16:29:58 +01:00
|
|
|
// NewFs contstructs an Fs from the path, container:path
|
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
|
|
|
c, err := swiftConnection(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-30 16:54:49 +02:00
|
|
|
return NewFsWithConnection(name, root, c, false)
|
2015-11-08 16:29:58 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// Return an Object from a path
|
2012-12-26 13:23:58 +01:00
|
|
|
//
|
2016-06-25 22:23:20 +02:00
|
|
|
// If it can't be found it returns the error fs.ErrorObjectNotFound.
|
|
|
|
func (f *Fs) newObjectWithInfo(remote string, info *swift.Object) (fs.Object, error) {
|
2015-11-07 12:14:46 +01:00
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
2012-12-26 13:23:58 +01:00
|
|
|
remote: remote,
|
|
|
|
}
|
2016-03-17 18:36:20 +01:00
|
|
|
// Note that due to a quirk of swift, dynamic large objects are
|
2015-12-02 22:23:56 +01:00
|
|
|
// returned as 0 bytes in the listing. Correct this here by
|
|
|
|
// making sure we read the full metadata for all 0 byte files.
|
2016-09-08 17:44:11 +02:00
|
|
|
// We don't read the metadata for directory marker objects.
|
|
|
|
if info != nil && info.Bytes == 0 && info.ContentType != "application/directory" {
|
2015-12-02 22:23:56 +01:00
|
|
|
info = nil
|
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
if info != nil {
|
2015-09-24 18:41:16 +02:00
|
|
|
// Set info but not headers
|
2015-10-03 16:30:31 +02:00
|
|
|
o.info = *info
|
2012-12-26 13:23:58 +01:00
|
|
|
} else {
|
2015-10-03 16:30:31 +02:00
|
|
|
err := o.readMetaData() // reads info and headers, returning an error
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2016-06-25 22:23:20 +02:00
|
|
|
return nil, err
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-25 22:23:20 +02:00
|
|
|
return o, nil
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:23:20 +02:00
|
|
|
// NewObject finds the Object at remote. If it can't be found it
|
|
|
|
// returns the error fs.ErrorObjectNotFound.
|
|
|
|
func (f *Fs) NewObject(remote string) (fs.Object, error) {
|
2016-06-25 22:58:34 +02:00
|
|
|
return f.newObjectWithInfo(remote, nil)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-04-21 21:06:21 +02:00
|
|
|
// listFn is called from list and listContainerRoot to handle an object.
|
|
|
|
type listFn func(remote string, object *swift.Object, isDirectory bool) error
|
2015-09-26 13:51:05 +02:00
|
|
|
|
|
|
|
// listContainerRoot lists the objects into the function supplied from
|
|
|
|
// the container and root supplied
|
2014-05-05 18:30:55 +02:00
|
|
|
//
|
2017-06-11 23:43:31 +02:00
|
|
|
// Set recurse to read sub directories
|
|
|
|
func (f *Fs) listContainerRoot(container, root string, dir string, recurse bool, fn listFn) error {
|
2016-04-23 22:46:52 +02:00
|
|
|
prefix := root
|
|
|
|
if dir != "" {
|
|
|
|
prefix += dir + "/"
|
|
|
|
}
|
2014-05-05 18:30:55 +02:00
|
|
|
// Options for ObjectsWalk
|
|
|
|
opts := swift.ObjectsOpts{
|
2016-04-23 22:46:52 +02:00
|
|
|
Prefix: prefix,
|
2017-06-06 16:03:52 +02:00
|
|
|
Limit: listChunks,
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
if !recurse {
|
2014-05-05 18:30:55 +02:00
|
|
|
opts.Delimiter = '/'
|
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
rootLength := len(root)
|
|
|
|
return f.c.ObjectsWalk(container, &opts, func(opts *swift.ObjectsOpts) (interface{}, error) {
|
|
|
|
objects, err := f.c.Objects(container, opts)
|
2014-05-05 18:30:55 +02:00
|
|
|
if err == nil {
|
|
|
|
for i := range objects {
|
|
|
|
object := &objects[i]
|
2016-04-21 21:06:21 +02:00
|
|
|
isDirectory := false
|
2017-06-11 23:43:31 +02:00
|
|
|
if !recurse {
|
2017-07-06 12:31:09 +02:00
|
|
|
isDirectory = strings.HasSuffix(object.Name, "/")
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
2017-07-19 00:38:48 +02:00
|
|
|
if !strings.HasPrefix(object.Name, prefix) {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Logf(f, "Odd name received %q", object.Name)
|
2014-05-05 18:30:55 +02:00
|
|
|
continue
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
2017-07-19 00:38:48 +02:00
|
|
|
if object.Name == prefix {
|
|
|
|
// If we have zero length directory markers ending in / then swift
|
|
|
|
// will return them in the listing for the directory which causes
|
|
|
|
// duplicate directories. Ignore them here.
|
|
|
|
continue
|
|
|
|
}
|
2014-05-05 18:30:55 +02:00
|
|
|
remote := object.Name[rootLength:]
|
2016-04-21 21:06:21 +02:00
|
|
|
err = fn(remote, object, isDirectory)
|
2015-09-26 13:51:05 +02:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-05 18:30:55 +02:00
|
|
|
return objects, err
|
|
|
|
})
|
2015-09-26 13:51:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-30 11:54:14 +02:00
|
|
|
type addEntryFn func(fs.DirEntry) error
|
2014-05-05 18:30:55 +02:00
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// list the objects into the function supplied
|
|
|
|
func (f *Fs) list(dir string, recurse bool, fn addEntryFn) error {
|
2017-09-14 19:00:00 +02:00
|
|
|
err := f.listContainerRoot(f.container, f.root, dir, recurse, func(remote string, object *swift.Object, isDirectory bool) (err error) {
|
2016-04-21 21:06:21 +02:00
|
|
|
if isDirectory {
|
2017-07-06 12:31:09 +02:00
|
|
|
remote = strings.TrimRight(remote, "/")
|
2017-06-30 14:37:29 +02:00
|
|
|
d := fs.NewDir(remote, time.Time{}).SetSize(object.Bytes)
|
2017-06-11 23:43:31 +02:00
|
|
|
err = fn(d)
|
2016-04-21 21:06:21 +02:00
|
|
|
} else {
|
2016-06-25 22:23:20 +02:00
|
|
|
o, err := f.newObjectWithInfo(remote, object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Storable does a full metadata read on 0 size objects which might be dynamic large objects
|
|
|
|
if o.Storable() {
|
2017-06-11 23:43:31 +02:00
|
|
|
err = fn(o)
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
return err
|
|
|
|
})
|
2017-09-14 19:00:00 +02:00
|
|
|
if err == swift.ContainerNotFound {
|
|
|
|
err = fs.ErrorDirNotFound
|
|
|
|
}
|
|
|
|
return err
|
2017-06-11 23:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// listDir lists a single directory
|
|
|
|
func (f *Fs) listDir(dir string) (entries fs.DirEntries, err error) {
|
|
|
|
if f.container == "" {
|
|
|
|
return nil, fs.ErrorListBucketRequired
|
|
|
|
}
|
|
|
|
// List the objects
|
2017-06-30 11:54:14 +02:00
|
|
|
err = f.list(dir, false, func(entry fs.DirEntry) error {
|
2017-06-11 23:43:31 +02:00
|
|
|
entries = append(entries, entry)
|
2016-04-21 21:06:21 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, err
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
return entries, nil
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-04-21 21:06:21 +02:00
|
|
|
// listContainers lists the containers
|
2017-06-11 23:43:31 +02:00
|
|
|
func (f *Fs) listContainers(dir string) (entries fs.DirEntries, err error) {
|
2016-04-23 22:46:52 +02:00
|
|
|
if dir != "" {
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, fs.ErrorListBucketRequired
|
2016-04-23 22:46:52 +02:00
|
|
|
}
|
2016-04-21 21:06:21 +02:00
|
|
|
containers, err := f.c.ContainersAll(nil)
|
|
|
|
if err != nil {
|
2017-06-11 23:43:31 +02:00
|
|
|
return nil, errors.Wrap(err, "container listing failed")
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
|
|
|
for _, container := range containers {
|
2017-06-30 14:37:29 +02:00
|
|
|
d := fs.NewDir(container.Name, time.Time{}).SetSize(container.Bytes).SetItems(container.Count)
|
2017-06-11 23:43:31 +02:00
|
|
|
entries = append(entries, d)
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
return entries, nil
|
2015-11-07 16:17:46 +01:00
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// List the objects and directories in dir into entries. The
|
|
|
|
// entries can be returned in any order but should be for a
|
|
|
|
// complete directory.
|
|
|
|
//
|
|
|
|
// dir should be "" to list the root, and should not have
|
|
|
|
// trailing slashes.
|
|
|
|
//
|
|
|
|
// This should return ErrDirNotFound if the directory isn't
|
|
|
|
// found.
|
|
|
|
func (f *Fs) List(dir string) (entries fs.DirEntries, err error) {
|
2014-05-05 18:30:55 +02:00
|
|
|
if f.container == "" {
|
2017-06-11 23:43:31 +02:00
|
|
|
return f.listContainers(dir)
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
return f.listDir(dir)
|
2013-01-23 23:43:20 +01:00
|
|
|
}
|
|
|
|
|
2017-06-05 17:14:24 +02:00
|
|
|
// ListR lists the objects and directories of the Fs starting
|
|
|
|
// from dir recursively into out.
|
2017-06-11 23:43:31 +02:00
|
|
|
//
|
|
|
|
// dir should be "" to start from the root, and should not
|
|
|
|
// have trailing slashes.
|
|
|
|
//
|
|
|
|
// This should return ErrDirNotFound if the directory isn't
|
|
|
|
// found.
|
|
|
|
//
|
|
|
|
// It should call callback for each tranche of entries read.
|
|
|
|
// These need not be returned in any particular order. If
|
|
|
|
// callback returns an error then the listing will stop
|
|
|
|
// immediately.
|
|
|
|
//
|
|
|
|
// Don't implement this unless you have a more efficient way
|
|
|
|
// of listing recursively that doing a directory traversal.
|
|
|
|
func (f *Fs) ListR(dir string, callback fs.ListRCallback) (err error) {
|
|
|
|
if f.container == "" {
|
|
|
|
return errors.New("container needed for recursive list")
|
|
|
|
}
|
|
|
|
list := fs.NewListRHelper(callback)
|
2017-06-30 11:54:14 +02:00
|
|
|
err = f.list(dir, true, func(entry fs.DirEntry) error {
|
2017-06-11 23:43:31 +02:00
|
|
|
return list.Add(entry)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return list.Flush()
|
2017-06-05 17:14:24 +02:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Put the object into the container
|
2013-01-10 22:58:46 +01:00
|
|
|
//
|
|
|
|
// Copy the reader in to the new object which is returned
|
|
|
|
//
|
2014-04-18 18:04:21 +02:00
|
|
|
// The new object may have been created if an error is returned
|
2017-05-28 13:44:22 +02:00
|
|
|
func (f *Fs) Put(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
2015-11-07 12:14:46 +01:00
|
|
|
// Temporary Object under construction
|
|
|
|
fs := &Object{
|
|
|
|
fs: f,
|
2016-02-18 12:35:25 +01:00
|
|
|
remote: src.Remote(),
|
2015-11-07 12:14:46 +01:00
|
|
|
}
|
2017-05-28 13:44:22 +02:00
|
|
|
return fs, fs.Update(in, src, options...)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 07:42:16 +02:00
|
|
|
// PutStream uploads to the remote path with the modTime given of indeterminate size
|
|
|
|
func (f *Fs) PutStream(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
|
|
|
return f.Put(in, src, options...)
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Mkdir creates the container if it doesn't exist
|
2016-11-25 22:52:43 +01:00
|
|
|
func (f *Fs) Mkdir(dir string) error {
|
2017-06-07 15:16:50 +02:00
|
|
|
f.containerOKMu.Lock()
|
|
|
|
defer f.containerOKMu.Unlock()
|
|
|
|
if f.containerOK {
|
2016-11-25 22:52:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-10 15:44:56 +02:00
|
|
|
// if we are at the root, then it is OK
|
|
|
|
if f.container == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-23 21:32:53 +01:00
|
|
|
// Check to see if container exists first
|
2017-08-30 16:54:49 +02:00
|
|
|
var err error = swift.ContainerNotFound
|
|
|
|
if !f.noCheckContainer {
|
|
|
|
_, _, err = f.c.Container(f.container)
|
|
|
|
}
|
2016-01-23 21:32:53 +01:00
|
|
|
if err == swift.ContainerNotFound {
|
2017-06-07 15:16:50 +02:00
|
|
|
err = f.c.ContainerCreate(f.container, nil)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
f.containerOK = true
|
2016-01-23 21:32:53 +01:00
|
|
|
}
|
|
|
|
return err
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-11-07 16:31:04 +01:00
|
|
|
// Rmdir deletes the container if the fs is at the root
|
2012-12-26 13:23:58 +01:00
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
2016-11-25 22:52:43 +01:00
|
|
|
func (f *Fs) Rmdir(dir string) error {
|
2017-06-07 15:16:50 +02:00
|
|
|
f.containerOKMu.Lock()
|
|
|
|
defer f.containerOKMu.Unlock()
|
2016-11-25 22:52:43 +01:00
|
|
|
if f.root != "" || dir != "" {
|
2015-11-07 16:31:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-06-07 15:16:50 +02:00
|
|
|
err := f.c.ContainerDelete(f.container)
|
|
|
|
if err == nil {
|
|
|
|
f.containerOK = false
|
|
|
|
}
|
|
|
|
return err
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Precision of the remote
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Precision() time.Duration {
|
2013-01-19 00:21:02 +01:00
|
|
|
return time.Nanosecond
|
|
|
|
}
|
|
|
|
|
2015-11-07 16:17:46 +01:00
|
|
|
// Purge deletes all the files and directories
|
|
|
|
//
|
|
|
|
// Implemented here so we can make sure we delete directory markers
|
|
|
|
func (f *Fs) Purge() error {
|
2016-04-21 21:06:21 +02:00
|
|
|
// Delete all the files including the directory markers
|
|
|
|
toBeDeleted := make(chan fs.Object, fs.Config.Transfers)
|
2016-06-25 15:27:44 +02:00
|
|
|
delErr := make(chan error, 1)
|
2016-04-21 21:06:21 +02:00
|
|
|
go func() {
|
2016-06-25 15:27:44 +02:00
|
|
|
delErr <- fs.DeleteFiles(toBeDeleted)
|
2016-04-21 21:06:21 +02:00
|
|
|
}()
|
2017-06-30 11:54:14 +02:00
|
|
|
err := f.list("", true, func(entry fs.DirEntry) error {
|
2017-06-11 23:43:31 +02:00
|
|
|
if o, ok := entry.(*Object); ok {
|
2016-06-25 15:27:44 +02:00
|
|
|
toBeDeleted <- o
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
close(toBeDeleted)
|
|
|
|
delError := <-delErr
|
|
|
|
if err == nil {
|
|
|
|
err = delError
|
|
|
|
}
|
2016-04-21 21:06:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-25 22:52:43 +01:00
|
|
|
return f.Rmdir("")
|
2015-11-07 16:17:46 +01:00
|
|
|
}
|
|
|
|
|
2015-02-14 19:48:08 +01:00
|
|
|
// Copy src to this remote using server side copy operations.
|
|
|
|
//
|
|
|
|
// This is stored with the remote path given
|
|
|
|
//
|
|
|
|
// It returns the destination Object and a possible error
|
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantCopy
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Copy(src fs.Object, remote string) (fs.Object, error) {
|
2017-06-28 22:14:53 +02:00
|
|
|
err := f.Mkdir("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-07 12:14:46 +01:00
|
|
|
srcObj, ok := src.(*Object)
|
2015-02-14 19:48:08 +01:00
|
|
|
if !ok {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(src, "Can't copy - not same remote type")
|
2015-02-14 19:48:08 +01:00
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
}
|
2015-11-07 12:14:46 +01:00
|
|
|
srcFs := srcObj.fs
|
2017-06-28 22:14:53 +02:00
|
|
|
_, err = f.c.ObjectCopy(srcFs.container, srcFs.root+srcObj.remote, f.container, f.root+remote, nil)
|
2015-02-14 19:48:08 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-25 22:23:20 +02:00
|
|
|
return f.NewObject(remote)
|
2015-02-14 19:48:08 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
// Hashes returns the supported hash sets.
|
|
|
|
func (f *Fs) Hashes() fs.HashSet {
|
|
|
|
return fs.HashSet(fs.HashMD5)
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Fs returns the parent Fs
|
2016-02-18 12:35:25 +01:00
|
|
|
func (o *Object) Fs() fs.Info {
|
2015-11-07 12:14:46 +01:00
|
|
|
return o.fs
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a string version
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) String() string {
|
2014-03-28 18:56:04 +01:00
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Remote returns the remote path
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Remote() string {
|
2013-06-27 21:13:07 +02:00
|
|
|
return o.remote
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
// Hash returns the Md5sum of an object returning a lowercase hex string
|
|
|
|
func (o *Object) Hash(t fs.HashType) (string, error) {
|
|
|
|
if t != fs.HashMD5 {
|
|
|
|
return "", fs.ErrHashUnsupported
|
|
|
|
}
|
2016-03-17 18:36:20 +01:00
|
|
|
isDynamicLargeObject, err := o.isDynamicLargeObject()
|
2015-09-23 08:57:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-03-17 18:36:20 +01:00
|
|
|
isStaticLargeObject, err := o.isStaticLargeObject()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if isDynamicLargeObject || isStaticLargeObject {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(o, "Returning empty Md5sum for swift large object")
|
2015-09-23 08:57:48 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
2013-06-27 21:13:07 +02:00
|
|
|
return strings.ToLower(o.info.Hash), nil
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 18:36:20 +01:00
|
|
|
// hasHeader checks for the header passed in returning false if the
|
|
|
|
// object isn't found.
|
|
|
|
func (o *Object) hasHeader(header string) (bool, error) {
|
2015-09-23 08:57:48 +02:00
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
2016-06-25 22:23:20 +02:00
|
|
|
if err == fs.ErrorObjectNotFound {
|
2015-09-26 13:51:05 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
2015-09-23 08:57:48 +02:00
|
|
|
return false, err
|
|
|
|
}
|
2016-03-17 18:36:20 +01:00
|
|
|
_, isDynamicLargeObject := (*o.headers)[header]
|
|
|
|
return isDynamicLargeObject, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isDynamicLargeObject checks for X-Object-Manifest header
|
|
|
|
func (o *Object) isDynamicLargeObject() (bool, error) {
|
|
|
|
return o.hasHeader("X-Object-Manifest")
|
|
|
|
}
|
|
|
|
|
|
|
|
// isStaticLargeObjectFile checks for the X-Static-Large-Object header
|
|
|
|
func (o *Object) isStaticLargeObject() (bool, error) {
|
|
|
|
return o.hasHeader("X-Static-Large-Object")
|
2015-09-23 08:57:48 +02:00
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Size returns the size of an object in bytes
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Size() int64 {
|
2013-06-27 21:13:07 +02:00
|
|
|
return o.info.Bytes
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// readMetaData gets the metadata if it hasn't already been fetched
|
|
|
|
//
|
|
|
|
// it also sets the info
|
2016-06-25 22:23:20 +02:00
|
|
|
//
|
|
|
|
// it returns fs.ErrorObjectNotFound if the object isn't found
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) readMetaData() (err error) {
|
2015-09-24 18:41:16 +02:00
|
|
|
if o.headers != nil {
|
2012-12-26 13:23:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
2015-11-07 12:14:46 +01:00
|
|
|
info, h, err := o.fs.c.Object(o.fs.container, o.fs.root+o.remote)
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2016-06-25 22:23:20 +02:00
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return fs.ErrorObjectNotFound
|
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
return err
|
|
|
|
}
|
2013-06-27 21:13:07 +02:00
|
|
|
o.info = info
|
2015-09-24 18:41:16 +02:00
|
|
|
o.headers = &h
|
2012-12-26 13:23:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
2013-01-02 16:21:55 +01:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// It attempts to read the objects mtime and if that isn't present the
|
|
|
|
// LastModified returned in the http headers
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) ModTime() time.Time {
|
2013-06-27 21:13:07 +02:00
|
|
|
err := o.readMetaData()
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(o, "Failed to read metadata: %s", err)
|
2013-06-27 21:13:07 +02:00
|
|
|
return o.info.LastModified
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
2015-09-24 18:41:16 +02:00
|
|
|
modTime, err := o.headers.ObjectMetadata().GetModTime()
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
// fs.Logf(o, "Failed to read mtime from object: %v", err)
|
2013-06-27 21:13:07 +02:00
|
|
|
return o.info.LastModified
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
2013-01-02 16:21:55 +01:00
|
|
|
return modTime
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// SetModTime sets the modification time of the local fs object
|
2016-03-22 16:07:10 +01:00
|
|
|
func (o *Object) SetModTime(modTime time.Time) error {
|
2013-06-27 21:13:07 +02:00
|
|
|
err := o.readMetaData()
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2016-03-22 16:07:10 +01:00
|
|
|
return err
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
2015-09-24 18:41:16 +02:00
|
|
|
meta := o.headers.ObjectMetadata()
|
|
|
|
meta.SetModTime(modTime)
|
|
|
|
newHeaders := meta.ObjectHeaders()
|
|
|
|
for k, v := range newHeaders {
|
|
|
|
(*o.headers)[k] = v
|
|
|
|
}
|
2015-11-08 21:45:42 +01:00
|
|
|
// Include any other metadata from request
|
|
|
|
for k, v := range *o.headers {
|
|
|
|
if strings.HasPrefix(k, "X-Object-") {
|
|
|
|
newHeaders[k] = v
|
|
|
|
}
|
|
|
|
}
|
2016-03-22 16:07:10 +01:00
|
|
|
return o.fs.c.ObjectUpdate(o.fs.container, o.fs.root+o.remote, newHeaders)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Storable returns if this object is storable
|
2015-11-07 16:17:46 +01:00
|
|
|
//
|
2015-12-02 22:23:56 +01:00
|
|
|
// It compares the Content-Type to directoryMarkerContentType - that
|
|
|
|
// makes it a directory marker which is not storable.
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Storable() bool {
|
2015-11-11 18:19:57 +01:00
|
|
|
return o.info.ContentType != directoryMarkerContentType
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2016-09-10 12:29:57 +02:00
|
|
|
func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
|
|
|
headers := fs.OpenOptionHeaders(options)
|
|
|
|
_, isRanging := headers["Range"]
|
|
|
|
in, _, err = o.fs.c.ObjectOpen(o.fs.container, o.fs.root+o.remote, !isRanging, headers)
|
2012-12-26 13:23:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-23 08:57:48 +02:00
|
|
|
// min returns the smallest of x, y
|
|
|
|
func min(x, y int64) int64 {
|
|
|
|
if x < y {
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2015-09-26 13:51:05 +02:00
|
|
|
// removeSegments removes any old segments from o
|
|
|
|
//
|
|
|
|
// if except is passed in then segments with that prefix won't be deleted
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) removeSegments(except string) error {
|
|
|
|
segmentsRoot := o.fs.root + o.remote + "/"
|
2017-06-11 23:43:31 +02:00
|
|
|
err := o.fs.listContainerRoot(o.fs.segmentsContainer, segmentsRoot, "", true, func(remote string, object *swift.Object, isDirectory bool) error {
|
2016-04-21 21:06:21 +02:00
|
|
|
if isDirectory {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
if except != "" && strings.HasPrefix(remote, except) {
|
2017-02-09 12:01:20 +01:00
|
|
|
// fs.Debugf(o, "Ignoring current segment file %q in container %q", segmentsRoot+remote, o.fs.segmentsContainer)
|
2015-09-26 13:51:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
segmentPath := segmentsRoot + remote
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(o, "Removing segment file %q in container %q", segmentPath, o.fs.segmentsContainer)
|
2015-11-07 12:14:46 +01:00
|
|
|
return o.fs.c.ObjectDelete(o.fs.segmentsContainer, segmentPath)
|
2015-09-26 13:51:05 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// remove the segments container if empty, ignore errors
|
2015-11-07 12:14:46 +01:00
|
|
|
err = o.fs.c.ContainerDelete(o.fs.segmentsContainer)
|
2015-09-26 13:51:05 +02:00
|
|
|
if err == nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(o, "Removed empty container %q", o.fs.segmentsContainer)
|
2015-09-26 13:51:05 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-27 19:55:21 +01:00
|
|
|
// urlEncode encodes a string so that it is a valid URL
|
|
|
|
//
|
|
|
|
// We don't use any of Go's standard methods as we need `/` not
|
|
|
|
// encoded but we need '&' encoded.
|
|
|
|
func urlEncode(str string) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for i := 0; i < len(str); i++ {
|
|
|
|
c := str[i]
|
|
|
|
if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '/' || c == '.' {
|
|
|
|
_ = buf.WriteByte(c)
|
|
|
|
} else {
|
|
|
|
_, _ = buf.WriteString(fmt.Sprintf("%%%02X", c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2015-09-26 13:51:05 +02:00
|
|
|
// updateChunks updates the existing object using chunks to a separate
|
|
|
|
// container. It returns a string which prefixes current segments.
|
2017-09-14 07:42:16 +02:00
|
|
|
func (o *Object) updateChunks(in0 io.Reader, headers swift.Headers, size int64, contentType string) (string, error) {
|
2015-09-26 13:51:05 +02:00
|
|
|
// Create the segmentsContainer if it doesn't exist
|
2017-10-25 01:17:13 +02:00
|
|
|
var err error = swift.ContainerNotFound
|
|
|
|
_, _, err = o.fs.c.Container(o.fs.segmentsContainer)
|
|
|
|
if err == swift.ContainerNotFound {
|
|
|
|
err = o.fs.c.ContainerCreate(o.fs.segmentsContainer, nil)
|
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-09-23 08:57:48 +02:00
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
// Upload the chunks
|
|
|
|
left := size
|
|
|
|
i := 0
|
|
|
|
uniquePrefix := fmt.Sprintf("%s/%d", swift.TimeToFloatString(time.Now()), size)
|
2015-11-07 12:14:46 +01:00
|
|
|
segmentsPath := fmt.Sprintf("%s%s/%s", o.fs.root, o.remote, uniquePrefix)
|
2017-09-14 07:42:16 +02:00
|
|
|
in := bufio.NewReader(in0)
|
|
|
|
for {
|
|
|
|
// can we read at least one byte?
|
|
|
|
if _, err := in.Peek(1); err != nil {
|
|
|
|
if left > 0 {
|
|
|
|
return "", err // read less than expected
|
|
|
|
}
|
|
|
|
fs.Debugf(o, "Uploading segments into %q seems done (%v)", o.fs.segmentsContainer, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
n := int64(chunkSize)
|
|
|
|
if size != -1 {
|
|
|
|
n = min(left, n)
|
|
|
|
headers["Content-Length"] = strconv.FormatInt(n, 10) // set Content-Length as we know it
|
|
|
|
left -= n
|
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
segmentReader := io.LimitReader(in, n)
|
|
|
|
segmentPath := fmt.Sprintf("%s/%08d", segmentsPath, i)
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(o, "Uploading segment file %q into %q", segmentPath, o.fs.segmentsContainer)
|
2015-11-07 12:14:46 +01:00
|
|
|
_, err := o.fs.c.ObjectPut(o.fs.segmentsContainer, segmentPath, segmentReader, true, "", "", headers)
|
2015-09-26 13:51:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
i++
|
2015-09-23 08:57:48 +02:00
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
// Upload the manifest
|
2016-02-27 19:55:21 +01:00
|
|
|
headers["X-Object-Manifest"] = urlEncode(fmt.Sprintf("%s/%s", o.fs.segmentsContainer, segmentsPath))
|
2015-10-03 16:38:35 +02:00
|
|
|
headers["Content-Length"] = "0" // set Content-Length as we know it
|
2015-09-26 13:51:05 +02:00
|
|
|
emptyReader := bytes.NewReader(nil)
|
2015-11-07 12:14:46 +01:00
|
|
|
manifestName := o.fs.root + o.remote
|
2016-09-21 23:13:24 +02:00
|
|
|
_, err = o.fs.c.ObjectPut(o.fs.container, manifestName, emptyReader, true, "", contentType, headers)
|
2015-09-26 13:51:05 +02:00
|
|
|
return uniquePrefix + "/", err
|
2015-09-23 08:57:48 +02:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Update the object with the contents of the io.Reader, modTime and size
|
|
|
|
//
|
|
|
|
// The new object may have been created if an error is returned
|
2017-05-28 13:44:22 +02:00
|
|
|
func (o *Object) Update(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
|
2017-06-10 15:44:56 +02:00
|
|
|
if o.fs.container == "" {
|
|
|
|
return fs.FatalError(errors.New("container name needed in remote"))
|
|
|
|
}
|
2017-06-07 15:16:50 +02:00
|
|
|
err := o.fs.Mkdir("")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-18 12:35:25 +01:00
|
|
|
size := src.Size()
|
|
|
|
modTime := src.ModTime()
|
|
|
|
|
2016-03-17 18:36:20 +01:00
|
|
|
// Note whether this is a dynamic large object before starting
|
|
|
|
isDynamicLargeObject, err := o.isDynamicLargeObject()
|
2015-09-26 13:51:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Set the mtime
|
|
|
|
m := swift.Metadata{}
|
|
|
|
m.SetModTime(modTime)
|
2016-09-21 23:13:24 +02:00
|
|
|
contentType := fs.MimeType(src)
|
2015-09-26 13:51:05 +02:00
|
|
|
headers := m.ObjectHeaders()
|
|
|
|
uniquePrefix := ""
|
2017-09-14 07:42:16 +02:00
|
|
|
if size > int64(chunkSize) || size == -1 {
|
2016-09-21 23:13:24 +02:00
|
|
|
uniquePrefix, err = o.updateChunks(in, headers, size, contentType)
|
2015-09-23 08:57:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-03 16:38:35 +02:00
|
|
|
headers["Content-Length"] = strconv.FormatInt(size, 10) // set Content-Length as we know it
|
2016-09-21 23:13:24 +02:00
|
|
|
_, err := o.fs.c.ObjectPut(o.fs.container, o.fs.root+o.remote, in, true, "", contentType, headers)
|
2015-09-23 08:57:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-19 13:36:56 +02:00
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
|
2016-03-17 18:36:20 +01:00
|
|
|
// If file was a dynamic large object then remove old/all segments
|
|
|
|
if isDynamicLargeObject {
|
2015-09-26 13:51:05 +02:00
|
|
|
err = o.removeSegments(uniquePrefix)
|
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Logf(o, "Failed to remove old segments - carrying on with upload: %v", err)
|
2015-09-26 13:51:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 13:36:56 +02:00
|
|
|
// Read the metadata from the newly created object
|
2015-09-24 18:41:16 +02:00
|
|
|
o.headers = nil // wipe old metadata
|
2015-09-23 08:57:48 +02:00
|
|
|
return o.readMetaData()
|
2014-04-18 18:04:21 +02:00
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Remove an object
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Remove() error {
|
2016-03-17 18:36:20 +01:00
|
|
|
isDynamicLargeObject, err := o.isDynamicLargeObject()
|
2015-09-23 08:57:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
// Remove file/manifest first
|
2015-11-07 12:14:46 +01:00
|
|
|
err = o.fs.c.ObjectDelete(o.fs.container, o.fs.root+o.remote)
|
2015-09-26 13:51:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// ...then segments if required
|
2016-03-17 18:36:20 +01:00
|
|
|
if isDynamicLargeObject {
|
2015-09-26 13:51:05 +02:00
|
|
|
err = o.removeSegments("")
|
2015-09-23 08:57:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
return nil
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 23:13:24 +02:00
|
|
|
// MimeType of an Object if known, "" otherwise
|
|
|
|
func (o *Object) MimeType() string {
|
|
|
|
return o.info.ContentType
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Check the interfaces are satisfied
|
2015-11-07 12:14:46 +01:00
|
|
|
var (
|
2017-09-14 07:42:16 +02:00
|
|
|
_ fs.Fs = &Fs{}
|
|
|
|
_ fs.Purger = &Fs{}
|
|
|
|
_ fs.PutStreamer = &Fs{}
|
|
|
|
_ fs.Copier = &Fs{}
|
|
|
|
_ fs.ListRer = &Fs{}
|
|
|
|
_ fs.Object = &Object{}
|
|
|
|
_ fs.MimeTyper = &Object{}
|
2015-11-07 12:14:46 +01:00
|
|
|
)
|