filer_pb_helper.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package filer_pb
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  10. "github.com/viant/ptrie"
  11. "google.golang.org/protobuf/proto"
  12. )
  13. func (entry *Entry) IsInRemoteOnly() bool {
  14. return len(entry.GetChunks()) == 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. }