2012-12-26 13:23:58 +01:00
|
|
|
// File system interface
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
package fs
|
2012-12-26 13:23:58 +01:00
|
|
|
|
|
|
|
import (
|
2012-12-31 17:40:34 +01:00
|
|
|
"fmt"
|
2012-12-26 13:23:58 +01:00
|
|
|
"io"
|
2012-12-31 17:40:34 +01:00
|
|
|
"log"
|
2013-06-27 21:13:07 +02:00
|
|
|
"regexp"
|
2012-12-26 13:23:58 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-07-13 19:58:06 +02:00
|
|
|
// Constants
|
|
|
|
const (
|
|
|
|
// User agent for Fs which can set it
|
|
|
|
UserAgent = "rclone/" + Version
|
|
|
|
)
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Filesystem registry
|
2014-03-15 17:06:11 +01:00
|
|
|
fsRegistry []*FsInfo
|
2013-06-27 21:13:07 +02:00
|
|
|
)
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// Filesystem info
|
|
|
|
type FsInfo struct {
|
2014-05-05 20:52:52 +02:00
|
|
|
// Name of this fs
|
|
|
|
Name string
|
|
|
|
// Create a new file system. If root refers to an existing
|
|
|
|
// object, then it should return a Fs which only returns that
|
|
|
|
// object.
|
|
|
|
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-15 17:06:11 +01:00
|
|
|
Options []Option
|
2013-06-27 21:13:07 +02:00
|
|
|
}
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// An options for a Fs
|
|
|
|
type Option struct {
|
|
|
|
Name string
|
|
|
|
Help string
|
|
|
|
Optional bool
|
|
|
|
Examples []OptionExample
|
|
|
|
}
|
|
|
|
|
|
|
|
// An example for an option
|
|
|
|
type OptionExample struct {
|
|
|
|
Value string
|
|
|
|
Help string
|
|
|
|
}
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// Register a filesystem
|
|
|
|
//
|
|
|
|
// Fs modules should use this in an init() function
|
2014-03-15 17:06:11 +01:00
|
|
|
func Register(info *FsInfo) {
|
|
|
|
fsRegistry = append(fsRegistry, info)
|
2013-06-27 21:13:07 +02:00
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// A Filesystem, describes the local filesystem and the remote object store
|
|
|
|
type Fs interface {
|
2013-01-18 19:54:19 +01:00
|
|
|
// String returns a description of the FS
|
2012-12-31 17:40:34 +01:00
|
|
|
String() string
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// List the Fs into a channel
|
2013-06-28 09:57:32 +02:00
|
|
|
List() ObjectsChan
|
2013-01-18 19:54:19 +01:00
|
|
|
|
2013-01-23 23:43:20 +01:00
|
|
|
// List the Fs directories/buckets/containers into a channel
|
2013-06-28 09:57:32 +02:00
|
|
|
ListDir() DirChan
|
2013-01-23 23:43:20 +01:00
|
|
|
|
2013-06-28 09:57:32 +02:00
|
|
|
// Find the Object at remote. Returns nil if can't be found
|
|
|
|
NewFsObject(remote string) Object
|
2013-01-18 19:54:19 +01: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
|
2013-06-28 09:57:32 +02:00
|
|
|
Put(in io.Reader, remote string, modTime time.Time, size int64) (Object, error)
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// Make the directory (container, bucket)
|
2012-12-26 13:23:58 +01:00
|
|
|
Mkdir() error
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// Remove the directory (container, bucket) if empty
|
2012-12-26 13:23:58 +01:00
|
|
|
Rmdir() error
|
2013-01-19 00:21:02 +01:00
|
|
|
|
|
|
|
// Precision of the ModTimes in this Fs
|
|
|
|
Precision() time.Duration
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A filesystem like object which can either be a remote object or a
|
|
|
|
// local file/directory
|
2013-06-28 09:57:32 +02:00
|
|
|
type Object interface {
|
2014-03-28 18:56:04 +01:00
|
|
|
// String returns a description of the Object
|
|
|
|
String() string
|
|
|
|
|
|
|
|
// Fs returns the Fs that this object is part of
|
|
|
|
Fs() Fs
|
|
|
|
|
2013-01-18 19:54:19 +01:00
|
|
|
// Remote returns the remote path
|
2012-12-26 13:23:58 +01:00
|
|
|
Remote() string
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// Md5sum returns the md5 checksum of the file
|
2012-12-26 13:23:58 +01:00
|
|
|
Md5sum() (string, error)
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// ModTime returns the modification date of the file
|
2013-01-02 16:21:55 +01:00
|
|
|
ModTime() time.Time
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// SetModTime sets the metadata on the object to set the modification date
|
2012-12-26 13:23:58 +01:00
|
|
|
SetModTime(time.Time)
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// Size returns the size of the file
|
2012-12-26 13:23:58 +01:00
|
|
|
Size() int64
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// Open opens the file for read. Call Close() on the returned io.ReadCloser
|
2012-12-26 13:23:58 +01:00
|
|
|
Open() (io.ReadCloser, error)
|
2013-01-18 19:54:19 +01:00
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Update in to the object with the modTime given of the given size
|
|
|
|
Update(in io.Reader, modTime time.Time, size int64) error
|
|
|
|
|
2013-01-18 19:54:19 +01:00
|
|
|
// Storable says whether this object can be stored
|
2012-12-26 13:23:58 +01:00
|
|
|
Storable() bool
|
2013-01-18 19:54:19 +01:00
|
|
|
|
|
|
|
// Removes this object
|
2012-12-26 13:23:58 +01:00
|
|
|
Remove() error
|
|
|
|
}
|
|
|
|
|
2013-01-18 18:01:47 +01:00
|
|
|
// Optional interfaces
|
|
|
|
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()
|
|
|
|
Purge() error
|
|
|
|
}
|
|
|
|
|
2013-06-28 09:57:32 +02:00
|
|
|
// A channel of Objects
|
|
|
|
type ObjectsChan chan Object
|
2012-12-26 13:23:58 +01:00
|
|
|
|
2013-06-28 09:57:32 +02:00
|
|
|
// A slice of Objects
|
|
|
|
type Objects []Object
|
2012-12-26 13:23:58 +01:00
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
// A pair of Objects
|
|
|
|
type ObjectPair struct {
|
|
|
|
src, dst Object
|
|
|
|
}
|
|
|
|
|
|
|
|
// A channel of ObjectPair
|
|
|
|
type ObjectPairChan chan ObjectPair
|
|
|
|
|
2013-01-23 23:43:20 +01:00
|
|
|
// A structure of directory/container/bucket lists
|
2013-06-28 09:57:32 +02:00
|
|
|
type Dir struct {
|
2013-01-23 23:43:20 +01: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
|
|
|
|
}
|
|
|
|
|
2013-06-28 09:57:32 +02:00
|
|
|
// A channel of Dir objects
|
|
|
|
type DirChan chan *Dir
|
2013-01-23 23:43:20 +01:00
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
// Pattern to match a url
|
2014-03-27 18:31:57 +01:00
|
|
|
var matcher = regexp.MustCompile(`^([\w_-]+):(.*)$`)
|
2014-03-15 17:06:11 +01:00
|
|
|
|
|
|
|
// Finds a FsInfo object for the name passed in
|
|
|
|
//
|
|
|
|
// Services are looked up in the config file
|
|
|
|
func Find(name string) (*FsInfo, error) {
|
|
|
|
for _, item := range fsRegistry {
|
|
|
|
if item.Name == name {
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Didn't find filing system for %q", name)
|
|
|
|
}
|
|
|
|
|
2012-12-29 12:35:41 +01:00
|
|
|
// NewFs makes a new Fs object from the path
|
|
|
|
//
|
2014-03-15 17:06:11 +01:00
|
|
|
// The path is of the form service://path
|
|
|
|
//
|
|
|
|
// Services are looked up in the config file
|
2012-12-29 12:35:41 +01:00
|
|
|
func NewFs(path string) (Fs, error) {
|
2014-03-15 17:06:11 +01:00
|
|
|
parts := matcher.FindStringSubmatch(path)
|
|
|
|
fsName, configName, fsPath := "local", "local", path
|
|
|
|
if parts != nil {
|
|
|
|
configName, fsPath = parts[1], parts[2]
|
|
|
|
var err error
|
|
|
|
fsName, err = ConfigFile.GetValue(configName, "type")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Didn't find section in config file for %q", configName)
|
2013-06-27 21:13:07 +02:00
|
|
|
}
|
2013-01-15 00:38:18 +01:00
|
|
|
}
|
2014-03-15 17:06:11 +01:00
|
|
|
fs, err := Find(fsName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fs.NewFs(configName, fsPath)
|
2012-12-29 12:35:41 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
// Outputs log for object
|
|
|
|
func OutputLog(o interface{}, text string, args ...interface{}) {
|
|
|
|
description := ""
|
|
|
|
if x, ok := o.(fmt.Stringer); ok {
|
|
|
|
description = x.String() + ": "
|
|
|
|
}
|
|
|
|
out := fmt.Sprintf(text, args...)
|
|
|
|
log.Print(description + out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write debuging output for this Object or Fs
|
|
|
|
func Debug(o interface{}, text string, args ...interface{}) {
|
2013-06-27 21:13:07 +02:00
|
|
|
if Config.Verbose {
|
2014-03-28 18:56:04 +01:00
|
|
|
OutputLog(o, text, args...)
|
2012-12-31 17:40:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
// Write log output for this Object or Fs
|
|
|
|
func Log(o interface{}, text string, args ...interface{}) {
|
2013-06-27 21:13:07 +02:00
|
|
|
if !Config.Quiet {
|
2014-03-28 18:56:04 +01:00
|
|
|
OutputLog(o, text, args...)
|
2012-12-31 17:40:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// checkClose is a utility function used to check the return from
|
|
|
|
// Close in a defer statement.
|
|
|
|
func checkClose(c io.Closer, err *error) {
|
|
|
|
cerr := c.Close()
|
|
|
|
if *err == nil {
|
|
|
|
*err = cerr
|
|
|
|
}
|
|
|
|
}
|