2015-09-23 01:47:16 +08:00
|
|
|
// Package fs is a generic file system interface for rclone object storage systems
|
2013-06-28 03:13:07 +08:00
|
|
|
package fs
|
2012-12-26 20:23:58 +08:00
|
|
|
|
|
|
|
import (
|
2013-01-01 00:40:34 +08:00
|
|
|
"fmt"
|
2012-12-26 20:23:58 +08:00
|
|
|
"io"
|
2013-01-01 00:40:34 +08:00
|
|
|
"log"
|
2016-04-22 03:06:21 +08:00
|
|
|
"math"
|
2016-06-05 01:49:27 +08:00
|
|
|
"os"
|
2015-03-03 01:04:34 +08:00
|
|
|
"path/filepath"
|
2013-06-28 03:13:07 +08:00
|
|
|
"regexp"
|
2016-02-16 02:11:53 +08:00
|
|
|
"sort"
|
2012-12-26 20:23:58 +08:00
|
|
|
"time"
|
2016-06-12 22:06:02 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2012-12-26 20:23:58 +08:00
|
|
|
)
|
|
|
|
|
2014-07-14 01:58:06 +08:00
|
|
|
// Constants
|
|
|
|
const (
|
2015-09-23 01:47:16 +08:00
|
|
|
// ModTimeNotSupported is a very large precision value to show
|
|
|
|
// mod time isn't supported on this Fs
|
2015-08-17 06:24:34 +08:00
|
|
|
ModTimeNotSupported = 100 * 365 * 24 * time.Hour
|
2016-04-22 03:06:21 +08:00
|
|
|
// MaxLevel is a sentinel representing an infinite depth for listings
|
|
|
|
MaxLevel = math.MaxInt32
|
2014-07-14 01:58:06 +08:00
|
|
|
)
|
|
|
|
|
2013-06-28 03:13:07 +08:00
|
|
|
// Globals
|
|
|
|
var (
|
2016-09-11 02:17:43 +08:00
|
|
|
// UserAgent set in the default Transport
|
2016-06-04 03:09:34 +08:00
|
|
|
UserAgent = "rclone/" + Version
|
2013-06-28 03:13:07 +08:00
|
|
|
// Filesystem registry
|
2016-02-18 19:35:25 +08:00
|
|
|
fsRegistry []*RegInfo
|
2015-09-23 01:47:16 +08:00
|
|
|
// ErrorNotFoundInConfigFile is returned by NewFs if not found in config file
|
2016-06-12 22:06:02 +08:00
|
|
|
ErrorNotFoundInConfigFile = errors.New("didn't find section in config file")
|
|
|
|
ErrorCantPurge = errors.New("can't purge directory")
|
|
|
|
ErrorCantCopy = errors.New("can't copy object - incompatible remotes")
|
|
|
|
ErrorCantMove = errors.New("can't move object - incompatible remotes")
|
|
|
|
ErrorCantDirMove = errors.New("can't move directory - incompatible remotes")
|
|
|
|
ErrorDirExists = errors.New("can't copy directory - destination already exists")
|
|
|
|
ErrorCantSetModTime = errors.New("can't set modified time")
|
|
|
|
ErrorDirNotFound = errors.New("directory not found")
|
2016-06-26 04:23:20 +08:00
|
|
|
ErrorObjectNotFound = errors.New("object not found")
|
2016-06-12 22:06:02 +08:00
|
|
|
ErrorLevelNotSupported = errors.New("level value not supported")
|
|
|
|
ErrorListAborted = errors.New("list aborted")
|
|
|
|
ErrorListOnlyRoot = errors.New("can only list from root")
|
2016-06-22 01:01:53 +08:00
|
|
|
ErrorIsFile = errors.New("is a file not a directory")
|
2016-06-25 21:28:26 +08:00
|
|
|
ErrorNotDeleting = errors.New("not deleting files as there were IO errors")
|
2016-07-11 18:36:46 +08:00
|
|
|
ErrorCantMoveOverlapping = errors.New("can't move files on overlapping remotes")
|
2013-06-28 03:13:07 +08:00
|
|
|
)
|
|
|
|
|
2016-02-18 19:35:25 +08:00
|
|
|
// RegInfo provides information about a filesystem
|
|
|
|
type RegInfo struct {
|
2014-05-06 02:52:52 +08:00
|
|
|
// Name of this fs
|
|
|
|
Name string
|
2016-02-16 02:11:53 +08:00
|
|
|
// Description of this fs - defaults to Name
|
|
|
|
Description string
|
2014-05-06 02:52:52 +08:00
|
|
|
// Create a new file system. If root refers to an existing
|
2016-06-22 01:01:53 +08:00
|
|
|
// object, then it should return a Fs which which points to
|
|
|
|
// the parent of that object and ErrorIsFile.
|
2014-05-06 02:52:52 +08:00
|
|
|
NewFs func(name string, root string) (Fs, error)
|
|
|
|
// Function to call to help with config
|
|
|
|
Config func(string)
|
|
|
|
// Options for the Fs configuration
|
2014-03-16 00:06:11 +08:00
|
|
|
Options []Option
|
2013-06-28 03:13:07 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Option is describes an option for the config wizard
|
2014-03-16 00:06:11 +08:00
|
|
|
type Option struct {
|
2016-08-15 00:16:06 +08:00
|
|
|
Name string
|
|
|
|
Help string
|
|
|
|
Optional bool
|
|
|
|
IsPassword bool
|
|
|
|
Examples OptionExamples
|
2014-03-16 00:06:11 +08:00
|
|
|
}
|
|
|
|
|
2016-02-16 02:11:53 +08:00
|
|
|
// OptionExamples is a slice of examples
|
|
|
|
type OptionExamples []OptionExample
|
|
|
|
|
|
|
|
// Len is part of sort.Interface.
|
|
|
|
func (os OptionExamples) Len() int { return len(os) }
|
|
|
|
|
|
|
|
// Swap is part of sort.Interface.
|
|
|
|
func (os OptionExamples) Swap(i, j int) { os[i], os[j] = os[j], os[i] }
|
|
|
|
|
|
|
|
// Less is part of sort.Interface.
|
|
|
|
func (os OptionExamples) Less(i, j int) bool { return os[i].Help < os[j].Help }
|
|
|
|
|
|
|
|
// Sort sorts an OptionExamples
|
|
|
|
func (os OptionExamples) Sort() { sort.Sort(os) }
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// OptionExample describes an example for an Option
|
2014-03-16 00:06:11 +08:00
|
|
|
type OptionExample struct {
|
|
|
|
Value string
|
|
|
|
Help string
|
|
|
|
}
|
|
|
|
|
2013-06-28 03:13:07 +08:00
|
|
|
// Register a filesystem
|
|
|
|
//
|
|
|
|
// Fs modules should use this in an init() function
|
2016-02-18 19:35:25 +08:00
|
|
|
func Register(info *RegInfo) {
|
2014-03-16 00:06:11 +08:00
|
|
|
fsRegistry = append(fsRegistry, info)
|
2013-06-28 03:13:07 +08:00
|
|
|
}
|
|
|
|
|
2016-05-08 00:12:34 +08:00
|
|
|
// ListFser is the interface for listing a remote Fs
|
|
|
|
type ListFser interface {
|
2016-04-24 04:46:52 +08:00
|
|
|
// List the objects and directories of the Fs starting from dir
|
2016-04-22 03:06:21 +08:00
|
|
|
//
|
2016-04-24 04:46:52 +08:00
|
|
|
// dir should be "" to start from the root, and should not
|
|
|
|
// have trailing slashes.
|
|
|
|
//
|
|
|
|
// This should return ErrDirNotFound (using out.SetError())
|
|
|
|
// if the directory isn't found.
|
2016-06-18 21:12:47 +08:00
|
|
|
//
|
|
|
|
// Fses must support recursion levels of fs.MaxLevel and 1.
|
|
|
|
// They may return ErrorLevelNotSupported otherwise.
|
2016-04-24 04:46:52 +08:00
|
|
|
List(out ListOpts, dir string)
|
2016-10-07 18:39:39 +08:00
|
|
|
|
|
|
|
// NewObject finds the Object at remote. If it can't be found
|
|
|
|
// it returns the error ErrorObjectNotFound.
|
|
|
|
NewObject(remote string) (Object, error)
|
2016-05-08 00:12:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fs is the interface a cloud storage system must provide
|
|
|
|
type Fs interface {
|
|
|
|
Info
|
|
|
|
ListFser
|
2013-01-24 06:43:20 +08:00
|
|
|
|
2013-01-19 02:54:19 +08:00
|
|
|
// Put in to the remote path with the modTime given of the given size
|
|
|
|
//
|
|
|
|
// May create the object even if it returns an error - if so
|
|
|
|
// will return the object and the error, otherwise will return
|
|
|
|
// nil and the error
|
2016-02-18 19:35:25 +08:00
|
|
|
Put(in io.Reader, src ObjectInfo) (Object, error)
|
2013-01-19 02:54:19 +08:00
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Mkdir makes the directory (container, bucket)
|
2014-07-25 05:50:11 +08:00
|
|
|
//
|
|
|
|
// Shouldn't return an error if it already exists
|
2016-11-26 05:52:43 +08:00
|
|
|
Mkdir(dir string) error
|
2013-01-19 02:54:19 +08:00
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Rmdir removes the directory (container, bucket) if empty
|
2014-07-25 05:50:11 +08:00
|
|
|
//
|
2014-07-29 04:02:00 +08:00
|
|
|
// Return an error if it doesn't exist or isn't empty
|
2016-11-26 05:52:43 +08:00
|
|
|
Rmdir(dir string) error
|
2016-02-18 19:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info provides an interface to reading information about a filesystem.
|
|
|
|
type Info interface {
|
|
|
|
// Name of the remote (as passed into NewFs)
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
// Root of the remote (as passed into NewFs)
|
|
|
|
Root() string
|
|
|
|
|
|
|
|
// String returns a description of the FS
|
|
|
|
String() string
|
2013-01-19 07:21:02 +08:00
|
|
|
|
|
|
|
// Precision of the ModTimes in this Fs
|
|
|
|
Precision() time.Duration
|
2016-01-11 20:39:33 +08:00
|
|
|
|
|
|
|
// Returns the supported hash types of the filesystem
|
|
|
|
Hashes() HashSet
|
2012-12-26 20:23:58 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Object is a filesystem like object provided by an Fs
|
2013-06-28 15:57:32 +08:00
|
|
|
type Object interface {
|
2016-02-18 19:35:25 +08:00
|
|
|
ObjectInfo
|
|
|
|
|
|
|
|
// SetModTime sets the metadata on the object to set the modification date
|
2016-03-22 23:07:10 +08:00
|
|
|
SetModTime(time.Time) error
|
2016-02-18 19:35:25 +08:00
|
|
|
|
|
|
|
// Open opens the file for read. Call Close() on the returned io.ReadCloser
|
2016-09-10 18:29:57 +08:00
|
|
|
Open(options ...OpenOption) (io.ReadCloser, error)
|
2016-02-18 19:35:25 +08:00
|
|
|
|
|
|
|
// Update in to the object with the modTime given of the given size
|
|
|
|
Update(in io.Reader, src ObjectInfo) error
|
|
|
|
|
|
|
|
// Removes this object
|
|
|
|
Remove() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectInfo contains information about an object.
|
|
|
|
type ObjectInfo interface {
|
2016-07-18 21:07:35 +08:00
|
|
|
BasicInfo
|
|
|
|
|
2016-02-18 19:35:25 +08:00
|
|
|
// Fs returns read only access to the Fs that this object is part of
|
|
|
|
Fs() Info
|
2014-03-29 01:56:04 +08:00
|
|
|
|
2016-02-18 19:35:25 +08:00
|
|
|
// Hash returns the selected checksum of the file
|
|
|
|
// If no checksum is available it returns ""
|
2016-01-11 20:39:33 +08:00
|
|
|
Hash(HashType) (string, error)
|
2013-01-19 02:54:19 +08:00
|
|
|
|
2016-07-18 21:07:35 +08:00
|
|
|
// Storable says whether this object can be stored
|
|
|
|
Storable() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// BasicInfo common interface for Dir and Object providing the very
|
|
|
|
// basic attributes of an object.
|
|
|
|
type BasicInfo interface {
|
2016-10-21 00:46:51 +08:00
|
|
|
// String returns a description of the Object
|
|
|
|
String() string
|
|
|
|
|
2016-07-18 21:07:35 +08:00
|
|
|
// Remote returns the remote path
|
|
|
|
Remote() string
|
|
|
|
|
2013-01-19 02:54:19 +08:00
|
|
|
// ModTime returns the modification date of the file
|
2015-08-17 06:24:34 +08:00
|
|
|
// It should return a best guess if one isn't available
|
2013-01-02 23:21:55 +08:00
|
|
|
ModTime() time.Time
|
2013-01-19 02:54:19 +08:00
|
|
|
|
|
|
|
// Size returns the size of the file
|
2012-12-26 20:23:58 +08:00
|
|
|
Size() int64
|
|
|
|
}
|
|
|
|
|
2016-09-22 05:13:24 +08:00
|
|
|
// MimeTyper is an optional interface for Object
|
|
|
|
type MimeTyper interface {
|
|
|
|
// MimeType returns the content type of the Object if
|
|
|
|
// known, or "" if not
|
|
|
|
MimeType() string
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Purger is an optional interfaces for Fs
|
2013-01-19 01:01:47 +08:00
|
|
|
type Purger interface {
|
|
|
|
// Purge all files in the root and the root directory
|
|
|
|
//
|
|
|
|
// Implement this if you have a way of deleting all the files
|
|
|
|
// quicker than just running Remove() on the result of List()
|
2014-07-29 04:02:00 +08:00
|
|
|
//
|
|
|
|
// Return an error if it doesn't exist
|
2013-01-19 01:01:47 +08:00
|
|
|
Purge() error
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Copier is an optional interface for Fs
|
2015-02-15 02:48:08 +08:00
|
|
|
type Copier interface {
|
|
|
|
// 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
|
|
|
|
Copy(src Object, remote string) (Object, error)
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Mover is an optional interface for Fs
|
2015-08-25 04:42:23 +08:00
|
|
|
type Mover interface {
|
|
|
|
// Move src to this remote using server side move 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.ErrorCantMove
|
|
|
|
Move(src Object, remote string) (Object, error)
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// DirMover is an optional interface for Fs
|
2015-08-25 04:42:23 +08:00
|
|
|
type DirMover interface {
|
2015-09-23 01:47:16 +08:00
|
|
|
// DirMove moves src to this remote using server side move
|
|
|
|
// operations.
|
2015-08-25 04:42:23 +08:00
|
|
|
//
|
|
|
|
// Will only be called if src.Fs().Name() == f.Name()
|
|
|
|
//
|
|
|
|
// If it isn't possible then return fs.ErrorCantDirMove
|
|
|
|
//
|
|
|
|
// If destination exists then return fs.ErrorDirExists
|
|
|
|
DirMove(src Fs) error
|
|
|
|
}
|
|
|
|
|
2015-11-08 23:27:55 +08:00
|
|
|
// UnWrapper is an optional interfaces for Fs
|
|
|
|
type UnWrapper interface {
|
|
|
|
// UnWrap returns the Fs that this Fs is wrapping
|
|
|
|
UnWrap() Fs
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:39:29 +08:00
|
|
|
// DirCacheFlusher is an optional interface for Fs
|
|
|
|
type DirCacheFlusher interface {
|
|
|
|
// DirCacheFlush resets the directory cache - used in testing
|
|
|
|
// as an optional interface
|
|
|
|
DirCacheFlush()
|
|
|
|
}
|
|
|
|
|
2016-06-12 22:06:27 +08:00
|
|
|
// PutUncheckeder is an optional interface for Fs
|
|
|
|
type PutUncheckeder interface {
|
|
|
|
// Put in to the remote path with the modTime given of the given size
|
|
|
|
//
|
|
|
|
// May create the object even if it returns an error - if so
|
|
|
|
// will return the object and the error, otherwise will return
|
|
|
|
// nil and the error
|
|
|
|
//
|
|
|
|
// May create duplicates or return errors if src already
|
|
|
|
// exists.
|
|
|
|
PutUnchecked(in io.Reader, src ObjectInfo) (Object, error)
|
|
|
|
}
|
|
|
|
|
2016-07-01 23:35:36 +08:00
|
|
|
// CleanUpper is an optional interfaces for Fs
|
|
|
|
type CleanUpper interface {
|
|
|
|
// CleanUp the trash in the Fs
|
|
|
|
//
|
|
|
|
// Implement this if you have a way of emptying the trash or
|
|
|
|
// otherwise cleaning up old versions of files.
|
|
|
|
CleanUp() error
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ObjectsChan is a channel of Objects
|
2013-06-28 15:57:32 +08:00
|
|
|
type ObjectsChan chan Object
|
2012-12-26 20:23:58 +08:00
|
|
|
|
2016-04-22 03:06:21 +08:00
|
|
|
// ListOpts describes the interface used for Fs.List operations
|
|
|
|
type ListOpts interface {
|
|
|
|
// Add an object to the output.
|
|
|
|
// If the function returns true, the operation has been aborted.
|
|
|
|
// Multiple goroutines can safely add objects concurrently.
|
|
|
|
Add(obj Object) (abort bool)
|
|
|
|
|
|
|
|
// Add a directory to the output.
|
|
|
|
// If the function returns true, the operation has been aborted.
|
|
|
|
// Multiple goroutines can safely add objects concurrently.
|
|
|
|
AddDir(dir *Dir) (abort bool)
|
|
|
|
|
|
|
|
// IncludeDirectory returns whether this directory should be
|
|
|
|
// included in the listing (and recursed into or not).
|
|
|
|
IncludeDirectory(remote string) bool
|
|
|
|
|
|
|
|
// SetError will set an error state, and will cause the listing to
|
|
|
|
// be aborted.
|
|
|
|
// Multiple goroutines can set the error state concurrently,
|
|
|
|
// but only the first will be returned to the caller.
|
|
|
|
SetError(err error)
|
|
|
|
|
|
|
|
// Level returns the level it should recurse to. Fses may
|
|
|
|
// ignore this in which case the listing will be less
|
|
|
|
// efficient.
|
|
|
|
Level() int
|
|
|
|
|
|
|
|
// Buffer returns the channel depth in use
|
|
|
|
Buffer() int
|
|
|
|
|
|
|
|
// Finished should be called when listing is finished
|
|
|
|
Finished()
|
|
|
|
|
|
|
|
// IsFinished returns whether Finished or SetError have been called
|
|
|
|
IsFinished() bool
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Objects is a slice of Object~s
|
2013-06-28 15:57:32 +08:00
|
|
|
type Objects []Object
|
2012-12-26 20:23:58 +08:00
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ObjectPair is a pair of Objects used to describe a potential copy
|
|
|
|
// operation.
|
2014-03-29 01:56:04 +08:00
|
|
|
type ObjectPair struct {
|
|
|
|
src, dst Object
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ObjectPairChan is a channel of ObjectPair
|
2014-03-29 01:56:04 +08:00
|
|
|
type ObjectPairChan chan ObjectPair
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Dir describes a directory for directory/container/bucket lists
|
2013-06-28 15:57:32 +08:00
|
|
|
type Dir struct {
|
2013-01-24 06:43:20 +08:00
|
|
|
Name string // name of the directory
|
|
|
|
When time.Time // modification or creation time - IsZero for unknown
|
|
|
|
Bytes int64 // size of directory and contents -1 for unknown
|
|
|
|
Count int64 // number of objects -1 for unknown
|
|
|
|
}
|
|
|
|
|
2016-10-21 00:46:51 +08:00
|
|
|
// String returns the name
|
|
|
|
func (d *Dir) String() string {
|
|
|
|
return d.Name
|
|
|
|
}
|
|
|
|
|
2016-07-18 21:07:35 +08:00
|
|
|
// Remote returns the remote path
|
|
|
|
func (d *Dir) Remote() string {
|
|
|
|
return d.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification date of the file
|
|
|
|
// It should return a best guess if one isn't available
|
|
|
|
func (d *Dir) ModTime() time.Time {
|
|
|
|
if !d.When.IsZero() {
|
|
|
|
return d.When
|
|
|
|
}
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the file
|
|
|
|
func (d *Dir) Size() int64 {
|
|
|
|
return d.Bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface
|
|
|
|
var _ BasicInfo = (*Dir)(nil)
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// DirChan is a channel of Dir objects
|
2013-06-28 15:57:32 +08:00
|
|
|
type DirChan chan *Dir
|
2013-01-24 06:43:20 +08:00
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// Find looks for an Info object for the name passed in
|
2014-03-16 00:06:11 +08:00
|
|
|
//
|
|
|
|
// Services are looked up in the config file
|
2016-02-18 19:35:25 +08:00
|
|
|
func Find(name string) (*RegInfo, error) {
|
2014-03-16 00:06:11 +08:00
|
|
|
for _, item := range fsRegistry {
|
|
|
|
if item.Name == name {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
}
|
2016-06-12 22:06:02 +08:00
|
|
|
return nil, errors.Errorf("didn't find filing system for %q", name)
|
2014-03-16 00:06:11 +08:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:26:14 +08:00
|
|
|
// MustFind looks for an Info object for the type name passed in
|
|
|
|
//
|
|
|
|
// Services are looked up in the config file
|
|
|
|
//
|
|
|
|
// Exits with a fatal error if not found
|
|
|
|
func MustFind(name string) *RegInfo {
|
|
|
|
fs, err := Find(name)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to find remote: %v", err)
|
|
|
|
}
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
2015-02-07 23:49:09 +08:00
|
|
|
// Pattern to match an rclone url
|
2015-10-13 00:54:09 +08:00
|
|
|
var matcher = regexp.MustCompile(`^([\w_ -]+):(.*)$`)
|
2015-02-07 23:49:09 +08:00
|
|
|
|
2016-06-22 01:01:53 +08:00
|
|
|
// ParseRemote deconstructs a path into configName, fsPath, looking up
|
|
|
|
// the fsName in the config file (returning NotFoundInConfigFile if not found)
|
|
|
|
func ParseRemote(path string) (fsInfo *RegInfo, configName, fsPath string, err error) {
|
|
|
|
parts := matcher.FindStringSubmatch(path)
|
|
|
|
var fsName string
|
|
|
|
fsName, configName, fsPath = "local", "local", path
|
|
|
|
if parts != nil && !isDriveLetter(parts[1]) {
|
|
|
|
configName, fsPath = parts[1], parts[2]
|
2016-12-21 02:03:09 +08:00
|
|
|
fsName = ConfigFileGet(configName, "type")
|
|
|
|
if fsName == "" {
|
2016-06-22 01:01:53 +08:00
|
|
|
return nil, "", "", ErrorNotFoundInConfigFile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// change native directory separators to / if there are any
|
|
|
|
fsPath = filepath.ToSlash(fsPath)
|
|
|
|
fsInfo, err = Find(fsName)
|
|
|
|
return fsInfo, configName, fsPath, err
|
|
|
|
}
|
|
|
|
|
2012-12-29 19:35:41 +08:00
|
|
|
// NewFs makes a new Fs object from the path
|
|
|
|
//
|
2014-08-01 04:24:52 +08:00
|
|
|
// The path is of the form remote:path
|
2014-03-16 00:06:11 +08:00
|
|
|
//
|
2014-08-01 04:24:52 +08:00
|
|
|
// Remotes are looked up in the config file. If the remote isn't
|
|
|
|
// found then NotFoundInConfigFile will be returned.
|
2015-02-07 23:49:09 +08:00
|
|
|
//
|
|
|
|
// On Windows avoid single character remote names as they can be mixed
|
|
|
|
// up with drive letters.
|
2012-12-29 19:35:41 +08:00
|
|
|
func NewFs(path string) (Fs, error) {
|
2016-06-22 01:01:53 +08:00
|
|
|
fsInfo, configName, fsPath, err := ParseRemote(path)
|
2014-03-16 00:06:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-22 01:01:53 +08:00
|
|
|
return fsInfo.NewFs(configName, fsPath)
|
2012-12-29 19:35:41 +08:00
|
|
|
}
|
|
|
|
|
2016-06-05 01:49:27 +08:00
|
|
|
// DebugLogger - logs to Stdout
|
|
|
|
var DebugLogger = log.New(os.Stdout, "", log.LstdFlags)
|
|
|
|
|
|
|
|
// makeLog produces a log string from the arguments passed in
|
|
|
|
func makeLog(o interface{}, text string, args ...interface{}) string {
|
2014-03-29 01:56:04 +08:00
|
|
|
out := fmt.Sprintf(text, args...)
|
2016-06-05 01:49:27 +08:00
|
|
|
if o == nil {
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v: %s", o, out)
|
2014-03-29 01:56:04 +08:00
|
|
|
}
|
|
|
|
|
2016-06-05 01:49:27 +08:00
|
|
|
// Debug writes debugging output for this Object or Fs
|
2014-03-29 01:56:04 +08:00
|
|
|
func Debug(o interface{}, text string, args ...interface{}) {
|
2013-06-28 03:13:07 +08:00
|
|
|
if Config.Verbose {
|
2016-06-05 01:49:27 +08:00
|
|
|
DebugLogger.Print(makeLog(o, text, args...))
|
2013-01-01 00:40:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 01:49:27 +08:00
|
|
|
// Log writes log output for this Object or Fs. This should be
|
|
|
|
// considered to be Info level logging.
|
2014-03-29 01:56:04 +08:00
|
|
|
func Log(o interface{}, text string, args ...interface{}) {
|
2013-06-28 03:13:07 +08:00
|
|
|
if !Config.Quiet {
|
2016-06-05 01:49:27 +08:00
|
|
|
log.Print(makeLog(o, text, args...))
|
2013-01-01 00:40:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 01:47:16 +08:00
|
|
|
// ErrorLog writes error log output for this Object or Fs. It
|
|
|
|
// unconditionally logs a message regardless of Config.Quiet or
|
|
|
|
// Config.Verbose.
|
2015-08-09 03:10:31 +08:00
|
|
|
func ErrorLog(o interface{}, text string, args ...interface{}) {
|
2016-06-05 01:49:27 +08:00
|
|
|
log.Print(makeLog(o, text, args...))
|
2015-08-09 03:10:31 +08:00
|
|
|
}
|
|
|
|
|
2015-11-29 02:13:08 +08:00
|
|
|
// CheckClose is a utility function used to check the return from
|
2012-12-26 20:23:58 +08:00
|
|
|
// Close in a defer statement.
|
2015-11-29 02:13:08 +08:00
|
|
|
func CheckClose(c io.Closer, err *error) {
|
2012-12-26 20:23:58 +08:00
|
|
|
cerr := c.Close()
|
|
|
|
if *err == nil {
|
|
|
|
*err = cerr
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 19:35:25 +08:00
|
|
|
|
|
|
|
// NewStaticObjectInfo returns a static ObjectInfo
|
|
|
|
// If hashes is nil and fs is not nil, the hash map will be replaced with
|
|
|
|
// empty hashes of the types supported by the fs.
|
|
|
|
func NewStaticObjectInfo(remote string, modTime time.Time, size int64, storable bool, hashes map[HashType]string, fs Info) ObjectInfo {
|
|
|
|
info := &staticObjectInfo{
|
|
|
|
remote: remote,
|
|
|
|
modTime: modTime,
|
|
|
|
size: size,
|
|
|
|
storable: storable,
|
|
|
|
hashes: hashes,
|
|
|
|
fs: fs,
|
|
|
|
}
|
|
|
|
if fs != nil && hashes == nil {
|
|
|
|
set := fs.Hashes().Array()
|
|
|
|
info.hashes = make(map[HashType]string)
|
|
|
|
for _, ht := range set {
|
|
|
|
info.hashes[ht] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
|
|
|
|
type staticObjectInfo struct {
|
|
|
|
remote string
|
|
|
|
modTime time.Time
|
|
|
|
size int64
|
|
|
|
storable bool
|
|
|
|
hashes map[HashType]string
|
|
|
|
fs Info
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *staticObjectInfo) Fs() Info { return i.fs }
|
|
|
|
func (i *staticObjectInfo) Remote() string { return i.remote }
|
2016-10-21 00:46:51 +08:00
|
|
|
func (i *staticObjectInfo) String() string { return i.remote }
|
2016-02-18 19:35:25 +08:00
|
|
|
func (i *staticObjectInfo) ModTime() time.Time { return i.modTime }
|
|
|
|
func (i *staticObjectInfo) Size() int64 { return i.size }
|
|
|
|
func (i *staticObjectInfo) Storable() bool { return i.storable }
|
|
|
|
func (i *staticObjectInfo) Hash(h HashType) (string, error) {
|
|
|
|
if len(i.hashes) == 0 {
|
|
|
|
return "", ErrHashUnsupported
|
|
|
|
}
|
|
|
|
if hash, ok := i.hashes[h]; ok {
|
|
|
|
return hash, nil
|
|
|
|
}
|
|
|
|
return "", ErrHashUnsupported
|
|
|
|
}
|