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.

211 lines
6.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. )
  13. func (fs *FilerServer) LookupDirectoryEntry(ctx context.Context, req *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) {
  14. entry, err := fs.filer.FindEntry(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  15. if err != nil {
  16. return nil, fmt.Errorf("%s not found under %s: %v", req.Name, req.Directory, err)
  17. }
  18. return &filer_pb.LookupDirectoryEntryResponse{
  19. Entry: &filer_pb.Entry{
  20. Name: req.Name,
  21. IsDirectory: entry.IsDirectory(),
  22. Chunks: entry.Chunks,
  23. },
  24. }, nil
  25. }
  26. func (fs *FilerServer) ListEntries(ctx context.Context, req *filer_pb.ListEntriesRequest) (*filer_pb.ListEntriesResponse, error) {
  27. entries, err := fs.filer.ListDirectoryEntries(filer2.FullPath(req.Directory), "", false, 1000)
  28. if err != nil {
  29. return nil, err
  30. }
  31. resp := &filer_pb.ListEntriesResponse{}
  32. for _, entry := range entries {
  33. resp.Entries = append(resp.Entries, &filer_pb.Entry{
  34. Name: entry.Name(),
  35. IsDirectory: entry.IsDirectory(),
  36. Chunks: entry.Chunks,
  37. Attributes: &filer_pb.FuseAttributes{
  38. FileSize: entry.Size(),
  39. Mtime: entry.Mtime.Unix(),
  40. Crtime: entry.Crtime.Unix(),
  41. Gid: entry.Gid,
  42. Uid: entry.Uid,
  43. FileMode: uint32(entry.Mode),
  44. Mime: entry.Mime,
  45. },
  46. })
  47. }
  48. return resp, nil
  49. }
  50. func (fs *FilerServer) GetEntryAttributes(ctx context.Context, req *filer_pb.GetEntryAttributesRequest) (*filer_pb.GetEntryAttributesResponse, error) {
  51. attributes := &filer_pb.FuseAttributes{}
  52. fullpath := filer2.NewFullPath(req.ParentDir, req.Name)
  53. entry, err := fs.filer.FindEntry(fullpath)
  54. if err != nil {
  55. attributes.FileSize = 0
  56. return nil, fmt.Errorf("FindEntry %s: %v", fullpath, err)
  57. }
  58. attributes.FileSize = entry.Size()
  59. attributes.FileMode = uint32(entry.Mode)
  60. attributes.Uid = entry.Uid
  61. attributes.Gid = entry.Gid
  62. attributes.Mtime = entry.Mtime.Unix()
  63. attributes.Crtime = entry.Crtime.Unix()
  64. attributes.Mime = entry.Mime
  65. glog.V(3).Infof("GetEntryAttributes %v size %d chunks %d: %+v", fullpath, attributes.FileSize, len(entry.Chunks), attributes)
  66. return &filer_pb.GetEntryAttributesResponse{
  67. Attributes: attributes,
  68. Chunks: entry.Chunks,
  69. }, nil
  70. }
  71. func (fs *FilerServer) LookupVolume(ctx context.Context, req *filer_pb.LookupVolumeRequest) (*filer_pb.LookupVolumeResponse, error) {
  72. lookupResult, err := operation.LookupVolumeIds(fs.filer.GetMaster(), req.VolumeIds)
  73. if err != nil {
  74. return nil, err
  75. }
  76. resp := &filer_pb.LookupVolumeResponse{
  77. LocationsMap: make(map[string]*filer_pb.Locations),
  78. }
  79. for vid, locations := range lookupResult {
  80. var locs []*filer_pb.Location
  81. for _, loc := range locations.Locations {
  82. locs = append(locs, &filer_pb.Location{
  83. Url: loc.Url,
  84. PublicUrl: loc.PublicUrl,
  85. })
  86. }
  87. resp.LocationsMap[vid] = &filer_pb.Locations{
  88. Locations: locs,
  89. }
  90. }
  91. return resp, nil
  92. }
  93. func (fs *FilerServer) CreateEntry(ctx context.Context, req *filer_pb.CreateEntryRequest) (resp *filer_pb.CreateEntryResponse, err error) {
  94. err = fs.filer.CreateEntry(&filer2.Entry{
  95. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  96. Attr: filer2.Attr{
  97. Mtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  98. Crtime: time.Unix(req.Entry.Attributes.Mtime, 0),
  99. Mode: os.FileMode(req.Entry.Attributes.FileMode),
  100. Uid: req.Entry.Attributes.Uid,
  101. Gid: req.Entry.Attributes.Gid,
  102. Mime: req.Entry.Attributes.Mime,
  103. },
  104. Chunks: req.Entry.Chunks,
  105. })
  106. if err == nil {
  107. }
  108. return &filer_pb.CreateEntryResponse{}, err
  109. }
  110. func (fs *FilerServer) UpdateEntry(ctx context.Context, req *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) {
  111. fullpath := filepath.Join(req.Directory, req.Entry.Name)
  112. entry, err := fs.filer.FindEntry(filer2.FullPath(fullpath))
  113. if err != nil {
  114. return &filer_pb.UpdateEntryResponse{}, fmt.Errorf("not found %s: %v", fullpath, err)
  115. }
  116. // remove old chunks if not included in the new ones
  117. unusedChunks := filer2.FindUnusedFileChunks(entry.Chunks, req.Entry.Chunks)
  118. chunks, garbages := filer2.CompactFileChunks(req.Entry.Chunks)
  119. newEntry := &filer2.Entry{
  120. FullPath: filer2.FullPath(filepath.Join(req.Directory, req.Entry.Name)),
  121. Attr: entry.Attr,
  122. Chunks: chunks,
  123. }
  124. glog.V(3).Infof("updating %s: %+v, chunks %d: %v => %+v, chunks %d: %v",
  125. fullpath, entry.Attr, len(entry.Chunks), entry.Chunks,
  126. req.Entry.Attributes, len(req.Entry.Chunks), req.Entry.Chunks)
  127. if req.Entry.Attributes != nil {
  128. if req.Entry.Attributes.Mtime != 0 {
  129. newEntry.Attr.Mtime = time.Unix(req.Entry.Attributes.Mtime, 0)
  130. }
  131. if req.Entry.Attributes.FileMode != 0 {
  132. newEntry.Attr.Mode = os.FileMode(req.Entry.Attributes.FileMode)
  133. }
  134. newEntry.Attr.Uid = req.Entry.Attributes.Uid
  135. newEntry.Attr.Gid = req.Entry.Attributes.Gid
  136. newEntry.Attr.Mime = req.Entry.Attributes.Mime
  137. }
  138. if err = fs.filer.UpdateEntry(newEntry); err == nil {
  139. for _, garbage := range unusedChunks {
  140. glog.V(0).Infof("deleting %s old chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
  141. operation.DeleteFile(fs.filer.GetMaster(), garbage.FileId, fs.jwt(garbage.FileId))
  142. }
  143. for _, garbage := range garbages {
  144. glog.V(0).Infof("deleting %s garbage chunk: %v, [%d, %d)", fullpath, garbage.FileId, garbage.Offset, garbage.Offset+int64(garbage.Size))
  145. operation.DeleteFile(fs.filer.GetMaster(), garbage.FileId, fs.jwt(garbage.FileId))
  146. }
  147. }
  148. return &filer_pb.UpdateEntryResponse{}, err
  149. }
  150. func (fs *FilerServer) DeleteEntry(ctx context.Context, req *filer_pb.DeleteEntryRequest) (resp *filer_pb.DeleteEntryResponse, err error) {
  151. err = fs.filer.DeleteEntryMetaAndData(filer2.FullPath(filepath.Join(req.Directory, req.Name)))
  152. return &filer_pb.DeleteEntryResponse{}, err
  153. }
  154. func (fs *FilerServer) AssignVolume(ctx context.Context, req *filer_pb.AssignVolumeRequest) (resp *filer_pb.AssignVolumeResponse, err error) {
  155. assignResult, err := operation.Assign(fs.filer.GetMaster(), &operation.VolumeAssignRequest{
  156. Count: uint64(req.Count),
  157. Replication: req.Replication,
  158. Collection: req.Collection,
  159. })
  160. if err != nil {
  161. return nil, fmt.Errorf("assign volume: %v", err)
  162. }
  163. if assignResult.Error != "" {
  164. return nil, fmt.Errorf("assign volume result: %v", assignResult.Error)
  165. }
  166. return &filer_pb.AssignVolumeResponse{
  167. FileId: assignResult.Fid,
  168. Count: int32(assignResult.Count),
  169. Url: assignResult.Url,
  170. PublicUrl: assignResult.PublicUrl,
  171. }, err
  172. }