filer_pb_helper.go 4.2 KB

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