seaweedfs/weed/filesys/dir_link.go

67 lines
1.6 KiB
Go
Raw Normal View History

2018-12-26 14:45:44 +08:00
package filesys
import (
"context"
"os"
"syscall"
"time"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2019-01-17 09:17:19 +08:00
"github.com/seaweedfs/fuse"
"github.com/seaweedfs/fuse/fs"
2018-12-26 14:45:44 +08:00
)
var _ = fs.NodeSymlinker(&Dir{})
var _ = fs.NodeReadlinker(&File{})
func (dir *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) {
glog.V(3).Infof("Symlink: %v/%v to %v", dir.Path, req.NewName, req.Target)
request := &filer_pb.CreateEntryRequest{
Directory: dir.Path,
Entry: &filer_pb.Entry{
Name: req.NewName,
IsDirectory: false,
Attributes: &filer_pb.FuseAttributes{
Mtime: time.Now().Unix(),
Crtime: time.Now().Unix(),
FileMode: uint32((os.FileMode(0777) | os.ModeSymlink) &^ dir.wfs.option.Umask),
2018-12-26 14:45:44 +08:00
Uid: req.Uid,
Gid: req.Gid,
SymlinkTarget: req.Target,
},
},
}
2019-05-03 15:24:35 +08:00
err := dir.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
2018-12-26 14:45:44 +08:00
if _, err := client.CreateEntry(ctx, request); err != nil {
glog.V(0).Infof("symlink %s/%s: %v", dir.Path, req.NewName, err)
return fuse.EIO
}
return nil
})
symlink := dir.newFile(req.NewName, request.Entry)
return symlink, err
}
func (file *File) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
2019-12-16 13:07:01 +08:00
if err := file.maybeLoadEntry(ctx); err != nil {
2018-12-26 14:45:44 +08:00
return "", err
}
if os.FileMode(file.entry.Attributes.FileMode)&os.ModeSymlink == 0 {
return "", fuse.Errno(syscall.EINVAL)
}
glog.V(3).Infof("Readlink: %v/%v => %v", file.dir.Path, file.Name, file.entry.Attributes.SymlinkTarget)
return file.entry.Attributes.SymlinkTarget, nil
}