2017-10-29 03:01:34 +08:00
|
|
|
// Package vfs provides a virtual filing system layer over rclone's
|
|
|
|
// native objects.
|
|
|
|
//
|
|
|
|
// It attempts to behave in a similar way to Go's filing system
|
|
|
|
// manipulation code in the os package. The same named function
|
|
|
|
// should behave in an identical fashion. The objects also obey Go's
|
|
|
|
// standard interfaces.
|
|
|
|
//
|
2017-10-30 05:11:17 +08:00
|
|
|
// Note that paths don't start or end with /, so the root directory
|
|
|
|
// may be referred to as "". However Stat strips slashes so you can
|
|
|
|
// use paths with slashes in.
|
|
|
|
//
|
2022-08-28 19:21:57 +08:00
|
|
|
// # It also includes directory caching
|
2017-11-03 19:35:36 +08:00
|
|
|
//
|
|
|
|
// The vfs package returns Error values to signal precisely which
|
2023-03-25 15:10:16 +08:00
|
|
|
// error conditions have occurred. It may also return general errors
|
2020-10-14 05:49:58 +08:00
|
|
|
// it receives. It tries to use os Error values (e.g. os.ErrExist)
|
2017-11-03 19:35:36 +08:00
|
|
|
// where possible.
|
2022-08-28 19:21:57 +08:00
|
|
|
//
|
2020-04-07 23:05:38 +08:00
|
|
|
//go:generate sh -c "go run make_open_tests.go | gofmt > open_test.go"
|
2017-10-29 03:01:34 +08:00
|
|
|
package vfs
|
2017-05-03 05:35:07 +08:00
|
|
|
|
|
|
|
import (
|
2018-04-07 02:13:27 +08:00
|
|
|
"context"
|
2023-10-05 22:32:50 +08:00
|
|
|
_ "embed"
|
2017-05-09 18:29:02 +08:00
|
|
|
"fmt"
|
2022-08-20 22:38:02 +08:00
|
|
|
"io"
|
2017-10-25 17:00:26 +08:00
|
|
|
"os"
|
2017-10-30 05:11:17 +08:00
|
|
|
"path"
|
2020-04-08 23:54:08 +08:00
|
|
|
"sort"
|
2017-05-03 05:35:07 +08:00
|
|
|
"strings"
|
2018-04-18 06:19:34 +08:00
|
|
|
"sync"
|
2017-05-03 05:35:07 +08:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2023-10-05 01:25:57 +08:00
|
|
|
"github.com/go-git/go-billy/v5"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2020-05-01 19:53:31 +08:00
|
|
|
"github.com/rclone/rclone/fs/cache"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
2021-11-18 00:11:08 +08:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2021-02-18 04:36:13 +08:00
|
|
|
"github.com/rclone/rclone/fs/walk"
|
2020-02-28 22:44:15 +08:00
|
|
|
"github.com/rclone/rclone/vfs/vfscache"
|
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
2017-10-29 03:01:34 +08:00
|
|
|
)
|
|
|
|
|
2023-10-05 22:32:50 +08:00
|
|
|
//go:embed vfs.md
|
2024-04-05 19:27:33 +08:00
|
|
|
var help string
|
|
|
|
|
|
|
|
// Help returns the help string cleaned up to simplify appending
|
|
|
|
func Help() string {
|
|
|
|
return strings.TrimSpace(help) + "\n\n"
|
|
|
|
}
|
2023-10-05 22:32:50 +08:00
|
|
|
|
2017-10-29 03:01:34 +08:00
|
|
|
// Node represents either a directory (*Dir) or a file (*File)
|
2017-05-03 05:35:07 +08:00
|
|
|
type Node interface {
|
2017-10-25 17:00:26 +08:00
|
|
|
os.FileInfo
|
2017-05-03 05:35:07 +08:00
|
|
|
IsFile() bool
|
|
|
|
Inode() uint64
|
2017-10-25 17:00:26 +08:00
|
|
|
SetModTime(modTime time.Time) error
|
2017-11-18 23:48:49 +08:00
|
|
|
Sync() error
|
2017-10-26 23:55:40 +08:00
|
|
|
Remove() error
|
|
|
|
RemoveAll() error
|
2017-10-27 00:02:48 +08:00
|
|
|
DirEntry() fs.DirEntry
|
2017-10-29 19:00:56 +08:00
|
|
|
VFS() *VFS
|
2017-10-30 18:14:39 +08:00
|
|
|
Open(flags int) (Handle, error)
|
2017-11-07 05:38:52 +08:00
|
|
|
Truncate(size int64) error
|
2017-11-18 19:47:21 +08:00
|
|
|
Path() string
|
2020-05-02 01:30:06 +08:00
|
|
|
SetSys(interface{})
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2017-10-29 03:01:34 +08:00
|
|
|
// Check interfaces
|
2017-05-03 05:35:07 +08:00
|
|
|
var (
|
|
|
|
_ Node = (*File)(nil)
|
|
|
|
_ Node = (*Dir)(nil)
|
|
|
|
)
|
|
|
|
|
2017-10-28 05:07:59 +08:00
|
|
|
// Nodes is a slice of Node
|
|
|
|
type Nodes []Node
|
|
|
|
|
|
|
|
// Sort functions
|
|
|
|
func (ns Nodes) Len() int { return len(ns) }
|
|
|
|
func (ns Nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
|
2017-11-18 19:47:21 +08:00
|
|
|
func (ns Nodes) Less(i, j int) bool { return ns[i].Path() < ns[j].Path() }
|
2017-10-28 05:07:59 +08:00
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// Noder represents something which can return a node
|
|
|
|
type Noder interface {
|
2017-05-09 18:29:02 +08:00
|
|
|
fmt.Stringer
|
2017-05-03 05:35:07 +08:00
|
|
|
Node() Node
|
|
|
|
}
|
|
|
|
|
2017-10-29 03:01:34 +08:00
|
|
|
// Check interfaces
|
2017-05-03 05:35:07 +08:00
|
|
|
var (
|
|
|
|
_ Noder = (*File)(nil)
|
|
|
|
_ Noder = (*Dir)(nil)
|
|
|
|
_ Noder = (*ReadFileHandle)(nil)
|
|
|
|
_ Noder = (*WriteFileHandle)(nil)
|
2017-11-07 05:38:52 +08:00
|
|
|
_ Noder = (*RWFileHandle)(nil)
|
2017-10-30 18:14:39 +08:00
|
|
|
_ Noder = (*DirHandle)(nil)
|
2017-05-03 05:35:07 +08:00
|
|
|
)
|
|
|
|
|
2017-11-07 05:38:52 +08:00
|
|
|
// OsFiler is the methods on *os.File
|
|
|
|
type OsFiler interface {
|
2017-10-30 05:11:17 +08:00
|
|
|
Chdir() error
|
|
|
|
Chmod(mode os.FileMode) error
|
|
|
|
Chown(uid, gid int) error
|
|
|
|
Close() error
|
|
|
|
Fd() uintptr
|
|
|
|
Name() string
|
|
|
|
Read(b []byte) (n int, err error)
|
|
|
|
ReadAt(b []byte, off int64) (n int, err error)
|
|
|
|
Readdir(n int) ([]os.FileInfo, error)
|
|
|
|
Readdirnames(n int) (names []string, err error)
|
|
|
|
Seek(offset int64, whence int) (ret int64, err error)
|
|
|
|
Stat() (os.FileInfo, error)
|
|
|
|
Sync() error
|
|
|
|
Truncate(size int64) error
|
|
|
|
Write(b []byte) (n int, err error)
|
|
|
|
WriteAt(b []byte, off int64) (n int, err error)
|
|
|
|
WriteString(s string) (n int, err error)
|
2017-11-07 05:38:52 +08:00
|
|
|
}
|
|
|
|
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 08:17:24 +08:00
|
|
|
// Handle is the interface satisfied by open files or directories.
|
2017-11-07 05:38:52 +08:00
|
|
|
// It is the methods on *os.File, plus a few more useful for FUSE
|
|
|
|
// filingsystems. Not all of them are supported.
|
|
|
|
type Handle interface {
|
|
|
|
OsFiler
|
2017-11-03 02:22:26 +08:00
|
|
|
// Additional methods useful for FUSE filesystems
|
|
|
|
Flush() error
|
|
|
|
Release() error
|
2017-11-03 17:32:18 +08:00
|
|
|
Node() Node
|
2017-11-07 05:38:52 +08:00
|
|
|
// Size() int64
|
2023-10-05 01:25:57 +08:00
|
|
|
Lock() error
|
|
|
|
Unlock() error
|
2017-10-30 05:11:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// baseHandle implements all the missing methods
|
|
|
|
type baseHandle struct{}
|
|
|
|
|
|
|
|
func (h baseHandle) Chdir() error { return ENOSYS }
|
|
|
|
func (h baseHandle) Chmod(mode os.FileMode) error { return ENOSYS }
|
|
|
|
func (h baseHandle) Chown(uid, gid int) error { return ENOSYS }
|
|
|
|
func (h baseHandle) Close() error { return ENOSYS }
|
|
|
|
func (h baseHandle) Fd() uintptr { return 0 }
|
|
|
|
func (h baseHandle) Name() string { return "" }
|
|
|
|
func (h baseHandle) Read(b []byte) (n int, err error) { return 0, ENOSYS }
|
|
|
|
func (h baseHandle) ReadAt(b []byte, off int64) (n int, err error) { return 0, ENOSYS }
|
|
|
|
func (h baseHandle) Readdir(n int) ([]os.FileInfo, error) { return nil, ENOSYS }
|
|
|
|
func (h baseHandle) Readdirnames(n int) (names []string, err error) { return nil, ENOSYS }
|
|
|
|
func (h baseHandle) Seek(offset int64, whence int) (ret int64, err error) { return 0, ENOSYS }
|
|
|
|
func (h baseHandle) Stat() (os.FileInfo, error) { return nil, ENOSYS }
|
|
|
|
func (h baseHandle) Sync() error { return nil }
|
|
|
|
func (h baseHandle) Truncate(size int64) error { return ENOSYS }
|
|
|
|
func (h baseHandle) Write(b []byte) (n int, err error) { return 0, ENOSYS }
|
|
|
|
func (h baseHandle) WriteAt(b []byte, off int64) (n int, err error) { return 0, ENOSYS }
|
|
|
|
func (h baseHandle) WriteString(s string) (n int, err error) { return 0, ENOSYS }
|
2017-11-03 02:22:26 +08:00
|
|
|
func (h baseHandle) Flush() (err error) { return ENOSYS }
|
|
|
|
func (h baseHandle) Release() (err error) { return ENOSYS }
|
2017-11-03 17:32:18 +08:00
|
|
|
func (h baseHandle) Node() Node { return nil }
|
2023-10-05 01:25:57 +08:00
|
|
|
func (h baseHandle) Unlock() error { return os.ErrInvalid }
|
|
|
|
func (h baseHandle) Lock() error { return os.ErrInvalid }
|
2017-10-30 05:11:17 +08:00
|
|
|
|
2017-11-07 05:38:52 +08:00
|
|
|
//func (h baseHandle) Size() int64 { return 0 }
|
|
|
|
|
2017-10-30 05:11:17 +08:00
|
|
|
// Check interfaces
|
|
|
|
var (
|
2023-10-05 01:25:57 +08:00
|
|
|
_ OsFiler = (*os.File)(nil)
|
|
|
|
_ Handle = (*baseHandle)(nil)
|
|
|
|
_ Handle = (*ReadFileHandle)(nil)
|
|
|
|
_ Handle = (*WriteFileHandle)(nil)
|
|
|
|
_ Handle = (*DirHandle)(nil)
|
|
|
|
_ billy.File = (Handle)(nil)
|
2017-10-30 05:11:17 +08:00
|
|
|
)
|
|
|
|
|
2017-10-29 03:01:34 +08:00
|
|
|
// VFS represents the top level filing system
|
|
|
|
type VFS struct {
|
2020-06-15 18:28:42 +08:00
|
|
|
f fs.Fs
|
|
|
|
root *Dir
|
|
|
|
Opt vfscommon.Options
|
|
|
|
cache *vfscache.Cache
|
|
|
|
cancelCache context.CancelFunc
|
|
|
|
usageMu sync.Mutex
|
|
|
|
usageTime time.Time
|
|
|
|
usage *fs.Usage
|
|
|
|
pollChan chan time.Duration
|
2023-08-19 05:18:56 +08:00
|
|
|
inUse atomic.Int32 // count of number of opens
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2020-06-15 18:28:42 +08:00
|
|
|
// Keep track of active VFS keyed on fs.ConfigString(f)
|
|
|
|
var (
|
|
|
|
activeMu sync.Mutex
|
|
|
|
active = map[string][]*VFS{}
|
|
|
|
)
|
|
|
|
|
2017-10-29 19:00:56 +08:00
|
|
|
// New creates a new VFS and root directory. If opt is nil, then
|
2017-10-30 01:37:54 +08:00
|
|
|
// DefaultOpt will be used
|
2020-02-28 22:44:15 +08:00
|
|
|
func New(f fs.Fs, opt *vfscommon.Options) *VFS {
|
2017-06-30 20:37:29 +08:00
|
|
|
fsDir := fs.NewDir("", time.Now())
|
2017-10-29 03:01:34 +08:00
|
|
|
vfs := &VFS{
|
2023-08-19 05:18:56 +08:00
|
|
|
f: f,
|
2017-10-30 01:37:54 +08:00
|
|
|
}
|
2023-08-19 05:18:56 +08:00
|
|
|
vfs.inUse.Store(1)
|
2017-10-30 01:37:54 +08:00
|
|
|
|
|
|
|
// Make a copy of the options
|
|
|
|
if opt != nil {
|
|
|
|
vfs.Opt = *opt
|
|
|
|
} else {
|
2024-07-03 18:34:29 +08:00
|
|
|
vfs.Opt = vfscommon.Opt
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
2017-05-26 05:05:49 +08:00
|
|
|
|
2022-06-16 18:11:14 +08:00
|
|
|
// Fill out anything else
|
|
|
|
vfs.Opt.Init()
|
2017-10-30 05:14:05 +08:00
|
|
|
|
2020-06-15 18:28:42 +08:00
|
|
|
// Find a VFS with the same name and options and return it if possible
|
|
|
|
activeMu.Lock()
|
|
|
|
defer activeMu.Unlock()
|
|
|
|
configName := fs.ConfigString(f)
|
|
|
|
for _, activeVFS := range active[configName] {
|
|
|
|
if vfs.Opt == activeVFS.Opt {
|
|
|
|
fs.Debugf(f, "Re-using VFS from active cache")
|
2023-08-19 05:18:56 +08:00
|
|
|
activeVFS.inUse.Add(1)
|
2020-06-15 18:28:42 +08:00
|
|
|
return activeVFS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Put the VFS into the active cache
|
|
|
|
active[configName] = append(active[configName], vfs)
|
|
|
|
|
2017-10-29 19:00:56 +08:00
|
|
|
// Create root directory
|
2017-10-29 03:01:34 +08:00
|
|
|
vfs.root = newDir(vfs, f, nil, fsDir)
|
2017-05-26 05:05:49 +08:00
|
|
|
|
2018-08-26 03:28:57 +08:00
|
|
|
// Start polling function
|
2020-08-19 00:31:39 +08:00
|
|
|
features := vfs.f.Features()
|
|
|
|
if do := features.ChangeNotify; do != nil {
|
2018-08-26 03:28:57 +08:00
|
|
|
vfs.pollChan = make(chan time.Duration)
|
2019-10-17 21:41:55 +08:00
|
|
|
do(context.TODO(), vfs.root.changeNotify, vfs.pollChan)
|
2024-07-03 00:31:51 +08:00
|
|
|
vfs.pollChan <- time.Duration(vfs.Opt.PollInterval)
|
2021-05-28 17:41:30 +08:00
|
|
|
} else if vfs.Opt.PollInterval > 0 {
|
2018-08-26 03:28:57 +08:00
|
|
|
fs.Infof(f, "poll-interval is not supported by this remote")
|
2017-05-26 05:05:49 +08:00
|
|
|
}
|
2017-11-07 05:38:52 +08:00
|
|
|
|
2020-08-19 00:31:39 +08:00
|
|
|
// Warn if can't stream
|
|
|
|
if !vfs.Opt.ReadOnly && vfs.Opt.CacheMode < vfscommon.CacheModeWrites && features.PutStream == nil {
|
|
|
|
fs.Logf(f, "--vfs-cache-mode writes or full is recommended for this remote as it can't stream")
|
|
|
|
}
|
|
|
|
|
2020-05-01 19:53:31 +08:00
|
|
|
// Pin the Fs into the cache so that when we use cache.NewFs
|
|
|
|
// with the same remote string we get this one. The Pin is
|
2020-09-01 00:46:58 +08:00
|
|
|
// removed when the vfs is finalized
|
|
|
|
cache.PinUntilFinalized(f, vfs)
|
2020-06-15 18:28:42 +08:00
|
|
|
|
2023-09-08 21:41:12 +08:00
|
|
|
// Refresh the dircache if required
|
|
|
|
if vfs.Opt.Refresh {
|
|
|
|
go vfs.refresh()
|
|
|
|
}
|
|
|
|
|
2023-05-14 18:46:06 +08:00
|
|
|
// This can take some time so do it after the Pin
|
|
|
|
vfs.SetCacheMode(vfs.Opt.CacheMode)
|
|
|
|
|
2017-10-29 03:01:34 +08:00
|
|
|
return vfs
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2023-09-08 21:41:12 +08:00
|
|
|
// refresh the directory cache for all directories
|
|
|
|
func (vfs *VFS) refresh() {
|
|
|
|
fs.Debugf(vfs.f, "Refreshing VFS directory cache")
|
|
|
|
err := vfs.root.readDirTree()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(vfs.f, "Error refreshing VFS directory cache: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 00:11:08 +08:00
|
|
|
// Stats returns info about the VFS
|
|
|
|
func (vfs *VFS) Stats() (out rc.Params) {
|
|
|
|
out = make(rc.Params)
|
|
|
|
out["fs"] = fs.ConfigString(vfs.f)
|
|
|
|
out["opt"] = vfs.Opt
|
2023-08-19 05:18:56 +08:00
|
|
|
out["inUse"] = vfs.inUse.Load()
|
2021-11-18 00:11:08 +08:00
|
|
|
|
|
|
|
var (
|
|
|
|
dirs int
|
|
|
|
files int
|
|
|
|
)
|
|
|
|
vfs.root.walk(func(d *Dir) {
|
|
|
|
dirs++
|
|
|
|
files += len(d.items)
|
|
|
|
})
|
|
|
|
inf := make(rc.Params)
|
|
|
|
out["metadataCache"] = inf
|
|
|
|
inf["dirs"] = dirs
|
|
|
|
inf["files"] = files
|
|
|
|
|
|
|
|
if vfs.cache != nil {
|
|
|
|
out["diskCache"] = vfs.cache.Stats()
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2020-06-15 18:28:42 +08:00
|
|
|
// Return the number of active cache entries and a VFS if any are in
|
|
|
|
// the cache.
|
|
|
|
func activeCacheEntries() (vfs *VFS, count int) {
|
|
|
|
activeMu.Lock()
|
|
|
|
for _, vfses := range active {
|
|
|
|
count += len(vfses)
|
|
|
|
if len(vfses) > 0 {
|
|
|
|
vfs = vfses[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
activeMu.Unlock()
|
|
|
|
return vfs, count
|
|
|
|
}
|
|
|
|
|
2019-08-01 05:19:23 +08:00
|
|
|
// Fs returns the Fs passed into the New call
|
|
|
|
func (vfs *VFS) Fs() fs.Fs {
|
|
|
|
return vfs.f
|
|
|
|
}
|
|
|
|
|
2018-04-16 23:38:32 +08:00
|
|
|
// SetCacheMode change the cache mode
|
2020-02-28 22:44:15 +08:00
|
|
|
func (vfs *VFS) SetCacheMode(cacheMode vfscommon.CacheMode) {
|
2020-06-15 18:28:42 +08:00
|
|
|
vfs.shutdownCache()
|
2018-04-16 23:38:32 +08:00
|
|
|
vfs.cache = nil
|
2020-02-28 22:44:15 +08:00
|
|
|
if cacheMode > vfscommon.CacheModeOff {
|
2018-04-16 23:38:32 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2020-06-23 22:18:58 +08:00
|
|
|
cache, err := vfscache.New(ctx, vfs.f, &vfs.Opt, vfs.AddVirtual) // FIXME pass on context or get from Opt?
|
2018-04-16 23:38:32 +08:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to create vfs cache - disabling: %v", err)
|
2020-02-28 22:44:15 +08:00
|
|
|
vfs.Opt.CacheMode = vfscommon.CacheModeOff
|
2018-04-16 23:38:32 +08:00
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
2019-10-07 04:05:21 +08:00
|
|
|
vfs.Opt.CacheMode = cacheMode
|
2020-06-15 18:28:42 +08:00
|
|
|
vfs.cancelCache = cancel
|
2018-04-16 23:38:32 +08:00
|
|
|
vfs.cache = cache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 18:28:42 +08:00
|
|
|
// shutdown the cache if it was running
|
|
|
|
func (vfs *VFS) shutdownCache() {
|
|
|
|
if vfs.cancelCache != nil {
|
|
|
|
vfs.cancelCache()
|
|
|
|
vfs.cancelCache = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops any background go-routines and removes the VFS from
|
|
|
|
// the active ache.
|
2017-11-07 05:38:52 +08:00
|
|
|
func (vfs *VFS) Shutdown() {
|
2023-08-19 05:18:56 +08:00
|
|
|
if vfs.inUse.Add(-1) > 0 {
|
2020-06-15 18:28:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove from active cache
|
|
|
|
activeMu.Lock()
|
|
|
|
configName := fs.ConfigString(vfs.f)
|
|
|
|
activeVFSes := active[configName]
|
|
|
|
for i, activeVFS := range activeVFSes {
|
|
|
|
if activeVFS == vfs {
|
|
|
|
activeVFSes[i] = nil
|
|
|
|
active[configName] = append(activeVFSes[:i], activeVFSes[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
2017-11-07 05:38:52 +08:00
|
|
|
}
|
2020-06-15 18:28:42 +08:00
|
|
|
activeMu.Unlock()
|
|
|
|
|
|
|
|
vfs.shutdownCache()
|
2017-11-07 05:38:52 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 19:59:01 +08:00
|
|
|
// CleanUp deletes the contents of the on disk cache
|
2017-11-07 05:38:52 +08:00
|
|
|
func (vfs *VFS) CleanUp() error {
|
2020-02-28 22:44:15 +08:00
|
|
|
if vfs.Opt.CacheMode == vfscommon.CacheModeOff {
|
2018-04-16 23:38:32 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-02-28 22:44:15 +08:00
|
|
|
return vfs.cache.CleanUp()
|
2017-11-07 05:38:52 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 19:59:01 +08:00
|
|
|
// FlushDirCache empties the directory cache
|
|
|
|
func (vfs *VFS) FlushDirCache() {
|
|
|
|
vfs.root.ForgetAll()
|
|
|
|
}
|
2017-11-18 19:57:40 +08:00
|
|
|
|
|
|
|
// WaitForWriters sleeps until all writers have finished or
|
|
|
|
// time.Duration has elapsed
|
|
|
|
func (vfs *VFS) WaitForWriters(timeout time.Duration) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(nil, "timeout=%v", timeout)("")
|
2020-04-17 18:18:58 +08:00
|
|
|
tickTime := 10 * time.Millisecond
|
2017-11-18 19:57:40 +08:00
|
|
|
deadline := time.NewTimer(timeout)
|
|
|
|
defer deadline.Stop()
|
|
|
|
tick := time.NewTimer(tickTime)
|
|
|
|
defer tick.Stop()
|
|
|
|
tick.Stop()
|
|
|
|
for {
|
2020-04-15 01:14:24 +08:00
|
|
|
writers := vfs.root.countActiveWriters()
|
2020-04-17 18:18:58 +08:00
|
|
|
cacheInUse := 0
|
|
|
|
if vfs.cache != nil {
|
|
|
|
cacheInUse = vfs.cache.TotalInUse()
|
|
|
|
}
|
|
|
|
if writers == 0 && cacheInUse == 0 {
|
2017-11-18 19:57:40 +08:00
|
|
|
return
|
|
|
|
}
|
2020-04-17 18:18:58 +08:00
|
|
|
fs.Debugf(nil, "Still %d writers active and %d cache items in use, waiting %v", writers, cacheInUse, tickTime)
|
2017-11-18 19:57:40 +08:00
|
|
|
tick.Reset(tickTime)
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
case <-deadline.C:
|
2020-04-17 18:18:58 +08:00
|
|
|
fs.Errorf(nil, "Exiting even though %d writers active and %d cache items in use after %v\n%s", writers, cacheInUse, timeout, vfs.cache.Dump())
|
2017-11-18 19:57:40 +08:00
|
|
|
return
|
|
|
|
}
|
2020-04-17 18:18:58 +08:00
|
|
|
tickTime *= 2
|
|
|
|
if tickTime > time.Second {
|
|
|
|
tickTime = time.Second
|
|
|
|
}
|
2017-11-18 19:57:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// Root returns the root node
|
2017-10-29 03:01:34 +08:00
|
|
|
func (vfs *VFS) Root() (*Dir, error) {
|
|
|
|
// fs.Debugf(vfs.f, "Root()")
|
|
|
|
return vfs.root, nil
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2023-08-19 05:18:56 +08:00
|
|
|
var inodeCount atomic.Uint64
|
2017-05-03 05:35:07 +08:00
|
|
|
|
2017-10-30 01:37:54 +08:00
|
|
|
// newInode creates a new unique inode number
|
|
|
|
func newInode() (inode uint64) {
|
2023-08-19 05:18:56 +08:00
|
|
|
return inodeCount.Add(1)
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
2017-10-29 19:36:38 +08:00
|
|
|
// Stat finds the Node by path starting from the root
|
|
|
|
//
|
|
|
|
// It is the equivalent of os.Stat - Node contains the os.FileInfo
|
|
|
|
// interface.
|
|
|
|
func (vfs *VFS) Stat(path string) (node Node, err error) {
|
2017-10-30 05:11:17 +08:00
|
|
|
path = strings.Trim(path, "/")
|
2017-10-29 03:01:34 +08:00
|
|
|
node = vfs.root
|
2017-05-03 05:35:07 +08:00
|
|
|
for path != "" {
|
|
|
|
i := strings.IndexRune(path, '/')
|
|
|
|
var name string
|
|
|
|
if i < 0 {
|
|
|
|
name, path = path, ""
|
|
|
|
} else {
|
|
|
|
name, path = path[:i], path[i+1:]
|
|
|
|
}
|
|
|
|
if name == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dir, ok := node.(*Dir)
|
|
|
|
if !ok {
|
|
|
|
// We need to look in a directory, but found a file
|
|
|
|
return nil, ENOENT
|
|
|
|
}
|
2017-10-29 19:36:38 +08:00
|
|
|
node, err = dir.Stat(name)
|
2017-05-03 05:35:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-10-30 05:11:17 +08:00
|
|
|
|
|
|
|
// StatParent finds the parent directory and the leaf name of a path
|
|
|
|
func (vfs *VFS) StatParent(name string) (dir *Dir, leaf string, err error) {
|
|
|
|
name = strings.Trim(name, "/")
|
|
|
|
parent, leaf := path.Split(name)
|
|
|
|
node, err := vfs.Stat(parent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
if node.IsFile() {
|
|
|
|
return nil, "", os.ErrExist
|
|
|
|
}
|
|
|
|
dir = node.(*Dir)
|
|
|
|
return dir, leaf, nil
|
|
|
|
}
|
|
|
|
|
2017-11-15 05:00:08 +08:00
|
|
|
// decodeOpenFlags returns a string representing the open flags
|
|
|
|
func decodeOpenFlags(flags int) string {
|
|
|
|
var out []string
|
|
|
|
rdwrMode := flags & accessModeMask
|
|
|
|
switch rdwrMode {
|
|
|
|
case os.O_RDONLY:
|
|
|
|
out = append(out, "O_RDONLY")
|
|
|
|
case os.O_WRONLY:
|
|
|
|
out = append(out, "O_WRONLY")
|
|
|
|
case os.O_RDWR:
|
|
|
|
out = append(out, "O_RDWR")
|
|
|
|
default:
|
|
|
|
out = append(out, fmt.Sprintf("0x%X", rdwrMode))
|
|
|
|
}
|
|
|
|
if flags&os.O_APPEND != 0 {
|
|
|
|
out = append(out, "O_APPEND")
|
|
|
|
}
|
|
|
|
if flags&os.O_CREATE != 0 {
|
|
|
|
out = append(out, "O_CREATE")
|
|
|
|
}
|
|
|
|
if flags&os.O_EXCL != 0 {
|
|
|
|
out = append(out, "O_EXCL")
|
|
|
|
}
|
|
|
|
if flags&os.O_SYNC != 0 {
|
|
|
|
out = append(out, "O_SYNC")
|
|
|
|
}
|
|
|
|
if flags&os.O_TRUNC != 0 {
|
|
|
|
out = append(out, "O_TRUNC")
|
|
|
|
}
|
|
|
|
flags &^= accessModeMask | os.O_APPEND | os.O_CREATE | os.O_EXCL | os.O_SYNC | os.O_TRUNC
|
|
|
|
if flags != 0 {
|
|
|
|
out = append(out, fmt.Sprintf("0x%X", flags))
|
|
|
|
}
|
|
|
|
return strings.Join(out, "|")
|
|
|
|
}
|
|
|
|
|
2017-10-30 05:11:17 +08:00
|
|
|
// OpenFile a file according to the flags and perm provided
|
|
|
|
func (vfs *VFS) OpenFile(name string, flags int, perm os.FileMode) (fd Handle, err error) {
|
2018-01-13 00:30:54 +08:00
|
|
|
defer log.Trace(name, "flags=%s, perm=%v", decodeOpenFlags(flags), perm)("fd=%v, err=%v", &fd, &err)
|
2018-02-24 06:39:28 +08:00
|
|
|
|
|
|
|
// http://pubs.opengroup.org/onlinepubs/7908799/xsh/open.html
|
|
|
|
// The result of using O_TRUNC with O_RDONLY is undefined.
|
|
|
|
// Linux seems to truncate the file, but we prefer to return EINVAL
|
|
|
|
if flags&accessModeMask == os.O_RDONLY && flags&os.O_TRUNC != 0 {
|
|
|
|
return nil, EINVAL
|
|
|
|
}
|
|
|
|
|
2017-10-30 05:11:17 +08:00
|
|
|
node, err := vfs.Stat(name)
|
|
|
|
if err != nil {
|
2017-11-06 20:22:45 +08:00
|
|
|
if err != ENOENT || flags&os.O_CREATE == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If not found and O_CREATE then create the file
|
|
|
|
dir, leaf, err := vfs.StatParent(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-07 20:34:18 +08:00
|
|
|
node, err = dir.Create(leaf, flags)
|
2017-11-06 20:22:45 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-30 05:11:17 +08:00
|
|
|
}
|
|
|
|
}
|
2017-10-30 18:14:39 +08:00
|
|
|
return node.Open(flags)
|
2017-10-30 05:11:17 +08:00
|
|
|
}
|
|
|
|
|
2020-04-08 23:54:08 +08:00
|
|
|
// Open opens the named file for reading. If successful, methods on
|
|
|
|
// the returned file can be used for reading; the associated file
|
|
|
|
// descriptor has mode O_RDONLY.
|
|
|
|
func (vfs *VFS) Open(name string) (Handle, error) {
|
|
|
|
return vfs.OpenFile(name, os.O_RDONLY, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates the named file with mode 0666 (before umask), truncating
|
|
|
|
// it if it already exists. If successful, methods on the returned
|
|
|
|
// File can be used for I/O; the associated file descriptor has mode
|
|
|
|
// O_RDWR.
|
|
|
|
func (vfs *VFS) Create(name string) (Handle, error) {
|
|
|
|
return vfs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
|
|
|
|
}
|
|
|
|
|
2017-10-30 05:11:17 +08:00
|
|
|
// Rename oldName to newName
|
|
|
|
func (vfs *VFS) Rename(oldName, newName string) error {
|
|
|
|
// find the parent directories
|
|
|
|
oldDir, oldLeaf, err := vfs.StatParent(oldName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newDir, newLeaf, err := vfs.StatParent(newName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = oldDir.Rename(oldLeaf, newLeaf, newDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-18 06:19:34 +08:00
|
|
|
|
2020-04-09 01:29:50 +08:00
|
|
|
// This works out the missing values from (total, used, free) using
|
|
|
|
// unknownFree as the intended free space
|
|
|
|
func fillInMissingSizes(total, used, free, unknownFree int64) (newTotal, newUsed, newFree int64) {
|
|
|
|
if total < 0 {
|
|
|
|
if free >= 0 {
|
|
|
|
total = free
|
|
|
|
} else {
|
|
|
|
total = unknownFree
|
|
|
|
}
|
|
|
|
if used >= 0 {
|
|
|
|
total += used
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// total is now defined
|
|
|
|
if used < 0 {
|
|
|
|
if free >= 0 {
|
|
|
|
used = total - free
|
|
|
|
} else {
|
|
|
|
used = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// used is now defined
|
|
|
|
if free < 0 {
|
|
|
|
free = total - used
|
|
|
|
}
|
|
|
|
return total, used, free
|
|
|
|
}
|
|
|
|
|
2021-03-03 03:11:57 +08:00
|
|
|
// If the total size isn't known then we will aim for this many bytes free (1 PiB)
|
2020-04-09 01:29:50 +08:00
|
|
|
const unknownFreeBytes = 1 << 50
|
|
|
|
|
2018-04-18 06:19:34 +08:00
|
|
|
// Statfs returns into about the filing system if known
|
|
|
|
//
|
|
|
|
// The values will be -1 if they aren't known
|
|
|
|
//
|
|
|
|
// This information is cached for the DirCacheTime interval
|
|
|
|
func (vfs *VFS) Statfs() (total, used, free int64) {
|
|
|
|
// defer log.Trace("/", "")("total=%d, used=%d, free=%d", &total, &used, &free)
|
|
|
|
vfs.usageMu.Lock()
|
|
|
|
defer vfs.usageMu.Unlock()
|
|
|
|
total, used, free = -1, -1, -1
|
|
|
|
doAbout := vfs.f.Features().About
|
2024-07-03 00:31:51 +08:00
|
|
|
if (doAbout != nil || vfs.Opt.UsedIsSize) && (vfs.usageTime.IsZero() || time.Since(vfs.usageTime) >= time.Duration(vfs.Opt.DirCacheTime)) {
|
2018-04-18 06:19:34 +08:00
|
|
|
var err error
|
2021-02-18 04:36:13 +08:00
|
|
|
ctx := context.TODO()
|
|
|
|
if doAbout == nil {
|
|
|
|
vfs.usage = &fs.Usage{}
|
|
|
|
} else {
|
|
|
|
vfs.usage, err = doAbout(ctx)
|
|
|
|
}
|
|
|
|
if vfs.Opt.UsedIsSize {
|
2021-10-15 00:49:41 +08:00
|
|
|
var usedBySizeAlgorithm int64
|
2021-02-18 04:36:13 +08:00
|
|
|
// Algorithm from `rclone size`
|
|
|
|
err = walk.ListR(ctx, vfs.f, "", true, -1, walk.ListObjects, func(entries fs.DirEntries) error {
|
|
|
|
entries.ForObject(func(o fs.Object) {
|
|
|
|
usedBySizeAlgorithm += o.Size()
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
vfs.usage.Used = &usedBySizeAlgorithm
|
2024-12-02 23:00:11 +08:00
|
|
|
// if we read a Total size then we should calculate Free from it
|
|
|
|
if vfs.usage.Total != nil {
|
|
|
|
vfs.usage.Free = nil
|
|
|
|
}
|
2021-02-18 04:36:13 +08:00
|
|
|
}
|
2018-04-18 06:19:34 +08:00
|
|
|
vfs.usageTime = time.Now()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(vfs.f, "Statfs failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-07-06 00:29:14 +08:00
|
|
|
|
2018-04-18 06:19:34 +08:00
|
|
|
if u := vfs.usage; u != nil {
|
|
|
|
if u.Total != nil {
|
|
|
|
total = *u.Total
|
|
|
|
}
|
|
|
|
if u.Free != nil {
|
|
|
|
free = *u.Free
|
|
|
|
}
|
|
|
|
if u.Used != nil {
|
|
|
|
used = *u.Used
|
|
|
|
}
|
|
|
|
}
|
2022-07-06 00:29:14 +08:00
|
|
|
|
|
|
|
if int64(vfs.Opt.DiskSpaceTotalSize) >= 0 {
|
|
|
|
total = int64(vfs.Opt.DiskSpaceTotalSize)
|
|
|
|
}
|
|
|
|
|
2020-04-09 01:29:50 +08:00
|
|
|
total, used, free = fillInMissingSizes(total, used, free, unknownFreeBytes)
|
2018-04-18 06:19:34 +08:00
|
|
|
return
|
|
|
|
}
|
2020-04-08 23:54:08 +08:00
|
|
|
|
|
|
|
// Remove removes the named file or (empty) directory.
|
|
|
|
func (vfs *VFS) Remove(name string) error {
|
|
|
|
node, err := vfs.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = node.Remove()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Chtimes changes the access and modification times of the named file, similar
|
|
|
|
// to the Unix utime() or utimes() functions.
|
|
|
|
//
|
|
|
|
// The underlying filesystem may truncate or round the values to a less precise
|
|
|
|
// time unit.
|
|
|
|
func (vfs *VFS) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|
|
|
node, err := vfs.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = node.SetModTime(mtime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 18:58:22 +08:00
|
|
|
// mkdir creates a new directory with the specified name and permission bits
|
|
|
|
// (before umask) returning the new directory node.
|
|
|
|
func (vfs *VFS) mkdir(name string, perm os.FileMode) (*Dir, error) {
|
|
|
|
dir, leaf, err := vfs.StatParent(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dir.Mkdir(leaf)
|
|
|
|
}
|
|
|
|
|
2020-04-08 23:54:08 +08:00
|
|
|
// Mkdir creates a new directory with the specified name and permission bits
|
|
|
|
// (before umask).
|
|
|
|
func (vfs *VFS) Mkdir(name string, perm os.FileMode) error {
|
2023-04-12 18:58:22 +08:00
|
|
|
_, err := vfs.mkdir(name, perm)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// mkdirAll creates a new directory with the specified name and
|
|
|
|
// permission bits (before umask) and all of its parent directories up
|
|
|
|
// to the root.
|
|
|
|
func (vfs *VFS) mkdirAll(name string, perm os.FileMode) (dir *Dir, err error) {
|
|
|
|
name = strings.Trim(name, "/")
|
|
|
|
// the root directory node already exists even if the directory isn't created yet
|
|
|
|
if name == "" {
|
|
|
|
return vfs.root, nil
|
|
|
|
}
|
|
|
|
var parent, leaf string
|
|
|
|
dir, leaf, err = vfs.StatParent(name)
|
|
|
|
if err == ENOENT {
|
|
|
|
parent, leaf = path.Split(name)
|
|
|
|
dir, err = vfs.mkdirAll(parent, perm)
|
|
|
|
}
|
2020-04-08 23:54:08 +08:00
|
|
|
if err != nil {
|
2023-04-12 18:58:22 +08:00
|
|
|
return nil, err
|
2020-04-08 23:54:08 +08:00
|
|
|
}
|
2023-04-12 18:58:22 +08:00
|
|
|
dir, err = dir.Mkdir(leaf)
|
2020-04-08 23:54:08 +08:00
|
|
|
if err != nil {
|
2023-04-12 18:58:22 +08:00
|
|
|
return nil, err
|
2020-04-08 23:54:08 +08:00
|
|
|
}
|
2023-04-12 18:58:22 +08:00
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MkdirAll creates a new directory with the specified name and
|
|
|
|
// permission bits (before umask) and all of its parent directories up
|
|
|
|
// to the root.
|
|
|
|
func (vfs *VFS) MkdirAll(name string, perm os.FileMode) error {
|
|
|
|
_, err := vfs.mkdirAll(name, perm)
|
|
|
|
return err
|
2020-04-08 23:54:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDir reads the directory named by dirname and returns
|
|
|
|
// a list of directory entries sorted by filename.
|
|
|
|
func (vfs *VFS) ReadDir(dirname string) ([]os.FileInfo, error) {
|
|
|
|
f, err := vfs.Open(dirname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list, err := f.Readdir(-1)
|
|
|
|
closeErr := f.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if closeErr != nil {
|
|
|
|
return nil, closeErr
|
|
|
|
}
|
|
|
|
sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFile reads the file named by filename and returns the contents.
|
|
|
|
// A successful call returns err == nil, not err == EOF. Because ReadFile
|
|
|
|
// reads the whole file, it does not treat an EOF from Read as an error
|
|
|
|
// to be reported.
|
|
|
|
func (vfs *VFS) ReadFile(filename string) (b []byte, err error) {
|
|
|
|
f, err := vfs.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fs.CheckClose(f, &err)
|
2022-08-20 22:38:02 +08:00
|
|
|
return io.ReadAll(f)
|
2020-04-08 23:54:08 +08:00
|
|
|
}
|
2020-06-23 22:18:58 +08:00
|
|
|
|
|
|
|
// AddVirtual adds the object (file or dir) to the directory cache
|
2022-12-06 19:54:44 +08:00
|
|
|
func (vfs *VFS) AddVirtual(remote string, size int64, isDir bool) (err error) {
|
|
|
|
remote = strings.TrimRight(remote, "/")
|
|
|
|
var dir *Dir
|
|
|
|
var parent, leaf string
|
|
|
|
if vfs.f.Features().CanHaveEmptyDirectories {
|
|
|
|
dir, leaf, err = vfs.StatParent(remote)
|
|
|
|
} else {
|
|
|
|
// Create parent of virtual directory since backend can't have empty directories
|
|
|
|
parent, leaf = path.Split(remote)
|
2024-07-03 18:34:29 +08:00
|
|
|
dir, err = vfs.mkdirAll(parent, os.FileMode(vfs.Opt.DirPerms))
|
2022-12-06 19:54:44 +08:00
|
|
|
}
|
2020-06-23 22:18:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dir.AddVirtual(leaf, size, false)
|
|
|
|
return nil
|
|
|
|
}
|