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.

120 lines
3.4 KiB

  1. package weed_server
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/operation"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "fmt"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "path/filepath"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. )
  12. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  13. found, entry, err := fs.filer.FindEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  14. if err != nil {
  15. return nil, err
  16. }
  17. if !found {
  18. return nil, fmt.Errorf("%s not found under %s", req.Name, req.Directory)
  19. }
  20. var fileId string
  21. if !entry.IsDirectory() && len(entry.Chunks) > 0 {
  22. fileId = string(entry.Chunks[0].Fid)
  23. }
  24. return &filer_pb.LookupDirectoryEntryResponse{
  25. Entry: &filer_pb.Entry{
  26. Name: req.Name,
  27. IsDirectory: entry.IsDirectory(),
  28. FileId: fileId,
  29. },
  30. }, nil
  31. }
  32. func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
  33. entries, err := fs.filer.ListDirectoryEntries(filer2.FullPath(req.Directory), "", false, 1000)
  34. if err != nil {
  35. return nil, err
  36. }
  37. resp := &filer_pb.ListEntriesResponse{}
  38. for _, entry := range entries {
  39. var fileId string
  40. if !entry.IsDirectory() && len(entry.Chunks) > 0 {
  41. fileId = string(entry.Chunks[0].Fid)
  42. }
  43. glog.V(0).Infof("%s attr=%v size=%d", entry.Name(), entry.Attr, filer2.Chunks(entry.Chunks).TotalSize())
  44. resp.Entries = append(resp.Entries, &filer_pb.Entry{
  45. Name: entry.Name(),
  46. IsDirectory: entry.IsDirectory(),
  47. FileId: fileId,
  48. Attributes: &filer_pb.FuseAttributes{
  49. FileSize: filer2.Chunks(entry.Chunks).TotalSize(),
  50. Mtime: entry.Mtime.Unix(),
  51. Gid: entry.Gid,
  52. Uid: entry.Uid,
  53. FileMode: uint32(entry.Mode),
  54. },
  55. })
  56. }
  57. return resp, nil
  58. }
  59. func (fs *FilerServer) GetFileAttributes(ctx context.Context, req *filer_pb.GetFileAttributesRequest) (*filer_pb.GetFileAttributesResponse, error) {
  60. attributes := &filer_pb.FuseAttributes{}
  61. found, entry, err := fs.filer.FindEntry(filer2.NewFullPath(req.ParentDir, req.Name))
  62. if err != nil {
  63. return nil, err
  64. }
  65. if !found {
  66. attributes.FileSize = 0
  67. } else {
  68. attributes.FileSize = filer2.Chunks(entry.Chunks).TotalSize()
  69. attributes.FileMode = uint32(entry.Mode)
  70. attributes.Uid = entry.Uid
  71. attributes.Gid = entry.Gid
  72. attributes.Mtime = entry.Mtime.Unix()
  73. }
  74. return &filer_pb.GetFileAttributesResponse{
  75. Attributes: attributes,
  76. }, nil
  77. }
  78. func (fs *FilerServer) GetFileContent(ctx context.Context, req *filer_pb.GetFileContentRequest) (*filer_pb.GetFileContentResponse, error) {
  79. server, err := operation.LookupFileId(fs.getMasterNode(), req.FileId)
  80. if err != nil {
  81. return nil, err
  82. }
  83. content, err := util.Get(server)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return &filer_pb.GetFileContentResponse{
  88. Content: content,
  89. }, nil
  90. }
  91. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  92. entry, err := fs.filer.DeleteEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  93. if err == nil {
  94. for _, chunk := range entry.Chunks {
  95. fid := string(chunk.Fid)
  96. if err = operation.DeleteFile(fs.getMasterNode(), fid, fs.jwt(fid)); err != nil {
  97. glog.V(0).Infof("deleting file %s: %v", fid, err)
  98. }
  99. }
  100. }
  101. return &filer_pb.DeleteEntryResponse{}, err
  102. }