dir_link.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package filesys
  2. import (
  3. "context"
  4. "os"
  5. "syscall"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/filer2"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/fuse"
  11. "github.com/seaweedfs/fuse/fs"
  12. )
  13. var _ = fs.NodeSymlinker(&Dir{})
  14. var _ = fs.NodeReadlinker(&File{})
  15. func (dir *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fs.Node, error) {
  16. glog.V(4).Infof("Symlink: %v/%v to %v", dir.FullPath(), req.NewName, req.Target)
  17. request := &filer_pb.CreateEntryRequest{
  18. Directory: dir.FullPath(),
  19. Entry: &filer_pb.Entry{
  20. Name: req.NewName,
  21. IsDirectory: false,
  22. Attributes: &filer_pb.FuseAttributes{
  23. Mtime: time.Now().Unix(),
  24. Crtime: time.Now().Unix(),
  25. FileMode: uint32((os.FileMode(0777) | os.ModeSymlink) &^ dir.wfs.option.Umask),
  26. Uid: req.Uid,
  27. Gid: req.Gid,
  28. SymlinkTarget: req.Target,
  29. },
  30. },
  31. }
  32. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  33. if err := filer_pb.CreateEntry(client, request); err != nil {
  34. glog.V(0).Infof("symlink %s/%s: %v", dir.FullPath(), req.NewName, err)
  35. return fuse.EIO
  36. }
  37. dir.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  38. return nil
  39. })
  40. symlink := dir.newFile(req.NewName, request.Entry)
  41. return symlink, err
  42. }
  43. func (file *File) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
  44. if err := file.maybeLoadEntry(ctx); err != nil {
  45. return "", err
  46. }
  47. if os.FileMode(file.entry.Attributes.FileMode)&os.ModeSymlink == 0 {
  48. return "", fuse.Errno(syscall.EINVAL)
  49. }
  50. glog.V(4).Infof("Readlink: %v/%v => %v", file.dir.FullPath(), file.Name, file.entry.Attributes.SymlinkTarget)
  51. return file.entry.Attributes.SymlinkTarget, nil
  52. }