You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.1 KiB

7 years ago
7 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "bazil.org/fuse"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "bazil.org/fuse/fs"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "path/filepath"
  11. "os"
  12. "time"
  13. )
  14. var _ = fs.Node(&File{})
  15. // var _ = fs.NodeOpener(&File{})
  16. // var _ = fs.NodeFsyncer(&File{})
  17. var _ = fs.Handle(&File{})
  18. var _ = fs.HandleReadAller(&File{})
  19. // var _ = fs.HandleReader(&File{})
  20. var _ = fs.HandleWriter(&File{})
  21. type File struct {
  22. FileId filer.FileId
  23. Name string
  24. dir *Dir
  25. wfs *WFS
  26. }
  27. func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
  28. fullPath := filepath.Join(file.dir.Path, file.Name)
  29. item := file.wfs.listDirectoryEntriesCache.Get(fullPath)
  30. var attributes *filer_pb.FuseAttributes
  31. if item != nil {
  32. attributes = item.Value().(*filer_pb.FuseAttributes)
  33. glog.V(1).Infof("read cached file %v attributes", file.Name)
  34. } else {
  35. err := file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  36. request := &filer_pb.GetFileAttributesRequest{
  37. Name: file.Name,
  38. ParentDir: file.dir.Path,
  39. }
  40. glog.V(1).Infof("read file size: %v", request)
  41. resp, err := client.GetFileAttributes(context, request)
  42. if err != nil {
  43. return err
  44. }
  45. attributes = resp.Attributes
  46. return nil
  47. })
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. attr.Mode = os.FileMode(attributes.FileMode)
  53. attr.Size = attributes.FileSize
  54. attr.Mtime = time.Unix(attributes.Mtime, 0)
  55. attr.Gid = attributes.Gid
  56. attr.Uid = attributes.Uid
  57. return nil
  58. }
  59. func (file *File) ReadAll(ctx context.Context) (content []byte, err error) {
  60. err = file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  61. request := &filer_pb.GetFileContentRequest{
  62. FileId: string(file.FileId),
  63. }
  64. glog.V(1).Infof("read file content: %v", request)
  65. resp, err := client.GetFileContent(ctx, request)
  66. if err != nil {
  67. return err
  68. }
  69. content = resp.Content
  70. return nil
  71. })
  72. return content, err
  73. }
  74. func (file *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  75. fmt.Printf("write file %+v\n", req)
  76. return nil
  77. }