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"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs/list"
|
2017-05-02 21:35:07 +00:00
|
|
|
"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
|
2018-03-01 15:51:05 +00:00
|
|
|
items map[string]Node // directory entries - can be empty but not nil
|
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-10-29 17:37:54 +00:00
|
|
|
inode: newInode(),
|
2018-03-01 15:51:05 +00:00
|
|
|
items: make(map[string]Node),
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-10-29 21:14:05 +00:00
|
|
|
return d.vfs.Opt.DirPerms
|
2017-10-25 09:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name (base) of the directory - satisfies Node interface
|
|
|
|
func (d *Dir) Name() (name string) {
|
|
|
|
name = path.Base(d.path)
|
|
|
|
if name == "." {
|
|
|
|
name = "/"
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2017-11-18 11:47:21 +00:00
|
|
|
// Path of the directory - satisfies Node interface
|
|
|
|
func (d *Dir) Path() (name string) {
|
|
|
|
return d.path
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:00:26 +00:00
|
|
|
// 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() {
|
2018-03-08 20:03:34 +00:00
|
|
|
d.ForgetPath("", fs.EntryDirectory)
|
2017-05-07 12:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-03-08 20:03:34 +00:00
|
|
|
func (d *Dir) ForgetPath(relativePath string, entryType fs.EntryType) {
|
|
|
|
// if we are requested to forget a file, we use its parent
|
2017-05-07 12:04:20 +00:00
|
|
|
absPath := path.Join(d.path, relativePath)
|
2018-03-08 20:03:34 +00:00
|
|
|
if entryType != fs.EntryDirectory {
|
|
|
|
absPath = path.Dir(absPath)
|
|
|
|
}
|
|
|
|
if absPath == "." || absPath == "/" {
|
2017-05-07 12:04:20 +00:00
|
|
|
absPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
d.walk(absPath, func(dir *Dir) {
|
|
|
|
fs.Debugf(dir.path, "forgetting directory cache")
|
|
|
|
dir.read = time.Time{}
|
2018-03-01 15:51:05 +00:00
|
|
|
dir.items = make(map[string]Node)
|
2017-05-07 12:04:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-29 21:14:05 +00:00
|
|
|
// walk runs a function on all cached 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.
|
2017-05-07 12:04:20 +00:00
|
|
|
func (d *Dir) walk(absPath string, fun func(*Dir)) {
|
2017-05-19 14:45:34 +00:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
2018-03-01 15:51:05 +00:00
|
|
|
for _, node := range d.items {
|
|
|
|
if dir, ok := node.(*Dir); ok {
|
|
|
|
dir.walk(absPath, fun)
|
2017-05-07 12:04:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2018-03-01 15:51:05 +00:00
|
|
|
d.items[node.Name()] = node
|
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()
|
2018-03-01 15:51:05 +00:00
|
|
|
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()
|
2018-03-01 15:51:05 +00:00
|
|
|
if d.read.IsZero() {
|
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)
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
entries, err := list.DirSorted(d.f, false, d.path)
|
2017-05-02 21:35:07 +00:00
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
// We treat directory not found as empty because we
|
|
|
|
// create directories on the fly
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the items by name
|
2018-03-01 15:51:05 +00:00
|
|
|
found := make(map[string]struct{})
|
2017-05-02 21:35:07 +00:00
|
|
|
for _, entry := range entries {
|
2018-03-01 15:51:05 +00:00
|
|
|
name := path.Base(entry.Remote())
|
2018-04-24 21:28:37 +00:00
|
|
|
if name == "." || name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-01 15:51:05 +00:00
|
|
|
node := d.items[name]
|
|
|
|
found[name] = struct{}{}
|
2017-05-02 21:35:07 +00:00
|
|
|
switch item := entry.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
obj := item
|
2018-03-01 15:51:05 +00:00
|
|
|
// Reuse old file value if it exists
|
|
|
|
if file, ok := node.(*File); node != nil && ok {
|
|
|
|
file.setObjectNoUpdate(obj)
|
|
|
|
} else {
|
|
|
|
node = 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
|
2018-03-01 15:51:05 +00:00
|
|
|
// Reuse old dir value if it exists
|
|
|
|
if node == nil || !node.IsDir() {
|
|
|
|
node = newDir(d.vfs, d.f, d, dir)
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("unknown type %T", item)
|
2017-10-30 10:14:39 +00:00
|
|
|
fs.Errorf(d, "readDir error: %v", err)
|
2017-05-02 21:35:07 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-01 15:51:05 +00:00
|
|
|
d.items[name] = node
|
|
|
|
}
|
|
|
|
// delete unused entries
|
|
|
|
for name := range d.items {
|
|
|
|
if _, ok := found[name]; !ok {
|
|
|
|
delete(d.items, name)
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
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) {
|
|
|
|
// 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-30 10:14:39 +00:00
|
|
|
fs.Errorf(d, "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
|
|
|
|
}
|
|
|
|
|
2017-11-06 21:38:52 +00:00
|
|
|
// accessModeMask masks off the read modes from the flags
|
|
|
|
const accessModeMask = (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)
|
|
|
|
|
2017-10-30 10:14:39 +00:00
|
|
|
// Open the directory according to the flags provided
|
|
|
|
func (d *Dir) Open(flags int) (fd Handle, err error) {
|
2017-11-06 21:38:52 +00:00
|
|
|
rdwrMode := flags & accessModeMask
|
2017-10-30 10:14:39 +00:00
|
|
|
if rdwrMode != os.O_RDONLY {
|
|
|
|
fs.Errorf(d, "Can only open directories read only")
|
2017-10-31 15:33:08 +00:00
|
|
|
return nil, EPERM
|
2017-10-30 10:14:39 +00:00
|
|
|
}
|
|
|
|
return newDirHandle(d), nil
|
|
|
|
}
|
|
|
|
|
2017-11-06 12:22:45 +00:00
|
|
|
// Create makes a new file node
|
2017-12-07 12:34:18 +00:00
|
|
|
func (d *Dir) Create(name string, flags int) (*File, error) {
|
2017-11-06 12:22:45 +00:00
|
|
|
// fs.Debugf(path, "Dir.Create")
|
2017-10-29 11:00:56 +00:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-11-06 12:22:45 +00:00
|
|
|
return nil, EROFS
|
2017-05-11 12:15:51 +00:00
|
|
|
}
|
2017-11-18 11:47:21 +00:00
|
|
|
// This gets added to the directory when the file is opened for write
|
2017-11-06 12:22:45 +00:00
|
|
|
return newFile(d, nil, name), nil
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-10-30 10:14:39 +00:00
|
|
|
fs.Errorf(d, "Dir.Mkdir failed to create directory: %v", err)
|
2017-05-02 21:35:07 +00:00
|
|
|
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 {
|
2017-10-30 10:14:39 +00:00
|
|
|
fs.Errorf(d, "Dir.Remove dir error: %v", err)
|
2017-10-26 15:55:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !empty {
|
2017-10-30 10:14:39 +00:00
|
|
|
fs.Errorf(d, "Dir.Remove not empty")
|
2017-10-26 15:55:40 +00:00
|
|
|
return ENOTEMPTY
|
|
|
|
}
|
|
|
|
// remove directory
|
|
|
|
err = d.f.Rmdir(d.path)
|
|
|
|
if err != nil {
|
2017-10-30 10:14:39 +00:00
|
|
|
fs.Errorf(d, "Dir.Remove failed to remove directory: %v", err)
|
2017-10-26 15:55:40 +00:00
|
|
|
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 {
|
2017-10-30 10:14:39 +00:00
|
|
|
fs.Errorf(d, "Dir.RemoveAll failed to read directory: %v", err)
|
2017-10-26 15:55:40 +00:00
|
|
|
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-11-18 11:47:21 +00:00
|
|
|
fs.Errorf(node.Path(), "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-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 {
|
2017-10-30 10:14:39 +00:00
|
|
|
fs.Errorf(d, "Dir.Remove error: %v", err)
|
2017-05-02 21:35:07 +00:00
|
|
|
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-11-18 11:47:21 +00:00
|
|
|
case nil:
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename cant rename open file")
|
|
|
|
return EPERM
|
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-11-18 11:47:21 +00:00
|
|
|
fs.Debugf(x, "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-11-18 11:47:21 +00:00
|
|
|
fs.Debugf(x, "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
|
|
|
|
}
|
|
|
|
|
2017-11-18 15:48:49 +00:00
|
|
|
// Sync the directory
|
2017-05-02 21:35:07 +00:00
|
|
|
//
|
|
|
|
// Note that we don't do anything except return OK
|
2017-11-18 15:48:49 +00:00
|
|
|
func (d *Dir) Sync() error {
|
2017-05-02 21:35:07 +00:00
|
|
|
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
|
|
|
|
}
|
2017-11-06 21:38:52 +00:00
|
|
|
|
|
|
|
// Truncate changes the size of the named file.
|
|
|
|
func (d *Dir) Truncate(size int64) error {
|
|
|
|
return ENOSYS
|
|
|
|
}
|