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.

112 lines
3.0 KiB

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