2016-07-18 00:03:23 +02:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2017-05-02 23:35:07 +02:00
|
|
|
"github.com/ncw/rclone/cmd/mountlib"
|
2016-07-18 00:03:23 +02:00
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DirEntry describes the contents of a directory entry
|
|
|
|
//
|
|
|
|
// It can be a file or a directory
|
|
|
|
//
|
|
|
|
// node may be nil, but o may not
|
|
|
|
type DirEntry struct {
|
|
|
|
o fs.BasicInfo
|
|
|
|
node fusefs.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dir represents a directory entry
|
|
|
|
type Dir struct {
|
2017-05-02 23:35:07 +02:00
|
|
|
*mountlib.Dir
|
|
|
|
// f fs.Fs
|
|
|
|
// path string
|
|
|
|
// modTime time.Time
|
|
|
|
// mu sync.RWMutex // protects the following
|
|
|
|
// read time.Time // time directory entry last read
|
|
|
|
// items map[string]*DirEntry
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satsified
|
|
|
|
var _ fusefs.Node = (*Dir)(nil)
|
|
|
|
|
2017-03-23 20:41:21 +01:00
|
|
|
// Attr updates the attributes of a directory
|
2016-07-18 00:03:23 +02:00
|
|
|
func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
|
2016-09-09 09:39:19 +02:00
|
|
|
a.Gid = gid
|
|
|
|
a.Uid = uid
|
2016-07-18 00:03:23 +02:00
|
|
|
a.Mode = os.ModeDir | dirPerms
|
2017-05-02 23:35:07 +02:00
|
|
|
modTime := d.ModTime()
|
|
|
|
a.Atime = modTime
|
|
|
|
a.Mtime = modTime
|
|
|
|
a.Ctime = modTime
|
|
|
|
a.Crtime = modTime
|
2017-02-06 22:17:45 +01:00
|
|
|
// FIXME include Valid so get some caching?
|
2017-05-02 23:35:07 +02:00
|
|
|
// FIXME fs.Debugf(d.path, "Dir.Attr %+v", a)
|
2016-07-18 00:03:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-23 20:41:21 +01:00
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeSetattrer = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Setattr handles attribute changes from FUSE. Currently supports ModTime only.
|
2017-05-02 23:35:07 +02:00
|
|
|
func (d *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) (err error) {
|
2017-03-23 20:41:21 +01:00
|
|
|
if noModTime {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Valid.MtimeNow() {
|
2017-05-02 23:35:07 +02:00
|
|
|
err = d.SetModTime(time.Now())
|
2017-03-23 20:41:21 +01:00
|
|
|
} else if req.Valid.Mtime() {
|
2017-05-02 23:35:07 +02:00
|
|
|
err = d.SetModTime(req.Mtime)
|
2017-03-23 20:41:21 +01:00
|
|
|
}
|
|
|
|
|
2017-05-02 23:35:07 +02:00
|
|
|
return translateError(err)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeRequestLookuper = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Lookup looks up a specific entry in the receiver.
|
|
|
|
//
|
|
|
|
// Lookup should return a Node corresponding to the entry. If the
|
|
|
|
// name does not exist in the directory, Lookup should return ENOENT.
|
|
|
|
//
|
|
|
|
// Lookup need not to handle the names "." and "..".
|
|
|
|
func (d *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fusefs.Node, err error) {
|
2017-05-02 23:35:07 +02:00
|
|
|
mnode, err := d.Dir.Lookup(req.Name)
|
2016-07-18 00:03:23 +02:00
|
|
|
if err != nil {
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, translateError(err)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
switch x := mnode.(type) {
|
|
|
|
case *mountlib.File:
|
|
|
|
return &File{x}, nil
|
|
|
|
case *mountlib.Dir:
|
|
|
|
return &Dir{x}, nil
|
|
|
|
}
|
|
|
|
panic("bad type")
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.HandleReadDirAller = (*Dir)(nil)
|
|
|
|
|
|
|
|
// ReadDirAll reads the contents of the directory
|
|
|
|
func (d *Dir) ReadDirAll(ctx context.Context) (dirents []fuse.Dirent, err error) {
|
2017-05-02 23:35:07 +02:00
|
|
|
items, err := d.Dir.ReadDirAll()
|
2016-07-18 00:03:23 +02:00
|
|
|
if err != nil {
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, translateError(err)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
for _, item := range items {
|
2016-07-18 00:03:23 +02:00
|
|
|
var dirent fuse.Dirent
|
2017-05-02 23:35:07 +02:00
|
|
|
switch x := item.Obj.(type) {
|
2016-07-18 00:03:23 +02:00
|
|
|
case fs.Object:
|
|
|
|
dirent = fuse.Dirent{
|
|
|
|
// Inode FIXME ???
|
|
|
|
Type: fuse.DT_File,
|
|
|
|
Name: path.Base(x.Remote()),
|
|
|
|
}
|
|
|
|
case *fs.Dir:
|
|
|
|
dirent = fuse.Dirent{
|
|
|
|
// Inode FIXME ???
|
|
|
|
Type: fuse.DT_Dir,
|
|
|
|
Name: path.Base(x.Remote()),
|
|
|
|
}
|
|
|
|
default:
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, errors.Errorf("unknown type %T", item)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
dirents = append(dirents, dirent)
|
|
|
|
}
|
|
|
|
return dirents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeCreater = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Create makes a new file
|
|
|
|
func (d *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fusefs.Node, fusefs.Handle, error) {
|
2017-05-02 23:35:07 +02:00
|
|
|
file, fh, err := d.Dir.Create(req.Name)
|
2016-07-18 00:03:23 +02:00
|
|
|
if err != nil {
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, nil, translateError(err)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
return &File{file}, &WriteFileHandle{fh}, err
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeMkdirer = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Mkdir creates a new directory
|
|
|
|
func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fusefs.Node, error) {
|
2017-05-02 23:35:07 +02:00
|
|
|
dir, err := d.Dir.Mkdir(req.Name)
|
2017-01-06 12:24:22 +01:00
|
|
|
if err != nil {
|
2017-05-02 23:35:07 +02:00
|
|
|
return nil, translateError(err)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
return &Dir{dir}, nil
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeRemover = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Remove 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) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
|
2017-05-02 23:35:07 +02:00
|
|
|
err := d.Dir.Remove(req.Name)
|
2016-07-18 00:03:23 +02:00
|
|
|
if err != nil {
|
2017-05-02 23:35:07 +02:00
|
|
|
return translateError(err)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeRenamer = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Rename the file
|
|
|
|
func (d *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fusefs.Node) error {
|
|
|
|
destDir, ok := newDir.(*Dir)
|
|
|
|
if !ok {
|
2017-05-02 23:35:07 +02:00
|
|
|
return errors.Errorf("Unknown Dir type %T", newDir)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
|
|
|
|
err := d.Dir.Rename(req.OldName, req.NewName, destDir.Dir)
|
2016-07-18 00:03:23 +02:00
|
|
|
if err != nil {
|
2017-05-02 23:35:07 +02:00
|
|
|
return translateError(err)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-02 22:30:32 +01:00
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeFsyncer = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Fsync the directory
|
|
|
|
func (d *Dir) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
|
2017-05-02 23:35:07 +02:00
|
|
|
err := d.Dir.Fsync()
|
|
|
|
if err != nil {
|
|
|
|
return translateError(err)
|
|
|
|
}
|
2017-02-02 22:30:32 +01:00
|
|
|
return nil
|
|
|
|
}
|