2017-10-28 21:01:34 +02:00
|
|
|
package vfs
|
2017-05-02 23:35:07 +02:00
|
|
|
|
|
|
|
import (
|
2017-10-25 11:00:26 +02:00
|
|
|
"os"
|
2017-05-02 23:35:07 +02:00
|
|
|
"path"
|
2017-10-27 23:07:59 +02:00
|
|
|
"sort"
|
2017-05-07 14:04:20 +02:00
|
|
|
"strings"
|
2017-05-02 23:35:07 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/list"
|
2018-07-27 16:36:22 +02:00
|
|
|
"github.com/ncw/rclone/fs/walk"
|
2017-05-02 23:35:07 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Dir represents a directory entry
|
|
|
|
type Dir struct {
|
2017-10-28 21:01:34 +02:00
|
|
|
vfs *VFS
|
2017-05-02 23:35:07 +02:00
|
|
|
inode uint64 // inode number
|
|
|
|
f fs.Fs
|
2017-10-26 17:05:34 +02:00
|
|
|
parent *Dir // parent, nil for root
|
2017-05-02 23:35:07 +02:00
|
|
|
path string
|
|
|
|
modTime time.Time
|
2017-10-26 17:05:34 +02:00
|
|
|
entry fs.Directory
|
2017-10-26 18:21:03 +02:00
|
|
|
mu sync.Mutex // protects the following
|
|
|
|
read time.Time // time directory entry last read
|
2018-03-01 16:51:05 +01:00
|
|
|
items map[string]Node // directory entries - can be empty but not nil
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-28 21:01:34 +02:00
|
|
|
func newDir(vfs *VFS, f fs.Fs, parent *Dir, fsDir fs.Directory) *Dir {
|
2017-05-02 23:35:07 +02:00
|
|
|
return &Dir{
|
2017-10-28 21:01:34 +02:00
|
|
|
vfs: vfs,
|
2017-05-02 23:35:07 +02:00
|
|
|
f: f,
|
2017-10-26 17:05:34 +02:00
|
|
|
parent: parent,
|
|
|
|
entry: fsDir,
|
2017-06-30 14:37:29 +02:00
|
|
|
path: fsDir.Remote(),
|
|
|
|
modTime: fsDir.ModTime(),
|
2017-10-29 18:37:54 +01:00
|
|
|
inode: newInode(),
|
2018-03-01 16:51:05 +01:00
|
|
|
items: make(map[string]Node),
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 12:29:02 +02:00
|
|
|
// String converts it to printablee
|
|
|
|
func (d *Dir) String() string {
|
|
|
|
if d == nil {
|
|
|
|
return "<nil *Dir>"
|
|
|
|
}
|
|
|
|
return d.path + "/"
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// IsFile returns false for Dir - satisfies Node interface
|
|
|
|
func (d *Dir) IsFile() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:00:26 +02: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 22:14:05 +01:00
|
|
|
return d.vfs.Opt.DirPerms
|
2017-10-25 11:00:26 +02: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 12:47:21 +01:00
|
|
|
// Path of the directory - satisfies Node interface
|
|
|
|
func (d *Dir) Path() (name string) {
|
|
|
|
return d.path
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:00:26 +02:00
|
|
|
// Sys returns underlying data source (can be nil) - satisfies Node interface
|
|
|
|
func (d *Dir) Sys() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02: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 14:04:20 +02:00
|
|
|
// ForgetAll ensures the directory and all its children are purged
|
|
|
|
// from the cache.
|
|
|
|
func (d *Dir) ForgetAll() {
|
2018-03-08 21:03:34 +01:00
|
|
|
d.ForgetPath("", fs.EntryDirectory)
|
2017-05-07 14:04:20 +02: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 21:03:34 +01: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 14:04:20 +02:00
|
|
|
absPath := path.Join(d.path, relativePath)
|
2018-03-08 21:03:34 +01:00
|
|
|
if entryType != fs.EntryDirectory {
|
|
|
|
absPath = path.Dir(absPath)
|
|
|
|
}
|
|
|
|
if absPath == "." || absPath == "/" {
|
2017-05-07 14:04:20 +02:00
|
|
|
absPath = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
d.walk(absPath, func(dir *Dir) {
|
|
|
|
fs.Debugf(dir.path, "forgetting directory cache")
|
|
|
|
dir.read = time.Time{}
|
2018-03-01 16:51:05 +01:00
|
|
|
dir.items = make(map[string]Node)
|
2017-05-07 14:04:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-29 22:14:05 +01: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 14:04:20 +02:00
|
|
|
func (d *Dir) walk(absPath string, fun func(*Dir)) {
|
2017-05-19 16:45:34 +02:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
2018-03-01 16:51:05 +01:00
|
|
|
for _, node := range d.items {
|
|
|
|
if dir, ok := node.(*Dir); ok {
|
|
|
|
dir.walk(absPath, fun)
|
2017-05-07 14:04:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.path == absPath || absPath == "" || strings.HasPrefix(d.path, absPath+"/") {
|
|
|
|
fun(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02: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 14:37:29 +02:00
|
|
|
func (d *Dir) rename(newParent *Dir, fsDir fs.Directory) {
|
2017-05-07 14:04:20 +02:00
|
|
|
d.ForgetAll()
|
2017-10-26 17:05:34 +02:00
|
|
|
d.parent = newParent
|
|
|
|
d.entry = fsDir
|
2017-06-30 14:37:29 +02:00
|
|
|
d.path = fsDir.Remote()
|
|
|
|
d.modTime = fsDir.ModTime()
|
2017-05-02 23:35:07 +02: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 18:21:03 +02:00
|
|
|
func (d *Dir) addObject(node Node) {
|
2017-05-02 23:35:07 +02:00
|
|
|
d.mu.Lock()
|
2018-03-01 16:51:05 +01:00
|
|
|
d.items[node.Name()] = node
|
2017-05-02 23:35:07 +02:00
|
|
|
d.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// delObject removes an object from the directory
|
|
|
|
func (d *Dir) delObject(leaf string) {
|
|
|
|
d.mu.Lock()
|
2018-03-01 16:51:05 +01:00
|
|
|
delete(d.items, leaf)
|
2017-05-02 23:35:07 +02:00
|
|
|
d.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-07-22 11:55:41 +02:00
|
|
|
// read the directory and sets d.items - must be called with the lock held
|
|
|
|
func (d *Dir) _readDir() error {
|
2017-05-02 23:35:07 +02:00
|
|
|
when := time.Now()
|
2018-03-01 16:51:05 +01:00
|
|
|
if d.read.IsZero() {
|
2017-05-09 12:39:33 +02:00
|
|
|
// fs.Debugf(d.path, "Reading directory")
|
2017-05-02 23:35:07 +02:00
|
|
|
} else {
|
|
|
|
age := when.Sub(d.read)
|
2017-10-29 12:00:56 +01:00
|
|
|
if age < d.vfs.Opt.DirCacheTime {
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fs.Debugf(d.path, "Re-reading directory (%v old)", age)
|
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
entries, err := list.DirSorted(d.f, false, d.path)
|
2017-05-02 23:35:07 +02: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
|
|
|
|
}
|
|
|
|
|
2018-07-27 16:36:22 +02:00
|
|
|
err = d._readDirFromEntries(entries, nil, time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.read = when
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dir) _readDirFromDirTree(dirTree walk.DirTree, when time.Time) error {
|
|
|
|
return d._readDirFromEntries(dirTree[d.path], dirTree, when)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dir) _readDirFromEntries(entries fs.DirEntries, dirTree walk.DirTree, when time.Time) error {
|
|
|
|
var err error
|
2017-05-02 23:35:07 +02:00
|
|
|
// Cache the items by name
|
2018-03-01 16:51:05 +01:00
|
|
|
found := make(map[string]struct{})
|
2017-05-02 23:35:07 +02:00
|
|
|
for _, entry := range entries {
|
2018-03-01 16:51:05 +01:00
|
|
|
name := path.Base(entry.Remote())
|
2018-04-24 23:28:37 +02:00
|
|
|
if name == "." || name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-01 16:51:05 +01:00
|
|
|
node := d.items[name]
|
|
|
|
found[name] = struct{}{}
|
2017-05-02 23:35:07 +02:00
|
|
|
switch item := entry.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
obj := item
|
2018-03-01 16:51:05 +01: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 14:37:29 +02:00
|
|
|
case fs.Directory:
|
2018-03-01 16:51:05 +01:00
|
|
|
// Reuse old dir value if it exists
|
|
|
|
if node == nil || !node.IsDir() {
|
2018-07-27 16:36:22 +02:00
|
|
|
node = newDir(d.vfs, d.f, d, item)
|
|
|
|
}
|
|
|
|
if dirTree != nil {
|
|
|
|
dir := node.(*Dir)
|
|
|
|
dir.mu.Lock()
|
|
|
|
err = dir._readDirFromDirTree(dirTree, when)
|
|
|
|
if err != nil {
|
|
|
|
dir.read = time.Time{}
|
|
|
|
} else {
|
|
|
|
dir.read = when
|
|
|
|
}
|
|
|
|
dir.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("unknown type %T", item)
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(d, "readDir error: %v", err)
|
2017-05-02 23:35:07 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-03-01 16:51:05 +01: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 23:35:07 +02:00
|
|
|
}
|
2018-07-27 16:36:22 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dir) readDirTree() error {
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
when := time.Now()
|
|
|
|
d.read = time.Time{}
|
|
|
|
fs.Debugf(d.path, "Reading directory tree")
|
|
|
|
dt, err := walk.NewDirTree(d.f, d.path, false, -1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = d._readDirFromDirTree(dt, when)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fs.Debugf(d.path, "Reading directory tree done in %s", time.Since(when))
|
2017-05-02 23:35:07 +02:00
|
|
|
d.read = when
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-29 12:36:38 +01:00
|
|
|
// stat a single item in the directory
|
2017-05-02 23:35:07 +02:00
|
|
|
//
|
|
|
|
// returns ENOENT if not found.
|
2017-10-29 12:36:38 +01:00
|
|
|
func (d *Dir) stat(leaf string) (Node, error) {
|
2017-07-22 11:55:41 +02:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err := d._readDir()
|
2017-05-02 23:35:07 +02: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 11:55:41 +02:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err := d._readDir()
|
2017-05-02 23:35:07 +02: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 12:39:33 +02:00
|
|
|
// fs.Debugf(d.path, "Dir.ModTime %v", d.modTime)
|
2017-05-02 23:35:07 +02:00
|
|
|
return d.modTime
|
|
|
|
}
|
|
|
|
|
2017-10-25 11:00:26 +02:00
|
|
|
// Size of the directory
|
|
|
|
func (d *Dir) Size() int64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// SetModTime sets the modTime for this dir
|
|
|
|
func (d *Dir) SetModTime(modTime time.Time) error {
|
2017-10-29 12:00:56 +01:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 14:15:51 +02:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
d.modTime = modTime
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-29 12:36:38 +01:00
|
|
|
// Stat looks up a specific entry in the receiver.
|
2017-05-02 23:35:07 +02:00
|
|
|
//
|
2017-10-29 12:36:38 +01: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 23:35:07 +02:00
|
|
|
//
|
2017-10-29 12:36:38 +01: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 23:35:07 +02:00
|
|
|
if err != nil {
|
|
|
|
if err != ENOENT {
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(d, "Dir.Stat error: %v", err)
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-29 12:36:38 +01:00
|
|
|
// fs.Debugf(path, "Dir.Stat OK")
|
2017-10-26 18:21:03 +02:00
|
|
|
return node, nil
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 23:07:59 +02:00
|
|
|
// ReadDirAll reads the contents of the directory sorted
|
|
|
|
func (d *Dir) ReadDirAll() (items Nodes, err error) {
|
2017-05-09 12:39:33 +02:00
|
|
|
// fs.Debugf(d.path, "Dir.ReadDirAll")
|
2017-07-22 11:55:41 +02:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
err = d._readDir()
|
2017-05-02 23:35:07 +02: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 23:07:59 +02:00
|
|
|
sort.Sort(items)
|
2017-05-09 12:39:33 +02:00
|
|
|
// fs.Debugf(d.path, "Dir.ReadDirAll OK with %d entries", len(items))
|
2017-05-02 23:35:07 +02:00
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
// accessModeMask masks off the read modes from the flags
|
|
|
|
const accessModeMask = (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)
|
|
|
|
|
2017-10-30 11:14:39 +01:00
|
|
|
// Open the directory according to the flags provided
|
|
|
|
func (d *Dir) Open(flags int) (fd Handle, err error) {
|
2017-11-06 22:38:52 +01:00
|
|
|
rdwrMode := flags & accessModeMask
|
2017-10-30 11:14:39 +01:00
|
|
|
if rdwrMode != os.O_RDONLY {
|
|
|
|
fs.Errorf(d, "Can only open directories read only")
|
2017-10-31 16:33:08 +01:00
|
|
|
return nil, EPERM
|
2017-10-30 11:14:39 +01:00
|
|
|
}
|
|
|
|
return newDirHandle(d), nil
|
|
|
|
}
|
|
|
|
|
2017-11-06 13:22:45 +01:00
|
|
|
// Create makes a new file node
|
2017-12-07 13:34:18 +01:00
|
|
|
func (d *Dir) Create(name string, flags int) (*File, error) {
|
2017-11-06 13:22:45 +01:00
|
|
|
// fs.Debugf(path, "Dir.Create")
|
2017-10-29 12:00:56 +01:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-11-06 13:22:45 +01:00
|
|
|
return nil, EROFS
|
2017-05-11 14:15:51 +02:00
|
|
|
}
|
2017-11-18 12:47:21 +01:00
|
|
|
// This gets added to the directory when the file is opened for write
|
2017-11-06 13:22:45 +01:00
|
|
|
return newFile(d, nil, name), nil
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a new directory
|
|
|
|
func (d *Dir) Mkdir(name string) (*Dir, error) {
|
2017-10-29 12:00:56 +01:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 14:15:51 +02:00
|
|
|
return nil, EROFS
|
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
path := path.Join(d.path, name)
|
2017-05-09 12:39:33 +02:00
|
|
|
// fs.Debugf(path, "Dir.Mkdir")
|
2017-05-02 23:35:07 +02:00
|
|
|
err := d.f.Mkdir(path)
|
|
|
|
if err != nil {
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(d, "Dir.Mkdir failed to create directory: %v", err)
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-30 14:37:29 +02:00
|
|
|
fsDir := fs.NewDir(path, time.Now())
|
2017-10-28 21:01:34 +02:00
|
|
|
dir := newDir(d.vfs, d.f, d, fsDir)
|
2017-10-26 18:21:03 +02:00
|
|
|
d.addObject(dir)
|
2017-05-09 12:39:33 +02:00
|
|
|
// fs.Debugf(path, "Dir.Mkdir OK")
|
2017-05-02 23:35:07 +02:00
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
2017-10-26 17:55:40 +02:00
|
|
|
// Remove the directory
|
|
|
|
func (d *Dir) Remove() error {
|
2017-10-29 12:00:56 +01:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-10-26 17:55:40 +02:00
|
|
|
return EROFS
|
|
|
|
}
|
|
|
|
// Check directory is empty first
|
|
|
|
empty, err := d.isEmpty()
|
|
|
|
if err != nil {
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(d, "Dir.Remove dir error: %v", err)
|
2017-10-26 17:55:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !empty {
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(d, "Dir.Remove not empty")
|
2017-10-26 17:55:40 +02:00
|
|
|
return ENOTEMPTY
|
|
|
|
}
|
|
|
|
// remove directory
|
|
|
|
err = d.f.Rmdir(d.path)
|
|
|
|
if err != nil {
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(d, "Dir.Remove failed to remove directory: %v", err)
|
2017-10-26 17:55:40 +02: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 12:00:56 +01:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-10-26 17:55:40 +02:00
|
|
|
return EROFS
|
|
|
|
}
|
|
|
|
// Remove contents of the directory
|
2017-10-26 18:21:03 +02:00
|
|
|
nodes, err := d.ReadDirAll()
|
2017-10-26 17:55:40 +02:00
|
|
|
if err != nil {
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(d, "Dir.RemoveAll failed to read directory: %v", err)
|
2017-10-26 17:55:40 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-10-26 18:21:03 +02:00
|
|
|
for _, node := range nodes {
|
|
|
|
err = node.RemoveAll()
|
2017-10-26 17:55:40 +02:00
|
|
|
if err != nil {
|
2017-11-18 12:47:21 +01:00
|
|
|
fs.Errorf(node.Path(), "Dir.RemoveAll failed to remove: %v", err)
|
2017-10-26 17:55:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d.Remove()
|
|
|
|
}
|
|
|
|
|
2017-10-26 18:02:48 +02:00
|
|
|
// DirEntry returns the underlying fs.DirEntry
|
|
|
|
func (d *Dir) DirEntry() (entry fs.DirEntry) {
|
|
|
|
return d.entry
|
|
|
|
}
|
|
|
|
|
2017-10-26 16:37:45 +02: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 12:00:56 +01:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 14:15:51 +02:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-09 12:39:33 +02:00
|
|
|
// fs.Debugf(path, "Dir.Remove")
|
2017-10-29 12:36:38 +01:00
|
|
|
node, err := d.stat(name)
|
2017-05-02 23:35:07 +02:00
|
|
|
if err != nil {
|
2017-10-30 11:14:39 +01:00
|
|
|
fs.Errorf(d, "Dir.Remove error: %v", err)
|
2017-05-02 23:35:07 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-10-26 18:21:03 +02:00
|
|
|
return node.Remove()
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the file
|
|
|
|
func (d *Dir) Rename(oldName, newName string, destDir *Dir) error {
|
2017-10-29 12:00:56 +01:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 14:15:51 +02:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
oldPath := path.Join(d.path, oldName)
|
|
|
|
newPath := path.Join(destDir.path, newName)
|
2017-05-09 12:39:33 +02:00
|
|
|
// fs.Debugf(oldPath, "Dir.Rename to %q", newPath)
|
2017-10-29 12:36:38 +01:00
|
|
|
oldNode, err := d.stat(oldName)
|
2017-05-02 23:35:07 +02:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-10-26 18:21:03 +02:00
|
|
|
switch x := oldNode.DirEntry().(type) {
|
2017-11-18 12:47:21 +01:00
|
|
|
case nil:
|
2018-05-24 20:45:11 +02:00
|
|
|
if oldFile, ok := oldNode.(*File); ok {
|
|
|
|
if err = oldFile.rename(destDir, newName); err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename can't rename open file that is not a vfs.File")
|
|
|
|
return EPERM
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
2018-05-24 20:45:11 +02:00
|
|
|
case fs.Object:
|
|
|
|
if oldFile, ok := oldNode.(*File); ok {
|
|
|
|
if err = oldFile.rename(destDir, newName); err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := errors.Errorf("Fs %q can't rename file that is not a vfs.File", d.f)
|
2017-05-02 23:35:07 +02:00
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 14:37:29 +02:00
|
|
|
case fs.Directory:
|
2017-05-02 23:35:07 +02: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 14:37:29 +02:00
|
|
|
srcRemote := x.Remote()
|
2017-05-02 23:35:07 +02: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 14:37:29 +02:00
|
|
|
newDir := fs.NewDirCopy(x).SetRemote(newPath)
|
2017-05-02 23:35:07 +02:00
|
|
|
// Update the node with the new details
|
|
|
|
if oldNode != nil {
|
|
|
|
if oldDir, ok := oldNode.(*Dir); ok {
|
2017-11-18 12:47:21 +01:00
|
|
|
fs.Debugf(x, "Updating dir with %v %p", newDir, oldDir)
|
2017-05-02 23:35:07 +02:00
|
|
|
oldDir.rename(destDir, newDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2017-10-26 18:21:03 +02:00
|
|
|
err = errors.Errorf("unknown type %T", oldNode)
|
2017-05-02 23:35:07 +02: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 18:21:03 +02:00
|
|
|
destDir.addObject(oldNode)
|
2017-05-02 23:35:07 +02:00
|
|
|
|
2017-05-09 12:39:33 +02:00
|
|
|
// fs.Debugf(newPath, "Dir.Rename renamed from %q", oldPath)
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-18 16:48:49 +01:00
|
|
|
// Sync the directory
|
2017-05-02 23:35:07 +02:00
|
|
|
//
|
|
|
|
// Note that we don't do anything except return OK
|
2017-11-18 16:48:49 +01:00
|
|
|
func (d *Dir) Sync() error {
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-29 12:00:56 +01:00
|
|
|
|
|
|
|
// VFS returns the instance of the VFS
|
|
|
|
func (d *Dir) VFS() *VFS {
|
|
|
|
return d.vfs
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
|
|
|
|
// Truncate changes the size of the named file.
|
|
|
|
func (d *Dir) Truncate(size int64) error {
|
|
|
|
return ENOSYS
|
|
|
|
}
|