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.

170 lines
4.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
3 years ago
4 years ago
  1. package filer_pb
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "github.com/golang/protobuf/proto"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  11. "github.com/viant/ptrie"
  12. )
  13. func (entry *Entry) IsInRemoteOnly() bool {
  14. return len(entry.Chunks) == 0 && entry.RemoteEntry != nil && entry.RemoteEntry.RemoteSize > 0
  15. }
  16. func (entry *Entry) IsDirectoryKeyObject() bool {
  17. return entry.IsDirectory && entry.Attributes != nil && entry.Attributes.Mime != ""
  18. }
  19. func (entry *Entry) FileMode() (fileMode os.FileMode) {
  20. if entry != nil && entry.Attributes != nil {
  21. fileMode = os.FileMode(entry.Attributes.FileMode)
  22. }
  23. return
  24. }
  25. func ToFileIdObject(fileIdStr string) (*FileId, error) {
  26. t, err := needle.ParseFileIdFromString(fileIdStr)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return &FileId{
  31. VolumeId: uint32(t.VolumeId),
  32. Cookie: uint32(t.Cookie),
  33. FileKey: uint64(t.Key),
  34. }, nil
  35. }
  36. func (fid *FileId) toFileIdString() string {
  37. return needle.NewFileId(needle.VolumeId(fid.VolumeId), fid.FileKey, fid.Cookie).String()
  38. }
  39. func (c *FileChunk) GetFileIdString() string {
  40. if c.FileId != "" {
  41. return c.FileId
  42. }
  43. if c.Fid != nil {
  44. c.FileId = c.Fid.toFileIdString()
  45. return c.FileId
  46. }
  47. return ""
  48. }
  49. func BeforeEntrySerialization(chunks []*FileChunk) {
  50. for _, chunk := range chunks {
  51. if chunk.FileId != "" {
  52. if fid, err := ToFileIdObject(chunk.FileId); err == nil {
  53. chunk.Fid = fid
  54. chunk.FileId = ""
  55. }
  56. }
  57. if chunk.SourceFileId != "" {
  58. if fid, err := ToFileIdObject(chunk.SourceFileId); err == nil {
  59. chunk.SourceFid = fid
  60. chunk.SourceFileId = ""
  61. }
  62. }
  63. }
  64. }
  65. func EnsureFid(chunk *FileChunk) {
  66. if chunk.Fid != nil {
  67. return
  68. }
  69. if fid, err := ToFileIdObject(chunk.FileId); err == nil {
  70. chunk.Fid = fid
  71. }
  72. }
  73. func AfterEntryDeserialization(chunks []*FileChunk) {
  74. for _, chunk := range chunks {
  75. if chunk.Fid != nil && chunk.FileId == "" {
  76. chunk.FileId = chunk.Fid.toFileIdString()
  77. }
  78. if chunk.SourceFid != nil && chunk.SourceFileId == "" {
  79. chunk.SourceFileId = chunk.SourceFid.toFileIdString()
  80. }
  81. }
  82. }
  83. func CreateEntry(client SeaweedFilerClient, request *CreateEntryRequest) error {
  84. resp, err := client.CreateEntry(context.Background(), request)
  85. if err != nil {
  86. glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, err)
  87. return fmt.Errorf("CreateEntry: %v", err)
  88. }
  89. if resp.Error != "" {
  90. glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, resp.Error)
  91. return fmt.Errorf("CreateEntry : %v", resp.Error)
  92. }
  93. return nil
  94. }
  95. func UpdateEntry(client SeaweedFilerClient, request *UpdateEntryRequest) error {
  96. _, err := client.UpdateEntry(context.Background(), request)
  97. if err != nil {
  98. glog.V(1).Infof("update entry %s/%s :%v", request.Directory, request.Entry.Name, err)
  99. return fmt.Errorf("UpdateEntry: %v", err)
  100. }
  101. return nil
  102. }
  103. func LookupEntry(client SeaweedFilerClient, request *LookupDirectoryEntryRequest) (*LookupDirectoryEntryResponse, error) {
  104. resp, err := client.LookupDirectoryEntry(context.Background(), request)
  105. if err != nil {
  106. if err == ErrNotFound || strings.Contains(err.Error(), ErrNotFound.Error()) {
  107. return nil, ErrNotFound
  108. }
  109. glog.V(3).Infof("read %s/%v: %v", request.Directory, request.Name, err)
  110. return nil, fmt.Errorf("LookupEntry1: %v", err)
  111. }
  112. if resp.Entry == nil {
  113. return nil, ErrNotFound
  114. }
  115. return resp, nil
  116. }
  117. var ErrNotFound = errors.New("filer: no entry is found in filer store")
  118. func IsEmpty(event *SubscribeMetadataResponse) bool {
  119. return event.EventNotification.NewEntry == nil && event.EventNotification.OldEntry == nil
  120. }
  121. func IsCreate(event *SubscribeMetadataResponse) bool {
  122. return event.EventNotification.NewEntry != nil && event.EventNotification.OldEntry == nil
  123. }
  124. func IsUpdate(event *SubscribeMetadataResponse) bool {
  125. return event.EventNotification.NewEntry != nil &&
  126. event.EventNotification.OldEntry != nil &&
  127. event.Directory == event.EventNotification.NewParentPath &&
  128. event.EventNotification.NewEntry.Name == event.EventNotification.OldEntry.Name
  129. }
  130. func IsDelete(event *SubscribeMetadataResponse) bool {
  131. return event.EventNotification.NewEntry == nil && event.EventNotification.OldEntry != nil
  132. }
  133. func IsRename(event *SubscribeMetadataResponse) bool {
  134. return event.EventNotification.NewEntry != nil &&
  135. event.EventNotification.OldEntry != nil &&
  136. (event.Directory != event.EventNotification.NewParentPath ||
  137. event.EventNotification.NewEntry.Name != event.EventNotification.OldEntry.Name)
  138. }
  139. var _ = ptrie.KeyProvider(&FilerConf_PathConf{})
  140. func (fp *FilerConf_PathConf) Key() interface{} {
  141. key, _ := proto.Marshal(fp)
  142. return string(key)
  143. }