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.

166 lines
4.5 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/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 IsEmpty(event *SubscribeMetadataResponse) bool {
  116. return event.EventNotification.NewEntry == nil && event.EventNotification.OldEntry == nil
  117. }
  118. func IsCreate(event *SubscribeMetadataResponse) bool {
  119. return event.EventNotification.NewEntry != nil && event.EventNotification.OldEntry == nil
  120. }
  121. func IsUpdate(event *SubscribeMetadataResponse) bool {
  122. return event.EventNotification.NewEntry != nil &&
  123. event.EventNotification.OldEntry != nil &&
  124. event.Directory == event.EventNotification.NewParentPath &&
  125. event.EventNotification.NewEntry.Name == event.EventNotification.OldEntry.Name
  126. }
  127. func IsDelete(event *SubscribeMetadataResponse) bool {
  128. return event.EventNotification.NewEntry == nil && event.EventNotification.OldEntry != nil
  129. }
  130. func IsRename(event *SubscribeMetadataResponse) bool {
  131. return event.EventNotification.NewEntry != nil &&
  132. event.EventNotification.OldEntry != nil &&
  133. (event.Directory != event.EventNotification.NewParentPath ||
  134. event.EventNotification.NewEntry.Name != event.EventNotification.OldEntry.Name)
  135. }
  136. var _ = ptrie.KeyProvider(&FilerConf_PathConf{})
  137. func (fp *FilerConf_PathConf) Key() interface{} {
  138. key, _ := proto.Marshal(fp)
  139. return string(key)
  140. }