2017-05-03 05:35:07 +08:00
|
|
|
package mountlib
|
|
|
|
|
|
|
|
import (
|
2017-10-25 17:00:26 +08:00
|
|
|
"os"
|
2017-05-03 05:35:07 +08:00
|
|
|
"path"
|
2017-05-07 20:04:20 +08:00
|
|
|
"strings"
|
2017-05-03 05:35:07 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DirEntry describes the contents of a directory entry
|
|
|
|
//
|
|
|
|
// It can be a file or a directory
|
|
|
|
type DirEntry struct {
|
2017-06-30 17:54:14 +08:00
|
|
|
Obj fs.DirEntry
|
2017-05-03 05:35:07 +08:00
|
|
|
Node Node
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dir represents a directory entry
|
|
|
|
type Dir struct {
|
|
|
|
fsys *FS
|
|
|
|
inode uint64 // inode number
|
|
|
|
f fs.Fs
|
2017-10-26 23:05:34 +08:00
|
|
|
parent *Dir // parent, nil for root
|
2017-05-03 05:35:07 +08:00
|
|
|
path string
|
|
|
|
modTime time.Time
|
2017-10-26 23:05:34 +08:00
|
|
|
entry fs.Directory
|
2017-07-22 17:55:41 +08:00
|
|
|
mu sync.Mutex // protects the following
|
|
|
|
read time.Time // time directory entry last read
|
|
|
|
items map[string]*DirEntry // NB can be nil when directory not read yet
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2017-10-26 23:05:34 +08:00
|
|
|
func newDir(fsys *FS, f fs.Fs, parent *Dir, fsDir fs.Directory) *Dir {
|
2017-05-03 05:35:07 +08:00
|
|
|
return &Dir{
|
|
|
|
fsys: fsys,
|
|
|
|
f: f,
|
2017-10-26 23:05:34 +08:00
|
|
|
parent: parent,
|
|
|
|
entry: fsDir,
|
2017-06-30 20:37:29 +08:00
|
|
|
path: fsDir.Remote(),
|
|
|
|
modTime: fsDir.ModTime(),
|
2017-05-03 05:35:07 +08:00
|
|
|
inode: NewInode(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:29:02 +08:00
|
|
|
// String converts it to printablee
|
|
|
|
func (d *Dir) String() string {
|
|
|
|
if d == nil {
|
|
|
|
return "<nil *Dir>"
|
|
|
|
}
|
|
|
|
return d.path + "/"
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// IsFile returns false for Dir - satisfies Node interface
|
|
|
|
func (d *Dir) IsFile() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:00:26 +08:00
|
|
|
// IsDir returns true for Dir - satisfies Node interface
|
|
|
|
func (d *Dir) IsDir() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mode bits of the directory - satisfies Node interface
|
|
|
|
func (d *Dir) Mode() (mode os.FileMode) {
|
|
|
|
return os.ModeDir | 0777
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name (base) of the directory - satisfies Node interface
|
|
|
|
func (d *Dir) Name() (name string) {
|
|
|
|
name = path.Base(d.path)
|
|
|
|
if name == "." {
|
|
|
|
name = "/"
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sys returns underlying data source (can be nil) - satisfies Node interface
|
|
|
|
func (d *Dir) Sys() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// Inode returns the inode number - satisfies Node interface
|
|
|
|
func (d *Dir) Inode() uint64 {
|
|
|
|
return d.inode
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node returns the Node assocuated with this - satisfies Noder interface
|
|
|
|
func (d *Dir) Node() Node {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:04:20 +08:00
|
|
|
// ForgetAll ensures the directory and all its children are purged
|
|
|
|
// from the cache.
|
|
|
|
func (d *Dir) ForgetAll() {
|
|
|
|
d.ForgetPath("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForgetPath clears the cache for itself and all subdirectories if
|
|
|
|
// they match the given path. The path is specified relative from the
|
|
|
|
// directory it is called from.
|
|
|
|
// It is not possible to traverse the directory tree upwards, i.e.
|
|
|
|
// you cannot clear the cache for the Dir's ancestors or siblings.
|
|
|
|
func (d *Dir) ForgetPath(relativePath string) {
|
|
|
|
absPath := path.Join(d.path, relativePath)
|
|
|
|
if absPath == "." {
|
|
|
|
absPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
d.walk(absPath, func(dir *Dir) {
|
|
|
|
fs.Debugf(dir.path, "forgetting directory cache")
|
|
|
|
dir.read = time.Time{}
|
|
|
|
dir.items = nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// walk runs a function on all directories whose path matches
|
|
|
|
// the given absolute one. It will be called on a directory's
|
|
|
|
// children first. It will not apply the function to parent
|
|
|
|
// nodes, regardless of the given path.
|
|
|
|
func (d *Dir) walk(absPath string, fun func(*Dir)) {
|
2017-05-19 22:45:34 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
2017-05-07 20:04:20 +08:00
|
|
|
if d.items != nil {
|
|
|
|
for _, entry := range d.items {
|
|
|
|
if dir, ok := entry.Node.(*Dir); ok {
|
|
|
|
dir.walk(absPath, fun)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.path == absPath || absPath == "" || strings.HasPrefix(d.path, absPath+"/") {
|
|
|
|
fun(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// rename should be called after the directory is renamed
|
|
|
|
//
|
|
|
|
// Reset the directory to new state, discarding all the objects and
|
|
|
|
// reading everything again
|
2017-06-30 20:37:29 +08:00
|
|
|
func (d *Dir) rename(newParent *Dir, fsDir fs.Directory) {
|
2017-05-07 20:04:20 +08:00
|
|
|
d.ForgetAll()
|
2017-10-26 23:05:34 +08:00
|
|
|
d.parent = newParent
|
|
|
|
d.entry = fsDir
|
2017-06-30 20:37:29 +08:00
|
|
|
d.path = fsDir.Remote()
|
|
|
|
d.modTime = fsDir.ModTime()
|
2017-05-03 05:35:07 +08:00
|
|
|
d.read = time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addObject adds a new object or directory to the directory
|
|
|
|
//
|
|
|
|
// note that we add new objects rather than updating old ones
|
2017-06-30 17:54:14 +08:00
|
|
|
func (d *Dir) addObject(o fs.DirEntry, node Node) *DirEntry {
|
2017-05-03 05:35:07 +08:00
|
|
|
item := &DirEntry{
|
|
|
|
Obj: o,
|
|
|
|
Node: node,
|
|
|
|
}
|
|
|
|
d.mu.Lock()
|
2017-07-22 17:55:41 +08:00
|
|
|
if d.items != nil {
|
|
|
|
d.items[path.Base(o.Remote())] = item
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
d.mu.Unlock()
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// delObject removes an object from the directory
|
|
|
|
func (d *Dir) delObject(leaf string) {
|
|
|
|
d.mu.Lock()
|
2017-07-22 17:55:41 +08:00
|
|
|
if d.items != nil {
|
|
|
|
delete(d.items, leaf)
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
d.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-07-22 17:55:41 +08:00
|
|
|
// read the directory and sets d.items - must be called with the lock held
|
|
|
|
func (d *Dir) _readDir() error {
|
2017-05-03 05:35:07 +08:00
|
|
|
when := time.Now()
|
2017-07-22 17:55:41 +08:00
|
|
|
if d.read.IsZero() || d.items == nil {
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(d.path, "Reading directory")
|
2017-05-03 05:35:07 +08:00
|
|
|
} else {
|
|
|
|
age := when.Sub(d.read)
|
2017-05-31 22:55:46 +08:00
|
|
|
if age < d.fsys.dirCacheTime {
|
2017-05-03 05:35:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fs.Debugf(d.path, "Re-reading directory (%v old)", age)
|
|
|
|
}
|
|
|
|
entries, err := fs.ListDirSorted(d.f, false, d.path)
|
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
// We treat directory not found as empty because we
|
|
|
|
// create directories on the fly
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// NB when we re-read a directory after its cache has expired
|
|
|
|
// we drop the old files which should lead to correct
|
|
|
|
// behaviour but may not be very efficient.
|
|
|
|
|
|
|
|
// Keep a note of the previous contents of the directory
|
|
|
|
oldItems := d.items
|
|
|
|
|
|
|
|
// Cache the items by name
|
|
|
|
d.items = make(map[string]*DirEntry, len(entries))
|
|
|
|
for _, entry := range entries {
|
|
|
|
switch item := entry.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
obj := item
|
|
|
|
name := path.Base(obj.Remote())
|
|
|
|
d.items[name] = &DirEntry{
|
|
|
|
Obj: obj,
|
2017-10-25 17:06:37 +08:00
|
|
|
Node: newFile(d, obj, name),
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
2017-06-30 20:37:29 +08:00
|
|
|
case fs.Directory:
|
2017-05-03 05:35:07 +08:00
|
|
|
dir := item
|
|
|
|
name := path.Base(dir.Remote())
|
|
|
|
// Use old dir value if it exists
|
2017-07-22 17:55:41 +08:00
|
|
|
if oldItems != nil {
|
|
|
|
if oldItem, ok := oldItems[name]; ok {
|
|
|
|
if _, ok := oldItem.Obj.(fs.Directory); ok {
|
|
|
|
d.items[name] = oldItem
|
|
|
|
continue
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
d.items[name] = &DirEntry{
|
|
|
|
Obj: dir,
|
2017-10-26 23:05:34 +08:00
|
|
|
Node: newDir(d.fsys, d.f, d, dir),
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("unknown type %T", item)
|
|
|
|
fs.Errorf(d.path, "readDir error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.read = when
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup a single item in the directory
|
|
|
|
//
|
|
|
|
// returns ENOENT if not found.
|
|
|
|
func (d *Dir) lookup(leaf string) (*DirEntry, error) {
|
2017-07-22 17:55:41 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err := d._readDir()
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
item, ok := d.items[leaf]
|
|
|
|
if !ok {
|
|
|
|
return nil, ENOENT
|
|
|
|
}
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if a directory is empty
|
|
|
|
func (d *Dir) isEmpty() (bool, error) {
|
2017-07-22 17:55:41 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err := d._readDir()
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(d.items) == 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModTime returns the modification time of the directory
|
|
|
|
func (d *Dir) ModTime() time.Time {
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(d.path, "Dir.ModTime %v", d.modTime)
|
2017-05-03 05:35:07 +08:00
|
|
|
return d.modTime
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:00:26 +08:00
|
|
|
// Size of the directory
|
|
|
|
func (d *Dir) Size() int64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// SetModTime sets the modTime for this dir
|
|
|
|
func (d *Dir) SetModTime(modTime time.Time) error {
|
2017-05-11 20:15:51 +08:00
|
|
|
if d.fsys.readOnly {
|
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
d.modTime = modTime
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup looks up a specific entry in the receiver.
|
|
|
|
//
|
|
|
|
// Lookup should return a Node corresponding to the entry. If the
|
|
|
|
// name does not exist in the directory, Lookup should return ENOENT.
|
|
|
|
//
|
|
|
|
// Lookup need not to handle the names "." and "..".
|
|
|
|
func (d *Dir) Lookup(name string) (node Node, err error) {
|
|
|
|
path := path.Join(d.path, name)
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Lookup")
|
2017-10-25 17:06:37 +08:00
|
|
|
item, err := d.lookup(name)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
if err != ENOENT {
|
|
|
|
fs.Errorf(path, "Dir.Lookup error: %v", err)
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Lookup OK")
|
2017-05-03 05:35:07 +08:00
|
|
|
return item.Node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDirAll reads the contents of the directory
|
|
|
|
func (d *Dir) ReadDirAll() (items []*DirEntry, err error) {
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(d.path, "Dir.ReadDirAll")
|
2017-07-22 17:55:41 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err = d._readDir()
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(d.path, "Dir.ReadDirAll error: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, item := range d.items {
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(d.path, "Dir.ReadDirAll OK with %d entries", len(items))
|
2017-05-03 05:35:07 +08:00
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create makes a new file
|
|
|
|
func (d *Dir) Create(name string) (*File, *WriteFileHandle, error) {
|
2017-05-11 20:15:51 +08:00
|
|
|
if d.fsys.readOnly {
|
|
|
|
return nil, nil, EROFS
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
path := path.Join(d.path, name)
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Create")
|
2017-05-03 05:35:07 +08:00
|
|
|
src := newCreateInfo(d.f, path)
|
|
|
|
// This gets added to the directory when the file is written
|
2017-05-09 18:29:02 +08:00
|
|
|
file := newFile(d, nil, name)
|
2017-05-03 05:35:07 +08:00
|
|
|
fh, err := newWriteFileHandle(d, file, src)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(path, "Dir.Create error: %v", err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Create OK")
|
2017-05-03 05:35:07 +08:00
|
|
|
return file, fh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a new directory
|
|
|
|
func (d *Dir) Mkdir(name string) (*Dir, error) {
|
2017-05-11 20:15:51 +08:00
|
|
|
if d.fsys.readOnly {
|
|
|
|
return nil, EROFS
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
path := path.Join(d.path, name)
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Mkdir")
|
2017-05-03 05:35:07 +08:00
|
|
|
err := d.f.Mkdir(path)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(path, "Dir.Mkdir failed to create directory: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-30 20:37:29 +08:00
|
|
|
fsDir := fs.NewDir(path, time.Now())
|
2017-10-26 23:05:34 +08:00
|
|
|
dir := newDir(d.fsys, d.f, d, fsDir)
|
2017-05-03 05:35:07 +08:00
|
|
|
d.addObject(fsDir, dir)
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Mkdir OK")
|
2017-05-03 05:35:07 +08:00
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
2017-10-26 22:37:45 +08:00
|
|
|
// RemoveName removes the entry with the given name from the receiver,
|
|
|
|
// which must be a directory. The entry to be removed may correspond
|
|
|
|
// to a file (unlink) or to a directory (rmdir).
|
|
|
|
func (d *Dir) RemoveName(name string) error {
|
2017-05-11 20:15:51 +08:00
|
|
|
if d.fsys.readOnly {
|
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
path := path.Join(d.path, name)
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Remove")
|
2017-10-25 17:06:37 +08:00
|
|
|
item, err := d.lookup(name)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(path, "Dir.Remove error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch x := item.Obj.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
err = x.Remove()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(path, "Dir.Remove file error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 20:37:29 +08:00
|
|
|
case fs.Directory:
|
2017-05-03 05:35:07 +08:00
|
|
|
// Check directory is empty first
|
|
|
|
dir := item.Node.(*Dir)
|
|
|
|
empty, err := dir.isEmpty()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(path, "Dir.Remove dir error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !empty {
|
|
|
|
fs.Errorf(path, "Dir.Remove not empty")
|
|
|
|
return ENOTEMPTY
|
|
|
|
}
|
|
|
|
// remove directory
|
|
|
|
err = d.f.Rmdir(path)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(path, "Dir.Remove failed to remove directory: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fs.Errorf(path, "Dir.Remove unknown type %T", item)
|
|
|
|
return errors.Errorf("unknown type %T", item)
|
|
|
|
}
|
|
|
|
// Remove the item from the directory listing
|
|
|
|
d.delObject(name)
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Remove OK")
|
2017-05-03 05:35:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the file
|
|
|
|
func (d *Dir) Rename(oldName, newName string, destDir *Dir) error {
|
2017-05-11 20:15:51 +08:00
|
|
|
if d.fsys.readOnly {
|
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
oldPath := path.Join(d.path, oldName)
|
|
|
|
newPath := path.Join(destDir.path, newName)
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(oldPath, "Dir.Rename to %q", newPath)
|
2017-10-25 17:06:37 +08:00
|
|
|
oldItem, err := d.lookup(oldName)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 17:54:14 +08:00
|
|
|
var newObj fs.DirEntry
|
2017-05-03 05:35:07 +08:00
|
|
|
oldNode := oldItem.Node
|
|
|
|
switch x := oldItem.Obj.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
oldObject := x
|
|
|
|
// FIXME: could Copy then Delete if Move not available
|
|
|
|
// - though care needed if case insensitive...
|
|
|
|
doMove := d.f.Features().Move
|
|
|
|
if doMove == nil {
|
|
|
|
err := errors.Errorf("Fs %q can't rename files (no Move)", d.f)
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newObject, err := doMove(oldObject, newPath)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newObj = newObject
|
|
|
|
// Update the node with the new details
|
|
|
|
if oldNode != nil {
|
|
|
|
if oldFile, ok := oldNode.(*File); ok {
|
|
|
|
fs.Debugf(oldItem.Obj, "Updating file with %v %p", newObject, oldFile)
|
|
|
|
oldFile.rename(destDir, newObject)
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 20:37:29 +08:00
|
|
|
case fs.Directory:
|
2017-05-03 05:35:07 +08:00
|
|
|
doDirMove := d.f.Features().DirMove
|
|
|
|
if doDirMove == nil {
|
|
|
|
err := errors.Errorf("Fs %q can't rename directories (no DirMove)", d.f)
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 20:37:29 +08:00
|
|
|
srcRemote := x.Remote()
|
2017-05-03 05:35:07 +08:00
|
|
|
dstRemote := newPath
|
|
|
|
err = doDirMove(d.f, srcRemote, dstRemote)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 20:37:29 +08:00
|
|
|
newDir := fs.NewDirCopy(x).SetRemote(newPath)
|
2017-05-03 05:35:07 +08:00
|
|
|
newObj = newDir
|
|
|
|
// Update the node with the new details
|
|
|
|
if oldNode != nil {
|
|
|
|
if oldDir, ok := oldNode.(*Dir); ok {
|
|
|
|
fs.Debugf(oldItem.Obj, "Updating dir with %v %p", newDir, oldDir)
|
|
|
|
oldDir.rename(destDir, newDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("unknown type %T", oldItem)
|
|
|
|
fs.Errorf(d.path, "Dir.ReadDirAll error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show moved - delete from old dir and add to new
|
|
|
|
d.delObject(oldName)
|
|
|
|
destDir.addObject(newObj, oldNode)
|
|
|
|
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(newPath, "Dir.Rename renamed from %q", oldPath)
|
2017-05-03 05:35:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fsync the directory
|
|
|
|
//
|
|
|
|
// Note that we don't do anything except return OK
|
|
|
|
func (d *Dir) Fsync() error {
|
|
|
|
return nil
|
|
|
|
}
|