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.

160 lines
4.7 KiB

7 years ago
7 years ago
  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. "time"
  12. "os"
  13. )
  14. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  15. found, entry, err := fs.filer.FindEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  16. if err != nil {
  17. return nil, err
  18. }
  19. if !found {
  20. return nil, fmt.Errorf("%s not found under %s", req.Name, req.Directory)
  21. }
  22. return &filer_pb.LookupDirectoryEntryResponse{
  23. Entry: &filer_pb.Entry{
  24. Name: req.Name,
  25. IsDirectory: entry.IsDirectory(),
  26. Chunks: entry.Chunks,
  27. },
  28. }, nil
  29. }
  30. func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
  31. entries, err := fs.filer.ListDirectoryEntries(filer2.FullPath(req.Directory), "", false, 1000)
  32. if err != nil {
  33. return nil, err
  34. }
  35. resp := &filer_pb.ListEntriesResponse{}
  36. for _, entry := range entries {
  37. resp.Entries = append(resp.Entries, &filer_pb.Entry{
  38. Name: entry.Name(),
  39. IsDirectory: entry.IsDirectory(),
  40. Chunks: entry.Chunks,
  41. Attributes: &filer_pb.FuseAttributes{
  42. FileSize: filer2.TotalSize(entry.Chunks),
  43. Mtime: entry.Mtime.Unix(),
  44. Gid: entry.Gid,
  45. Uid: entry.Uid,
  46. FileMode: uint32(entry.Mode),
  47. },
  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. found, entry, err := fs.filer.FindEntry(filer2.NewFullPath(req.ParentDir, req.Name))
  55. if err != nil {
  56. return nil, err
  57. }
  58. if !found {
  59. attributes.FileSize = 0
  60. } else {
  61. attributes.FileSize = filer2.TotalSize(entry.Chunks)
  62. attributes.FileMode = uint32(entry.Mode)
  63. attributes.Uid = entry.Uid
  64. attributes.Gid = entry.Gid
  65. attributes.Mtime = entry.Mtime.Unix()
  66. }
  67. return &filer_pb.GetFileAttributesResponse{
  68. Attributes: attributes,
  69. }, nil
  70. }
  71. func (fs *FilerServer) GetFileContent(ctx context.Context, req *filer_pb.GetFileContentRequest) (*filer_pb.GetFileContentResponse, error) {
  72. server, err := operation.LookupFileId(fs.getMasterNode(), req.FileId)
  73. if err != nil {
  74. return nil, err
  75. }
  76. content, err := util.Get(server)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return &filer_pb.GetFileContentResponse{
  81. Content: content,
  82. }, nil
  83. }
  84. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  85. err = fs.filer.CreateEntry(&filer2.Entry{
  86. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  87. Attr: filer2.Attr{
  88. Mtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  89. Crtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  90. Mode: os.FileMode(req.Entry.Attributes.FileMode),
  91. Uid: req.Entry.Attributes.Uid,
  92. Gid: req.Entry.Attributes.Gid,
  93. },
  94. })
  95. if err == nil {
  96. }
  97. return &filer_pb.CreateEntryResponse{}, err
  98. }
  99. func (fs *FilerServer) AppendFileChunks(ctx context.Context, req *filer_pb.AppendFileChunksRequest) (*filer_pb.AppendFileChunksResponse, error) {
  100. err := fs.filer.AppendFileChunk(
  101. filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  102. req.Entry.Chunks,
  103. )
  104. return &filer_pb.AppendFileChunksResponse{}, err
  105. }
  106. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  107. entry, err := fs.filer.DeleteEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  108. if err == nil {
  109. for _, chunk := range entry.Chunks {
  110. if err = operation.DeleteFile(fs.getMasterNode(), chunk.FileId, fs.jwt(chunk.FileId)); err != nil {
  111. glog.V(0).Infof("deleting file %s: %v", chunk.FileId, err)
  112. }
  113. }
  114. }
  115. return &filer_pb.DeleteEntryResponse{}, err
  116. }
  117. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  118. assignResult, err := operation.Assign(fs.master, &operation.VolumeAssignRequest{
  119. Count: uint64(req.Count),
  120. Replication: req.Replication,
  121. Collection: req.Collection,
  122. })
  123. if err != nil {
  124. return nil, fmt.Errorf("assign volume: %v", err)
  125. }
  126. if assignResult.Error != "" {
  127. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  128. }
  129. return &filer_pb.AssignVolumeResponse{
  130. FileId: assignResult.Fid,
  131. Count: int32(assignResult.Count),
  132. Url: assignResult.Url,
  133. PublicUrl: assignResult.PublicUrl,
  134. }, err
  135. }