2012-12-26 13:23:58 +01:00
|
|
|
// Swift interface
|
2013-06-27 21:13:07 +02:00
|
|
|
package swift
|
2012-12-26 13:23:58 +01:00
|
|
|
|
2013-01-23 23:43:20 +01:00
|
|
|
// FIXME need to prevent anything but ListDir working for swift://
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
import (
|
2012-12-29 12:35:41 +01:00
|
|
|
"errors"
|
|
|
|
"flag"
|
2012-12-26 13:23:58 +01:00
|
|
|
"fmt"
|
2013-07-11 18:37:33 +02:00
|
|
|
"github.com/ncw/rclone/fs"
|
2012-12-26 13:23:58 +01:00
|
|
|
"github.com/ncw/swift"
|
|
|
|
"io"
|
|
|
|
"log"
|
2012-12-29 12:35:41 +01:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2012-12-26 13:23:58 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// Pattern to match a swift url
|
|
|
|
var Match = regexp.MustCompile(`^swift://([^/]*)(.*)$`)
|
|
|
|
|
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
|
|
|
fs.Register(Match, NewFs)
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// FsSwift represents a remote swift server
|
|
|
|
type FsSwift struct {
|
|
|
|
c swift.Connection // the connection to the swift server
|
|
|
|
container string // the container we are working on
|
2013-06-27 20:51:03 +02:00
|
|
|
root string // the path we are working on if any
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FsObjectSwift describes a swift object
|
|
|
|
//
|
|
|
|
// Will definitely have info but maybe not meta
|
|
|
|
type FsObjectSwift struct {
|
|
|
|
swift *FsSwift // what this object is part of
|
|
|
|
remote string // The remote path
|
|
|
|
info swift.Object // Info from the swift object if known
|
|
|
|
meta *swift.Metadata // The object metadata if known
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2012-12-29 12:35:41 +01:00
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
|
|
|
// FIXME make these part of swift so we get a standard set of flags?
|
2013-06-28 19:24:06 +02:00
|
|
|
authUrl = flag.String("swift-auth", os.Getenv("ST_AUTH"), "Auth URL for server. Defaults to environment var ST_AUTH.")
|
|
|
|
userName = flag.String("swift-user", os.Getenv("ST_USER"), "User name. Defaults to environment var ST_USER.")
|
|
|
|
apiKey = flag.String("swift-key", os.Getenv("ST_KEY"), "API key (password). Defaults to environment var ST_KEY.")
|
|
|
|
snet = flag.Bool("swift-snet", false, "Use internal service network") // FIXME not implemented
|
2012-12-29 12:35:41 +01:00
|
|
|
)
|
|
|
|
|
2012-12-31 17:40:34 +01:00
|
|
|
// String converts this FsSwift to a string
|
|
|
|
func (f *FsSwift) String() string {
|
|
|
|
return fmt.Sprintf("Swift container %s", f.container)
|
|
|
|
}
|
|
|
|
|
2012-12-29 12:35:41 +01:00
|
|
|
// parseParse parses a swift 'url'
|
|
|
|
func parsePath(path string) (container, directory string, err error) {
|
2013-06-27 21:13:07 +02:00
|
|
|
parts := Match.FindAllStringSubmatch(path, -1)
|
2012-12-29 12:35:41 +01:00
|
|
|
if len(parts) != 1 || len(parts[0]) != 3 {
|
|
|
|
err = fmt.Errorf("Couldn't parse swift url %q", path)
|
|
|
|
} else {
|
|
|
|
container, directory = parts[0][1], parts[0][2]
|
|
|
|
directory = strings.Trim(directory, "/")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// swiftConnection makes a connection to swift
|
|
|
|
func swiftConnection() (*swift.Connection, error) {
|
|
|
|
if *userName == "" {
|
|
|
|
return nil, errors.New("Need -user or environmental variable ST_USER")
|
|
|
|
}
|
|
|
|
if *apiKey == "" {
|
|
|
|
return nil, errors.New("Need -key or environmental variable ST_KEY")
|
|
|
|
}
|
|
|
|
if *authUrl == "" {
|
|
|
|
return nil, errors.New("Need -auth or environmental variable ST_AUTH")
|
|
|
|
}
|
|
|
|
c := &swift.Connection{
|
|
|
|
UserName: *userName,
|
|
|
|
ApiKey: *apiKey,
|
|
|
|
AuthUrl: *authUrl,
|
|
|
|
}
|
|
|
|
err := c.Authenticate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// NewFs contstructs an FsSwift from the path, container:path
|
|
|
|
func NewFs(path string) (fs.Fs, error) {
|
2012-12-29 12:35:41 +01:00
|
|
|
container, directory, err := parsePath(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if directory != "" {
|
|
|
|
return nil, fmt.Errorf("Directories not supported yet in %q", path)
|
|
|
|
}
|
|
|
|
c, err := swiftConnection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-06-27 20:49:35 +02:00
|
|
|
f := &FsSwift{c: *c, container: container, root: directory}
|
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
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsSwift) NewFsObjectWithInfo(remote string, info *swift.Object) fs.Object {
|
2012-12-26 13:23:58 +01:00
|
|
|
fs := &FsObjectSwift{
|
|
|
|
swift: f,
|
|
|
|
remote: remote,
|
|
|
|
}
|
|
|
|
if info != nil {
|
|
|
|
// Set info but not meta
|
|
|
|
fs.info = *info
|
|
|
|
} else {
|
|
|
|
err := fs.readMetaData() // reads info and meta, returning an error
|
|
|
|
if err != nil {
|
2012-12-31 17:40:34 +01:00
|
|
|
// logged already FsDebug("Failed to read info: %s", err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsSwift) NewFsObject(remote string) fs.Object {
|
2012-12-26 13:23:58 +01:00
|
|
|
return f.NewFsObjectWithInfo(remote, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the path returning a channel of FsObjects
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsSwift) List() fs.ObjectsChan {
|
|
|
|
out := make(fs.ObjectsChan, fs.Config.Checkers)
|
2012-12-26 13:23:58 +01:00
|
|
|
go func() {
|
|
|
|
// FIXME use a smaller limit?
|
2012-12-28 17:38:51 +01:00
|
|
|
err := f.c.ObjectsWalk(f.container, nil, func(opts *swift.ObjectsOpts) (interface{}, error) {
|
|
|
|
objects, err := f.c.Objects(f.container, opts)
|
|
|
|
if err == nil {
|
|
|
|
for i := range objects {
|
|
|
|
object := &objects[i]
|
|
|
|
if fs := f.NewFsObjectWithInfo(object.Name, object); fs != nil {
|
|
|
|
out <- fs
|
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-28 17:38:51 +01:00
|
|
|
return objects, err
|
2012-12-26 13:23:58 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2012-12-26 13:23:58 +01:00
|
|
|
log.Printf("Couldn't read container %q: %s", f.container, err)
|
|
|
|
}
|
|
|
|
close(out)
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2013-01-23 23:43:20 +01:00
|
|
|
// Lists the containers
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsSwift) ListDir() fs.DirChan {
|
|
|
|
out := make(fs.DirChan, fs.Config.Checkers)
|
2013-01-23 23:43:20 +01:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
containers, err := f.c.ContainersAll(nil)
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2013-01-23 23:43:20 +01:00
|
|
|
log.Printf("Couldn't list containers: %s", err)
|
|
|
|
} else {
|
|
|
|
for _, container := range containers {
|
2013-06-28 09:57:32 +02:00
|
|
|
out <- &fs.Dir{
|
2013-01-23 23:43:20 +01:00
|
|
|
Name: container.Name,
|
|
|
|
Bytes: container.Bytes,
|
|
|
|
Count: container.Count,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Put the FsObject into the container
|
2013-01-10 22:58:46 +01:00
|
|
|
//
|
|
|
|
// Copy the reader in to the new object which is returned
|
|
|
|
//
|
|
|
|
// The new object may have been created
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsSwift) Put(in io.Reader, remote string, modTime time.Time, size int64) (fs.Object, error) {
|
2012-12-26 13:23:58 +01:00
|
|
|
// Temporary FsObject under construction
|
2013-01-10 22:58:46 +01:00
|
|
|
fs := &FsObjectSwift{swift: f, remote: remote}
|
2012-12-26 13:23:58 +01:00
|
|
|
|
|
|
|
// Set the mtime
|
|
|
|
m := swift.Metadata{}
|
2013-01-10 22:58:46 +01:00
|
|
|
m.SetModTime(modTime)
|
|
|
|
_, err := f.c.ObjectPut(f.container, remote, in, true, "", "", m.ObjectHeaders())
|
|
|
|
return fs, err
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the container if it doesn't exist
|
|
|
|
func (f *FsSwift) Mkdir() error {
|
|
|
|
return f.c.ContainerCreate(f.container, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir deletes the container
|
|
|
|
//
|
|
|
|
// Returns an error if it isn't empty
|
|
|
|
func (f *FsSwift) Rmdir() error {
|
|
|
|
return f.c.ContainerDelete(f.container)
|
|
|
|
}
|
|
|
|
|
2013-01-19 00:21:02 +01:00
|
|
|
// Return the precision
|
|
|
|
func (fs *FsSwift) Precision() time.Duration {
|
|
|
|
return time.Nanosecond
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
|
|
// Return the remote path
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) Remote() string {
|
|
|
|
return o.remote
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Md5sum returns the Md5sum of an object returning a lowercase hex string
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) Md5sum() (string, error) {
|
|
|
|
return strings.ToLower(o.info.Hash), nil
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) Size() int64 {
|
|
|
|
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
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) readMetaData() (err error) {
|
|
|
|
if o.meta != nil {
|
2012-12-26 13:23:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
2013-06-27 21:13:07 +02:00
|
|
|
info, h, err := o.swift.c.Object(o.swift.container, o.remote)
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Debug(o, "Failed to read info: %s", err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
meta := h.ObjectMetadata()
|
2013-06-27 21:13:07 +02:00
|
|
|
o.info = info
|
|
|
|
o.meta = &meta
|
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
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) ModTime() time.Time {
|
|
|
|
err := o.readMetaData()
|
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 metadata: %s", err)
|
2013-06-27 21:13:07 +02:00
|
|
|
return o.info.LastModified
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
2013-06-27 21:13:07 +02:00
|
|
|
modTime, err := o.meta.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
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the modification time of the local fs object
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) SetModTime(modTime time.Time) {
|
|
|
|
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()
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to read metadata: %s", err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return
|
|
|
|
}
|
2013-06-27 21:13:07 +02:00
|
|
|
o.meta.SetModTime(modTime)
|
|
|
|
err = o.swift.c.ObjectUpdate(o.swift.container, o.remote, o.meta.ObjectHeaders())
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Log(o, "Failed to update remote mtime: %s", err)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this object storable
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) Storable() bool {
|
2012-12-26 13:23:58 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open an object for read
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) Open() (in io.ReadCloser, err error) {
|
|
|
|
in, _, err = o.swift.c.ObjectOpen(o.swift.container, o.remote, true, nil)
|
2012-12-26 13:23:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectSwift) Remove() error {
|
|
|
|
return o.swift.c.ObjectDelete(o.swift.container, o.remote)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the interfaces are satisfied
|
2013-06-27 21:13:07 +02:00
|
|
|
var _ fs.Fs = &FsSwift{}
|
2013-06-28 09:57:32 +02:00
|
|
|
var _ fs.Object = &FsObjectSwift{}
|