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.

200 lines
6.2 KiB

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) GetEntryAttributes(ctx context.Context, req *filer_pb.GetEntryAttributesRequest) (*filer_pb.GetEntryAttributesResponse, error) {
  53. attributes := &filer_pb.FuseAttributes{}
  54. fullpath := filer2.NewFullPath(req.ParentDir, req.Name)
  55. found, entry, err := fs.filer.FindEntry(fullpath)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if !found {
  60. attributes.FileSize = 0
  61. return nil, fmt.Errorf("file %s not found", fullpath)
  62. }
  63. attributes.FileSize = filer2.TotalSize(entry.Chunks)
  64. attributes.FileMode = uint32(entry.Mode)
  65. attributes.Uid = entry.Uid
  66. attributes.Gid = entry.Gid
  67. attributes.Mtime = entry.Mtime.Unix()
  68. glog.V(0).Infof("GetEntryAttributes %v size %d chunks %d: %+v", fullpath, attributes.FileSize, len(entry.Chunks), attributes)
  69. return &filer_pb.GetEntryAttributesResponse{
  70. Attributes: attributes,
  71. Chunks: entry.Chunks,
  72. }, nil
  73. }
  74. func (fs *FilerServer) GetFileContent(ctx context.Context, req *filer_pb.GetFileContentRequest) (*filer_pb.GetFileContentResponse, error) {
  75. server, err := operation.LookupFileId(fs.getMasterNode(), req.FileId)
  76. if err != nil {
  77. return nil, err
  78. }
  79. content, err := util.Get(server)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &filer_pb.GetFileContentResponse{
  84. Content: content,
  85. }, nil
  86. }
  87. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  88. err = fs.filer.CreateEntry(&filer2.Entry{
  89. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  90. Attr: filer2.Attr{
  91. Mtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  92. Crtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  93. Mode: os.FileMode(req.Entry.Attributes.FileMode),
  94. Uid: req.Entry.Attributes.Uid,
  95. Gid: req.Entry.Attributes.Gid,
  96. },
  97. Chunks: req.Entry.Chunks,
  98. })
  99. if err == nil {
  100. }
  101. return &filer_pb.CreateEntryResponse{}, err
  102. }
  103. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  104. fullpath := filepath.Join(req.Directory, req.Entry.Name)
  105. found, entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
  106. if err != nil {
  107. return &filer_pb.UpdateEntryResponse{}, err
  108. }
  109. if !found {
  110. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("file not found: %s", fullpath)
  111. }
  112. // remove old chunks if not included in the new ones
  113. unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
  114. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  115. err = fs.filer.UpdateEntry(&filer2.Entry{
  116. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  117. Attr: filer2.Attr{
  118. Mtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  119. Crtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  120. Mode: os.FileMode(req.Entry.Attributes.FileMode),
  121. Uid: req.Entry.Attributes.Uid,
  122. Gid: req.Entry.Attributes.Gid,
  123. },
  124. Chunks: chunks,
  125. })
  126. if err == nil {
  127. for _, garbage := range unusedChunks {
  128. glog.V(0).Infof("deleting %s old chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
  129. operation.DeleteFile(fs.master, garbage.FileId, fs.jwt(garbage.FileId))
  130. }
  131. for _, garbage := range garbages {
  132. glog.V(0).Infof("deleting %s garbage chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
  133. operation.DeleteFile(fs.master, garbage.FileId, fs.jwt(garbage.FileId))
  134. }
  135. }
  136. return &filer_pb.UpdateEntryResponse{}, err
  137. }
  138. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  139. entry, err := fs.filer.DeleteEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  140. if err == nil {
  141. for _, chunk := range entry.Chunks {
  142. if err = operation.DeleteFile(fs.getMasterNode(), chunk.FileId, fs.jwt(chunk.FileId)); err != nil {
  143. glog.V(0).Infof("deleting file %s: %v", chunk.FileId, err)
  144. }
  145. }
  146. }
  147. return &filer_pb.DeleteEntryResponse{}, err
  148. }
  149. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  150. assignResult, err := operation.Assign(fs.master, &operation.VolumeAssignRequest{
  151. Count: uint64(req.Count),
  152. Replication: req.Replication,
  153. Collection: req.Collection,
  154. })
  155. if err != nil {
  156. return nil, fmt.Errorf("assign volume: %v", err)
  157. }
  158. if assignResult.Error != "" {
  159. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  160. }
  161. return &filer_pb.AssignVolumeResponse{
  162. FileId: assignResult.Fid,
  163. Count: int32(assignResult.Count),
  164. Url: assignResult.Url,
  165. PublicUrl: assignResult.PublicUrl,
  166. }, err
  167. }