file.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. )
  11. var _ = fs.Node(&File{})
  12. // var _ = fs.NodeOpener(&File{})
  13. // var _ = fs.NodeFsyncer(&File{})
  14. var _ = fs.Handle(&File{})
  15. var _ = fs.HandleReadAller(&File{})
  16. // var _ = fs.HandleReader(&File{})
  17. var _ = fs.HandleWriter(&File{})
  18. type File struct {
  19. FileId filer.FileId
  20. Name string
  21. wfs *WFS
  22. }
  23. func (file *File) Attr(context context.Context, attr *fuse.Attr) error {
  24. attr.Mode = 0444
  25. return file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  26. request := &filer_pb.GetFileAttributesRequest{
  27. Name: file.Name,
  28. ParentDir: "", //TODO add parent folder
  29. FileId: string(file.FileId),
  30. }
  31. glog.V(1).Infof("read file size: %v", request)
  32. resp, err := client.GetFileAttributes(context, request)
  33. if err != nil {
  34. return err
  35. }
  36. attr.Size = resp.Attributes.FileSize
  37. return nil
  38. })
  39. }
  40. func (file *File) ReadAll(ctx context.Context) (content []byte, err error) {
  41. err = file.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  42. request := &filer_pb.GetFileContentRequest{
  43. FileId: string(file.FileId),
  44. }
  45. glog.V(1).Infof("read file content: %v", request)
  46. resp, err := client.GetFileContent(ctx, request)
  47. if err != nil {
  48. return err
  49. }
  50. content = resp.Content
  51. return nil
  52. })
  53. return content, err
  54. }
  55. func (file *File) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  56. fmt.Printf("write file %+v\n", req)
  57. return nil
  58. }