2017-05-03 05:35:07 +08:00
|
|
|
package mountlib
|
|
|
|
|
|
|
|
import (
|
2017-05-09 18:29:02 +08:00
|
|
|
"fmt"
|
2017-05-03 05:35:07 +08:00
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Node represents either a *Dir or a *File
|
|
|
|
type Node interface {
|
|
|
|
IsFile() bool
|
|
|
|
Inode() uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ Node = (*File)(nil)
|
|
|
|
_ Node = (*Dir)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ Noder = (*File)(nil)
|
|
|
|
_ Noder = (*Dir)(nil)
|
|
|
|
_ Noder = (*ReadFileHandle)(nil)
|
|
|
|
_ Noder = (*WriteFileHandle)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// FS represents the top level filing system
|
|
|
|
type FS struct {
|
2017-05-31 22:55:46 +08:00
|
|
|
f fs.Fs
|
|
|
|
root *Dir
|
|
|
|
noSeek bool // don't allow seeking if set
|
|
|
|
noChecksum bool // don't check checksums if set
|
|
|
|
readOnly bool // if set FS is read only
|
|
|
|
dirCacheTime time.Duration // how long to consider directory listing cache valid
|
2017-05-03 05:35:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFS creates a new filing system and root directory
|
|
|
|
func NewFS(f fs.Fs) *FS {
|
|
|
|
fsDir := &fs.Dir{
|
|
|
|
Name: "",
|
|
|
|
When: time.Now(),
|
|
|
|
}
|
|
|
|
fsys := &FS{
|
|
|
|
f: f,
|
|
|
|
}
|
2017-05-26 05:05:49 +08:00
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
fsys.root = newDir(fsys, f, fsDir)
|
2017-05-26 05:05:49 +08:00
|
|
|
|
|
|
|
return fsys
|
|
|
|
}
|
|
|
|
|
2017-05-31 22:55:46 +08:00
|
|
|
// SetDirCacheTime allows to set how long a directory listing is considered
|
|
|
|
// valid. Set to 0 always request a fresh version from the remote.
|
|
|
|
func (fsys *FS) SetDirCacheTime(dirCacheTime time.Duration) *FS {
|
|
|
|
fsys.dirCacheTime = dirCacheTime
|
|
|
|
return fsys
|
|
|
|
}
|
|
|
|
|
2017-05-26 05:05:49 +08:00
|
|
|
// PollChanges will poll the remote every pollInterval for changes if the remote
|
|
|
|
// supports it. If a non-polling option is used, the given time interval can be
|
|
|
|
// ignored
|
|
|
|
func (fsys *FS) PollChanges(pollInterval time.Duration) *FS {
|
|
|
|
doDirChangeNotify := fsys.f.Features().DirChangeNotify
|
|
|
|
if doDirChangeNotify != nil {
|
|
|
|
doDirChangeNotify(fsys.root.ForgetPath, pollInterval)
|
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
return fsys
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoSeek disables seeking of files
|
|
|
|
func (fsys *FS) NoSeek() *FS {
|
|
|
|
fsys.noSeek = true
|
|
|
|
return fsys
|
|
|
|
}
|
|
|
|
|
2017-05-09 00:47:22 +08:00
|
|
|
// NoChecksum disables checksum checking
|
|
|
|
func (fsys *FS) NoChecksum() *FS {
|
|
|
|
fsys.noChecksum = true
|
|
|
|
return fsys
|
|
|
|
}
|
|
|
|
|
2017-05-11 20:15:51 +08:00
|
|
|
// ReadOnly sets the fs into read only mode, returning EROFS for any
|
|
|
|
// write operations.
|
|
|
|
func (fsys *FS) ReadOnly() *FS {
|
|
|
|
fsys.readOnly = true
|
|
|
|
return fsys
|
|
|
|
}
|
|
|
|
|
2017-05-03 05:35:07 +08:00
|
|
|
// Root returns the root node
|
|
|
|
func (fsys *FS) Root() (*Dir, error) {
|
2017-05-09 18:39:33 +08:00
|
|
|
// fs.Debugf(fsys.f, "Root()")
|
2017-05-03 05:35:07 +08:00
|
|
|
return fsys.root, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var inodeCount uint64
|
|
|
|
|
|
|
|
// NewInode creates a new unique inode number
|
|
|
|
func NewInode() (inode uint64) {
|
|
|
|
return atomic.AddUint64(&inodeCount, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup finds the Node by path starting from the root
|
2017-05-09 01:05:12 +08:00
|
|
|
func (fsys *FS) Lookup(path string) (node Node, err error) {
|
|
|
|
node = fsys.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
|
|
|
|
}
|
|
|
|
node, err = dir.Lookup(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Statfs is called to obtain file system metadata.
|
|
|
|
// It should write that data to resp.
|
2017-05-09 01:05:12 +08:00
|
|
|
func (fsys *FS) Statfs() error {
|
2017-05-03 05:35:07 +08:00
|
|
|
/* FIXME
|
|
|
|
const blockSize = 4096
|
|
|
|
const fsBlocks = (1 << 50) / blockSize
|
|
|
|
resp.Blocks = fsBlocks // Total data blocks in file system.
|
|
|
|
resp.Bfree = fsBlocks // Free blocks in file system.
|
|
|
|
resp.Bavail = fsBlocks // Free blocks in file system if you're not root.
|
|
|
|
resp.Files = 1E9 // Total files in file system.
|
|
|
|
resp.Ffree = 1E9 // Free files in file system.
|
|
|
|
resp.Bsize = blockSize // Block size
|
|
|
|
resp.Namelen = 255 // Maximum file name length?
|
|
|
|
resp.Frsize = blockSize // Fragment size, smallest addressable data size in the file system.
|
|
|
|
*/
|
|
|
|
return nil
|
|
|
|
}
|