2015-09-22 17:47:16 +00:00
|
|
|
// Package local provides a filesystem interface
|
2013-06-27 19:13:07 +00:00
|
|
|
package local
|
2012-12-26 12:23:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-01-18 23:21:02 +00:00
|
|
|
"io/ioutil"
|
2012-12-26 12:23:58 +00:00
|
|
|
"os"
|
2016-04-21 19:06:21 +00:00
|
|
|
"path"
|
2012-12-26 12:23:58 +00:00
|
|
|
"path/filepath"
|
2015-09-22 17:47:16 +00:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2013-01-18 23:21:02 +00:00
|
|
|
"sync"
|
2012-12-26 12:23:58 +00:00
|
|
|
"time"
|
2015-05-21 17:40:16 +00:00
|
|
|
"unicode/utf8"
|
2014-03-15 16:06:11 +00:00
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs/config"
|
|
|
|
"github.com/ncw/rclone/fs/config/flags"
|
|
|
|
"github.com/ncw/rclone/fs/hash"
|
2018-01-22 19:44:00 +00:00
|
|
|
"github.com/ncw/rclone/lib/readers"
|
2016-05-30 18:49:21 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-09-08 15:19:41 +00:00
|
|
|
"google.golang.org/appengine/log"
|
2012-12-26 12:23:58 +00:00
|
|
|
)
|
|
|
|
|
2017-01-29 13:43:20 +00:00
|
|
|
var (
|
2018-01-12 16:30:54 +00:00
|
|
|
followSymlinks = flags.BoolP("copy-links", "L", false, "Follow symlinks and copy the pointed to item.")
|
|
|
|
skipSymlinks = flags.BoolP("skip-links", "", false, "Don't warn about skipped symlinks.")
|
|
|
|
noUTFNorm = flags.BoolP("local-no-unicode-normalization", "", false, "Don't apply unicode normalization to paths and filenames")
|
2018-04-09 14:27:58 +00:00
|
|
|
noCheckUpdated = flags.BoolP("local-no-check-updated", "", false, "Don't check to see if the files change during upload")
|
2017-01-29 13:43:20 +00:00
|
|
|
)
|
|
|
|
|
2016-11-03 11:51:36 +00:00
|
|
|
// Constants
|
|
|
|
const devUnset = 0xdeadbeefcafebabe // a device id meaning it is unset
|
2016-11-02 07:26:09 +00:00
|
|
|
|
2013-06-27 19:13:07 +00:00
|
|
|
// Register with Fs
|
|
|
|
func init() {
|
2016-02-18 11:35:25 +00:00
|
|
|
fsi := &fs.RegInfo{
|
2016-02-15 18:11:53 +00:00
|
|
|
Name: "local",
|
|
|
|
Description: "Local Disk",
|
|
|
|
NewFs: NewFs,
|
2016-02-29 16:57:23 +00:00
|
|
|
Options: []fs.Option{{
|
2016-01-04 11:28:47 +00: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 19:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 11:14:46 +00:00
|
|
|
// Fs represents a local filesystem rooted at root
|
|
|
|
type Fs struct {
|
2015-08-22 15:53:11 +00:00
|
|
|
name string // the name of the remote
|
2016-08-18 22:16:47 +00:00
|
|
|
root string // The root directory (OS path)
|
2017-01-13 17:21:47 +00:00
|
|
|
features *fs.Features // optional features
|
2016-11-03 11:51:36 +00:00
|
|
|
dev uint64 // device number of root node
|
2015-05-21 17:40:16 +00:00
|
|
|
precisionOk sync.Once // Whether we need to read the precision
|
|
|
|
precision time.Duration // precision of local filesystem
|
2016-03-10 14:51:56 +00:00
|
|
|
wmu sync.Mutex // used for locking access to 'warned'.
|
2015-05-21 17:40:16 +00:00
|
|
|
warned map[string]struct{} // whether we have warned about this string
|
2016-01-04 11:28:47 +00:00
|
|
|
nounc bool // Skip UNC conversion on Windows
|
2017-01-29 13:43:20 +00:00
|
|
|
// do os.Lstat or os.Stat
|
2018-03-08 16:31:44 +00:00
|
|
|
lstat func(name string) (os.FileInfo, error)
|
|
|
|
dirNames *mapper // directory name mapping
|
|
|
|
objectHashesMu sync.Mutex // global lock for Object.hashes
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 11:14:46 +00:00
|
|
|
// Object represents a local filesystem object
|
|
|
|
type Object struct {
|
2017-06-30 09:24:06 +00:00
|
|
|
fs *Fs // The Fs this object is part of
|
|
|
|
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
|
|
|
|
size int64 // file metadata - always present
|
|
|
|
mode os.FileMode
|
|
|
|
modTime time.Time
|
2018-01-12 16:30:54 +00:00
|
|
|
hashes map[hash.Type]string // Hashes
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------
|
2012-12-29 11:35:41 +00:00
|
|
|
|
2015-11-07 11:14:46 +00:00
|
|
|
// NewFs constructs an Fs from the path
|
2014-03-15 16:06:11 +00:00
|
|
|
func NewFs(name, root string) (fs.Fs, error) {
|
2015-09-11 09:37:12 +00:00
|
|
|
var err error
|
|
|
|
|
2017-09-08 15:19:41 +00:00
|
|
|
if *noUTFNorm {
|
|
|
|
log.Errorf(nil, "The --local-no-unicode-normalization flag is deprecated and will be removed")
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
nounc := config.FileGet(name, "nounc")
|
2015-11-07 11:14:46 +00:00
|
|
|
f := &Fs{
|
2017-03-16 22:37:56 +00:00
|
|
|
name: name,
|
|
|
|
warned: make(map[string]struct{}),
|
|
|
|
nounc: nounc == "true",
|
|
|
|
dev: devUnset,
|
|
|
|
lstat: os.Lstat,
|
|
|
|
dirNames: newMapper(),
|
2015-05-21 17:40:16 +00:00
|
|
|
}
|
2016-08-18 22:16:47 +00:00
|
|
|
f.root = f.cleanPath(root)
|
2017-08-09 14:27:43 +00:00
|
|
|
f.features = (&fs.Features{
|
|
|
|
CaseInsensitive: f.caseInsensitive(),
|
|
|
|
CanHaveEmptyDirectories: true,
|
|
|
|
}).Fill(f)
|
2017-01-29 13:43:20 +00:00
|
|
|
if *followSymlinks {
|
|
|
|
f.lstat = os.Stat
|
|
|
|
}
|
2015-09-11 09:37:12 +00:00
|
|
|
|
2014-05-05 18:52:52 +00:00
|
|
|
// Check to see if this points to a file
|
2017-01-29 13:43:20 +00:00
|
|
|
fi, err := f.lstat(f.root)
|
2016-11-03 11:51:36 +00:00
|
|
|
if err == nil {
|
|
|
|
f.dev = readDevice(fi)
|
|
|
|
}
|
2014-05-05 18:52:52 +00:00
|
|
|
if err == nil && fi.Mode().IsRegular() {
|
|
|
|
// It is a file, so use the parent as the root
|
2016-06-25 20:23:20 +00:00
|
|
|
f.root, _ = getDirFile(f.root)
|
2016-06-21 17:01:53 +00:00
|
|
|
// return an error with an fs which points to the parent
|
|
|
|
return f, fs.ErrorIsFile
|
2014-05-05 18:52:52 +00:00
|
|
|
}
|
2012-12-29 11:35:41 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
2012-12-26 12:23:58 +00:00
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Name of the remote (as passed into NewFs)
|
2015-11-07 11:14:46 +00:00
|
|
|
func (f *Fs) Name() string {
|
2015-08-22 15:53:11 +00:00
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Root of the remote (as passed into NewFs)
|
2015-11-07 11:14:46 +00:00
|
|
|
func (f *Fs) Root() string {
|
2015-09-01 19:45:27 +00:00
|
|
|
return f.root
|
|
|
|
}
|
|
|
|
|
2015-11-07 11:14:46 +00:00
|
|
|
// String converts this Fs to a string
|
|
|
|
func (f *Fs) String() string {
|
2012-12-31 16:40:34 +00:00
|
|
|
return fmt.Sprintf("Local file system at %s", f.root)
|
|
|
|
}
|
|
|
|
|
2017-01-13 17:21:47 +00:00
|
|
|
// Features returns the optional features of this Fs
|
|
|
|
func (f *Fs) Features() *fs.Features {
|
|
|
|
return f.features
|
|
|
|
}
|
|
|
|
|
|
|
|
// caseInsenstive returns whether the remote is case insensitive or not
|
|
|
|
func (f *Fs) caseInsensitive() bool {
|
|
|
|
// FIXME not entirely accurate since you can have case
|
|
|
|
// sensitive Fses on darwin and case insenstive Fses on linux.
|
|
|
|
// Should probably check but that would involve creating a
|
|
|
|
// file in the remote to be most accurate which probably isn't
|
|
|
|
// desirable.
|
|
|
|
return runtime.GOOS == "windows" || runtime.GOOS == "darwin"
|
|
|
|
}
|
|
|
|
|
2016-06-25 20:58:34 +00:00
|
|
|
// newObject makes a half completed Object
|
2017-03-16 22:37:56 +00:00
|
|
|
//
|
|
|
|
// if dstPath is empty then it is made from remote
|
|
|
|
func (f *Fs) newObject(remote, dstPath string) *Object {
|
|
|
|
if dstPath == "" {
|
|
|
|
dstPath = f.cleanPath(filepath.Join(f.root, remote))
|
|
|
|
}
|
2016-08-18 22:16:47 +00:00
|
|
|
remote = f.cleanRemote(remote)
|
2015-11-07 11:14:46 +00:00
|
|
|
return &Object{
|
|
|
|
fs: f,
|
|
|
|
remote: remote,
|
|
|
|
path: dstPath,
|
|
|
|
}
|
2015-08-31 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 20:58:34 +00:00
|
|
|
// Return an Object from a path
|
2012-12-26 12:23:58 +00:00
|
|
|
//
|
|
|
|
// May return nil if an error occurred
|
2017-03-16 22:37:56 +00:00
|
|
|
func (f *Fs) newObjectWithInfo(remote, dstPath string, info os.FileInfo) (fs.Object, error) {
|
|
|
|
o := f.newObject(remote, dstPath)
|
2012-12-26 12:23:58 +00:00
|
|
|
if info != nil {
|
2017-06-30 09:24:06 +00:00
|
|
|
o.setMetadata(info)
|
2012-12-26 12:23:58 +00:00
|
|
|
} else {
|
2013-06-27 19:13:07 +00:00
|
|
|
err := o.lstat()
|
2012-12-26 12:23:58 +00:00
|
|
|
if err != nil {
|
2016-06-25 20:23:20 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, fs.ErrorObjectNotFound
|
|
|
|
}
|
2018-01-16 20:00:16 +00:00
|
|
|
if os.IsPermission(err) {
|
|
|
|
return nil, fs.ErrorPermissionDenied
|
|
|
|
}
|
2016-06-25 20:23:20 +00:00
|
|
|
return nil, err
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 09:24:06 +00:00
|
|
|
if o.mode.IsDir() {
|
2017-02-25 11:09:57 +00:00
|
|
|
return nil, errors.Wrapf(fs.ErrorNotAFile, "%q", remote)
|
|
|
|
}
|
2016-06-25 20:23:20 +00:00
|
|
|
return o, nil
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 20:23:20 +00: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) {
|
2017-03-16 22:37:56 +00:00
|
|
|
return f.newObjectWithInfo(remote, "", nil)
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:43:31 +00:00
|
|
|
// List the objects and directories in dir into entries. The
|
|
|
|
// entries can be returned in any order but should be for a
|
|
|
|
// complete directory.
|
|
|
|
//
|
|
|
|
// dir should be "" to list the root, and should not have
|
|
|
|
// trailing slashes.
|
|
|
|
//
|
|
|
|
// This should return ErrDirNotFound if the directory isn't
|
|
|
|
// found.
|
|
|
|
func (f *Fs) List(dir string) (entries fs.DirEntries, err error) {
|
|
|
|
dir = f.dirNames.Load(dir)
|
|
|
|
fsDirPath := f.cleanPath(filepath.Join(f.root, dir))
|
|
|
|
remote := f.cleanRemote(dir)
|
|
|
|
_, err = os.Stat(fsDirPath)
|
2016-04-21 19:06:21 +00:00
|
|
|
if err != nil {
|
2017-06-11 21:43:31 +00:00
|
|
|
return nil, fs.ErrorDirNotFound
|
2016-04-21 19:06:21 +00:00
|
|
|
}
|
2016-11-02 07:26:09 +00:00
|
|
|
|
2017-06-11 21:43:31 +00:00
|
|
|
fd, err := os.Open(fsDirPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to open directory %q", dir)
|
|
|
|
}
|
2016-04-21 19:06:21 +00:00
|
|
|
defer func() {
|
2017-06-11 21:43:31 +00:00
|
|
|
cerr := fd.Close()
|
|
|
|
if cerr != nil && err == nil {
|
|
|
|
err = errors.Wrapf(cerr, "failed to close directory %q:", dir)
|
2016-04-21 19:06:21 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
fis, err := fd.Readdir(1024)
|
|
|
|
if err == io.EOF && len(fis) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-06-11 21:43:31 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to read directory %q", dir)
|
2016-04-21 19:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
name := fi.Name()
|
2017-01-29 13:43:20 +00:00
|
|
|
mode := fi.Mode()
|
2016-04-21 19:06:21 +00:00
|
|
|
newRemote := path.Join(remote, name)
|
2017-06-11 21:43:31 +00:00
|
|
|
newPath := filepath.Join(fsDirPath, name)
|
2017-01-29 13:43:20 +00:00
|
|
|
// Follow symlinks if required
|
|
|
|
if *followSymlinks && (mode&os.ModeSymlink) != 0 {
|
|
|
|
fi, err = os.Stat(newPath)
|
|
|
|
if err != nil {
|
2017-06-11 21:43:31 +00:00
|
|
|
return nil, err
|
2017-01-29 13:43:20 +00:00
|
|
|
}
|
|
|
|
mode = fi.Mode()
|
|
|
|
}
|
2016-04-21 19:06:21 +00:00
|
|
|
if fi.IsDir() {
|
2016-09-07 18:49:42 +00:00
|
|
|
// Ignore directories which are symlinks. These are junction points under windows which
|
|
|
|
// are kind of a souped up symlink. Unix doesn't have directories which are symlinks.
|
2017-06-11 21:43:31 +00:00
|
|
|
if (mode&os.ModeSymlink) == 0 && f.dev == readDevice(fi) {
|
2017-06-30 12:37:29 +00:00
|
|
|
d := fs.NewDir(f.dirNames.Save(newRemote, f.cleanRemote(newRemote)), fi.ModTime())
|
2017-06-11 21:43:31 +00:00
|
|
|
entries = append(entries, d)
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
2016-04-21 19:06:21 +00:00
|
|
|
} else {
|
2017-03-16 22:37:56 +00:00
|
|
|
fso, err := f.newObjectWithInfo(newRemote, newPath, fi)
|
2016-06-25 20:23:20 +00:00
|
|
|
if err != nil {
|
2017-06-11 21:43:31 +00:00
|
|
|
return nil, err
|
2016-06-25 20:23:20 +00:00
|
|
|
}
|
2017-06-11 21:43:31 +00:00
|
|
|
if fso.Storable() {
|
|
|
|
entries = append(entries, fso)
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 19:06:21 +00:00
|
|
|
}
|
2017-06-11 21:43:31 +00:00
|
|
|
return entries, nil
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 22:16:47 +00:00
|
|
|
// cleanRemote makes string a valid UTF-8 string for remote strings.
|
2015-05-21 17:40:16 +00:00
|
|
|
//
|
|
|
|
// Any invalid UTF-8 characters will be replaced with utf8.RuneError
|
2016-08-18 22:16:47 +00:00
|
|
|
// It also normalises the UTF-8 and converts the slashes if necessary.
|
|
|
|
func (f *Fs) cleanRemote(name string) string {
|
2015-09-11 09:37:12 +00:00
|
|
|
if !utf8.ValidString(name) {
|
2016-03-10 14:51:56 +00:00
|
|
|
f.wmu.Lock()
|
2015-09-11 09:37:12 +00:00
|
|
|
if _, ok := f.warned[name]; !ok {
|
2017-02-11 20:19:44 +00:00
|
|
|
fs.Logf(f, "Replacing invalid UTF-8 characters in %q", name)
|
2015-09-11 09:37:12 +00:00
|
|
|
f.warned[name] = struct{}{}
|
|
|
|
}
|
2016-03-10 14:51:56 +00:00
|
|
|
f.wmu.Unlock()
|
2015-09-11 09:37:12 +00:00
|
|
|
name = string([]rune(name))
|
2015-05-21 17:40:16 +00:00
|
|
|
}
|
2016-08-18 22:16:47 +00:00
|
|
|
name = filepath.ToSlash(name)
|
2015-09-11 09:37:12 +00:00
|
|
|
return name
|
2015-05-21 17:40:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 22:37:56 +00:00
|
|
|
// mapper maps raw to cleaned directory names
|
|
|
|
type mapper struct {
|
|
|
|
mu sync.RWMutex // mutex to protect the below
|
|
|
|
m map[string]string // map of un-normalised directory names
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMapper() *mapper {
|
|
|
|
return &mapper{
|
|
|
|
m: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup a directory name to make a local name (reverses
|
|
|
|
// cleanDirName)
|
|
|
|
//
|
|
|
|
// FIXME this is temporary before we make a proper Directory object
|
|
|
|
func (m *mapper) Load(in string) string {
|
|
|
|
m.mu.RLock()
|
|
|
|
out, ok := m.m[in]
|
|
|
|
m.mu.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleans a directory name recording if it needed to be altered
|
|
|
|
//
|
|
|
|
// FIXME this is temporary before we make a proper Directory object
|
|
|
|
func (m *mapper) Save(in, out string) string {
|
|
|
|
if in != out {
|
|
|
|
m.mu.Lock()
|
|
|
|
m.m[out] = in
|
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2016-06-25 20:58:34 +00:00
|
|
|
// Put the Object to the local filesystem
|
2017-05-28 11:44:22 +00:00
|
|
|
func (f *Fs) Put(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
2016-02-18 11:35:25 +00:00
|
|
|
remote := src.Remote()
|
2016-06-25 20:58:34 +00:00
|
|
|
// Temporary Object under construction - info filled in by Update()
|
2017-03-16 22:37:56 +00:00
|
|
|
o := f.newObject(remote, "")
|
2017-05-28 11:44:22 +00:00
|
|
|
err := o.Update(in, src, options...)
|
2014-07-19 10:34:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return o, nil
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 19:42:35 +00:00
|
|
|
// PutStream uploads to the remote path with the modTime given of indeterminate size
|
|
|
|
func (f *Fs) PutStream(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
|
|
|
|
return f.Put(in, src, options...)
|
|
|
|
}
|
|
|
|
|
2012-12-26 12:23:58 +00:00
|
|
|
// Mkdir creates the directory if it doesn't exist
|
2016-11-25 21:52:43 +00:00
|
|
|
func (f *Fs) Mkdir(dir string) error {
|
2015-09-11 09:37:12 +00:00
|
|
|
// FIXME: https://github.com/syncthing/syncthing/blob/master/lib/osutil/mkdirall_windows.go
|
2016-12-05 18:09:45 +00:00
|
|
|
root := f.cleanPath(filepath.Join(f.root, dir))
|
2016-11-25 21:52:43 +00:00
|
|
|
err := os.MkdirAll(root, 0777)
|
2016-11-03 11:51:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-25 21:52:43 +00:00
|
|
|
if dir == "" {
|
2017-01-29 13:43:20 +00:00
|
|
|
fi, err := f.lstat(root)
|
2016-11-25 21:52:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.dev = readDevice(fi)
|
2016-11-03 11:51:36 +00:00
|
|
|
}
|
|
|
|
return nil
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rmdir removes the directory
|
|
|
|
//
|
|
|
|
// If it isn't empty it will return an error
|
2016-11-25 21:52:43 +00:00
|
|
|
func (f *Fs) Rmdir(dir string) error {
|
2016-12-05 18:09:45 +00:00
|
|
|
root := f.cleanPath(filepath.Join(f.root, dir))
|
|
|
|
return os.Remove(root)
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Precision of the file system
|
2015-11-07 11:14:46 +00:00
|
|
|
func (f *Fs) Precision() (precision time.Duration) {
|
2013-01-18 23:21:02 +00:00
|
|
|
f.precisionOk.Do(func() {
|
|
|
|
f.precision = f.readPrecision()
|
|
|
|
})
|
|
|
|
return f.precision
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the precision
|
2015-11-07 11:14:46 +00:00
|
|
|
func (f *Fs) readPrecision() (precision time.Duration) {
|
2013-01-18 23:21:02 +00:00
|
|
|
// Default precision of 1s
|
|
|
|
precision = time.Second
|
|
|
|
|
|
|
|
// Create temporary file and test it
|
2013-06-27 19:00:01 +00:00
|
|
|
fd, err := ioutil.TempFile("", "rclone")
|
2013-01-18 23:21:02 +00: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 17:19:49 +00:00
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
return time.Second
|
|
|
|
}
|
2013-01-18 23:21:02 +00:00
|
|
|
|
|
|
|
// Delete it on return
|
|
|
|
defer func() {
|
|
|
|
// fmt.Println("Remove temp file")
|
2014-07-25 17:19:49 +00:00
|
|
|
_ = os.Remove(path) // ignore error
|
2013-01-18 23:21:02 +00: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 19:13:07 +00:00
|
|
|
err := os.Chtimes(path, t, t)
|
2013-01-18 23:21:02 +00: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)
|
2018-04-04 16:12:30 +00:00
|
|
|
if fi.ModTime().Equal(t) {
|
2013-01-18 23:21:02 +00:00
|
|
|
// fmt.Println("Precision detected as", duration)
|
|
|
|
return duration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-19 11:00:42 +00: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 11:14:46 +00:00
|
|
|
func (f *Fs) Purge() error {
|
2017-01-29 13:43:20 +00:00
|
|
|
fi, err := f.lstat(f.root)
|
2014-07-28 20:02:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.Mode().IsDir() {
|
2016-06-12 14:06:02 +00:00
|
|
|
return errors.Errorf("can't purge non directory: %q", f.root)
|
2014-07-28 20:02:00 +00:00
|
|
|
}
|
2014-07-19 11:00:42 +00:00
|
|
|
return os.RemoveAll(f.root)
|
|
|
|
}
|
|
|
|
|
2015-08-31 20:05:51 +00: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 11:14:46 +00:00
|
|
|
func (f *Fs) Move(src fs.Object, remote string) (fs.Object, error) {
|
|
|
|
srcObj, ok := src.(*Object)
|
2015-08-31 20:05:51 +00:00
|
|
|
if !ok {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(src, "Can't move - not same remote type")
|
2015-08-31 20:05:51 +00:00
|
|
|
return nil, fs.ErrorCantMove
|
|
|
|
}
|
|
|
|
|
2016-06-25 20:58:34 +00:00
|
|
|
// Temporary Object under construction
|
2017-03-16 22:37:56 +00:00
|
|
|
dstObj := f.newObject(remote, "")
|
2015-08-31 20:05:51 +00:00
|
|
|
|
|
|
|
// Check it is a file if it exists
|
|
|
|
err := dstObj.lstat()
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// OK
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2017-06-30 09:24:06 +00:00
|
|
|
} else if !dstObj.mode.IsRegular() {
|
2015-08-31 20:05:51 +00:00
|
|
|
// It isn't a file
|
2016-06-12 14:06:02 +00:00
|
|
|
return nil, errors.New("can't move file onto non-file")
|
2015-08-31 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create destination
|
|
|
|
err = dstObj.mkdirAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the move
|
|
|
|
err = os.Rename(srcObj.path, dstObj.path)
|
2017-08-04 21:16:29 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// race condition, source was deleted in the meantime
|
|
|
|
return nil, err
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
// not enough rights to write to dst
|
2015-08-31 20:05:51 +00:00
|
|
|
return nil, err
|
2017-08-04 21:16:29 +00:00
|
|
|
} else if err != nil {
|
|
|
|
// not quite clear, but probably trying to move a file across file system
|
|
|
|
// boundaries. Copying might still work.
|
2018-02-28 21:27:34 +00:00
|
|
|
fs.Debugf(src, "Can't move: %v: trying copy", err)
|
2017-08-04 21:16:29 +00:00
|
|
|
return nil, fs.ErrorCantMove
|
2015-08-31 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the info
|
|
|
|
err = dstObj.lstat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dstObj, nil
|
|
|
|
}
|
|
|
|
|
2017-02-05 21:20:56 +00:00
|
|
|
// DirMove moves src, srcRemote to this remote at dstRemote
|
|
|
|
// using server side move operations.
|
2015-08-31 20:05:51 +00: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
|
2017-02-05 21:20:56 +00:00
|
|
|
func (f *Fs) DirMove(src fs.Fs, srcRemote, dstRemote string) error {
|
2015-11-07 11:14:46 +00:00
|
|
|
srcFs, ok := src.(*Fs)
|
2015-08-31 20:05:51 +00:00
|
|
|
if !ok {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(srcFs, "Can't move directory - not same remote type")
|
2015-08-31 20:05:51 +00:00
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
}
|
2017-02-05 21:20:56 +00:00
|
|
|
srcPath := f.cleanPath(filepath.Join(srcFs.root, srcRemote))
|
|
|
|
dstPath := f.cleanPath(filepath.Join(f.root, dstRemote))
|
2015-09-11 09:37:12 +00:00
|
|
|
|
2015-08-31 20:05:51 +00:00
|
|
|
// Check if destination exists
|
2017-02-05 21:20:56 +00:00
|
|
|
_, err := os.Lstat(dstPath)
|
2015-08-31 20:05:51 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return fs.ErrorDirExists
|
|
|
|
}
|
2015-09-11 09:37:12 +00:00
|
|
|
|
2017-02-05 21:20:56 +00:00
|
|
|
// Create parent of destination
|
|
|
|
dstParentPath, _ := getDirFile(dstPath)
|
|
|
|
err = os.MkdirAll(dstParentPath, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-31 20:05:51 +00:00
|
|
|
// Do the move
|
2018-02-26 12:55:05 +00:00
|
|
|
err = os.Rename(srcPath, dstPath)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// race condition, source was deleted in the meantime
|
|
|
|
return err
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
// not enough rights to write to dst
|
|
|
|
return err
|
|
|
|
} else if err != nil {
|
|
|
|
// not quite clear, but probably trying to move directory across file system
|
|
|
|
// boundaries. Copying might still work.
|
2018-02-28 21:27:34 +00:00
|
|
|
fs.Debugf(src, "Can't move dir: %v: trying copy", err)
|
2018-02-26 12:55:05 +00:00
|
|
|
return fs.ErrorCantDirMove
|
|
|
|
}
|
|
|
|
return nil
|
2015-08-31 20:05:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 12:39:33 +00:00
|
|
|
// Hashes returns the supported hash sets.
|
2018-01-12 16:30:54 +00:00
|
|
|
func (f *Fs) Hashes() hash.Set {
|
2018-01-18 20:27:52 +00:00
|
|
|
return hash.Supported
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
|
2012-12-26 12:23:58 +00:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Fs returns the parent Fs
|
2016-02-18 11:35:25 +00:00
|
|
|
func (o *Object) Fs() fs.Info {
|
2015-11-07 11:14:46 +00:00
|
|
|
return o.fs
|
2014-03-28 17:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a string version
|
2015-11-07 11:14:46 +00:00
|
|
|
func (o *Object) String() string {
|
2014-03-28 17:56:04 +00:00
|
|
|
if o == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return o.remote
|
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Remote returns the remote path
|
2015-11-07 11:14:46 +00:00
|
|
|
func (o *Object) Remote() string {
|
2016-07-15 13:18:09 +00:00
|
|
|
return o.remote
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 12:39:33 +00:00
|
|
|
// Hash returns the requested hash of a file as a lowercase hex string
|
2018-01-12 16:30:54 +00:00
|
|
|
func (o *Object) Hash(r hash.Type) (string, error) {
|
2016-01-11 12:39:33 +00:00
|
|
|
// Check that the underlying file hasn't changed
|
2017-06-30 09:24:06 +00:00
|
|
|
oldtime := o.modTime
|
|
|
|
oldsize := o.size
|
2016-01-17 10:35:38 +00:00
|
|
|
err := o.lstat()
|
|
|
|
if err != nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return "", errors.Wrap(err, "hash: failed to stat")
|
2016-01-17 10:35:38 +00:00
|
|
|
}
|
2016-01-11 12:39:33 +00:00
|
|
|
|
2018-03-08 16:31:44 +00:00
|
|
|
o.fs.objectHashesMu.Lock()
|
|
|
|
hashes := o.hashes
|
|
|
|
o.fs.objectHashesMu.Unlock()
|
2016-01-11 12:39:33 +00:00
|
|
|
|
2018-03-08 16:31:44 +00:00
|
|
|
if !o.modTime.Equal(oldtime) || oldsize != o.size || hashes == nil {
|
|
|
|
hashes = make(map[hash.Type]string)
|
2016-01-11 12:39:33 +00:00
|
|
|
in, err := os.Open(o.path)
|
|
|
|
if err != nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return "", errors.Wrap(err, "hash: failed to open")
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
2018-03-08 16:31:44 +00:00
|
|
|
hashes, err = hash.Stream(in)
|
2016-01-11 12:39:33 +00:00
|
|
|
closeErr := in.Close()
|
|
|
|
if err != nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return "", errors.Wrap(err, "hash: failed to read")
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
|
|
|
if closeErr != nil {
|
2016-06-12 14:06:02 +00:00
|
|
|
return "", errors.Wrap(closeErr, "hash: failed to close")
|
2016-01-11 12:39:33 +00:00
|
|
|
}
|
2018-03-08 16:31:44 +00:00
|
|
|
o.fs.objectHashesMu.Lock()
|
|
|
|
o.hashes = hashes
|
|
|
|
o.fs.objectHashesMu.Unlock()
|
2014-07-19 10:06:25 +00:00
|
|
|
}
|
2018-03-08 16:31:44 +00:00
|
|
|
return hashes[r], nil
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of an object in bytes
|
2015-11-07 11:14:46 +00:00
|
|
|
func (o *Object) Size() int64 {
|
2017-06-30 09:24:06 +00:00
|
|
|
return o.size
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the object
|
2015-11-07 11:14:46 +00:00
|
|
|
func (o *Object) ModTime() time.Time {
|
2017-06-30 09:24:06 +00:00
|
|
|
return o.modTime
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// SetModTime sets the modification time of the local fs object
|
2016-03-22 15:07:10 +00:00
|
|
|
func (o *Object) SetModTime(modTime time.Time) error {
|
2013-06-27 19:13:07 +00:00
|
|
|
err := os.Chtimes(o.path, modTime, modTime)
|
2012-12-26 12:23:58 +00:00
|
|
|
if err != nil {
|
2016-03-22 15:07:10 +00:00
|
|
|
return err
|
2014-07-24 21:51:34 +00:00
|
|
|
}
|
|
|
|
// Re-read metadata
|
2016-03-22 15:07:10 +00:00
|
|
|
return o.lstat()
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 17:47:16 +00:00
|
|
|
// Storable returns a boolean showing if this object is storable
|
2015-11-07 11:14:46 +00:00
|
|
|
func (o *Object) Storable() bool {
|
2016-10-18 14:24:29 +00:00
|
|
|
// Check for control characters in the remote name and show non storable
|
|
|
|
for _, c := range o.Remote() {
|
|
|
|
if c >= 0x00 && c < 0x20 || c == 0x7F {
|
2017-02-11 20:19:44 +00:00
|
|
|
fs.Logf(o.fs, "Can't store file with control characters: %q", o.Remote())
|
2016-10-18 14:24:29 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 09:24:06 +00:00
|
|
|
mode := o.mode
|
2016-09-07 19:23:10 +00:00
|
|
|
// On windows a file with os.ModeSymlink represents a file with reparse points
|
|
|
|
if runtime.GOOS == "windows" && (mode&os.ModeSymlink) != 0 {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf(o, "Clearing symlink bit to allow a file with reparse points to be copied")
|
2016-09-07 19:23:10 +00:00
|
|
|
mode &^= os.ModeSymlink
|
|
|
|
}
|
2017-01-29 13:43:20 +00:00
|
|
|
if mode&os.ModeSymlink != 0 {
|
2017-07-21 10:15:58 +00:00
|
|
|
if !*skipSymlinks {
|
|
|
|
fs.Logf(o, "Can't follow symlink without -L/--copy-links")
|
|
|
|
}
|
2017-01-29 13:43:20 +00:00
|
|
|
return false
|
|
|
|
} else if mode&(os.ModeNamedPipe|os.ModeSocket|os.ModeDevice) != 0 {
|
2017-02-11 20:19:44 +00:00
|
|
|
fs.Logf(o, "Can't transfer non file/directory")
|
2012-12-26 12:23:58 +00:00
|
|
|
return false
|
|
|
|
} else if mode&os.ModeDir != 0 {
|
2017-02-09 11:01:20 +00:00
|
|
|
// fs.Debugf(o, "Skipping directory")
|
2012-12-26 12:23:58 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-07-19 10:06:25 +00:00
|
|
|
// localOpenFile wraps an io.ReadCloser and updates the md5sum of the
|
|
|
|
// object that is read
|
|
|
|
type localOpenFile struct {
|
2018-01-12 16:30:54 +00:00
|
|
|
o *Object // object that is open
|
|
|
|
in io.ReadCloser // handle we are wrapping
|
|
|
|
hash *hash.MultiHasher // currently accumulating hashes
|
2018-01-31 20:18:31 +00:00
|
|
|
fd *os.File // file object reference
|
2014-07-19 10:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes from the object - see io.Reader
|
|
|
|
func (file *localOpenFile) Read(p []byte) (n int, err error) {
|
2018-04-09 14:27:58 +00:00
|
|
|
if !*noCheckUpdated {
|
|
|
|
// Check if file has the same size and modTime
|
|
|
|
fi, err := file.fd.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, errors.Wrap(err, "can't read status of source file while transferring")
|
|
|
|
}
|
|
|
|
if file.o.size != fi.Size() {
|
|
|
|
return 0, errors.Errorf("can't copy - source file is being updated (size changed from %d to %d)", file.o.size, fi.Size())
|
|
|
|
}
|
|
|
|
if !file.o.modTime.Equal(fi.ModTime()) {
|
|
|
|
return 0, errors.Errorf("can't copy - source file is being updated (mod time changed from %v to %v)", file.o.modTime, fi.ModTime())
|
|
|
|
}
|
2018-01-31 20:18:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-19 10:06:25 +00:00
|
|
|
n, err = file.in.Read(p)
|
|
|
|
if n > 0 {
|
|
|
|
// Hash routines never return an error
|
|
|
|
_, _ = file.hash.Write(p[:n])
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-19 12:49:34 +00:00
|
|
|
// Close the object and update the hashes
|
2014-07-19 10:06:25 +00:00
|
|
|
func (file *localOpenFile) Close() (err error) {
|
|
|
|
err = file.in.Close()
|
|
|
|
if err == nil {
|
2016-06-19 12:49:34 +00:00
|
|
|
if file.hash.Size() == file.o.Size() {
|
2018-03-08 16:31:44 +00:00
|
|
|
file.o.fs.objectHashesMu.Lock()
|
2016-06-19 12:49:34 +00:00
|
|
|
file.o.hashes = file.hash.Sums()
|
2018-03-08 16:31:44 +00:00
|
|
|
file.o.fs.objectHashesMu.Unlock()
|
2016-06-19 12:49:34 +00:00
|
|
|
}
|
2014-07-19 10:06:25 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-12-26 12:23:58 +00:00
|
|
|
// Open an object for read
|
2016-09-10 10:29:57 +00:00
|
|
|
func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) {
|
2018-01-27 10:07:17 +00:00
|
|
|
var offset, limit int64 = 0, -1
|
2018-01-18 20:27:52 +00:00
|
|
|
hashes := hash.Supported
|
2016-09-10 10:29:57 +00:00
|
|
|
for _, option := range options {
|
|
|
|
switch x := option.(type) {
|
|
|
|
case *fs.SeekOption:
|
2018-01-27 10:07:17 +00:00
|
|
|
offset = x.Offset
|
2018-01-20 19:37:38 +00:00
|
|
|
case *fs.RangeOption:
|
2018-01-22 19:44:00 +00:00
|
|
|
offset, limit = x.Decode(o.size)
|
2017-05-28 11:44:22 +00:00
|
|
|
case *fs.HashesOption:
|
|
|
|
hashes = x.Hashes
|
2016-09-10 10:29:57 +00:00
|
|
|
default:
|
|
|
|
if option.Mandatory() {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Logf(o, "Unsupported mandatory option: %v", option)
|
2016-09-10 10:29:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Open(o.path)
|
2014-07-19 10:06:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-01-22 19:44:00 +00:00
|
|
|
wrappedFd := readers.NewLimitedReadCloser(fd, limit)
|
2016-09-10 10:29:57 +00:00
|
|
|
if offset != 0 {
|
|
|
|
// seek the object
|
2018-04-06 18:53:06 +00:00
|
|
|
_, err = fd.Seek(offset, io.SeekStart)
|
2016-09-10 10:29:57 +00:00
|
|
|
// don't attempt to make checksums
|
2018-01-20 19:37:38 +00:00
|
|
|
return wrappedFd, err
|
2016-09-10 10:29:57 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
hash, err := hash.NewMultiHasherTypes(hashes)
|
2017-05-28 11:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-07-19 10:06:25 +00:00
|
|
|
// Update the md5sum as we go along
|
|
|
|
in = &localOpenFile{
|
|
|
|
o: o,
|
2018-01-20 19:37:38 +00:00
|
|
|
in: wrappedFd,
|
2017-05-28 11:44:22 +00:00
|
|
|
hash: hash,
|
2018-01-31 20:18:31 +00:00
|
|
|
fd: fd,
|
2014-07-19 10:06:25 +00:00
|
|
|
}
|
2016-09-10 10:29:57 +00:00
|
|
|
return in, nil
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 20:05:51 +00:00
|
|
|
// mkdirAll makes all the directories needed to store the object
|
2015-11-07 11:14:46 +00:00
|
|
|
func (o *Object) mkdirAll() error {
|
2015-09-11 09:37:12 +00:00
|
|
|
dir, _ := getDirFile(o.path)
|
2015-08-31 20:05:51 +00:00
|
|
|
return os.MkdirAll(dir, 0777)
|
|
|
|
}
|
|
|
|
|
2014-04-18 16:04:21 +00:00
|
|
|
// Update the object from in with modTime and size
|
2017-05-28 11:44:22 +00:00
|
|
|
func (o *Object) Update(in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
|
2018-01-18 20:27:52 +00:00
|
|
|
hashes := hash.Supported
|
2017-05-28 11:44:22 +00:00
|
|
|
for _, option := range options {
|
|
|
|
switch x := option.(type) {
|
|
|
|
case *fs.HashesOption:
|
|
|
|
hashes = x.Hashes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 20:05:51 +00:00
|
|
|
err := o.mkdirAll()
|
2014-04-18 16:04:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-02 21:46:28 +00:00
|
|
|
out, err := os.OpenFile(o.path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
2014-04-18 16:04:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-18 11:35:25 +00:00
|
|
|
// Calculate the hash of the object we are reading as we go along
|
2018-01-12 16:30:54 +00:00
|
|
|
hash, err := hash.NewMultiHasherTypes(hashes)
|
2017-05-28 11:44:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-19 10:06:25 +00:00
|
|
|
in = io.TeeReader(in, hash)
|
|
|
|
|
2014-04-18 16:04:21 +00:00
|
|
|
_, err = io.Copy(out, in)
|
2016-11-04 17:06:56 +00:00
|
|
|
closeErr := out.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = closeErr
|
|
|
|
}
|
2014-04-18 16:04:21 +00:00
|
|
|
if err != nil {
|
2017-02-11 20:19:44 +00:00
|
|
|
fs.Logf(o, "Removing partially written file on error: %v", err)
|
2016-11-04 17:06:56 +00:00
|
|
|
if removeErr := os.Remove(o.path); removeErr != nil {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Errorf(o, "Failed to remove partially written file: %v", removeErr)
|
2016-11-04 17:06:56 +00:00
|
|
|
}
|
2014-04-18 16:04:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-11 12:39:33 +00:00
|
|
|
// All successful so update the hashes
|
2018-03-08 16:31:44 +00:00
|
|
|
o.fs.objectHashesMu.Lock()
|
2016-01-11 12:39:33 +00:00
|
|
|
o.hashes = hash.Sums()
|
2018-03-08 16:31:44 +00:00
|
|
|
o.fs.objectHashesMu.Unlock()
|
2014-07-19 10:06:25 +00:00
|
|
|
|
2014-04-18 16:04:21 +00:00
|
|
|
// Set the mtime
|
2016-03-22 15:07:10 +00:00
|
|
|
err = o.SetModTime(src.ModTime())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-19 10:34:44 +00:00
|
|
|
|
|
|
|
// ReRead info now that we have finished
|
|
|
|
return o.lstat()
|
2014-04-18 16:04:21 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 09:24:06 +00:00
|
|
|
// setMetadata sets the file info from the os.FileInfo passed in
|
|
|
|
func (o *Object) setMetadata(info os.FileInfo) {
|
2017-11-03 16:12:16 +00:00
|
|
|
// Don't overwrite the info if we don't need to
|
|
|
|
// this avoids upsetting the race detector
|
|
|
|
if o.size != info.Size() {
|
|
|
|
o.size = info.Size()
|
|
|
|
}
|
2017-11-05 09:02:23 +00:00
|
|
|
if !o.modTime.Equal(info.ModTime()) {
|
2017-11-03 16:12:16 +00:00
|
|
|
o.modTime = info.ModTime()
|
|
|
|
}
|
|
|
|
if o.mode != info.Mode() {
|
|
|
|
o.mode = info.Mode()
|
|
|
|
}
|
2017-06-30 09:24:06 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 20:58:34 +00:00
|
|
|
// Stat a Object into info
|
2015-11-07 11:14:46 +00:00
|
|
|
func (o *Object) lstat() error {
|
2017-01-29 13:43:20 +00:00
|
|
|
info, err := o.fs.lstat(o.path)
|
2017-06-30 09:24:06 +00:00
|
|
|
if err == nil {
|
|
|
|
o.setMetadata(info)
|
|
|
|
}
|
2012-12-26 12:23:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an object
|
2015-11-07 11:14:46 +00:00
|
|
|
func (o *Object) Remove() error {
|
2018-04-04 13:24:04 +00:00
|
|
|
return remove(o.path)
|
2012-12-26 12:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 22:16:47 +00:00
|
|
|
// Return the directory and file from an OS path. Assumes
|
|
|
|
// os.PathSeparator is used.
|
2015-09-11 09:37:12 +00:00
|
|
|
func getDirFile(s string) (string, string) {
|
|
|
|
i := strings.LastIndex(s, string(os.PathSeparator))
|
2016-05-06 12:52:50 +00:00
|
|
|
dir, file := s[:i], s[i+1:]
|
|
|
|
if dir == "" {
|
|
|
|
dir = string(os.PathSeparator)
|
|
|
|
}
|
|
|
|
return dir, file
|
2015-09-11 09:37:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 22:16:47 +00: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 18:44:15 +00:00
|
|
|
if s == "" {
|
|
|
|
return s
|
|
|
|
}
|
2015-09-11 09:37:12 +00:00
|
|
|
s = filepath.Clean(s)
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
s = strings.Replace(s, `/`, `\`, -1)
|
2016-05-30 18:44:15 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2015-09-11 09:37:12 +00:00
|
|
|
|
2016-08-18 22:16:47 +00: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 18:44:15 +00:00
|
|
|
//
|
2016-08-18 22:16:47 +00: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 18:44:15 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2015-09-11 09:37:12 +00:00
|
|
|
if !filepath.IsAbs(s) && !strings.HasPrefix(s, "\\") {
|
|
|
|
s2, err := filepath.Abs(s)
|
|
|
|
if err == nil {
|
|
|
|
s = s2
|
|
|
|
}
|
|
|
|
}
|
2016-08-18 22:16:47 +00:00
|
|
|
if !f.nounc {
|
|
|
|
// Convert to UNC
|
|
|
|
s = uncPath(s)
|
2016-01-04 11:28:47 +00:00
|
|
|
}
|
2016-08-18 22:16:47 +00:00
|
|
|
s = cleanWindowsName(f, s)
|
|
|
|
} else {
|
|
|
|
if !filepath.IsAbs(s) {
|
|
|
|
s2, err := filepath.Abs(s)
|
|
|
|
if err == nil {
|
|
|
|
s = s2
|
|
|
|
}
|
2015-09-11 09:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-09-17 09:50:41 +00:00
|
|
|
// Pattern to match a windows absolute path: "c:\" and similar
|
|
|
|
var isAbsWinDrive = regexp.MustCompile(`^[a-zA-Z]\:\\`)
|
2015-09-11 09:37:12 +00: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 09:50:41 +00:00
|
|
|
|
|
|
|
// Trim "\\" from path and add UNC prefix.
|
2015-09-11 09:37:12 +00:00
|
|
|
return `\\?\UNC\` + strings.TrimPrefix(s, `\\`)
|
|
|
|
}
|
2015-09-17 09:50:41 +00:00
|
|
|
if isAbsWinDrive.MatchString(s) {
|
2015-09-11 09:37:12 +00:00
|
|
|
return `\\?\` + s
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2016-08-18 22:16:47 +00:00
|
|
|
// cleanWindowsName will clean invalid Windows characters replacing them with _
|
2015-11-07 11:14:46 +00:00
|
|
|
func cleanWindowsName(f *Fs, name string) string {
|
2015-10-16 09:49:00 +00: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 14:51:56 +00:00
|
|
|
f.wmu.Lock()
|
2015-10-16 09:49:00 +00:00
|
|
|
if _, ok := f.warned[name]; !ok {
|
2017-02-11 20:19:44 +00:00
|
|
|
fs.Logf(f, "Replacing invalid characters in %q to %q", name, name2)
|
2015-10-16 09:49:00 +00:00
|
|
|
f.warned[name] = struct{}{}
|
|
|
|
}
|
2016-03-10 14:51:56 +00:00
|
|
|
f.wmu.Unlock()
|
2015-10-16 09:49:00 +00:00
|
|
|
}
|
|
|
|
return name2
|
|
|
|
}
|
|
|
|
|
2012-12-26 12:23:58 +00:00
|
|
|
// Check the interfaces are satisfied
|
2015-11-07 11:14:46 +00:00
|
|
|
var (
|
2017-08-03 19:42:35 +00:00
|
|
|
_ fs.Fs = &Fs{}
|
|
|
|
_ fs.Purger = &Fs{}
|
|
|
|
_ fs.PutStreamer = &Fs{}
|
|
|
|
_ fs.Mover = &Fs{}
|
|
|
|
_ fs.DirMover = &Fs{}
|
|
|
|
_ fs.Object = &Object{}
|
2015-11-07 11:14:46 +00:00
|
|
|
)
|