2017-10-28 19:01:34 +00:00
|
|
|
package vfs
|
2017-05-02 21:35:07 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-25 09:00:26 +00:00
|
|
|
"os"
|
2017-05-02 21:35:07 +00:00
|
|
|
"path"
|
2017-10-27 21:07:59 +00:00
|
|
|
"sort"
|
2017-05-07 12:04:20 +00:00
|
|
|
"strings"
|
2017-05-02 21:35:07 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Dir represents a directory entry
|
|
|
|
type Dir struct {
|
2017-10-28 19:01:34 +00:00
|
|
|
vfs *VFS
|
2017-05-02 21:35:07 +00:00
|
|
|
inode uint64 // inode number
|
|
|
|
f fs.Fs
|
2017-10-26 15:05:34 +00:00
|
|
|
parent *Dir // parent, nil for root
|
2017-05-02 21:35:07 +00:00
|
|
|
path string
|
|
|
|
modTime time.Time
|
2017-10-26 15:05:34 +00:00
|
|
|
entry fs.Directory
|
2017-10-26 16:21:03 +00:00
|
|
|
mu sync.Mutex // protects the following
|
|
|
|
read time.Time // time directory entry last read
|
|
|
|
items map[string]Node // NB can be nil when directory not read yet
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-28 19:01:34 +00:00
|
|
|
func newDir(vfs *VFS, f fs.Fs, parent *Dir, fsDir fs.Directory) *Dir {
|
2017-05-02 21:35:07 +00:00
|
|
|
return &Dir{
|
2017-10-28 19:01:34 +00:00
|
|
|
vfs: vfs,
|
2017-05-02 21:35:07 +00:00
|
|
|
f: f,
|
2017-10-26 15:05:34 +00:00
|
|
|
parent: parent,
|
|
|
|
entry: fsDir,
|
2017-06-30 12:37:29 +00:00
|
|
|
path: fsDir.Remote(),
|
|
|
|
modTime: fsDir.ModTime(),
|
2017-05-02 21:35:07 +00:00
|
|
|
inode: NewInode(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 10:29:02 +00:00
|
|
|
// String converts it to printablee
|
|
|
|
func (d *Dir) String() string {
|
|
|
|
if d == nil {
|
|
|
|
return "<nil *Dir>"
|
|
|
|
}
|
|
|
|
return d.path + "/"
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:35:07 +00:00
|
|
|
// IsFile returns false for Dir - satisfies Node interface
|
|
|
|
func (d *Dir) IsFile() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:00:26 +00: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-02 21:35:07 +00: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 12:04:20 +00: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 14:45:34 +00:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
2017-05-07 12:04:20 +00:00
|
|
|
if d.items != nil {
|
2017-10-26 16:21:03 +00:00
|
|
|
for _, node := range d.items {
|
|
|
|
if dir, ok := node.(*Dir); ok {
|
2017-05-07 12:04:20 +00:00
|
|
|
dir.walk(absPath, fun)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.path == absPath || absPath == "" || strings.HasPrefix(d.path, absPath+"/") {
|
|
|
|
fun(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:35:07 +00: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 12:37:29 +00:00
|
|
|
func (d *Dir) rename(newParent *Dir, fsDir fs.Directory) {
|
2017-05-07 12:04:20 +00:00
|
|
|
d.ForgetAll()
|
2017-10-26 15:05:34 +00:00
|
|
|
d.parent = newParent
|
|
|
|
d.entry = fsDir
|
2017-06-30 12:37:29 +00:00
|
|
|
d.path = fsDir.Remote()
|
|
|
|
d.modTime = fsDir.ModTime()
|
2017-05-02 21:35:07 +00: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-10-26 16:21:03 +00:00
|
|
|
func (d *Dir) addObject(node Node) {
|
2017-05-02 21:35:07 +00:00
|
|
|
d.mu.Lock()
|
2017-07-22 09:55:41 +00:00
|
|
|
if d.items != nil {
|
2017-10-26 16:21:03 +00:00
|
|
|
d.items[node.Name()] = node
|
2017-07-22 09:55:41 +00:00
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
d.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// delObject removes an object from the directory
|
|
|
|
func (d *Dir) delObject(leaf string) {
|
|
|
|
d.mu.Lock()
|
2017-07-22 09:55:41 +00:00
|
|
|
if d.items != nil {
|
|
|
|
delete(d.items, leaf)
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
d.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-07-22 09:55:41 +00:00
|
|
|
// read the directory and sets d.items - must be called with the lock held
|
|
|
|
func (d *Dir) _readDir() error {
|
2017-05-02 21:35:07 +00:00
|
|
|
when := time.Now()
|
2017-07-22 09:55:41 +00:00
|
|
|
if d.read.IsZero() || d.items == nil {
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(d.path, "Reading directory")
|
2017-05-02 21:35:07 +00:00
|
|
|
} else {
|
|
|
|
age := when.Sub(d.read)
|
2017-10-29 11:00:56 +00:00
|
|
|
if age < d.vfs.Opt.DirCacheTime {
|
2017-05-02 21:35:07 +00: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
|
2017-10-26 16:21:03 +00:00
|
|
|
d.items = make(map[string]Node, len(entries))
|
2017-05-02 21:35:07 +00:00
|
|
|
for _, entry := range entries {
|
|
|
|
switch item := entry.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
obj := item
|
|
|
|
name := path.Base(obj.Remote())
|
2017-10-26 16:21:03 +00:00
|
|
|
d.items[name] = newFile(d, obj, name)
|
2017-06-30 12:37:29 +00:00
|
|
|
case fs.Directory:
|
2017-05-02 21:35:07 +00:00
|
|
|
dir := item
|
|
|
|
name := path.Base(dir.Remote())
|
|
|
|
// Use old dir value if it exists
|
2017-07-22 09:55:41 +00:00
|
|
|
if oldItems != nil {
|
2017-10-26 16:21:03 +00:00
|
|
|
if oldNode, ok := oldItems[name]; ok {
|
|
|
|
if oldNode.IsDir() {
|
|
|
|
d.items[name] = oldNode
|
2017-07-22 09:55:41 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-28 19:01:34 +00:00
|
|
|
d.items[name] = newDir(d.vfs, d.f, d, dir)
|
2017-05-02 21:35:07 +00:00
|
|
|
default:
|
|
|
|
err = errors.Errorf("unknown type %T", item)
|
|
|
|
fs.Errorf(d.path, "readDir error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.read = when
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-29 11:36:38 +00:00
|
|
|
// stat a single item in the directory
|
2017-05-02 21:35:07 +00:00
|
|
|
//
|
|
|
|
// returns ENOENT if not found.
|
2017-10-29 11:36:38 +00:00
|
|
|
func (d *Dir) stat(leaf string) (Node, error) {
|
2017-07-22 09:55:41 +00:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err := d._readDir()
|
2017-05-02 21:35:07 +00: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 09:55:41 +00:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err := d._readDir()
|
2017-05-02 21:35:07 +00: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 10:39:33 +00:00
|
|
|
// fs.Debugf(d.path, "Dir.ModTime %v", d.modTime)
|
2017-05-02 21:35:07 +00:00
|
|
|
return d.modTime
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:00:26 +00:00
|
|
|
// Size of the directory
|
|
|
|
func (d *Dir) Size() int64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-05-02 21:35:07 +00:00
|
|
|
// SetModTime sets the modTime for this dir
|
|
|
|
func (d *Dir) SetModTime(modTime time.Time) error {
|
2017-10-29 11:00:56 +00:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 12:15:51 +00:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
d.modTime = modTime
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-29 11:36:38 +00:00
|
|
|
// Stat looks up a specific entry in the receiver.
|
2017-05-02 21:35:07 +00:00
|
|
|
//
|
2017-10-29 11:36:38 +00:00
|
|
|
// Stat should return a Node corresponding to the entry. If the
|
|
|
|
// name does not exist in the directory, Stat should return ENOENT.
|
2017-05-02 21:35:07 +00:00
|
|
|
//
|
2017-10-29 11:36:38 +00:00
|
|
|
// Stat need not to handle the names "." and "..".
|
|
|
|
func (d *Dir) Stat(name string) (node Node, err error) {
|
2017-05-02 21:35:07 +00:00
|
|
|
path := path.Join(d.path, name)
|
2017-10-29 11:36:38 +00:00
|
|
|
// fs.Debugf(path, "Dir.Stat")
|
|
|
|
node, err = d.stat(name)
|
2017-05-02 21:35:07 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != ENOENT {
|
2017-10-29 11:36:38 +00:00
|
|
|
fs.Errorf(path, "Dir.Stat error: %v", err)
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-29 11:36:38 +00:00
|
|
|
// fs.Debugf(path, "Dir.Stat OK")
|
2017-10-26 16:21:03 +00:00
|
|
|
return node, nil
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 21:07:59 +00:00
|
|
|
// ReadDirAll reads the contents of the directory sorted
|
|
|
|
func (d *Dir) ReadDirAll() (items Nodes, err error) {
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(d.path, "Dir.ReadDirAll")
|
2017-07-22 09:55:41 +00:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err = d._readDir()
|
2017-05-02 21:35:07 +00: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-10-27 21:07:59 +00:00
|
|
|
sort.Sort(items)
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(d.path, "Dir.ReadDirAll OK with %d entries", len(items))
|
2017-05-02 21:35:07 +00:00
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create makes a new file
|
|
|
|
func (d *Dir) Create(name string) (*File, *WriteFileHandle, error) {
|
2017-10-29 11:00:56 +00:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 12:15:51 +00:00
|
|
|
return nil, nil, EROFS
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
path := path.Join(d.path, name)
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(path, "Dir.Create")
|
2017-05-02 21:35:07 +00:00
|
|
|
src := newCreateInfo(d.f, path)
|
|
|
|
// This gets added to the directory when the file is written
|
2017-05-09 10:29:02 +00:00
|
|
|
file := newFile(d, nil, name)
|
2017-05-02 21:35:07 +00: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 10:39:33 +00:00
|
|
|
// fs.Debugf(path, "Dir.Create OK")
|
2017-05-02 21:35:07 +00:00
|
|
|
return file, fh, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a new directory
|
|
|
|
func (d *Dir) Mkdir(name string) (*Dir, error) {
|
2017-10-29 11:00:56 +00:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 12:15:51 +00:00
|
|
|
return nil, EROFS
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
path := path.Join(d.path, name)
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(path, "Dir.Mkdir")
|
2017-05-02 21:35:07 +00: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 12:37:29 +00:00
|
|
|
fsDir := fs.NewDir(path, time.Now())
|
2017-10-28 19:01:34 +00:00
|
|
|
dir := newDir(d.vfs, d.f, d, fsDir)
|
2017-10-26 16:21:03 +00:00
|
|
|
d.addObject(dir)
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(path, "Dir.Mkdir OK")
|
2017-05-02 21:35:07 +00:00
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
2017-10-26 15:55:40 +00:00
|
|
|
// Remove the directory
|
|
|
|
func (d *Dir) Remove() error {
|
2017-10-29 11:00:56 +00:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-10-26 15:55:40 +00:00
|
|
|
return EROFS
|
|
|
|
}
|
|
|
|
// Check directory is empty first
|
|
|
|
empty, err := d.isEmpty()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(d.path, "Dir.Remove dir error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !empty {
|
|
|
|
fs.Errorf(d.path, "Dir.Remove not empty")
|
|
|
|
return ENOTEMPTY
|
|
|
|
}
|
|
|
|
// remove directory
|
|
|
|
err = d.f.Rmdir(d.path)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(d.path, "Dir.Remove failed to remove directory: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove the item from the parent directory listing
|
|
|
|
if d.parent != nil {
|
|
|
|
d.parent.delObject(d.Name())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAll removes the directory and any contents recursively
|
|
|
|
func (d *Dir) RemoveAll() error {
|
2017-10-29 11:00:56 +00:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-10-26 15:55:40 +00:00
|
|
|
return EROFS
|
|
|
|
}
|
|
|
|
// Remove contents of the directory
|
2017-10-26 16:21:03 +00:00
|
|
|
nodes, err := d.ReadDirAll()
|
2017-10-26 15:55:40 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(d.path, "Dir.RemoveAll failed to read directory: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-10-26 16:21:03 +00:00
|
|
|
for _, node := range nodes {
|
|
|
|
err = node.RemoveAll()
|
2017-10-26 15:55:40 +00:00
|
|
|
if err != nil {
|
2017-10-26 16:21:03 +00:00
|
|
|
fs.Errorf(node.DirEntry(), "Dir.RemoveAll failed to remove: %v", err)
|
2017-10-26 15:55:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d.Remove()
|
|
|
|
}
|
|
|
|
|
2017-10-26 16:02:48 +00:00
|
|
|
// DirEntry returns the underlying fs.DirEntry
|
|
|
|
func (d *Dir) DirEntry() (entry fs.DirEntry) {
|
|
|
|
return d.entry
|
|
|
|
}
|
|
|
|
|
2017-10-26 14:37:45 +00: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-10-29 11:00:56 +00:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 12:15:51 +00:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
path := path.Join(d.path, name)
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(path, "Dir.Remove")
|
2017-10-29 11:36:38 +00:00
|
|
|
node, err := d.stat(name)
|
2017-05-02 21:35:07 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(path, "Dir.Remove error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-10-26 16:21:03 +00:00
|
|
|
return node.Remove()
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the file
|
|
|
|
func (d *Dir) Rename(oldName, newName string, destDir *Dir) error {
|
2017-10-29 11:00:56 +00:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 12:15:51 +00:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
oldPath := path.Join(d.path, oldName)
|
|
|
|
newPath := path.Join(destDir.path, newName)
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(oldPath, "Dir.Rename to %q", newPath)
|
2017-10-29 11:36:38 +00:00
|
|
|
oldNode, err := d.stat(oldName)
|
2017-05-02 21:35:07 +00:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-10-26 16:21:03 +00:00
|
|
|
switch x := oldNode.DirEntry().(type) {
|
2017-05-02 21:35:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
// Update the node with the new details
|
|
|
|
if oldNode != nil {
|
|
|
|
if oldFile, ok := oldNode.(*File); ok {
|
2017-10-26 16:21:03 +00:00
|
|
|
fs.Debugf(oldNode.DirEntry(), "Updating file with %v %p", newObject, oldFile)
|
2017-05-02 21:35:07 +00:00
|
|
|
oldFile.rename(destDir, newObject)
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 12:37:29 +00:00
|
|
|
case fs.Directory:
|
2017-05-02 21:35:07 +00: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 12:37:29 +00:00
|
|
|
srcRemote := x.Remote()
|
2017-05-02 21:35:07 +00: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 12:37:29 +00:00
|
|
|
newDir := fs.NewDirCopy(x).SetRemote(newPath)
|
2017-05-02 21:35:07 +00:00
|
|
|
// Update the node with the new details
|
|
|
|
if oldNode != nil {
|
|
|
|
if oldDir, ok := oldNode.(*Dir); ok {
|
2017-10-26 16:21:03 +00:00
|
|
|
fs.Debugf(oldNode.DirEntry(), "Updating dir with %v %p", newDir, oldDir)
|
2017-05-02 21:35:07 +00:00
|
|
|
oldDir.rename(destDir, newDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2017-10-26 16:21:03 +00:00
|
|
|
err = errors.Errorf("unknown type %T", oldNode)
|
2017-05-02 21:35:07 +00:00
|
|
|
fs.Errorf(d.path, "Dir.ReadDirAll error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show moved - delete from old dir and add to new
|
|
|
|
d.delObject(oldName)
|
2017-10-26 16:21:03 +00:00
|
|
|
destDir.addObject(oldNode)
|
2017-05-02 21:35:07 +00:00
|
|
|
|
2017-05-09 10:39:33 +00:00
|
|
|
// fs.Debugf(newPath, "Dir.Rename renamed from %q", oldPath)
|
2017-05-02 21:35:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fsync the directory
|
|
|
|
//
|
|
|
|
// Note that we don't do anything except return OK
|
|
|
|
func (d *Dir) Fsync() error {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-29 11:00:56 +00:00
|
|
|
|
|
|
|
// VFS returns the instance of the VFS
|
|
|
|
func (d *Dir) VFS() *VFS {
|
|
|
|
return d.vfs
|
|
|
|
}
|