2017-04-02 02:53:44 +08:00
|
|
|
// Package scan does concurrent scanning of an Fs building up a directory tree.
|
|
|
|
package scan
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2021-11-04 18:12:57 +08:00
|
|
|
"fmt"
|
2017-04-02 02:53:44 +08:00
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/walk"
|
2017-04-02 02:53:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Dir represents a directory found in the remote
|
|
|
|
type Dir struct {
|
2020-12-28 22:08:12 +08:00
|
|
|
parent *Dir
|
|
|
|
path string
|
|
|
|
mu sync.Mutex
|
|
|
|
size int64
|
2022-04-05 21:46:56 +08:00
|
|
|
count int64
|
|
|
|
countUnknownSize int64
|
2020-12-28 22:08:12 +08:00
|
|
|
entries fs.DirEntries
|
|
|
|
dirs map[string]*Dir
|
|
|
|
readError error
|
|
|
|
entriesHaveErrors bool
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parent returns the directory above this one
|
|
|
|
func (d *Dir) Parent() *Dir {
|
|
|
|
// no locking needed since these are write once in newDir()
|
|
|
|
return d.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path returns the position of the dir in the filesystem
|
|
|
|
func (d *Dir) Path() string {
|
|
|
|
// no locking needed since these are write once in newDir()
|
|
|
|
return d.path
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a new directory
|
2020-12-28 22:08:12 +08:00
|
|
|
func newDir(parent *Dir, dirPath string, entries fs.DirEntries, err error) *Dir {
|
2017-04-02 02:53:44 +08:00
|
|
|
d := &Dir{
|
2020-12-28 22:08:12 +08:00
|
|
|
parent: parent,
|
|
|
|
path: dirPath,
|
|
|
|
entries: entries,
|
|
|
|
dirs: make(map[string]*Dir),
|
|
|
|
readError: err,
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
// Count size in this dir
|
|
|
|
for _, entry := range entries {
|
|
|
|
if o, ok := entry.(fs.Object); ok {
|
|
|
|
d.count++
|
2022-04-05 21:46:56 +08:00
|
|
|
size := o.Size()
|
|
|
|
if size < 0 {
|
|
|
|
// Some backends may return -1 because size of object is not known
|
|
|
|
d.countUnknownSize++
|
|
|
|
} else {
|
|
|
|
d.size += size
|
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set my directory entry in parent
|
|
|
|
if parent != nil {
|
|
|
|
parent.mu.Lock()
|
|
|
|
leaf := path.Base(dirPath)
|
|
|
|
d.parent.dirs[leaf] = d
|
|
|
|
parent.mu.Unlock()
|
|
|
|
}
|
|
|
|
// Accumulate counts in parents
|
|
|
|
for ; parent != nil; parent = parent.parent {
|
|
|
|
parent.mu.Lock()
|
|
|
|
parent.size += d.size
|
2022-04-05 21:46:56 +08:00
|
|
|
parent.count += d.count
|
|
|
|
parent.countUnknownSize += d.countUnknownSize
|
2020-12-28 22:08:12 +08:00
|
|
|
if d.readError != nil {
|
|
|
|
parent.entriesHaveErrors = true
|
|
|
|
}
|
2017-04-02 02:53:44 +08:00
|
|
|
parent.mu.Unlock()
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entries returns a copy of the entries in the directory
|
|
|
|
func (d *Dir) Entries() fs.DirEntries {
|
|
|
|
return append(fs.DirEntries(nil), d.entries...)
|
|
|
|
}
|
|
|
|
|
2018-10-14 22:14:03 +08:00
|
|
|
// Remove removes the i-th entry from the
|
|
|
|
// in-memory representation of the remote directory
|
|
|
|
func (d *Dir) Remove(i int) {
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
d.remove(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// removes the i-th entry from the
|
|
|
|
// in-memory representation of the remote directory
|
|
|
|
//
|
|
|
|
// Call with d.mu held
|
|
|
|
func (d *Dir) remove(i int) {
|
|
|
|
size := d.entries[i].Size()
|
2022-04-05 21:46:56 +08:00
|
|
|
countUnknownSize := int64(0)
|
|
|
|
if size < 0 {
|
|
|
|
size = 0
|
|
|
|
countUnknownSize = 1
|
|
|
|
}
|
2018-10-14 22:14:03 +08:00
|
|
|
count := int64(1)
|
|
|
|
|
|
|
|
subDir, ok := d.getDir(i)
|
|
|
|
if ok {
|
|
|
|
size = subDir.size
|
|
|
|
count = subDir.count
|
2022-04-05 21:46:56 +08:00
|
|
|
countUnknownSize = subDir.countUnknownSize
|
2018-10-14 22:14:03 +08:00
|
|
|
delete(d.dirs, path.Base(subDir.path))
|
|
|
|
}
|
|
|
|
|
|
|
|
d.size -= size
|
|
|
|
d.count -= count
|
2022-04-05 21:46:56 +08:00
|
|
|
d.countUnknownSize -= countUnknownSize
|
2018-10-14 22:14:03 +08:00
|
|
|
d.entries = append(d.entries[:i], d.entries[i+1:]...)
|
|
|
|
|
|
|
|
dir := d
|
|
|
|
// populate changed size and count to parent(s)
|
|
|
|
for parent := d.parent; parent != nil; parent = parent.parent {
|
|
|
|
parent.mu.Lock()
|
|
|
|
parent.dirs[path.Base(dir.path)] = dir
|
|
|
|
parent.size -= size
|
|
|
|
parent.count -= count
|
2022-04-05 21:46:56 +08:00
|
|
|
parent.countUnknownSize -= countUnknownSize
|
2018-10-14 22:14:03 +08:00
|
|
|
dir = parent
|
|
|
|
parent.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 02:53:44 +08:00
|
|
|
// gets the directory of the i-th entry
|
|
|
|
//
|
|
|
|
// returns nil if it is a file
|
|
|
|
// returns a flag as to whether is directory or not
|
|
|
|
//
|
|
|
|
// Call with d.mu held
|
|
|
|
func (d *Dir) getDir(i int) (subDir *Dir, isDir bool) {
|
|
|
|
obj := d.entries[i]
|
2017-06-30 20:37:29 +08:00
|
|
|
dir, ok := obj.(fs.Directory)
|
2017-04-02 02:53:44 +08:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
leaf := path.Base(dir.Remote())
|
|
|
|
subDir = d.dirs[leaf]
|
|
|
|
return subDir, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDir returns the Dir of the i-th entry
|
|
|
|
//
|
|
|
|
// returns nil if it is a file
|
|
|
|
// returns a flag as to whether is directory or not
|
|
|
|
func (d *Dir) GetDir(i int) (subDir *Dir, isDir bool) {
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
return d.getDir(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attr returns the size and count for the directory
|
|
|
|
func (d *Dir) Attr() (size int64, count int64) {
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
return d.size, d.count
|
|
|
|
}
|
|
|
|
|
|
|
|
// AttrI returns the size, count and flags for the i-th directory entry
|
2022-04-05 21:46:56 +08:00
|
|
|
func (d *Dir) AttrI(i int) (size int64, count int64, countUnknownSize int64, isDir bool, readable bool, entriesHaveErrors bool, err error) {
|
2017-04-02 02:53:44 +08:00
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
subDir, isDir := d.getDir(i)
|
2020-12-28 22:08:12 +08:00
|
|
|
|
2017-04-02 02:53:44 +08:00
|
|
|
if !isDir {
|
2022-04-05 21:46:56 +08:00
|
|
|
return d.entries[i].Size(), 0, 0, false, true, d.entriesHaveErrors, d.readError
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
if subDir == nil {
|
2022-04-05 21:46:56 +08:00
|
|
|
return 0, 0, 0, true, false, false, nil
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
size, count = subDir.Attr()
|
2022-04-05 21:46:56 +08:00
|
|
|
return size, count, subDir.countUnknownSize, true, true, subDir.entriesHaveErrors, subDir.readError
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan the Fs passed in, returning a root directory channel and an
|
|
|
|
// error channel
|
2019-06-17 16:34:30 +08:00
|
|
|
func Scan(ctx context.Context, f fs.Fs) (chan *Dir, chan error, chan struct{}) {
|
2020-11-05 19:33:32 +08:00
|
|
|
ci := fs.GetConfig(ctx)
|
2017-04-02 02:53:44 +08:00
|
|
|
root := make(chan *Dir, 1)
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
updated := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
parents := map[string]*Dir{}
|
2020-11-05 19:33:32 +08:00
|
|
|
err := walk.Walk(ctx, f, "", false, ci.MaxDepth, func(dirPath string, entries fs.DirEntries, err error) error {
|
2017-04-02 02:53:44 +08:00
|
|
|
var parent *Dir
|
|
|
|
if dirPath != "" {
|
|
|
|
parentPath := path.Dir(dirPath)
|
|
|
|
if parentPath == "." {
|
|
|
|
parentPath = ""
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
parent, ok = parents[parentPath]
|
|
|
|
if !ok {
|
2021-11-04 18:12:57 +08:00
|
|
|
errChan <- fmt.Errorf("couldn't find parent for %q", dirPath)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-28 22:08:12 +08:00
|
|
|
d := newDir(parent, dirPath, entries, err)
|
2017-04-02 02:53:44 +08:00
|
|
|
parents[dirPath] = d
|
|
|
|
if dirPath == "" {
|
|
|
|
root <- d
|
|
|
|
}
|
|
|
|
// Mark updated
|
|
|
|
select {
|
|
|
|
case updated <- struct{}{}:
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-11-04 18:12:57 +08:00
|
|
|
errChan <- fmt.Errorf("ncdu listing failed: %w", err)
|
2017-04-02 02:53:44 +08:00
|
|
|
}
|
|
|
|
errChan <- nil
|
|
|
|
}()
|
|
|
|
return root, errChan, updated
|
|
|
|
}
|