2015-09-22 19:47:16 +02:00
|
|
|
// Package local provides a filesystem interface
|
2013-06-27 21:13:07 +02:00
|
|
|
package local
|
2012-12-26 13:23:58 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-01-19 00:21:02 +01:00
|
|
|
"io/ioutil"
|
2012-12-26 13:23:58 +01:00
|
|
|
"os"
|
2016-04-21 21:06:21 +02:00
|
|
|
"path"
|
2012-12-26 13:23:58 +01:00
|
|
|
"path/filepath"
|
2015-09-22 19:47:16 +02:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2013-01-19 00:21:02 +01:00
|
|
|
"sync"
|
2012-12-26 13:23:58 +01:00
|
|
|
"time"
|
2015-05-21 19:40:16 +02:00
|
|
|
"unicode/utf8"
|
2014-03-15 17:06:11 +01:00
|
|
|
|
2016-08-19 00:16:47 +02:00
|
|
|
"golang.org/x/text/unicode/norm"
|
|
|
|
|
2014-03-15 17:06:11 +01:00
|
|
|
"github.com/ncw/rclone/fs"
|
2016-05-30 20:49:21 +02:00
|
|
|
"github.com/pkg/errors"
|
2012-12-26 13:23:58 +01:00
|
|
|
)
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2016-02-18 12:35:25 +01:00
|
|
|
fsi := &fs.RegInfo{
|
2016-02-15 19:11:53 +01:00
|
|
|
Name: "local",
|
|
|
|
Description: "Local Disk",
|
|
|
|
NewFs: NewFs,
|
2016-02-29 17:57:23 +01:00
|
|
|
Options: []fs.Option{{
|
2016-01-04 12:28:47 +01:00
|
|
|
Name: "nounc",
|
|
|
|
Help: "Disable UNC (long path names) conversion on Windows",
|
|
|
|
Optional: true,
|
|
|
|
Examples: []fs.OptionExample{{
|
|
|
|
Value: "true",
|
|
|
|
Help: "Disables long file names",
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
fs.Register(fsi)
|
2013-06-27 21:13:07 +02:00
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// Fs represents a local filesystem rooted at root
|
|
|
|
type Fs struct {
|
2015-08-22 17:53:11 +02:00
|
|
|
name string // the name of the remote
|
2016-08-19 00:16:47 +02:00
|
|
|
root string // The root directory (OS path)
|
2015-05-21 19:40:16 +02:00
|
|
|
precisionOk sync.Once // Whether we need to read the precision
|
|
|
|
precision time.Duration // precision of local filesystem
|
2016-03-10 15:51:56 +01:00
|
|
|
wmu sync.Mutex // used for locking access to 'warned'.
|
2015-05-21 19:40:16 +02:00
|
|
|
warned map[string]struct{} // whether we have warned about this string
|
2016-01-04 12:28:47 +01:00
|
|
|
nounc bool // Skip UNC conversion on Windows
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// Object represents a local filesystem object
|
|
|
|
type Object struct {
|
2016-01-11 13:39:33 +01:00
|
|
|
fs *Fs // The Fs this object is part of
|
2016-07-15 15:18:09 +02:00
|
|
|
remote string // The remote path - properly UTF-8 encoded - for rclone
|
|
|
|
path string // The local path - may not be properly UTF-8 encoded - for OS
|
2016-01-11 13:39:33 +01:00
|
|
|
info os.FileInfo // Interface for file info (always present)
|
|
|
|
hashes map[fs.HashType]string // Hashes
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
2012-12-29 12:35:41 +01:00
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// NewFs constructs an Fs from the path
|
2014-03-15 17:06:11 +01:00
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
2015-09-11 11:37:12 +02:00
|
|
|
var err error
|
|
|
|
|
2016-01-04 12:28:47 +01:00
|
|
|
nounc, _ := fs.ConfigFile.GetValue(name, "nounc")
|
2015-11-07 12:14:46 +01:00
|
|
|
f := &Fs{
|
2015-08-22 17:53:11 +02:00
|
|
|
name: name,
|
2015-05-21 19:40:16 +02:00
|
|
|
warned: make(map[string]struct{}),
|
2016-01-04 12:28:47 +01:00
|
|
|
nounc: nounc == "true",
|
2015-05-21 19:40:16 +02:00
|
|
|
}
|
2016-08-19 00:16:47 +02:00
|
|
|
f.root = f.cleanPath(root)
|
2015-09-11 11:37:12 +02:00
|
|
|
|
2014-05-05 20:52:52 +02:00
|
|
|
// Check to see if this points to a file
|
|
|
|
fi, err := os.Lstat(f.root)
|
|
|
|
if err == nil && fi.Mode().IsRegular() {
|
|
|
|
// It is a file, so use the parent as the root
|
2016-06-25 22:23:20 +02:00
|
|
|
f.root, _ = getDirFile(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
|
|
|
|
}
|
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
|
|
|
return f.root
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:14:46 +01:00
|
|
|
// String converts this Fs to a string
|
|
|
|
func (f *Fs) String() string {
|
2012-12-31 17:40:34 +01:00
|
|
|
return fmt.Sprintf("Local file system at %s", f.root)
|
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// newObject makes a half completed Object
|
|
|
|
func (f *Fs) newObject(remote string) *Object {
|
2016-08-19 00:16:47 +02:00
|
|
|
dstPath := f.cleanPath(filepath.Join(f.root, remote))
|
|
|
|
remote = f.cleanRemote(remote)
|
2015-11-07 12:14:46 +01:00
|
|
|
return &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
path: dstPath,
|
|
|
|
}
|
2015-08-31 22:05:51 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// Return an Object from a path
|
2012-12-26 13:23:58 +01:00
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2016-06-25 22:23:20 +02:00
|
|
|
func (f *Fs) newObjectWithInfo(remote string, info os.FileInfo) (fs.Object, error) {
|
2016-06-25 22:58:34 +02:00
|
|
|
o := f.newObject(remote)
|
2012-12-26 13:23:58 +01:00
|
|
|
if info != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
o.info = info
|
2012-12-26 13:23:58 +01:00
|
|
|
} else {
|
2013-06-27 21:13:07 +02:00
|
|
|
err := o.lstat()
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2016-06-25 22:23:20 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
|
|
|
}
|
|
|
|
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 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
|
|
|
// listArgs is the arguments that a new list takes
|
|
|
|
type listArgs struct {
|
|
|
|
remote string
|
|
|
|
dirpath string
|
|
|
|
level int
|
|
|
|
}
|
|
|
|
|
|
|
|
// list traverses the directory passed in, listing to out.
|
|
|
|
// it returns a boolean whether it is finished or not.
|
|
|
|
func (f *Fs) list(out fs.ListOpts, remote string, dirpath string, level int) (subdirs []listArgs) {
|
|
|
|
fd, err := os.Open(dirpath)
|
|
|
|
if err != nil {
|
2016-05-30 20:49:21 +02:00
|
|
|
out.SetError(errors.Wrapf(err, "failed to open directory %q", dirpath))
|
2016-04-21 21:06:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := fd.Close()
|
|
|
|
if err != nil {
|
2016-05-30 20:49:21 +02:00
|
|
|
out.SetError(errors.Wrapf(err, "failed to close directory %q:", dirpath))
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
fis, err := fd.Readdir(1024)
|
|
|
|
if err == io.EOF && len(fis) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-05-30 20:49:21 +02:00
|
|
|
out.SetError(errors.Wrapf(err, "failed to read directory %q", dirpath))
|
|
|
|
|
2016-04-21 21:06:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
name := fi.Name()
|
|
|
|
newRemote := path.Join(remote, name)
|
|
|
|
newPath := filepath.Join(dirpath, name)
|
|
|
|
if fi.IsDir() {
|
|
|
|
if out.IncludeDirectory(newRemote) {
|
|
|
|
dir := &fs.Dir{
|
2016-08-19 00:16:47 +02:00
|
|
|
Name: f.cleanRemote(newRemote),
|
2016-04-21 21:06:21 +02:00
|
|
|
When: fi.ModTime(),
|
|
|
|
Bytes: 0,
|
|
|
|
Count: 0,
|
|
|
|
}
|
|
|
|
if out.AddDir(dir) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if level > 0 {
|
|
|
|
subdirs = append(subdirs, listArgs{remote: newRemote, dirpath: newPath, level: level - 1})
|
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
2016-04-21 21:06:21 +02:00
|
|
|
} else {
|
2016-06-25 22:23:20 +02:00
|
|
|
fso, err := f.newObjectWithInfo(newRemote, fi)
|
|
|
|
if err != nil {
|
|
|
|
out.SetError(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if fso.Storable() && out.Add(fso) {
|
|
|
|
return nil
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 21:06:21 +02:00
|
|
|
}
|
|
|
|
return subdirs
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the path into out
|
|
|
|
//
|
|
|
|
// Ignores everything which isn't Storable, eg links etc
|
2016-04-23 22:46:52 +02:00
|
|
|
func (f *Fs) List(out fs.ListOpts, dir string) {
|
2016-04-21 21:06:21 +02:00
|
|
|
defer out.Finished()
|
2016-08-19 00:16:47 +02:00
|
|
|
root := f.cleanPath(filepath.Join(f.root, dir))
|
|
|
|
dir = f.cleanRemote(dir)
|
2016-04-23 22:46:52 +02:00
|
|
|
_, err := os.Stat(root)
|
2016-04-21 21:06:21 +02:00
|
|
|
if err != nil {
|
|
|
|
out.SetError(fs.ErrorDirNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
in := make(chan listArgs, out.Buffer())
|
|
|
|
var wg sync.WaitGroup // sync closing of go routines
|
|
|
|
var traversing sync.WaitGroup // running directory traversals
|
|
|
|
|
|
|
|
// Start the process
|
|
|
|
traversing.Add(1)
|
2016-05-07 15:50:35 +02:00
|
|
|
in <- listArgs{remote: dir, dirpath: root, level: out.Level() - 1}
|
2016-04-21 21:06:21 +02:00
|
|
|
for i := 0; i < fs.Config.Checkers; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for job := range in {
|
|
|
|
if out.IsFinished() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newJobs := f.list(out, job.remote, job.dirpath, job.level)
|
|
|
|
// Now we have traversed this directory, send
|
|
|
|
// these ones off for traversal
|
|
|
|
if len(newJobs) != 0 {
|
|
|
|
traversing.Add(len(newJobs))
|
|
|
|
go func() {
|
|
|
|
for _, newJob := range newJobs {
|
|
|
|
in <- newJob
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
traversing.Done()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for traversal to finish
|
|
|
|
traversing.Wait()
|
|
|
|
close(in)
|
|
|
|
wg.Wait()
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:16:47 +02:00
|
|
|
// cleanRemote makes string a valid UTF-8 string for remote strings.
|
2015-05-21 19:40:16 +02:00
|
|
|
//
|
|
|
|
// Any invalid UTF-8 characters will be replaced with utf8.RuneError
|
2016-08-19 00:16:47 +02:00
|
|
|
// It also normalises the UTF-8 and converts the slashes if necessary.
|
|
|
|
func (f *Fs) cleanRemote(name string) string {
|
2015-09-11 11:37:12 +02:00
|
|
|
if !utf8.ValidString(name) {
|
2016-03-10 15:51:56 +01:00
|
|
|
f.wmu.Lock()
|
2015-09-11 11:37:12 +02:00
|
|
|
if _, ok := f.warned[name]; !ok {
|
|
|
|
fs.Debug(f, "Replacing invalid UTF-8 characters in %q", name)
|
|
|
|
f.warned[name] = struct{}{}
|
|
|
|
}
|
2016-03-10 15:51:56 +01:00
|
|
|
f.wmu.Unlock()
|
2015-09-11 11:37:12 +02:00
|
|
|
name = string([]rune(name))
|
2015-05-21 19:40:16 +02:00
|
|
|
}
|
2016-08-19 00:16:47 +02:00
|
|
|
name = norm.NFC.String(name)
|
|
|
|
name = filepath.ToSlash(name)
|
2015-09-11 11:37:12 +02:00
|
|
|
return name
|
2015-05-21 19:40:16 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// Put the Object to the local filesystem
|
2016-02-18 12:35:25 +01:00
|
|
|
func (f *Fs) Put(in io.Reader, src fs.ObjectInfo) (fs.Object, error) {
|
|
|
|
remote := src.Remote()
|
2016-06-25 22:58:34 +02:00
|
|
|
// Temporary Object under construction - info filled in by Update()
|
|
|
|
o := f.newObject(remote)
|
2016-02-18 12:35:25 +01:00
|
|
|
err := o.Update(in, src)
|
2014-07-19 12:34:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return o, nil
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates the directory if it doesn't exist
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Mkdir() error {
|
2015-09-11 11:37:12 +02:00
|
|
|
// FIXME: https://github.com/syncthing/syncthing/blob/master/lib/osutil/mkdirall_windows.go
|
2015-07-29 11:21:12 +02:00
|
|
|
return os.MkdirAll(f.root, 0777)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir removes the directory
|
|
|
|
//
|
|
|
|
// If it isn't empty it will return an error
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Rmdir() error {
|
2012-12-26 13:23:58 +01:00
|
|
|
return os.Remove(f.root)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Precision of the file system
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Precision() (precision time.Duration) {
|
2013-01-19 00:21:02 +01:00
|
|
|
f.precisionOk.Do(func() {
|
|
|
|
f.precision = f.readPrecision()
|
|
|
|
})
|
|
|
|
return f.precision
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the precision
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) readPrecision() (precision time.Duration) {
|
2013-01-19 00:21:02 +01:00
|
|
|
// Default precision of 1s
|
|
|
|
precision = time.Second
|
|
|
|
|
|
|
|
// Create temporary file and test it
|
2013-06-27 21:00:01 +02:00
|
|
|
fd, err := ioutil.TempFile("", "rclone")
|
2013-01-19 00:21:02 +01:00
|
|
|
if err != nil {
|
|
|
|
// If failed return 1s
|
|
|
|
// fmt.Println("Failed to create temp file", err)
|
|
|
|
return time.Second
|
|
|
|
}
|
|
|
|
path := fd.Name()
|
|
|
|
// fmt.Println("Created temp file", path)
|
2014-07-25 19:19:49 +02:00
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
return time.Second
|
|
|
|
}
|
2013-01-19 00:21:02 +01:00
|
|
|
|
|
|
|
// Delete it on return
|
|
|
|
defer func() {
|
|
|
|
// fmt.Println("Remove temp file")
|
2014-07-25 19:19:49 +02:00
|
|
|
_ = os.Remove(path) // ignore error
|
2013-01-19 00:21:02 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
// Find the minimum duration we can detect
|
|
|
|
for duration := time.Duration(1); duration < time.Second; duration *= 10 {
|
|
|
|
// Current time with delta
|
|
|
|
t := time.Unix(time.Now().Unix(), int64(duration))
|
2013-06-27 21:13:07 +02:00
|
|
|
err := os.Chtimes(path, t, t)
|
2013-01-19 00:21:02 +01:00
|
|
|
if err != nil {
|
|
|
|
// fmt.Println("Failed to Chtimes", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the actual time back
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
// fmt.Println("Failed to Stat", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it matches - have found the precision
|
|
|
|
// fmt.Println("compare", fi.ModTime(), t)
|
|
|
|
if fi.ModTime() == t {
|
|
|
|
// fmt.Println("Precision detected as", duration)
|
|
|
|
return duration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-19 13:00:42 +02:00
|
|
|
// Purge deletes all the files and directories
|
|
|
|
//
|
|
|
|
// Optional interface: Only implement this if you have a way of
|
|
|
|
// deleting all the files quicker than just running Remove() on the
|
|
|
|
// result of List()
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Purge() error {
|
2014-07-28 22:02:00 +02:00
|
|
|
fi, err := os.Lstat(f.root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.Mode().IsDir() {
|
2016-06-12 16:06:02 +02:00
|
|
|
return errors.Errorf("can't purge non directory: %q", f.root)
|
2014-07-28 22:02:00 +02:00
|
|
|
}
|
2014-07-19 13:00:42 +02:00
|
|
|
return os.RemoveAll(f.root)
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
// 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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) Move(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*Object)
|
2015-08-31 22:05:51 +02:00
|
|
|
if !ok {
|
|
|
|
fs.Debug(src, "Can't move - not same remote type")
|
|
|
|
return nil, fs.ErrorCantMove
|
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// Temporary Object under construction
|
|
|
|
dstObj := f.newObject(remote)
|
2015-08-31 22:05:51 +02:00
|
|
|
|
|
|
|
// Check it is a file if it exists
|
|
|
|
err := dstObj.lstat()
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// OK
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !dstObj.info.Mode().IsRegular() {
|
|
|
|
// It isn't a file
|
2016-06-12 16:06:02 +02:00
|
|
|
return nil, errors.New("can't move file onto non-file")
|
2015-08-31 22:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create destination
|
|
|
|
err = dstObj.mkdirAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the move
|
|
|
|
err = os.Rename(srcObj.path, dstObj.path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the info
|
|
|
|
err = dstObj.lstat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// DirMove moves src directory to this remote using server side move
|
|
|
|
// operations.
|
2015-08-31 22:05:51 +02: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
|
2015-11-07 12:14:46 +01:00
|
|
|
func (f *Fs) DirMove(src fs.Fs) error {
|
|
|
|
srcFs, ok := src.(*Fs)
|
2015-08-31 22:05:51 +02:00
|
|
|
if !ok {
|
|
|
|
fs.Debug(srcFs, "Can't move directory - not same remote type")
|
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
// Check if source exists
|
|
|
|
sstat, err := os.Lstat(srcFs.root)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// And is a directory
|
|
|
|
if !sstat.IsDir() {
|
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
// Check if destination exists
|
2015-09-22 19:47:16 +02:00
|
|
|
_, err = os.Lstat(f.root)
|
2015-08-31 22:05:51 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return fs.ErrorDirExists
|
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
// Do the move
|
2015-09-22 19:47:16 +02:00
|
|
|
return os.Rename(srcFs.root, f.root)
|
2015-08-31 22:05:51 +02:00
|
|
|
}
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
// Hashes returns the supported hash sets.
|
|
|
|
func (f *Fs) Hashes() fs.HashSet {
|
|
|
|
return fs.SupportedHashes
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-07-15 15:18:09 +02:00
|
|
|
return o.remote
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
// Hash returns the requested hash of a file as a lowercase hex string
|
|
|
|
func (o *Object) Hash(r fs.HashType) (string, error) {
|
|
|
|
// Check that the underlying file hasn't changed
|
|
|
|
oldtime := o.info.ModTime()
|
|
|
|
oldsize := o.info.Size()
|
2016-01-17 11:35:38 +01:00
|
|
|
err := o.lstat()
|
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.Wrap(err, "hash: failed to stat")
|
2016-01-17 11:35:38 +01:00
|
|
|
}
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
if !o.info.ModTime().Equal(oldtime) || oldsize != o.info.Size() {
|
|
|
|
o.hashes = nil
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
2016-01-11 13:39:33 +01:00
|
|
|
|
|
|
|
if o.hashes == nil {
|
|
|
|
o.hashes = make(map[fs.HashType]string)
|
|
|
|
in, err := os.Open(o.path)
|
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.Wrap(err, "hash: failed to open")
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
o.hashes, err = fs.HashStream(in)
|
|
|
|
closeErr := in.Close()
|
|
|
|
if err != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.Wrap(err, "hash: failed to read")
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
|
|
|
if closeErr != nil {
|
2016-06-12 16:06:02 +02:00
|
|
|
return "", errors.Wrap(closeErr, "hash: failed to close")
|
2016-01-11 13:39:33 +01:00
|
|
|
}
|
2014-07-19 12:06:25 +02:00
|
|
|
}
|
2016-01-11 13:39:33 +01:00
|
|
|
return o.hashes[r], 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.Size()
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) ModTime() time.Time {
|
2013-06-27 21:13:07 +02:00
|
|
|
return o.info.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 := os.Chtimes(o.path, modTime, modTime)
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2016-03-22 16:07:10 +01:00
|
|
|
return err
|
2014-07-24 23:51:34 +02:00
|
|
|
}
|
|
|
|
// Re-read metadata
|
2016-03-22 16:07:10 +01:00
|
|
|
return o.lstat()
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Storable returns a boolean showing if this object is storable
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Storable() bool {
|
2013-06-27 21:13:07 +02:00
|
|
|
mode := o.info.Mode()
|
2012-12-26 13:23:58 +01:00
|
|
|
if mode&(os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) != 0 {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Debug(o, "Can't transfer non file/directory")
|
2012-12-26 13:23:58 +01:00
|
|
|
return false
|
|
|
|
} else if mode&os.ModeDir != 0 {
|
2014-07-23 00:06:01 +02:00
|
|
|
// fs.Debug(o, "Skipping directory")
|
2012-12-26 13:23:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-07-19 12:06:25 +02:00
|
|
|
// localOpenFile wraps an io.ReadCloser and updates the md5sum of the
|
|
|
|
// object that is read
|
|
|
|
type localOpenFile struct {
|
2016-01-11 13:39:33 +01:00
|
|
|
o *Object // object that is open
|
|
|
|
in io.ReadCloser // handle we are wrapping
|
|
|
|
hash *fs.MultiHasher // currently accumulating hashes
|
2014-07-19 12:06:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes from the object - see io.Reader
|
|
|
|
func (file *localOpenFile) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = file.in.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
// Hash routines never return an error
|
|
|
|
_, _ = file.hash.Write(p[:n])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-19 14:49:34 +02:00
|
|
|
// Close the object and update the hashes
|
2014-07-19 12:06:25 +02:00
|
|
|
func (file *localOpenFile) Close() (err error) {
|
|
|
|
err = file.in.Close()
|
|
|
|
if err == nil {
|
2016-06-19 14:49:34 +02:00
|
|
|
if file.hash.Size() == file.o.Size() {
|
|
|
|
file.o.hashes = file.hash.Sums()
|
|
|
|
}
|
2014-07-19 12:06:25 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-06-27 21:13:07 +02:00
|
|
|
in, err = os.Open(o.path)
|
2014-07-19 12:06:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Update the md5sum as we go along
|
|
|
|
in = &localOpenFile{
|
|
|
|
o: o,
|
|
|
|
in: in,
|
2016-01-11 13:39:33 +01:00
|
|
|
hash: fs.NewMultiHasher(),
|
2014-07-19 12:06:25 +02:00
|
|
|
}
|
2012-12-26 13:23:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
// mkdirAll makes all the directories needed to store the object
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) mkdirAll() error {
|
2015-09-11 11:37:12 +02:00
|
|
|
dir, _ := getDirFile(o.path)
|
2015-08-31 22:05:51 +02:00
|
|
|
return os.MkdirAll(dir, 0777)
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Update the object from in with modTime and size
|
2016-02-18 12:35:25 +01:00
|
|
|
func (o *Object) Update(in io.Reader, src fs.ObjectInfo) error {
|
2015-08-31 22:05:51 +02:00
|
|
|
err := o.mkdirAll()
|
2014-04-18 18:04:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := os.Create(o.path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-18 12:35:25 +01:00
|
|
|
// Calculate the hash of the object we are reading as we go along
|
2016-01-11 13:39:33 +01:00
|
|
|
hash := fs.NewMultiHasher()
|
2014-07-19 12:06:25 +02:00
|
|
|
in = io.TeeReader(in, hash)
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
outErr := out.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if outErr != nil {
|
|
|
|
return outErr
|
|
|
|
}
|
|
|
|
|
2016-01-11 13:39:33 +01:00
|
|
|
// All successful so update the hashes
|
|
|
|
o.hashes = hash.Sums()
|
2014-07-19 12:06:25 +02:00
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Set the mtime
|
2016-03-22 16:07:10 +01:00
|
|
|
err = o.SetModTime(src.ModTime())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-19 12:34:44 +02:00
|
|
|
|
|
|
|
// ReRead info now that we have finished
|
|
|
|
return o.lstat()
|
2014-04-18 18:04:21 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 22:58:34 +02:00
|
|
|
// Stat a Object into info
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) lstat() error {
|
2013-06-27 21:13:07 +02:00
|
|
|
info, err := os.Lstat(o.path)
|
|
|
|
o.info = info
|
2012-12-26 13:23:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
2015-11-07 12:14:46 +01:00
|
|
|
func (o *Object) Remove() error {
|
2013-06-27 21:13:07 +02:00
|
|
|
return os.Remove(o.path)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:16:47 +02:00
|
|
|
// Return the directory and file from an OS path. Assumes
|
|
|
|
// os.PathSeparator is used.
|
2015-09-11 11:37:12 +02:00
|
|
|
func getDirFile(s string) (string, string) {
|
|
|
|
i := strings.LastIndex(s, string(os.PathSeparator))
|
2016-05-06 14:52:50 +02:00
|
|
|
dir, file := s[:i], s[i+1:]
|
|
|
|
if dir == "" {
|
|
|
|
dir = string(os.PathSeparator)
|
|
|
|
}
|
|
|
|
return dir, file
|
2015-09-11 11:37:12 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:16:47 +02:00
|
|
|
// cleanPathFragment cleans an OS path fragment which is part of a
|
|
|
|
// bigger path and not necessarily absolute
|
|
|
|
func cleanPathFragment(s string) string {
|
2016-05-30 20:44:15 +02:00
|
|
|
if s == "" {
|
|
|
|
return s
|
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
s = filepath.Clean(s)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
s = strings.Replace(s, `/`, `\`, -1)
|
2016-05-30 20:44:15 +02:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
|
2016-08-19 00:16:47 +02:00
|
|
|
// cleanPath cleans and makes absolute the path passed in and returns
|
|
|
|
// an OS path.
|
|
|
|
//
|
|
|
|
// The input might be in OS form or rclone form or a mixture, but the
|
|
|
|
// output is in OS form.
|
2016-05-30 20:44:15 +02:00
|
|
|
//
|
2016-08-19 00:16:47 +02:00
|
|
|
// On windows it makes the path UNC also and replaces any characters
|
|
|
|
// Windows can't deal with with their replacements.
|
|
|
|
func (f *Fs) cleanPath(s string) string {
|
|
|
|
s = cleanPathFragment(s)
|
2016-05-30 20:44:15 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
2015-09-11 11:37:12 +02:00
|
|
|
if !filepath.IsAbs(s) && !strings.HasPrefix(s, "\\") {
|
|
|
|
s2, err := filepath.Abs(s)
|
|
|
|
if err == nil {
|
|
|
|
s = s2
|
|
|
|
}
|
|
|
|
}
|
2016-08-19 00:16:47 +02:00
|
|
|
if !f.nounc {
|
|
|
|
// Convert to UNC
|
|
|
|
s = uncPath(s)
|
2016-01-04 12:28:47 +01:00
|
|
|
}
|
2016-08-19 00:16:47 +02:00
|
|
|
s = cleanWindowsName(f, s)
|
|
|
|
} else {
|
|
|
|
if !filepath.IsAbs(s) {
|
|
|
|
s2, err := filepath.Abs(s)
|
|
|
|
if err == nil {
|
|
|
|
s = s2
|
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-09-17 11:50:41 +02:00
|
|
|
// Pattern to match a windows absolute path: "c:\" and similar
|
|
|
|
var isAbsWinDrive = regexp.MustCompile(`^[a-zA-Z]\:\\`)
|
2015-09-11 11:37:12 +02:00
|
|
|
|
|
|
|
// uncPath converts an absolute Windows path
|
|
|
|
// to a UNC long path.
|
|
|
|
func uncPath(s string) string {
|
|
|
|
// UNC can NOT use "/", so convert all to "\"
|
|
|
|
s = strings.Replace(s, `/`, `\`, -1)
|
|
|
|
|
|
|
|
// If prefix is "\\", we already have a UNC path or server.
|
|
|
|
if strings.HasPrefix(s, `\\`) {
|
|
|
|
// If already long path, just keep it
|
|
|
|
if strings.HasPrefix(s, `\\?\`) {
|
|
|
|
return s
|
|
|
|
}
|
2015-09-17 11:50:41 +02:00
|
|
|
|
|
|
|
// Trim "\\" from path and add UNC prefix.
|
2015-09-11 11:37:12 +02:00
|
|
|
return `\\?\UNC\` + strings.TrimPrefix(s, `\\`)
|
|
|
|
}
|
2015-09-17 11:50:41 +02:00
|
|
|
if isAbsWinDrive.MatchString(s) {
|
2015-09-11 11:37:12 +02:00
|
|
|
return `\\?\` + s
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:16:47 +02:00
|
|
|
// cleanWindowsName will clean invalid Windows characters replacing them with _
|
2015-11-07 12:14:46 +01:00
|
|
|
func cleanWindowsName(f *Fs, name string) string {
|
2015-10-16 11:49:00 +02:00
|
|
|
original := name
|
|
|
|
var name2 string
|
|
|
|
if strings.HasPrefix(name, `\\?\`) {
|
|
|
|
name2 = `\\?\`
|
|
|
|
name = strings.TrimPrefix(name, `\\?\`)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(name, `//?/`) {
|
|
|
|
name2 = `//?/`
|
|
|
|
name = strings.TrimPrefix(name, `//?/`)
|
|
|
|
}
|
|
|
|
// Colon is allowed as part of a drive name X:\
|
|
|
|
colonAt := strings.Index(name, ":")
|
|
|
|
if colonAt > 0 && colonAt < 3 && len(name) > colonAt+1 {
|
|
|
|
// Copy to name2, which is unfiltered
|
|
|
|
name2 += name[0 : colonAt+1]
|
|
|
|
name = name[colonAt+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
name2 += strings.Map(func(r rune) rune {
|
|
|
|
switch r {
|
|
|
|
case '<', '>', '"', '|', '?', '*', ':':
|
|
|
|
return '_'
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, name)
|
|
|
|
|
|
|
|
if name2 != original && f != nil {
|
2016-03-10 15:51:56 +01:00
|
|
|
f.wmu.Lock()
|
2015-10-16 11:49:00 +02:00
|
|
|
if _, ok := f.warned[name]; !ok {
|
|
|
|
fs.Debug(f, "Replacing invalid characters in %q to %q", name, name2)
|
|
|
|
f.warned[name] = struct{}{}
|
|
|
|
}
|
2016-03-10 15:51:56 +01:00
|
|
|
f.wmu.Unlock()
|
2015-10-16 11:49:00 +02:00
|
|
|
}
|
|
|
|
return name2
|
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
_ fs.Purger = &Fs{}
|
|
|
|
_ fs.Mover = &Fs{}
|
|
|
|
_ fs.DirMover = &Fs{}
|
|
|
|
_ fs.Object = &Object{}
|
|
|
|
)
|