2017-10-28 21:01:34 +02: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-29 22:11:17 +01: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.
|
|
|
|
//
|
2017-10-28 21:01:34 +02:00
|
|
|
// It also includes directory caching
|
2017-11-03 12:35:36 +01:00
|
|
|
//
|
|
|
|
// The vfs package returns Error values to signal precisely which
|
|
|
|
// error conditions have ocurred. It may also return general errors
|
|
|
|
// it receives. It tries to use os Error values (eg os.ErrExist)
|
|
|
|
// where possible.
|
2017-10-28 21:01:34 +02:00
|
|
|
package vfs
|
2017-05-02 23:35:07 +02:00
|
|
|
|
|
|
|
import (
|
2018-04-06 20:13:27 +02:00
|
|
|
"context"
|
2017-05-09 12:29:02 +02:00
|
|
|
"fmt"
|
2017-10-25 11:00:26 +02:00
|
|
|
"os"
|
2017-10-29 22:11:17 +01:00
|
|
|
"path"
|
2017-05-02 23:35:07 +02:00
|
|
|
"strings"
|
2018-04-18 00:19:34 +02:00
|
|
|
"sync"
|
2017-05-02 23:35:07 +02:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/log"
|
2017-10-28 21:01:34 +02:00
|
|
|
)
|
|
|
|
|
2017-10-29 18:37:54 +01:00
|
|
|
// DefaultOpt is the default values uses for Opt
|
|
|
|
var DefaultOpt = Options{
|
2017-11-06 22:38:52 +01:00
|
|
|
NoModTime: false,
|
|
|
|
NoChecksum: false,
|
|
|
|
NoSeek: false,
|
|
|
|
DirCacheTime: 5 * 60 * time.Second,
|
|
|
|
PollInterval: time.Minute,
|
|
|
|
ReadOnly: false,
|
|
|
|
Umask: 0,
|
|
|
|
UID: ^uint32(0), // these values instruct WinFSP-FUSE to use the current user
|
|
|
|
GID: ^uint32(0), // overriden for non windows in mount_unix.go
|
|
|
|
DirPerms: os.FileMode(0777) | os.ModeDir,
|
|
|
|
FilePerms: os.FileMode(0666),
|
|
|
|
CacheMode: CacheModeOff,
|
|
|
|
CacheMaxAge: 3600 * time.Second,
|
|
|
|
CachePollInterval: 60 * time.Second,
|
2017-10-29 18:37:54 +01:00
|
|
|
}
|
|
|
|
|
2017-10-28 21:01:34 +02:00
|
|
|
// Node represents either a directory (*Dir) or a file (*File)
|
2017-05-02 23:35:07 +02:00
|
|
|
type Node interface {
|
2017-10-25 11:00:26 +02:00
|
|
|
os.FileInfo
|
2017-05-02 23:35:07 +02:00
|
|
|
IsFile() bool
|
|
|
|
Inode() uint64
|
2017-10-25 11:00:26 +02:00
|
|
|
SetModTime(modTime time.Time) error
|
2017-11-18 16:48:49 +01:00
|
|
|
Sync() error
|
2017-10-26 17:55:40 +02:00
|
|
|
Remove() error
|
|
|
|
RemoveAll() error
|
2017-10-26 18:02:48 +02:00
|
|
|
DirEntry() fs.DirEntry
|
2017-10-29 12:00:56 +01:00
|
|
|
VFS() *VFS
|
2017-10-30 11:14:39 +01:00
|
|
|
Open(flags int) (Handle, error)
|
2017-11-06 22:38:52 +01:00
|
|
|
Truncate(size int64) error
|
2017-11-18 12:47:21 +01:00
|
|
|
Path() string
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-28 21:01:34 +02:00
|
|
|
// Check interfaces
|
2017-05-02 23:35:07 +02:00
|
|
|
var (
|
|
|
|
_ Node = (*File)(nil)
|
|
|
|
_ Node = (*Dir)(nil)
|
|
|
|
)
|
|
|
|
|
2017-10-27 23:07:59 +02: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 12:47:21 +01:00
|
|
|
func (ns Nodes) Less(i, j int) bool { return ns[i].Path() < ns[j].Path() }
|
2017-10-27 23:07:59 +02:00
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// Noder represents something which can return a node
|
|
|
|
type Noder interface {
|
2017-05-09 12:29:02 +02:00
|
|
|
fmt.Stringer
|
2017-05-02 23:35:07 +02:00
|
|
|
Node() Node
|
|
|
|
}
|
|
|
|
|
2017-10-28 21:01:34 +02:00
|
|
|
// Check interfaces
|
2017-05-02 23:35:07 +02:00
|
|
|
var (
|
|
|
|
_ Noder = (*File)(nil)
|
|
|
|
_ Noder = (*Dir)(nil)
|
|
|
|
_ Noder = (*ReadFileHandle)(nil)
|
|
|
|
_ Noder = (*WriteFileHandle)(nil)
|
2017-11-06 22:38:52 +01:00
|
|
|
_ Noder = (*RWFileHandle)(nil)
|
2017-10-30 11:14:39 +01:00
|
|
|
_ Noder = (*DirHandle)(nil)
|
2017-05-02 23:35:07 +02:00
|
|
|
)
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
// OsFiler is the methods on *os.File
|
|
|
|
type OsFiler interface {
|
2017-10-29 22:11:17 +01: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-06 22:38:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle is the interface statisified by open files or directories.
|
|
|
|
// 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-02 19:22:26 +01:00
|
|
|
// Additional methods useful for FUSE filesystems
|
|
|
|
Flush() error
|
|
|
|
Release() error
|
2017-11-03 10:32:18 +01:00
|
|
|
Node() Node
|
2017-11-06 22:38:52 +01:00
|
|
|
// Size() int64
|
2017-10-29 22:11:17 +01: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-02 19:22:26 +01:00
|
|
|
func (h baseHandle) Flush() (err error) { return ENOSYS }
|
|
|
|
func (h baseHandle) Release() (err error) { return ENOSYS }
|
2017-11-03 10:32:18 +01:00
|
|
|
func (h baseHandle) Node() Node { return nil }
|
2017-10-29 22:11:17 +01:00
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
//func (h baseHandle) Size() int64 { return 0 }
|
|
|
|
|
2017-10-29 22:11:17 +01:00
|
|
|
// Check interfaces
|
|
|
|
var (
|
2017-11-06 22:38:52 +01:00
|
|
|
_ OsFiler = (*os.File)(nil)
|
|
|
|
_ Handle = (*baseHandle)(nil)
|
|
|
|
_ Handle = (*ReadFileHandle)(nil)
|
|
|
|
_ Handle = (*WriteFileHandle)(nil)
|
|
|
|
_ Handle = (*DirHandle)(nil)
|
2017-10-29 22:11:17 +01:00
|
|
|
)
|
|
|
|
|
2017-10-28 21:01:34 +02:00
|
|
|
// VFS represents the top level filing system
|
|
|
|
type VFS struct {
|
2018-04-18 00:19:34 +02:00
|
|
|
f fs.Fs
|
|
|
|
root *Dir
|
|
|
|
Opt Options
|
|
|
|
cache *cache
|
|
|
|
cancel context.CancelFunc
|
|
|
|
usageMu sync.Mutex
|
|
|
|
usageTime time.Time
|
|
|
|
usage *fs.Usage
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2017-10-29 12:00:56 +01:00
|
|
|
// Options is options for creating the vfs
|
|
|
|
type Options struct {
|
2017-11-06 22:38:52 +01:00
|
|
|
NoSeek bool // don't allow seeking if set
|
|
|
|
NoChecksum bool // don't check checksums if set
|
|
|
|
ReadOnly bool // if set VFS is read only
|
|
|
|
NoModTime bool // don't read mod times for files
|
|
|
|
DirCacheTime time.Duration // how long to consider directory listing cache valid
|
|
|
|
PollInterval time.Duration
|
|
|
|
Umask int
|
|
|
|
UID uint32
|
|
|
|
GID uint32
|
|
|
|
DirPerms os.FileMode
|
|
|
|
FilePerms os.FileMode
|
2018-02-18 15:18:12 +01:00
|
|
|
ChunkSize fs.SizeSuffix // if > 0 read files in chunks
|
|
|
|
ChunkSizeLimit fs.SizeSuffix // if > ChunkSize double the chunk size after each chunk until reached
|
2017-11-06 22:38:52 +01:00
|
|
|
CacheMode CacheMode
|
|
|
|
CacheMaxAge time.Duration
|
|
|
|
CachePollInterval time.Duration
|
2017-10-29 12:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new VFS and root directory. If opt is nil, then
|
2017-10-29 18:37:54 +01:00
|
|
|
// DefaultOpt will be used
|
2017-10-29 12:00:56 +01:00
|
|
|
func New(f fs.Fs, opt *Options) *VFS {
|
2017-06-30 14:37:29 +02:00
|
|
|
fsDir := fs.NewDir("", time.Now())
|
2017-10-28 21:01:34 +02:00
|
|
|
vfs := &VFS{
|
2017-10-29 18:37:54 +01:00
|
|
|
f: f,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy of the options
|
|
|
|
if opt != nil {
|
|
|
|
vfs.Opt = *opt
|
|
|
|
} else {
|
|
|
|
vfs.Opt = DefaultOpt
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
2017-05-25 23:05:49 +02:00
|
|
|
|
2017-10-29 12:00:56 +01:00
|
|
|
// Mask the permissions with the umask
|
|
|
|
vfs.Opt.DirPerms &= ^os.FileMode(vfs.Opt.Umask)
|
|
|
|
vfs.Opt.FilePerms &= ^os.FileMode(vfs.Opt.Umask)
|
2017-05-25 23:05:49 +02:00
|
|
|
|
2017-10-29 22:14:05 +01:00
|
|
|
// Make sure directories are returned as directories
|
|
|
|
vfs.Opt.DirPerms |= os.ModeDir
|
|
|
|
|
2017-10-29 12:00:56 +01:00
|
|
|
// Create root directory
|
2017-10-28 21:01:34 +02:00
|
|
|
vfs.root = newDir(vfs, f, nil, fsDir)
|
2017-05-25 23:05:49 +02:00
|
|
|
|
2017-10-29 12:00:56 +01:00
|
|
|
// Start polling if required
|
|
|
|
if vfs.Opt.PollInterval > 0 {
|
2018-03-08 21:03:34 +01:00
|
|
|
if do := vfs.f.Features().ChangeNotify; do != nil {
|
2018-05-02 15:28:17 +02:00
|
|
|
do(vfs.notifyFunc, vfs.Opt.PollInterval)
|
2017-11-06 14:43:40 +01:00
|
|
|
} else {
|
2018-02-09 08:57:50 +01:00
|
|
|
fs.Infof(f, "poll-interval is not supported by this remote")
|
2017-10-29 18:37:54 +01:00
|
|
|
}
|
2017-05-25 23:05:49 +02:00
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
|
2018-04-16 17:38:32 +02:00
|
|
|
vfs.SetCacheMode(vfs.Opt.CacheMode)
|
2018-03-16 21:45:34 +01:00
|
|
|
|
|
|
|
// add the remote control
|
|
|
|
vfs.addRC()
|
2017-10-28 21:01:34 +02:00
|
|
|
return vfs
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2018-04-16 17:38:32 +02:00
|
|
|
// SetCacheMode change the cache mode
|
|
|
|
func (vfs *VFS) SetCacheMode(cacheMode CacheMode) {
|
|
|
|
vfs.Shutdown()
|
|
|
|
vfs.cache = nil
|
|
|
|
if vfs.Opt.CacheMode > CacheModeOff {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cache, err := newCache(ctx, vfs.f, &vfs.Opt) // FIXME pass on context or get from Opt?
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to create vfs cache - disabling: %v", err)
|
|
|
|
vfs.Opt.CacheMode = CacheModeOff
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vfs.cancel = cancel
|
|
|
|
vfs.cache = cache
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:38:52 +01:00
|
|
|
// Shutdown stops any background go-routines
|
|
|
|
func (vfs *VFS) Shutdown() {
|
|
|
|
if vfs.cancel != nil {
|
|
|
|
vfs.cancel()
|
|
|
|
vfs.cancel = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 12:59:01 +01:00
|
|
|
// CleanUp deletes the contents of the on disk cache
|
2017-11-06 22:38:52 +01:00
|
|
|
func (vfs *VFS) CleanUp() error {
|
2018-04-16 17:38:32 +02:00
|
|
|
if vfs.Opt.CacheMode == CacheModeOff {
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-06 22:38:52 +01:00
|
|
|
return vfs.cache.cleanUp()
|
|
|
|
}
|
|
|
|
|
2017-11-18 12:59:01 +01:00
|
|
|
// FlushDirCache empties the directory cache
|
|
|
|
func (vfs *VFS) FlushDirCache() {
|
|
|
|
vfs.root.ForgetAll()
|
|
|
|
}
|
2017-11-18 12:57:40 +01:00
|
|
|
|
|
|
|
// WaitForWriters sleeps until all writers have finished or
|
|
|
|
// time.Duration has elapsed
|
|
|
|
func (vfs *VFS) WaitForWriters(timeout time.Duration) {
|
2018-01-12 17:30:54 +01:00
|
|
|
defer log.Trace(nil, "timeout=%v", timeout)("")
|
2017-11-18 12:57:40 +01:00
|
|
|
const tickTime = 1 * time.Second
|
|
|
|
deadline := time.NewTimer(timeout)
|
|
|
|
defer deadline.Stop()
|
|
|
|
tick := time.NewTimer(tickTime)
|
|
|
|
defer tick.Stop()
|
|
|
|
tick.Stop()
|
|
|
|
for {
|
|
|
|
writers := 0
|
|
|
|
vfs.root.walk("", func(d *Dir) {
|
|
|
|
fs.Debugf(d.path, "Looking for writers")
|
|
|
|
// NB d.mu is held by walk() here
|
|
|
|
for leaf, item := range d.items {
|
|
|
|
fs.Debugf(leaf, "reading active writers")
|
|
|
|
if file, ok := item.(*File); ok {
|
|
|
|
n := file.activeWriters()
|
|
|
|
if n != 0 {
|
|
|
|
fs.Debugf(file, "active writers %d", n)
|
|
|
|
}
|
|
|
|
writers += n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if writers == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fs.Debugf(nil, "Still %d writers active, waiting %v", writers, tickTime)
|
|
|
|
tick.Reset(tickTime)
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
break
|
|
|
|
case <-deadline.C:
|
|
|
|
fs.Errorf(nil, "Exiting even though %d writers are active after %v", writers, timeout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
// Root returns the root node
|
2017-10-28 21:01:34 +02:00
|
|
|
func (vfs *VFS) Root() (*Dir, error) {
|
|
|
|
// fs.Debugf(vfs.f, "Root()")
|
|
|
|
return vfs.root, nil
|
2017-05-02 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var inodeCount uint64
|
|
|
|
|
2017-10-29 18:37:54 +01:00
|
|
|
// newInode creates a new unique inode number
|
|
|
|
func newInode() (inode uint64) {
|
2017-05-02 23:35:07 +02:00
|
|
|
return atomic.AddUint64(&inodeCount, 1)
|
|
|
|
}
|
|
|
|
|
2017-10-29 12:36:38 +01: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-29 22:11:17 +01:00
|
|
|
path = strings.Trim(path, "/")
|
2017-10-28 21:01:34 +02:00
|
|
|
node = vfs.root
|
2017-05-02 23:35:07 +02: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 12:36:38 +01:00
|
|
|
node, err = dir.Stat(name)
|
2017-05-02 23:35:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2017-10-29 22:11:17 +01: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-14 22:00:08 +01: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-29 22:11:17 +01: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-12 17:30:54 +01:00
|
|
|
defer log.Trace(name, "flags=%s, perm=%v", decodeOpenFlags(flags), perm)("fd=%v, err=%v", &fd, &err)
|
2018-02-23 23:39:28 +01: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-29 22:11:17 +01:00
|
|
|
node, err := vfs.Stat(name)
|
|
|
|
if err != nil {
|
2017-11-06 13:22:45 +01: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 13:34:18 +01:00
|
|
|
node, err = dir.Create(leaf, flags)
|
2017-11-06 13:22:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-10-29 22:11:17 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-30 11:14:39 +01:00
|
|
|
return node.Open(flags)
|
2017-10-29 22:11:17 +01: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 00:19:34 +02: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
|
|
|
|
if doAbout == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if vfs.usageTime.IsZero() || time.Since(vfs.usageTime) >= vfs.Opt.DirCacheTime {
|
|
|
|
var err error
|
|
|
|
vfs.usage, err = doAbout()
|
|
|
|
vfs.usageTime = time.Now()
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(vfs.f, "Statfs failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-05-02 15:28:17 +02:00
|
|
|
|
|
|
|
// notifyFunc removes the last path segement for directories and calls ForgetPath with the result.
|
|
|
|
//
|
|
|
|
// This ensures that new or renamed directories appear in their parent.
|
|
|
|
func (vfs *VFS) notifyFunc(relativePath string, entryType fs.EntryType) {
|
|
|
|
if entryType == fs.EntryDirectory {
|
|
|
|
relativePath = path.Dir(relativePath)
|
|
|
|
}
|
|
|
|
vfs.root.ForgetPath(relativePath, entryType)
|
|
|
|
}
|