2012-12-26 13:23:58 +01:00
|
|
|
// Local filesystem interface
|
2013-06-27 21:13:07 +02:00
|
|
|
package local
|
2012-12-26 13:23:58 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
2014-07-19 12:06:25 +02:00
|
|
|
"encoding/hex"
|
2012-12-26 13:23:58 +01:00
|
|
|
"fmt"
|
2014-07-19 12:06:25 +02:00
|
|
|
"hash"
|
2012-12-26 13:23:58 +01:00
|
|
|
"io"
|
2013-01-19 00:21:02 +01:00
|
|
|
"io/ioutil"
|
2012-12-26 13:23:58 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2015-09-11 11:37:12 +02:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2012-12-26 13:23:58 +01:00
|
|
|
)
|
|
|
|
|
2013-06-27 21:13:07 +02:00
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2014-03-15 17:06:11 +01:00
|
|
|
fs.Register(&fs.FsInfo{
|
|
|
|
Name: "local",
|
|
|
|
NewFs: NewFs,
|
|
|
|
})
|
2013-06-27 21:13:07 +02:00
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// FsLocal represents a local filesystem rooted at root
|
|
|
|
type FsLocal struct {
|
2015-08-22 17:53:11 +02:00
|
|
|
name string // the name of the remote
|
2015-05-21 19:40:16 +02:00
|
|
|
root string // The root directory
|
|
|
|
precisionOk sync.Once // Whether we need to read the precision
|
|
|
|
precision time.Duration // precision of local filesystem
|
|
|
|
warned map[string]struct{} // whether we have warned about this string
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FsObjectLocal represents a local filesystem object
|
|
|
|
type FsObjectLocal struct {
|
2015-05-21 19:40:16 +02:00
|
|
|
local *FsLocal // The Fs this object is part of
|
2012-12-26 13:23:58 +01:00
|
|
|
remote string // The remote path
|
|
|
|
path string // The local path
|
2014-07-19 12:34:44 +02:00
|
|
|
info os.FileInfo // Interface for file info (always present)
|
2014-07-19 12:06:25 +02:00
|
|
|
md5sum string // the md5sum of the object or "" if not calculated
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
2012-12-29 12:35:41 +01:00
|
|
|
|
2015-09-11 11:37:12 +02:00
|
|
|
// NewFs constructs an FsLocal 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
|
|
|
|
|
2015-05-21 19:40:16 +02:00
|
|
|
f := &FsLocal{
|
2015-08-22 17:53:11 +02:00
|
|
|
name: name,
|
2015-05-21 19:40:16 +02:00
|
|
|
warned: make(map[string]struct{}),
|
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
f.root = filterPath(f.cleanUtf8(root))
|
|
|
|
|
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
|
2015-09-11 11:37:12 +02:00
|
|
|
var remote string
|
|
|
|
f.root, remote = getDirFile(f.root)
|
2014-05-05 20:52:52 +02:00
|
|
|
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
|
|
|
|
2015-08-22 17:53:11 +02:00
|
|
|
// The name of the remote (as passed into NewFs)
|
|
|
|
func (f *FsLocal) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2015-09-01 21:45:27 +02:00
|
|
|
// The root of the remote (as passed into NewFs)
|
|
|
|
func (f *FsLocal) Root() string {
|
|
|
|
return f.root
|
|
|
|
}
|
|
|
|
|
2012-12-31 17:40:34 +01:00
|
|
|
// String converts this FsLocal to a string
|
|
|
|
func (f *FsLocal) String() string {
|
|
|
|
return fmt.Sprintf("Local file system at %s", f.root)
|
|
|
|
}
|
|
|
|
|
2015-08-31 22:05:51 +02:00
|
|
|
// newFsObject makes a half completed FsObjectLocal
|
|
|
|
func (f *FsLocal) newFsObject(remote string) *FsObjectLocal {
|
|
|
|
remote = filepath.ToSlash(remote)
|
2015-09-11 11:37:12 +02:00
|
|
|
dstPath := filterPath(filepath.Join(f.root, f.cleanUtf8(remote)))
|
2015-08-31 22:05:51 +02:00
|
|
|
return &FsObjectLocal{local: f, remote: remote, path: dstPath}
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Return an FsObject from a path
|
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2014-07-29 18:50:07 +02:00
|
|
|
func (f *FsLocal) newFsObjectWithInfo(remote string, info os.FileInfo) fs.Object {
|
2015-08-31 22:05:51 +02:00
|
|
|
o := f.newFsObject(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 {
|
2015-08-31 22:05:51 +02:00
|
|
|
fs.Debug(o, "Failed to stat %s: %s", o.path, err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 21:13:07 +02:00
|
|
|
return o
|
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 *FsLocal) 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
|
|
|
}
|
|
|
|
|
2012-12-28 17:38:51 +01:00
|
|
|
// List the path returning a channel of FsObjects
|
2012-12-26 13:23:58 +01:00
|
|
|
//
|
2012-12-28 17:38:51 +01:00
|
|
|
// Ignores everything which isn't Storable, eg links etc
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsLocal) List() fs.ObjectsChan {
|
|
|
|
out := make(fs.ObjectsChan, fs.Config.Checkers)
|
2012-12-26 13:23:58 +01:00
|
|
|
go func() {
|
|
|
|
err := filepath.Walk(f.root, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Failed to open directory: %s: %s", path, err)
|
2012-12-26 13:23:58 +01:00
|
|
|
} else {
|
|
|
|
remote, err := filepath.Rel(f.root, path)
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Failed to get relative path %s: %s", path, err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if remote == "." {
|
|
|
|
return nil
|
|
|
|
// remote = ""
|
|
|
|
}
|
2014-07-29 18:50:07 +02:00
|
|
|
if fs := f.newFsObjectWithInfo(remote, fi); fs != nil {
|
2012-12-26 13:23:58 +01:00
|
|
|
if fs.Storable() {
|
|
|
|
out <- fs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Failed to open directory: %s: %s", f.root, err)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
close(out)
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2015-05-21 19:40:16 +02:00
|
|
|
// CleanUtf8 makes string a valid UTF-8 string
|
|
|
|
//
|
|
|
|
// Any invalid UTF-8 characters will be replaced with utf8.RuneError
|
|
|
|
func (f *FsLocal) cleanUtf8(name string) string {
|
2015-09-11 11:37:12 +02:00
|
|
|
if !utf8.ValidString(name) {
|
|
|
|
if _, ok := f.warned[name]; !ok {
|
|
|
|
fs.Debug(f, "Replacing invalid UTF-8 characters in %q", name)
|
|
|
|
f.warned[name] = struct{}{}
|
|
|
|
}
|
|
|
|
name = string([]rune(name))
|
2015-05-21 19:40:16 +02:00
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
var name2 string
|
|
|
|
if strings.HasPrefix(name, `\\?\`) {
|
|
|
|
name2 = `\\?\`
|
|
|
|
strings.TrimPrefix(name, `\\?\`)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(name, `//?/`) {
|
|
|
|
name2 = `//?/`
|
|
|
|
strings.TrimPrefix(name, `//?/`)
|
|
|
|
}
|
|
|
|
name2 += strings.Map(func(r rune) rune {
|
|
|
|
switch r {
|
|
|
|
case '<', '>', '"', '|', '?', '*', '&':
|
|
|
|
return '_'
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, name)
|
|
|
|
|
|
|
|
if name2 != name {
|
|
|
|
if _, ok := f.warned[name]; !ok {
|
|
|
|
fs.Debug(f, "Replacing invalid UTF-8 characters in %q", name)
|
|
|
|
f.warned[name] = struct{}{}
|
|
|
|
}
|
|
|
|
name = name2
|
|
|
|
}
|
2015-05-21 19:40:16 +02:00
|
|
|
}
|
2015-09-11 11:37:12 +02:00
|
|
|
return name
|
2015-05-21 19:40:16 +02:00
|
|
|
}
|
|
|
|
|
2013-01-23 23:43:20 +01:00
|
|
|
// Walk the path returning a channel of FsObjects
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsLocal) ListDir() fs.DirChan {
|
|
|
|
out := make(fs.DirChan, fs.Config.Checkers)
|
2013-01-23 23:43:20 +01:00
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
items, err := ioutil.ReadDir(f.root)
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Couldn't find read directory: %s", err)
|
2013-01-23 23:43:20 +01:00
|
|
|
} else {
|
|
|
|
for _, item := range items {
|
|
|
|
if item.IsDir() {
|
2013-06-28 09:57:32 +02:00
|
|
|
dir := &fs.Dir{
|
2015-05-21 19:40:16 +02:00
|
|
|
Name: f.cleanUtf8(item.Name()),
|
2013-01-23 23:43:20 +01:00
|
|
|
When: item.ModTime(),
|
|
|
|
Bytes: 0,
|
|
|
|
Count: 0,
|
|
|
|
}
|
|
|
|
// Go down the tree to count the files and directories
|
2015-09-11 11:37:12 +02:00
|
|
|
dirpath := filterPath(filepath.Join(f.root, item.Name()))
|
2013-01-23 23:43:20 +01:00
|
|
|
err := filepath.Walk(dirpath, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Failed to open directory: %s: %s", path, err)
|
2013-01-23 23:43:20 +01:00
|
|
|
} else {
|
|
|
|
dir.Count += 1
|
|
|
|
dir.Bytes += fi.Size()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2013-06-27 21:13:07 +02:00
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(f, "Failed to open directory: %s: %s", dirpath, err)
|
2013-01-23 23:43:20 +01:00
|
|
|
}
|
|
|
|
out <- dir
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// err := f.findRoot(false)
|
|
|
|
}()
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Puts the FsObject to the local filesystem
|
2013-06-28 09:57:32 +02:00
|
|
|
func (f *FsLocal) Put(in io.Reader, remote string, modTime time.Time, size int64) (fs.Object, error) {
|
2014-07-19 12:34:44 +02:00
|
|
|
// Temporary FsObject under construction - info filled in by Update()
|
2015-08-31 22:05:51 +02:00
|
|
|
o := f.newFsObject(remote)
|
2014-07-19 12:34:44 +02:00
|
|
|
err := o.Update(in, modTime, size)
|
|
|
|
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
|
|
|
|
func (f *FsLocal) 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
|
|
|
|
func (f *FsLocal) Rmdir() error {
|
|
|
|
return os.Remove(f.root)
|
|
|
|
}
|
|
|
|
|
2013-01-19 00:21:02 +01:00
|
|
|
// Return the precision
|
|
|
|
func (f *FsLocal) Precision() (precision time.Duration) {
|
|
|
|
f.precisionOk.Do(func() {
|
|
|
|
f.precision = f.readPrecision()
|
|
|
|
})
|
|
|
|
return f.precision
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the precision
|
|
|
|
func (f *FsLocal) readPrecision() (precision time.Duration) {
|
|
|
|
// 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()
|
|
|
|
func (f *FsLocal) 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() {
|
|
|
|
return fmt.Errorf("Can't Purge non directory: %q", f.root)
|
|
|
|
}
|
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
|
|
|
|
func (dstFs *FsLocal) Move(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*FsObjectLocal)
|
|
|
|
if !ok {
|
|
|
|
fs.Debug(src, "Can't move - not same remote type")
|
|
|
|
return nil, fs.ErrorCantMove
|
|
|
|
}
|
|
|
|
|
|
|
|
// Temporary FsObject under construction
|
|
|
|
dstObj := dstFs.newFsObject(remote)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
return nil, fmt.Errorf("Can't move file onto non-file")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move src to this remote using server side move operations.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
func (dstFs *FsLocal) DirMove(src fs.Fs) error {
|
|
|
|
srcFs, ok := src.(*FsLocal)
|
|
|
|
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-11 11:37:12 +02:00
|
|
|
_, err = os.Lstat(dstFs.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
|
|
|
|
return os.Rename(srcFs.root, dstFs.root)
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
// Return the parent Fs
|
|
|
|
func (o *FsObjectLocal) Fs() fs.Fs {
|
|
|
|
return o.local
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a string version
|
|
|
|
func (o *FsObjectLocal) String() string {
|
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Return the remote path
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectLocal) Remote() string {
|
2015-05-21 19:40:16 +02:00
|
|
|
return o.local.cleanUtf8(o.remote)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Md5sum calculates the Md5sum of a file returning a lowercase hex string
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectLocal) Md5sum() (string, error) {
|
2014-07-19 12:06:25 +02:00
|
|
|
if o.md5sum != "" {
|
|
|
|
return o.md5sum, nil
|
|
|
|
}
|
2013-06-27 21:13:07 +02:00
|
|
|
in, err := os.Open(o.path)
|
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 open: %s", err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
hash := md5.New()
|
|
|
|
_, err = io.Copy(hash, in)
|
2014-07-19 12:06:25 +02:00
|
|
|
closeErr := in.Close()
|
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: %s", err)
|
2012-12-26 13:23:58 +01:00
|
|
|
return "", err
|
|
|
|
}
|
2014-07-19 12:06:25 +02:00
|
|
|
if closeErr != nil {
|
|
|
|
fs.Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
fs.ErrorLog(o, "Failed to close: %s", closeErr)
|
2014-07-19 12:06:25 +02:00
|
|
|
return "", closeErr
|
|
|
|
}
|
|
|
|
o.md5sum = hex.EncodeToString(hash.Sum(nil))
|
|
|
|
return o.md5sum, 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 *FsObjectLocal) Size() int64 {
|
|
|
|
return o.info.Size()
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectLocal) ModTime() time.Time {
|
|
|
|
return o.info.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 *FsObjectLocal) SetModTime(modTime time.Time) {
|
|
|
|
err := os.Chtimes(o.path, modTime, modTime)
|
2012-12-26 13:23:58 +01:00
|
|
|
if err != nil {
|
2013-06-28 09:57:32 +02:00
|
|
|
fs.Debug(o, "Failed to set mtime on file: %s", err)
|
2014-07-24 23:51:34 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Re-read metadata
|
|
|
|
err = o.lstat()
|
|
|
|
if err != nil {
|
|
|
|
fs.Debug(o, "Failed to stat: %s", err)
|
|
|
|
return
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this object storable
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectLocal) Storable() bool {
|
|
|
|
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 {
|
|
|
|
o *FsObjectLocal // object that is open
|
|
|
|
in io.ReadCloser // handle we are wrapping
|
|
|
|
hash hash.Hash // currently accumulating MD5
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the object and update the md5sum
|
|
|
|
func (file *localOpenFile) Close() (err error) {
|
|
|
|
err = file.in.Close()
|
|
|
|
if err == nil {
|
|
|
|
file.o.md5sum = hex.EncodeToString(file.hash.Sum(nil))
|
|
|
|
} else {
|
|
|
|
file.o.md5sum = ""
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Open an object for read
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectLocal) Open() (in io.ReadCloser, err error) {
|
|
|
|
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,
|
|
|
|
hash: md5.New(),
|
|
|
|
}
|
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
|
|
|
|
func (o *FsObjectLocal) 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
|
|
|
|
func (o *FsObjectLocal) Update(in io.Reader, modTime time.Time, size int64) 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
|
|
|
|
}
|
|
|
|
|
2014-07-19 12:06:25 +02:00
|
|
|
// Calculate the md5sum of the object we are reading as we go along
|
|
|
|
hash := md5.New()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-19 12:06:25 +02:00
|
|
|
// All successful so update the md5sum
|
|
|
|
o.md5sum = hex.EncodeToString(hash.Sum(nil))
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Set the mtime
|
|
|
|
o.SetModTime(modTime)
|
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
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Stat a FsObject into info
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectLocal) lstat() error {
|
|
|
|
info, err := os.Lstat(o.path)
|
|
|
|
o.info = info
|
2012-12-26 13:23:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
2013-06-27 21:13:07 +02:00
|
|
|
func (o *FsObjectLocal) Remove() error {
|
|
|
|
return os.Remove(o.path)
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-09-11 11:37:12 +02:00
|
|
|
// Return the current directory and file from a path
|
|
|
|
// Assumes os.PathSeparator is used.
|
|
|
|
func getDirFile(s string) (string, string) {
|
|
|
|
i := strings.LastIndex(s, string(os.PathSeparator))
|
|
|
|
return s[:i], s[i+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterPath(s string) string {
|
|
|
|
s = filepath.Clean(s)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
s = strings.Replace(s, `/`, `\`, -1)
|
|
|
|
|
|
|
|
if !filepath.IsAbs(s) && !strings.HasPrefix(s, "\\") {
|
|
|
|
s2, err := filepath.Abs(s)
|
|
|
|
if err == nil {
|
|
|
|
s = s2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to UNC
|
|
|
|
return uncPath(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !filepath.IsAbs(s) {
|
|
|
|
s2, err := filepath.Abs(s)
|
|
|
|
if err == nil {
|
|
|
|
s = s2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pattern to match a windows absolute path: c:\temp path.
|
|
|
|
var isAbsWinDrive = regexp.MustCompile(`[a-zA-Z]\:\\`)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
// Trim "//" from path and add UNC prefix.
|
|
|
|
return `\\?\UNC\` + strings.TrimPrefix(s, `\\`)
|
|
|
|
}
|
|
|
|
if isAbsWinDrive.Match([]byte(s)) {
|
|
|
|
return `\\?\` + s
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Check the interfaces are satisfied
|
2013-06-27 21:13:07 +02:00
|
|
|
var _ fs.Fs = &FsLocal{}
|
2014-07-19 13:00:42 +02:00
|
|
|
var _ fs.Purger = &FsLocal{}
|
2015-08-31 22:05:51 +02:00
|
|
|
var _ fs.Mover = &FsLocal{}
|
|
|
|
var _ fs.DirMover = &FsLocal{}
|
2013-06-28 09:57:32 +02:00
|
|
|
var _ fs.Object = &FsObjectLocal{}
|