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 (
|
2015-09-23 08:57:48 +02:00
|
|
|
"bytes"
|
2012-12-29 12:35:41 +01:00
|
|
|
"errors"
|
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"
|
|
|
|
"time"
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/swift"
|
2015-09-23 08:57:48 +02:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
2015-11-07 16:17:46 +01:00
|
|
|
// Constants
|
|
|
|
const (
|
|
|
|
directoryMarkerContentType = "application/directory" // content type of directory marker objects
|
|
|
|
)
|
|
|
|
|
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() {
|
2015-09-22 19:47:16 +02:00
|
|
|
fs.Register(&fs.Info{
|
2014-03-15 17:06:11 +01:00
|
|
|
Name: "swift",
|
|
|
|
NewFs: NewFs,
|
|
|
|
Options: []fs.Option{{
|
|
|
|
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",
|
|
|
|
}},
|
2014-11-24 13:05:10 +01:00
|
|
|
}, {
|
|
|
|
Name: "tenant",
|
|
|
|
Help: "Tenant name - optional",
|
2015-03-04 18:09:53 +01:00
|
|
|
}, {
|
|
|
|
Name: "region",
|
|
|
|
Help: "Region name - optional",
|
2014-03-15 17:06:11 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2015-09-23 08:57:48 +02:00
|
|
|
// snet = flag.Bool("swift-snet", false, "Use internal service network") // FIXME not implemented
|
|
|
|
pflag.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 {
|
2015-09-26 13:51:05 +02:00
|
|
|
name string // name of this remote
|
|
|
|
c swift.Connection // the connection to the swift server
|
|
|
|
container string // the container we are working on
|
|
|
|
segmentsContainer string // container to store the segments (if any) in
|
|
|
|
root string // the path we are working on if any
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
err = fmt.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) {
|
|
|
|
userName := fs.ConfigFile.MustValue(name, "user")
|
|
|
|
if userName == "" {
|
|
|
|
return nil, errors.New("user not found")
|
2012-12-29 12:35:41 +01:00
|
|
|
}
|
2014-03-15 17:06:11 +01:00
|
|
|
apiKey := fs.ConfigFile.MustValue(name, "key")
|
|
|
|
if apiKey == "" {
|
|
|
|
return nil, errors.New("key not found")
|
2012-12-29 12:35:41 +01:00
|
|
|
}
|
2015-09-22 19:47:16 +02:00
|
|
|
authURL := fs.ConfigFile.MustValue(name, "auth")
|
|
|
|
if authURL == "" {
|
2014-03-15 17:06:11 +01:00
|
|
|
return nil, errors.New("auth not found")
|
2012-12-29 12:35:41 +01:00
|
|
|
}
|
|
|
|
c := &swift.Connection{
|
2015-05-10 12:25:54 +02:00
|
|
|
UserName: userName,
|
|
|
|
ApiKey: apiKey,
|
2015-09-22 19:47:16 +02:00
|
|
|
AuthUrl: authURL,
|
2015-05-10 12:25:54 +02:00
|
|
|
UserAgent: fs.UserAgent,
|
|
|
|
Tenant: fs.ConfigFile.MustValue(name, "tenant"),
|
|
|
|
Region: fs.ConfigFile.MustValue(name, "region"),
|
|
|
|
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
|
|
|
}
|
|
|
|
err := c.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// NewFs contstructs an Fs from the path, container:path
|
2014-05-05 20:52:52 +02:00
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
|
|
|
container, directory, err := parsePath(root)
|
2012-12-29 12:35:41 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-03-15 17:06:11 +01:00
|
|
|
c, err := swiftConnection(name)
|
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,
|
|
|
|
c: *c,
|
|
|
|
container: container,
|
|
|
|
segmentsContainer: container + "_segments",
|
|
|
|
root: directory,
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
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
|
|
|
remote := path.Base(directory)
|
|
|
|
f.root = path.Dir(directory)
|
|
|
|
if f.root == "." {
|
|
|
|
f.root = ""
|
|
|
|
} else {
|
|
|
|
f.root += "/"
|
|
|
|
}
|
|
|
|
obj := f.NewFsObject(remote)
|
|
|
|
// return a Fs Limited to this object
|
|
|
|
return fs.NewLimited(f, obj), nil
|
|
|
|
}
|
|
|
|
}
|
2012-12-29 12:35:41 +01:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) newFsObjectWithInfo(remote string, info *swift.Object) fs.Object {
|
|
|
|
o := &Object{
|
|
|
|
fs: f,
|
2012-12-26 13:23:58 +01:00
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
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 {
|
2015-10-03 16:30:31 +02:00
|
|
|
fs.Debug(o, "Failed to read metadata: %s", err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-10-03 16:30:31 +02:00
|
|
|
return o
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// NewFsObject returns an FsObject from a path
|
2012-12-26 13:23:58 +01:00
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) NewFsObject(remote string) fs.Object {
|
2014-07-29 18:50:07 +02:00
|
|
|
return f.newFsObjectWithInfo(remote, nil)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-09-26 13:51:05 +02:00
|
|
|
// listFn is called from list and listContainerRoot to handle an object
|
|
|
|
type listFn func(string, *swift.Object) error
|
|
|
|
|
|
|
|
// listContainerRoot lists the objects into the function supplied from
|
|
|
|
// the container and root supplied
|
2014-05-05 18:30:55 +02:00
|
|
|
//
|
|
|
|
// If directories is set it only sends directories
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) listContainerRoot(container, root string, directories bool, fn listFn) error {
|
2014-05-05 18:30:55 +02:00
|
|
|
// Options for ObjectsWalk
|
|
|
|
opts := swift.ObjectsOpts{
|
2015-09-26 13:51:05 +02:00
|
|
|
Prefix: root,
|
2014-05-05 18:30:55 +02:00
|
|
|
Limit: 256,
|
|
|
|
}
|
|
|
|
if directories {
|
|
|
|
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]
|
|
|
|
// FIXME if there are no directories, swift gives back the files for some reason!
|
2014-07-24 23:51:10 +02:00
|
|
|
if directories {
|
|
|
|
if !strings.HasSuffix(object.Name, "/") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
object.Name = object.Name[:len(object.Name)-1]
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
if !strings.HasPrefix(object.Name, root) {
|
2014-05-05 18:30:55 +02:00
|
|
|
fs.Log(f, "Odd name received %q", object.Name)
|
|
|
|
continue
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
2014-05-05 18:30:55 +02:00
|
|
|
remote := object.Name[rootLength:]
|
2015-09-26 13:51:05 +02:00
|
|
|
err = fn(remote, object)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// list the objects into the function supplied
|
|
|
|
//
|
|
|
|
// If directories is set it only sends directories
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) list(directories bool, fn listFn) {
|
2015-09-26 13:51:05 +02:00
|
|
|
err := f.listContainerRoot(f.container, f.root, directories, fn)
|
2014-05-05 18:30:55 +02:00
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Couldn't read container %q: %s", f.container, err)
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-07 16:17:46 +01:00
|
|
|
// listFiles walks the path returning a channel of FsObjects
|
|
|
|
//
|
|
|
|
// if ignoreStorable is set then it outputs the file even if Storable() is false
|
|
|
|
func (f *Fs) listFiles(ignoreStorable bool) fs.ObjectsChan {
|
2014-05-05 18:30:55 +02:00
|
|
|
out := make(fs.ObjectsChan, fs.Config.Checkers)
|
|
|
|
if f.container == "" {
|
|
|
|
// Return no objects at top level list
|
2012-12-26 13:23:58 +01:00
|
|
|
close(out)
|
2014-05-05 18:30:55 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Can't list objects at root - choose a container using lsd")
|
2014-05-05 18:30:55 +02:00
|
|
|
} else {
|
|
|
|
// List the objects
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2015-09-26 13:51:05 +02:00
|
|
|
f.list(false, func(remote string, object *swift.Object) error {
|
|
|
|
if o := f.newFsObjectWithInfo(remote, object); o != nil {
|
2015-11-07 16:17:46 +01:00
|
|
|
// Storable does a full metadata read on 0 size objects which might be manifest files
|
|
|
|
storable := o.Storable()
|
|
|
|
if storable || ignoreStorable {
|
|
|
|
out <- o
|
2015-09-26 13:51:05 +02:00
|
|
|
}
|
2014-05-05 18:30:55 +02:00
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
return nil
|
2014-05-05 18:30:55 +02:00
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-11-07 16:17:46 +01:00
|
|
|
// List walks the path returning a channel of FsObjects
|
|
|
|
func (f *Fs) List() fs.ObjectsChan {
|
|
|
|
return f.listFiles(false)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// ListDir lists the containers
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) ListDir() fs.DirChan {
|
2013-06-28 09:57:32 +02:00
|
|
|
out := make(fs.DirChan, fs.Config.Checkers)
|
2014-05-05 18:30:55 +02:00
|
|
|
if f.container == "" {
|
|
|
|
// List the containers
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
containers, err := f.c.ContainersAll(nil)
|
|
|
|
if err != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Couldn't list containers: %v", err)
|
2014-05-05 18:30:55 +02:00
|
|
|
} else {
|
|
|
|
for _, container := range containers {
|
|
|
|
out <- &fs.Dir{
|
|
|
|
Name: container.Name,
|
|
|
|
Bytes: container.Bytes,
|
|
|
|
Count: container.Count,
|
|
|
|
}
|
2013-01-23 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-05 18:30:55 +02:00
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
// List the directories in the path in the container
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
2015-09-26 13:51:05 +02:00
|
|
|
f.list(true, func(remote string, object *swift.Object) error {
|
2014-05-05 18:30:55 +02:00
|
|
|
out <- &fs.Dir{
|
|
|
|
Name: remote,
|
|
|
|
Bytes: object.Bytes,
|
|
|
|
Count: 0,
|
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
return nil
|
2014-05-05 18:30:55 +02:00
|
|
|
})
|
|
|
|
}()
|
|
|
|
}
|
2013-01-23 23:43:20 +01:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Put(in io.Reader, remote string, modTime time.Time, size int64) (fs.Object, error) {
|
|
|
|
// Temporary Object under construction
|
|
|
|
fs := &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
2014-04-18 18:04:21 +02:00
|
|
|
return fs, fs.Update(in, modTime, size)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the container if it doesn't exist
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Mkdir() error {
|
2012-12-26 13:23:58 +01:00
|
|
|
return f.c.ContainerCreate(f.container, nil)
|
|
|
|
}
|
|
|
|
|
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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Rmdir() error {
|
2015-11-07 16:31:04 +01:00
|
|
|
if f.root != "" {
|
|
|
|
return nil
|
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
return f.c.ContainerDelete(f.container)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fs.DeleteFiles(f.listFiles(true))
|
|
|
|
return f.Rmdir()
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
srcObj, ok := src.(*Object)
|
2015-02-14 19:48:08 +01:00
|
|
|
if !ok {
|
|
|
|
fs.Debug(src, "Can't copy - not same remote type")
|
|
|
|
return nil, fs.ErrorCantCopy
|
|
|
|
}
|
2015-11-07 12:14:46 +01:00
|
|
|
srcFs := srcObj.fs
|
2015-02-14 19:48:08 +01:00
|
|
|
_, err := f.c.ObjectCopy(srcFs.container, srcFs.root+srcObj.remote, f.container, f.root+remote, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f.NewFsObject(remote), nil
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Fs returns the parent Fs
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Fs() fs.Fs {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Md5sum returns the Md5sum of an object returning a lowercase hex string
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Md5sum() (string, error) {
|
2015-09-23 08:57:48 +02:00
|
|
|
isManifest, err := o.isManifestFile()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if isManifest {
|
2015-09-26 13:51:05 +02:00
|
|
|
fs.Debug(o, "Returning empty Md5sum for swift manifest file")
|
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
|
|
|
}
|
|
|
|
|
2015-09-23 08:57:48 +02:00
|
|
|
// isManifestFile checks for manifest header
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) isManifestFile() (bool, error) {
|
2015-09-23 08:57:48 +02:00
|
|
|
err := o.readMetaData()
|
|
|
|
if err != nil {
|
2015-09-26 13:51:05 +02:00
|
|
|
if err == swift.ObjectNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
2015-09-23 08:57:48 +02:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
_, isManifestFile := (*o.headers)["X-Object-Manifest"]
|
|
|
|
return isManifestFile, nil
|
|
|
|
}
|
|
|
|
|
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
|
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 {
|
|
|
|
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 {
|
2015-10-03 16:30:31 +02:00
|
|
|
fs.Debug(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 {
|
2013-06-28 09:57:32 +02:00
|
|
|
// fs.Log(o, "Failed to read mtime from object: %s", 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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) SetModTime(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 {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(o, "Failed to read metadata: %s", err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
2015-11-07 12:14:46 +01:00
|
|
|
err = o.fs.c.ObjectUpdate(o.fs.container, o.fs.root+o.remote, newHeaders)
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(o, "Failed to update remote mtime: %s", err)
|
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
|
|
|
//
|
|
|
|
// It reads the metadata for <= directoryMarkerMaxSize byte objects then 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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Open() (in io.ReadCloser, err error) {
|
|
|
|
in, _, err = o.fs.c.ObjectOpen(o.fs.container, o.fs.root+o.remote, true, nil)
|
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 + "/"
|
|
|
|
err := o.fs.listContainerRoot(o.fs.segmentsContainer, segmentsRoot, false, func(remote string, object *swift.Object) error {
|
2015-09-26 13:51:05 +02:00
|
|
|
if except != "" && strings.HasPrefix(remote, except) {
|
2015-11-07 12:14:46 +01:00
|
|
|
// fs.Debug(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
|
2015-11-07 12:14:46 +01:00
|
|
|
fs.Debug(o, "Removing segment file %q in container %q", segmentPath, o.fs.segmentsContainer)
|
|
|
|
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 {
|
2015-11-07 12:14:46 +01:00
|
|
|
fs.Debug(o, "Removed empty container %q", o.fs.segmentsContainer)
|
2015-09-26 13:51:05 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateChunks updates the existing object using chunks to a separate
|
|
|
|
// container. It returns a string which prefixes current segments.
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) updateChunks(in io.Reader, headers swift.Headers, size int64) (string, error) {
|
2015-09-26 13:51:05 +02:00
|
|
|
// Create the segmentsContainer if it doesn't exist
|
2015-11-07 12:14:46 +01:00
|
|
|
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)
|
2015-09-26 13:51:05 +02:00
|
|
|
for left > 0 {
|
|
|
|
n := min(left, int64(chunkSize))
|
2015-10-03 16:38:35 +02:00
|
|
|
headers["Content-Length"] = strconv.FormatInt(n, 10) // set Content-Length as we know it
|
2015-09-26 13:51:05 +02:00
|
|
|
segmentReader := io.LimitReader(in, n)
|
|
|
|
segmentPath := fmt.Sprintf("%s/%08d", segmentsPath, i)
|
2015-11-07 12:14:46 +01:00
|
|
|
fs.Debug(o, "Uploading segment file %q into %q", segmentPath, o.fs.segmentsContainer)
|
|
|
|
_, 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
|
|
|
|
}
|
|
|
|
left -= n
|
|
|
|
i++
|
2015-09-23 08:57:48 +02:00
|
|
|
}
|
2015-09-26 13:51:05 +02:00
|
|
|
// Upload the manifest
|
2015-11-07 12:14:46 +01:00
|
|
|
headers["X-Object-Manifest"] = 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
|
|
|
|
_, err = o.fs.c.ObjectPut(o.fs.container, manifestName, emptyReader, true, "", "", 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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Update(in io.Reader, modTime time.Time, size int64) error {
|
2015-09-26 13:51:05 +02:00
|
|
|
// Note whether this has a manifest before starting
|
|
|
|
isManifest, err := o.isManifestFile()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Set the mtime
|
|
|
|
m := swift.Metadata{}
|
|
|
|
m.SetModTime(modTime)
|
2015-09-26 13:51:05 +02:00
|
|
|
headers := m.ObjectHeaders()
|
|
|
|
uniquePrefix := ""
|
2015-09-23 08:57:48 +02:00
|
|
|
if size > int64(chunkSize) {
|
2015-09-26 13:51:05 +02:00
|
|
|
uniquePrefix, err = o.updateChunks(in, headers, size)
|
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
|
2015-11-07 12:14:46 +01:00
|
|
|
_, err := o.fs.c.ObjectPut(o.fs.container, o.fs.root+o.remote, in, true, "", "", 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
|
|
|
|
|
|
|
// If file was a manifest then remove old/all segments
|
|
|
|
if isManifest {
|
|
|
|
err = o.removeSegments(uniquePrefix)
|
|
|
|
if err != nil {
|
|
|
|
fs.Log(o, "Failed to remove old segments - carrying on with upload: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-09-23 08:57:48 +02:00
|
|
|
isManifestFile, err := o.isManifestFile()
|
|
|
|
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
|
2015-09-23 08:57:48 +02:00
|
|
|
if isManifestFile {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2015-11-07 12:14:46 +01:00
|
|
|
var (
|
|
|
|
_ fs.Fs = &Fs{}
|
2015-11-07 16:17:46 +01:00
|
|
|
_ fs.Purger = &Fs{}
|
2015-11-07 12:14:46 +01:00
|
|
|
_ fs.Copier = &Fs{}
|
|
|
|
_ fs.Object = &Object{}
|
|
|
|
)
|