2017-10-29 03:01:34 +08:00
|
|
|
package vfs
|
2017-05-03 05:35:07 +08:00
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2017-10-25 17:00:26 +08:00
|
|
|
"os"
|
2017-05-03 05:35:07 +08:00
|
|
|
"path"
|
2017-10-28 05:07:59 +08:00
|
|
|
"sort"
|
2017-05-07 20:04:20 +08:00
|
|
|
"strings"
|
2017-05-03 05:35:07 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/dirtree"
|
|
|
|
"github.com/rclone/rclone/fs/list"
|
2019-10-17 21:41:55 +08:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
|
|
|
"github.com/rclone/rclone/fs/walk"
|
2017-05-03 05:35:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Dir represents a directory entry
|
|
|
|
type Dir struct {
|
2019-11-07 05:48:43 +08:00
|
|
|
vfs *VFS // read only
|
|
|
|
inode uint64 // read only: inode number
|
|
|
|
f fs.Fs // read only
|
|
|
|
|
|
|
|
mu sync.RWMutex // protects the following
|
|
|
|
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-10-27 00:21:03 +08:00
|
|
|
read time.Time // time directory entry last read
|
2018-03-01 23:51:05 +08:00
|
|
|
items map[string]Node // directory entries - can be empty but not nil
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2017-10-29 03:01:34 +08:00
|
|
|
func newDir(vfs *VFS, f fs.Fs, parent *Dir, fsDir fs.Directory) *Dir {
|
2017-05-03 05:35:07 +08:00
|
|
|
return &Dir{
|
2017-10-29 03:01:34 +08:00
|
|
|
vfs: vfs,
|
2017-05-03 05:35:07 +08:00
|
|
|
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(),
|
2019-06-17 16:34:30 +08:00
|
|
|
modTime: fsDir.ModTime(context.TODO()),
|
2017-10-30 01:37:54 +08:00
|
|
|
inode: newInode(),
|
2018-03-01 23:51:05 +08:00
|
|
|
items: make(map[string]Node),
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-09 18:29:02 +08:00
|
|
|
// String converts it to printablee
|
|
|
|
func (d *Dir) String() string {
|
|
|
|
if d == nil {
|
|
|
|
return "<nil *Dir>"
|
|
|
|
}
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RLock()
|
|
|
|
defer d.mu.RUnlock()
|
2017-05-09 18:29:02 +08:00
|
|
|
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) {
|
2017-10-30 05:14:05 +08:00
|
|
|
return d.vfs.Opt.DirPerms
|
2017-10-25 17:00:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name (base) of the directory - satisfies Node interface
|
|
|
|
func (d *Dir) Name() (name string) {
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RLock()
|
2017-10-25 17:00:26 +08:00
|
|
|
name = path.Base(d.path)
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RUnlock()
|
2017-10-25 17:00:26 +08:00
|
|
|
if name == "." {
|
|
|
|
name = "/"
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2017-11-18 19:47:21 +08:00
|
|
|
// Path of the directory - satisfies Node interface
|
|
|
|
func (d *Dir) Path() (name string) {
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RLock()
|
|
|
|
defer d.mu.RUnlock()
|
2017-11-18 19:47:21 +08:00
|
|
|
return d.path
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:00:26 +08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-10-08 14:45:02 +08:00
|
|
|
// forgetDirPath 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 does not invalidate or clear the cache of the parent directory.
|
|
|
|
func (d *Dir) forgetDirPath(relativePath string) {
|
|
|
|
if dir := d.cachedDir(relativePath); dir != nil {
|
|
|
|
dir.walk(func(dir *Dir) {
|
2019-11-07 05:48:43 +08:00
|
|
|
// this is called with the mutex held
|
2019-10-08 14:45:02 +08:00
|
|
|
fs.Debugf(dir.path, "forgetting directory cache")
|
|
|
|
dir.read = time.Time{}
|
|
|
|
dir.items = make(map[string]Node)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:04:20 +08:00
|
|
|
// ForgetAll ensures the directory and all its children are purged
|
|
|
|
// from the cache.
|
2019-10-08 14:45:02 +08:00
|
|
|
//
|
|
|
|
// It does not invalidate or clear the cache of the parent directory.
|
2017-05-07 20:04:20 +08:00
|
|
|
func (d *Dir) ForgetAll() {
|
2019-10-08 14:45:02 +08:00
|
|
|
d.forgetDirPath("")
|
2017-05-07 20:04:20 +08:00
|
|
|
}
|
|
|
|
|
2019-10-17 21:41:55 +08:00
|
|
|
// invalidateDir invalidates the directory cache for absPath relative to this dir
|
|
|
|
func (d *Dir) invalidateDir(absPath string) {
|
|
|
|
node := d.vfs.root.cachedNode(absPath)
|
|
|
|
if dir, ok := node.(*Dir); ok {
|
|
|
|
dir.mu.Lock()
|
|
|
|
if !dir.read.IsZero() {
|
|
|
|
fs.Debugf(dir.path, "invalidating directory cache")
|
|
|
|
dir.read = time.Time{}
|
|
|
|
}
|
|
|
|
dir.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// changeNotify invalidates the directory cache for the relativePath
|
|
|
|
// passed in.
|
|
|
|
//
|
|
|
|
// if entryType is a directory it invalidates the parent of the directory too.
|
|
|
|
func (d *Dir) changeNotify(relativePath string, entryType fs.EntryType) {
|
|
|
|
defer log.Trace(d.path, "relativePath=%q, type=%v", relativePath, entryType)("")
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RLock()
|
2019-10-17 21:41:55 +08:00
|
|
|
absPath := path.Join(d.path, relativePath)
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RUnlock()
|
2019-10-17 21:41:55 +08:00
|
|
|
d.invalidateDir(findParent(absPath))
|
|
|
|
if entryType == fs.EntryDirectory {
|
|
|
|
d.invalidateDir(absPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:04:20 +08:00
|
|
|
// ForgetPath clears the cache for itself and all subdirectories if
|
|
|
|
// they match the given path. The path is specified relative from the
|
2018-09-26 00:27:37 +08:00
|
|
|
// directory it is called from. The cache of the parent directory is
|
|
|
|
// marked as stale, but not cleared otherwise.
|
2017-05-07 20:04:20 +08:00
|
|
|
// 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-09 04:03:34 +08:00
|
|
|
func (d *Dir) ForgetPath(relativePath string, entryType fs.EntryType) {
|
2019-10-17 21:41:55 +08:00
|
|
|
defer log.Trace(d.path, "relativePath=%q, type=%v", relativePath, entryType)("")
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RLock()
|
|
|
|
absPath := path.Join(d.path, relativePath)
|
|
|
|
d.mu.RUnlock()
|
|
|
|
if absPath != "" {
|
2019-10-17 21:41:55 +08:00
|
|
|
d.invalidateDir(findParent(absPath))
|
2017-05-07 20:04:20 +08:00
|
|
|
}
|
2018-09-26 00:27:37 +08:00
|
|
|
if entryType == fs.EntryDirectory {
|
2019-10-08 14:45:02 +08:00
|
|
|
d.forgetDirPath(relativePath)
|
2018-09-26 00:27:37 +08:00
|
|
|
}
|
2017-05-07 20:04:20 +08:00
|
|
|
}
|
|
|
|
|
2018-09-26 00:27:37 +08:00
|
|
|
// walk runs a function on all cached directories. It will be called
|
|
|
|
// on a directory's children first.
|
2019-11-07 05:48:43 +08:00
|
|
|
//
|
|
|
|
// The mutex will be held for the directory when fun is called
|
2018-09-26 00:27:37 +08:00
|
|
|
func (d *Dir) walk(fun func(*Dir)) {
|
2017-05-19 22:45:34 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
2018-03-01 23:51:05 +08:00
|
|
|
for _, node := range d.items {
|
|
|
|
if dir, ok := node.(*Dir); ok {
|
2018-09-26 00:27:37 +08:00
|
|
|
dir.walk(fun)
|
2017-05-07 20:04:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:27:37 +08:00
|
|
|
fun(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// age returns the duration since the last time the directory contents
|
|
|
|
// was read and the content is cosidered stale. age will be 0 and
|
|
|
|
// stale true if the last read time is empty.
|
|
|
|
// age must be called with d.mu held.
|
2019-11-07 05:48:43 +08:00
|
|
|
func (d *Dir) _age(when time.Time) (age time.Duration, stale bool) {
|
2018-09-26 00:27:37 +08:00
|
|
|
if d.read.IsZero() {
|
|
|
|
return age, true
|
2017-05-07 20:04:20 +08:00
|
|
|
}
|
2018-09-26 00:27:37 +08:00
|
|
|
age = when.Sub(d.read)
|
|
|
|
stale = age > d.vfs.Opt.DirCacheTime
|
|
|
|
return
|
2017-05-07 20:04:20 +08:00
|
|
|
}
|
|
|
|
|
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()
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.Lock()
|
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()
|
2019-06-17 16:34:30 +08:00
|
|
|
d.modTime = fsDir.ModTime(context.TODO())
|
2017-05-03 05:35:07 +08:00
|
|
|
d.read = time.Time{}
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.Unlock()
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// addObject adds a new object or directory to the directory
|
|
|
|
//
|
|
|
|
// note that we add new objects rather than updating old ones
|
2017-10-27 00:21:03 +08:00
|
|
|
func (d *Dir) addObject(node Node) {
|
2017-05-03 05:35:07 +08:00
|
|
|
d.mu.Lock()
|
2018-03-01 23:51:05 +08:00
|
|
|
d.items[node.Name()] = node
|
2017-05-03 05:35:07 +08:00
|
|
|
d.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// delObject removes an object from the directory
|
|
|
|
func (d *Dir) delObject(leaf string) {
|
|
|
|
d.mu.Lock()
|
2018-03-01 23:51:05 +08:00
|
|
|
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()
|
2019-11-07 05:48:43 +08:00
|
|
|
if age, stale := d._age(when); stale {
|
2018-09-26 00:27:37 +08:00
|
|
|
if age != 0 {
|
|
|
|
fs.Debugf(d.path, "Re-reading directory (%v old)", age)
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
2018-09-26 00:27:37 +08:00
|
|
|
} else {
|
|
|
|
return nil
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
entries, err := list.DirSorted(context.TODO(), d.f, false, d.path)
|
2017-05-03 05:35:07 +08: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 22:36:22 +08:00
|
|
|
err = d._readDirFromEntries(entries, nil, time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.read = when
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-31 18:34:59 +08:00
|
|
|
// update d.items for each dir in the DirTree below this one and
|
|
|
|
// set the last read time - must be called with the lock held
|
2019-06-27 22:54:43 +08:00
|
|
|
func (d *Dir) _readDirFromDirTree(dirTree dirtree.DirTree, when time.Time) error {
|
2018-07-27 22:36:22 +08:00
|
|
|
return d._readDirFromEntries(dirTree[d.path], dirTree, when)
|
|
|
|
}
|
|
|
|
|
2018-07-31 18:34:59 +08:00
|
|
|
// update d.items and if dirTree is not nil update each dir in the DirTree below this one and
|
|
|
|
// set the last read time - must be called with the lock held
|
2019-06-27 22:54:43 +08:00
|
|
|
func (d *Dir) _readDirFromEntries(entries fs.DirEntries, dirTree dirtree.DirTree, when time.Time) error {
|
2018-07-27 22:36:22 +08:00
|
|
|
var err error
|
2017-05-03 05:35:07 +08:00
|
|
|
// Cache the items by name
|
2018-03-01 23:51:05 +08:00
|
|
|
found := make(map[string]struct{})
|
2017-05-03 05:35:07 +08:00
|
|
|
for _, entry := range entries {
|
2018-03-01 23:51:05 +08:00
|
|
|
name := path.Base(entry.Remote())
|
2018-04-25 05:28:37 +08:00
|
|
|
if name == "." || name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
2018-03-01 23:51:05 +08:00
|
|
|
node := d.items[name]
|
|
|
|
found[name] = struct{}{}
|
2017-05-03 05:35:07 +08:00
|
|
|
switch item := entry.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
obj := item
|
2018-03-01 23:51:05 +08: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 20:37:29 +08:00
|
|
|
case fs.Directory:
|
2018-03-01 23:51:05 +08:00
|
|
|
// Reuse old dir value if it exists
|
|
|
|
if node == nil || !node.IsDir() {
|
2018-07-27 22:36:22 +08: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-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("unknown type %T", item)
|
2017-10-30 18:14:39 +08:00
|
|
|
fs.Errorf(d, "readDir error: %v", err)
|
2017-05-03 05:35:07 +08:00
|
|
|
return err
|
|
|
|
}
|
2018-03-01 23:51:05 +08:00
|
|
|
d.items[name] = node
|
|
|
|
}
|
|
|
|
// delete unused entries
|
|
|
|
for name := range d.items {
|
|
|
|
if _, ok := found[name]; !ok {
|
|
|
|
delete(d.items, name)
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
2018-07-27 22:36:22 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:30:32 +08:00
|
|
|
// readDirTree forces a refresh of the complete directory tree
|
2018-07-27 22:36:22 +08:00
|
|
|
func (d *Dir) readDirTree() error {
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RLock()
|
2019-03-05 01:25:04 +08:00
|
|
|
f, path := d.f, d.path
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RUnlock()
|
2018-07-27 22:36:22 +08:00
|
|
|
when := time.Now()
|
2019-03-05 01:25:04 +08:00
|
|
|
fs.Debugf(path, "Reading directory tree")
|
2019-06-17 16:34:30 +08:00
|
|
|
dt, err := walk.NewDirTree(context.TODO(), f, path, false, -1)
|
2018-07-27 22:36:22 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-05 01:25:04 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
d.read = time.Time{}
|
2018-07-27 22:36:22 +08:00
|
|
|
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-03 05:35:07 +08:00
|
|
|
d.read = when
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:30:32 +08:00
|
|
|
// readDir forces a refresh of the directory
|
|
|
|
func (d *Dir) readDir() error {
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
d.read = time.Time{}
|
|
|
|
return d._readDir()
|
|
|
|
}
|
|
|
|
|
2017-10-29 19:36:38 +08:00
|
|
|
// stat a single item in the directory
|
2017-05-03 05:35:07 +08:00
|
|
|
//
|
|
|
|
// returns ENOENT if not found.
|
2019-09-05 04:30:48 +08:00
|
|
|
// returns a custom error if directory on a case-insensitive file system
|
|
|
|
// contains files with names that differ only by case.
|
2017-10-29 19:36:38 +08:00
|
|
|
func (d *Dir) stat(leaf string) (Node, 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]
|
2019-09-05 04:30:48 +08:00
|
|
|
|
|
|
|
if !ok && d.vfs.Opt.CaseInsensitive {
|
|
|
|
leafLower := strings.ToLower(leaf)
|
|
|
|
for name, node := range d.items {
|
|
|
|
if strings.ToLower(name) == leafLower {
|
|
|
|
if ok {
|
|
|
|
// duplicate case insensitive match is an error
|
|
|
|
return nil, errors.Errorf("duplicate filename %q detected with --vfs-case-insensitive set", leaf)
|
|
|
|
}
|
|
|
|
// found a case insenstive match
|
|
|
|
ok = true
|
|
|
|
item = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
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 {
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.RLock()
|
|
|
|
defer d.mu.RUnlock()
|
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-10-29 19:00:56 +08:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 20:15:51 +08:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
d.modTime = modTime
|
2019-11-07 05:48:43 +08:00
|
|
|
d.mu.Unlock()
|
2017-05-03 05:35:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-26 00:27:37 +08:00
|
|
|
func (d *Dir) cachedDir(relativePath string) (dir *Dir) {
|
|
|
|
dir, _ = d.cachedNode(relativePath).(*Dir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dir) cachedNode(relativePath string) Node {
|
|
|
|
segments := strings.Split(strings.Trim(relativePath, "/"), "/")
|
|
|
|
var node Node = d
|
|
|
|
for _, s := range segments {
|
|
|
|
if s == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if dir, ok := node.(*Dir); ok {
|
|
|
|
dir.mu.Lock()
|
|
|
|
node = dir.items[s]
|
|
|
|
dir.mu.Unlock()
|
|
|
|
|
|
|
|
if node != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2017-10-29 19:36:38 +08:00
|
|
|
// Stat looks up a specific entry in the receiver.
|
2017-05-03 05:35:07 +08:00
|
|
|
//
|
2017-10-29 19:36:38 +08: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-03 05:35:07 +08:00
|
|
|
//
|
2017-10-29 19:36:38 +08: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-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
if err != ENOENT {
|
2017-10-30 18:14:39 +08:00
|
|
|
fs.Errorf(d, "Dir.Stat error: %v", err)
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-29 19:36:38 +08:00
|
|
|
// fs.Debugf(path, "Dir.Stat OK")
|
2017-10-27 00:21:03 +08:00
|
|
|
return node, nil
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2017-10-28 05:07:59 +08:00
|
|
|
// ReadDirAll reads the contents of the directory sorted
|
|
|
|
func (d *Dir) ReadDirAll() (items Nodes, 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()
|
|
|
|
err = d._readDir()
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(d.path, "Dir.ReadDirAll error: %v", err)
|
2020-02-27 23:50:41 +08:00
|
|
|
d.mu.Unlock()
|
2017-05-03 05:35:07 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, item := range d.items {
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
2020-02-27 23:50:41 +08:00
|
|
|
d.mu.Unlock()
|
2017-10-28 05:07:59 +08:00
|
|
|
sort.Sort(items)
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-07 05:38:52 +08:00
|
|
|
// accessModeMask masks off the read modes from the flags
|
|
|
|
const accessModeMask = (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)
|
|
|
|
|
2017-10-30 18:14:39 +08:00
|
|
|
// Open the directory according to the flags provided
|
|
|
|
func (d *Dir) Open(flags int) (fd Handle, err error) {
|
2017-11-07 05:38:52 +08:00
|
|
|
rdwrMode := flags & accessModeMask
|
2017-10-30 18:14:39 +08:00
|
|
|
if rdwrMode != os.O_RDONLY {
|
|
|
|
fs.Errorf(d, "Can only open directories read only")
|
2017-10-31 23:33:08 +08:00
|
|
|
return nil, EPERM
|
2017-10-30 18:14:39 +08:00
|
|
|
}
|
|
|
|
return newDirHandle(d), nil
|
|
|
|
}
|
|
|
|
|
2017-11-06 20:22:45 +08:00
|
|
|
// Create makes a new file node
|
2017-12-07 20:34:18 +08:00
|
|
|
func (d *Dir) Create(name string, flags int) (*File, error) {
|
2017-11-06 20:22:45 +08:00
|
|
|
// fs.Debugf(path, "Dir.Create")
|
2017-10-29 19:00:56 +08:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-11-06 20:22:45 +08:00
|
|
|
return nil, EROFS
|
2017-05-11 20:15:51 +08:00
|
|
|
}
|
2017-11-18 19:47:21 +08:00
|
|
|
// This gets added to the directory when the file is opened for write
|
2017-11-06 20:22:45 +08:00
|
|
|
return newFile(d, nil, name), nil
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mkdir creates a new directory
|
|
|
|
func (d *Dir) Mkdir(name string) (*Dir, error) {
|
2017-10-29 19:00:56 +08:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 20:15:51 +08:00
|
|
|
return nil, EROFS
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
path := path.Join(d.path, name)
|
2019-02-28 00:38:32 +08:00
|
|
|
node, err := d.stat(name)
|
|
|
|
switch err {
|
|
|
|
case ENOENT:
|
|
|
|
// not found, carry on
|
|
|
|
case nil:
|
|
|
|
// found so check what it is
|
|
|
|
if node.IsDir() {
|
|
|
|
return node.(*Dir), err
|
|
|
|
}
|
|
|
|
return nil, EEXIST
|
|
|
|
default:
|
|
|
|
// a different error - report
|
|
|
|
fs.Errorf(d, "Dir.Mkdir failed to read directory: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Mkdir")
|
2019-06-17 16:34:30 +08:00
|
|
|
err = d.f.Mkdir(context.TODO(), path)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
2017-10-30 18:14:39 +08:00
|
|
|
fs.Errorf(d, "Dir.Mkdir failed to create directory: %v", err)
|
2017-05-03 05:35:07 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-30 20:37:29 +08:00
|
|
|
fsDir := fs.NewDir(path, time.Now())
|
2017-10-29 03:01:34 +08:00
|
|
|
dir := newDir(d.vfs, d.f, d, fsDir)
|
2017-10-27 00:21:03 +08:00
|
|
|
d.addObject(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 23:55:40 +08:00
|
|
|
// Remove the directory
|
|
|
|
func (d *Dir) Remove() error {
|
2017-10-29 19:00:56 +08:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-10-26 23:55:40 +08:00
|
|
|
return EROFS
|
|
|
|
}
|
|
|
|
// Check directory is empty first
|
|
|
|
empty, err := d.isEmpty()
|
|
|
|
if err != nil {
|
2017-10-30 18:14:39 +08:00
|
|
|
fs.Errorf(d, "Dir.Remove dir error: %v", err)
|
2017-10-26 23:55:40 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !empty {
|
2017-10-30 18:14:39 +08:00
|
|
|
fs.Errorf(d, "Dir.Remove not empty")
|
2017-10-26 23:55:40 +08:00
|
|
|
return ENOTEMPTY
|
|
|
|
}
|
|
|
|
// remove directory
|
2019-06-17 16:34:30 +08:00
|
|
|
err = d.f.Rmdir(context.TODO(), d.path)
|
2017-10-26 23:55:40 +08:00
|
|
|
if err != nil {
|
2017-10-30 18:14:39 +08:00
|
|
|
fs.Errorf(d, "Dir.Remove failed to remove directory: %v", err)
|
2017-10-26 23:55:40 +08: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 19:00:56 +08:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-10-26 23:55:40 +08:00
|
|
|
return EROFS
|
|
|
|
}
|
|
|
|
// Remove contents of the directory
|
2017-10-27 00:21:03 +08:00
|
|
|
nodes, err := d.ReadDirAll()
|
2017-10-26 23:55:40 +08:00
|
|
|
if err != nil {
|
2017-10-30 18:14:39 +08:00
|
|
|
fs.Errorf(d, "Dir.RemoveAll failed to read directory: %v", err)
|
2017-10-26 23:55:40 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-10-27 00:21:03 +08:00
|
|
|
for _, node := range nodes {
|
|
|
|
err = node.RemoveAll()
|
2017-10-26 23:55:40 +08:00
|
|
|
if err != nil {
|
2017-11-18 19:47:21 +08:00
|
|
|
fs.Errorf(node.Path(), "Dir.RemoveAll failed to remove: %v", err)
|
2017-10-26 23:55:40 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d.Remove()
|
|
|
|
}
|
|
|
|
|
2017-10-27 00:02:48 +08:00
|
|
|
// DirEntry returns the underlying fs.DirEntry
|
|
|
|
func (d *Dir) DirEntry() (entry fs.DirEntry) {
|
|
|
|
return d.entry
|
|
|
|
}
|
|
|
|
|
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-10-29 19:00:56 +08:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 20:15:51 +08:00
|
|
|
return EROFS
|
|
|
|
}
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(path, "Dir.Remove")
|
2017-10-29 19:36:38 +08:00
|
|
|
node, err := d.stat(name)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
2017-10-30 18:14:39 +08:00
|
|
|
fs.Errorf(d, "Dir.Remove error: %v", err)
|
2017-05-03 05:35:07 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-10-27 00:21:03 +08:00
|
|
|
return node.Remove()
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the file
|
|
|
|
func (d *Dir) Rename(oldName, newName string, destDir *Dir) error {
|
2017-10-29 19:00:56 +08:00
|
|
|
if d.vfs.Opt.ReadOnly {
|
2017-05-11 20:15:51 +08:00
|
|
|
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-29 19:36:38 +08:00
|
|
|
oldNode, err := d.stat(oldName)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-10-27 00:21:03 +08:00
|
|
|
switch x := oldNode.DirEntry().(type) {
|
2017-11-18 19:47:21 +08:00
|
|
|
case nil:
|
2018-05-25 02:45:11 +08:00
|
|
|
if oldFile, ok := oldNode.(*File); ok {
|
2019-06-17 16:34:30 +08:00
|
|
|
if err = oldFile.rename(context.TODO(), destDir, newName); err != nil {
|
2018-05-25 02:45:11 +08:00
|
|
|
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-03 05:35:07 +08:00
|
|
|
}
|
2018-05-25 02:45:11 +08:00
|
|
|
case fs.Object:
|
|
|
|
if oldFile, ok := oldNode.(*File); ok {
|
2019-06-17 16:34:30 +08:00
|
|
|
if err = oldFile.rename(context.TODO(), destDir, newName); err != nil {
|
2018-05-25 02:45:11 +08:00
|
|
|
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-03 05:35:07 +08:00
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 20:37:29 +08:00
|
|
|
case fs.Directory:
|
2019-01-15 19:45:44 +08:00
|
|
|
features := d.f.Features()
|
|
|
|
if features.DirMove == nil && features.Move == nil && features.Copy == nil {
|
|
|
|
err := errors.Errorf("Fs %q can't rename directories (no DirMove, Move or Copy)", d.f)
|
2017-05-03 05:35:07 +08:00
|
|
|
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
|
2019-06-17 16:34:30 +08:00
|
|
|
err = operations.DirMove(context.TODO(), d.f, srcRemote, dstRemote)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(oldPath, "Dir.Rename error: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
newDir := fs.NewDirCopy(context.TODO(), x).SetRemote(newPath)
|
2017-05-03 05:35:07 +08:00
|
|
|
// Update the node with the new details
|
|
|
|
if oldNode != nil {
|
|
|
|
if oldDir, ok := oldNode.(*Dir); ok {
|
2017-11-18 19:47:21 +08:00
|
|
|
fs.Debugf(x, "Updating dir with %v %p", newDir, oldDir)
|
2017-05-03 05:35:07 +08:00
|
|
|
oldDir.rename(destDir, newDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2017-10-27 00:21:03 +08:00
|
|
|
err = errors.Errorf("unknown type %T", oldNode)
|
2019-02-28 00:38:32 +08:00
|
|
|
fs.Errorf(d.path, "Dir.Rename error: %v", err)
|
2017-05-03 05:35:07 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show moved - delete from old dir and add to new
|
|
|
|
d.delObject(oldName)
|
2017-10-27 00:21:03 +08:00
|
|
|
destDir.addObject(oldNode)
|
2017-05-03 05:35:07 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-11-18 23:48:49 +08:00
|
|
|
// Sync the directory
|
2017-05-03 05:35:07 +08:00
|
|
|
//
|
|
|
|
// Note that we don't do anything except return OK
|
2017-11-18 23:48:49 +08:00
|
|
|
func (d *Dir) Sync() error {
|
2017-05-03 05:35:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-29 19:00:56 +08:00
|
|
|
|
|
|
|
// VFS returns the instance of the VFS
|
|
|
|
func (d *Dir) VFS() *VFS {
|
|
|
|
return d.vfs
|
|
|
|
}
|
2017-11-07 05:38:52 +08:00
|
|
|
|
|
|
|
// Truncate changes the size of the named file.
|
|
|
|
func (d *Dir) Truncate(size int64) error {
|
|
|
|
return ENOSYS
|
|
|
|
}
|