2016-07-17 22:03:23 +00:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2018-03-02 16:39:42 +00:00
|
|
|
"github.com/ncw/rclone/cmd/mountlib"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs/log"
|
2017-10-28 19:01:34 +00:00
|
|
|
"github.com/ncw/rclone/vfs"
|
2016-07-17 22:03:23 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Dir represents a directory entry
|
|
|
|
type Dir struct {
|
2017-10-28 19:01:34 +00:00
|
|
|
*vfs.Dir
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satsified
|
|
|
|
var _ fusefs.Node = (*Dir)(nil)
|
|
|
|
|
2017-03-23 19:41:21 +00:00
|
|
|
// Attr updates the attributes of a directory
|
2017-05-09 10:39:33 +00:00
|
|
|
func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "")("attr=%+v, err=%v", a, &err)
|
2018-03-02 16:39:42 +00:00
|
|
|
a.Valid = mountlib.AttrTimeout
|
2017-10-29 11:00:56 +00:00
|
|
|
a.Gid = d.VFS().Opt.GID
|
|
|
|
a.Uid = d.VFS().Opt.UID
|
|
|
|
a.Mode = os.ModeDir | d.VFS().Opt.DirPerms
|
2017-05-02 21:35:07 +00:00
|
|
|
modTime := d.ModTime()
|
|
|
|
a.Atime = modTime
|
|
|
|
a.Mtime = modTime
|
|
|
|
a.Ctime = modTime
|
|
|
|
a.Crtime = modTime
|
2017-02-06 21:17:45 +00:00
|
|
|
// FIXME include Valid so get some caching?
|
2017-05-02 21:35:07 +00:00
|
|
|
// FIXME fs.Debugf(d.path, "Dir.Attr %+v", a)
|
2016-07-17 22:03:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:41:21 +00:00
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeSetattrer = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Setattr handles attribute changes from FUSE. Currently supports ModTime only.
|
2017-05-02 21:35:07 +00:00
|
|
|
func (d *Dir) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "stat=%+v", req)("err=%v", &err)
|
2017-10-29 11:00:56 +00:00
|
|
|
if d.VFS().Opt.NoModTime {
|
2017-03-23 19:41:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Valid.MtimeNow() {
|
2017-05-02 21:35:07 +00:00
|
|
|
err = d.SetModTime(time.Now())
|
2017-03-23 19:41:21 +00:00
|
|
|
} else if req.Valid.Mtime() {
|
2017-05-02 21:35:07 +00:00
|
|
|
err = d.SetModTime(req.Mtime)
|
2017-03-23 19:41:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 21:35:07 +00:00
|
|
|
return translateError(err)
|
2016-07-17 22:03:23 +00: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) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "name=%q", req.Name)("node=%+v, err=%v", &node, &err)
|
2017-10-29 11:36:38 +00:00
|
|
|
mnode, err := d.Dir.Stat(req.Name)
|
2016-07-17 22:03:23 +00:00
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
return nil, translateError(err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2018-03-02 16:39:42 +00:00
|
|
|
resp.EntryValid = mountlib.AttrTimeout
|
2017-05-02 21:35:07 +00:00
|
|
|
switch x := mnode.(type) {
|
2017-10-28 19:01:34 +00:00
|
|
|
case *vfs.File:
|
2017-05-02 21:35:07 +00:00
|
|
|
return &File{x}, nil
|
2017-10-28 19:01:34 +00:00
|
|
|
case *vfs.Dir:
|
2017-05-02 21:35:07 +00:00
|
|
|
return &Dir{x}, nil
|
|
|
|
}
|
|
|
|
panic("bad type")
|
2016-07-17 22:03:23 +00: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-09 10:39:33 +00:00
|
|
|
itemsRead := -1
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "")("item=%d, err=%v", &itemsRead, &err)
|
2017-05-02 21:35:07 +00:00
|
|
|
items, err := d.Dir.ReadDirAll()
|
2016-07-17 22:03:23 +00:00
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
return nil, translateError(err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2017-11-18 11:47:21 +00:00
|
|
|
for _, node := range items {
|
|
|
|
var dirent = fuse.Dirent{
|
|
|
|
// Inode FIXME ???
|
|
|
|
Type: fuse.DT_File,
|
|
|
|
Name: node.Name(),
|
|
|
|
}
|
|
|
|
if node.IsDir() {
|
|
|
|
dirent.Type = fuse.DT_Dir
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
dirents = append(dirents, dirent)
|
|
|
|
}
|
2017-05-09 10:39:33 +00:00
|
|
|
itemsRead = len(dirents)
|
2016-07-17 22:03:23 +00:00
|
|
|
return dirents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeCreater = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Create makes a new file
|
2017-05-09 10:39:33 +00:00
|
|
|
func (d *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (node fusefs.Node, handle fusefs.Handle, err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "name=%q", req.Name)("node=%v, handle=%v, err=%v", &node, &handle, &err)
|
2017-12-07 12:34:18 +00:00
|
|
|
file, err := d.Dir.Create(req.Name, int(req.Flags))
|
2017-11-06 12:22:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, translateError(err)
|
|
|
|
}
|
2017-11-18 11:50:58 +00:00
|
|
|
fh, err := file.Open(int(req.Flags) | os.O_CREATE)
|
2016-07-17 22:03:23 +00:00
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
return nil, nil, translateError(err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2017-11-02 18:22:26 +00:00
|
|
|
return &File{file}, &FileHandle{fh}, err
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeMkdirer = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Mkdir creates a new directory
|
2017-05-09 10:39:33 +00:00
|
|
|
func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (node fusefs.Node, err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "name=%q", req.Name)("node=%+v, err=%v", &node, &err)
|
2017-05-02 21:35:07 +00:00
|
|
|
dir, err := d.Dir.Mkdir(req.Name)
|
2017-01-06 11:24:22 +00:00
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
return nil, translateError(err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
return &Dir{dir}, nil
|
2016-07-17 22:03:23 +00: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).
|
2017-05-09 10:39:33 +00:00
|
|
|
func (d *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "name=%q", req.Name)("err=%v", &err)
|
2017-10-26 14:37:45 +00:00
|
|
|
err = d.Dir.RemoveName(req.Name)
|
2016-07-17 22:03:23 +00:00
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
return translateError(err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeRenamer = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Rename the file
|
2017-05-09 10:39:33 +00:00
|
|
|
func (d *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fusefs.Node) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "oldName=%q, newName=%q, newDir=%+v", req.OldName, req.NewName, newDir)("err=%v", &err)
|
2016-07-17 22:03:23 +00:00
|
|
|
destDir, ok := newDir.(*Dir)
|
|
|
|
if !ok {
|
2017-05-02 21:35:07 +00:00
|
|
|
return errors.Errorf("Unknown Dir type %T", newDir)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
|
2017-05-09 10:39:33 +00:00
|
|
|
err = d.Dir.Rename(req.OldName, req.NewName, destDir.Dir)
|
2016-07-17 22:03:23 +00:00
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
return translateError(err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-02 21:30:32 +00:00
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeFsyncer = (*Dir)(nil)
|
|
|
|
|
|
|
|
// Fsync the directory
|
2017-05-09 10:39:33 +00:00
|
|
|
func (d *Dir) Fsync(ctx context.Context, req *fuse.FsyncRequest) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(d, "")("err=%v", &err)
|
2017-11-18 15:48:49 +00:00
|
|
|
err = d.Dir.Sync()
|
2017-05-02 21:35:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return translateError(err)
|
|
|
|
}
|
2017-02-02 21:30:32 +00:00
|
|
|
return nil
|
|
|
|
}
|