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.

114 lines
2.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
  1. package filer_pb
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. )
  10. func ToFileIdObject(fileIdStr string) (*FileId, error) {
  11. t, err := needle.ParseFileIdFromString(fileIdStr)
  12. if err != nil {
  13. return nil, err
  14. }
  15. return &FileId{
  16. VolumeId: uint32(t.VolumeId),
  17. Cookie: uint32(t.Cookie),
  18. FileKey: uint64(t.Key),
  19. }, nil
  20. }
  21. func (fid *FileId) toFileIdString() string {
  22. return needle.NewFileId(needle.VolumeId(fid.VolumeId), fid.FileKey, fid.Cookie).String()
  23. }
  24. func (c *FileChunk) GetFileIdString() string {
  25. if c.FileId != "" {
  26. return c.FileId
  27. }
  28. if c.Fid != nil {
  29. c.FileId = c.Fid.toFileIdString()
  30. return c.FileId
  31. }
  32. return ""
  33. }
  34. func BeforeEntrySerialization(chunks []*FileChunk) {
  35. for _, chunk := range chunks {
  36. if chunk.FileId != "" {
  37. if fid, err := ToFileIdObject(chunk.FileId); err == nil {
  38. chunk.Fid = fid
  39. chunk.FileId = ""
  40. }
  41. }
  42. if chunk.SourceFileId != "" {
  43. if fid, err := ToFileIdObject(chunk.SourceFileId); err == nil {
  44. chunk.SourceFid = fid
  45. chunk.SourceFileId = ""
  46. }
  47. }
  48. }
  49. }
  50. func EnsureFid(chunk *FileChunk) {
  51. if chunk.Fid != nil {
  52. return
  53. }
  54. if fid, err := ToFileIdObject(chunk.FileId); err == nil {
  55. chunk.Fid = fid
  56. }
  57. }
  58. func AfterEntryDeserialization(chunks []*FileChunk) {
  59. for _, chunk := range chunks {
  60. if chunk.Fid != nil && chunk.FileId == "" {
  61. chunk.FileId = chunk.Fid.toFileIdString()
  62. }
  63. if chunk.SourceFid != nil && chunk.SourceFileId == "" {
  64. chunk.SourceFileId = chunk.SourceFid.toFileIdString()
  65. }
  66. }
  67. }
  68. func CreateEntry(client SeaweedFilerClient, request *CreateEntryRequest) error {
  69. resp, err := client.CreateEntry(context.Background(), request)
  70. if err != nil {
  71. glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, err)
  72. return fmt.Errorf("CreateEntry: %v", err)
  73. }
  74. if resp.Error != "" {
  75. glog.V(1).Infof("create entry %s/%s %v: %v", request.Directory, request.Entry.Name, request.OExcl, resp.Error)
  76. return fmt.Errorf("CreateEntry : %v", resp.Error)
  77. }
  78. return nil
  79. }
  80. func LookupEntry(client SeaweedFilerClient, request *LookupDirectoryEntryRequest) (*LookupDirectoryEntryResponse, error) {
  81. resp, err := client.LookupDirectoryEntry(context.Background(), request)
  82. if err != nil {
  83. if err == ErrNotFound || strings.Contains(err.Error(), ErrNotFound.Error()) {
  84. return nil, ErrNotFound
  85. }
  86. glog.V(3).Infof("read %s/%v: %v", request.Directory, request.Name, err)
  87. return nil, fmt.Errorf("LookupEntry1: %v", err)
  88. }
  89. if resp.Entry == nil {
  90. return nil, ErrNotFound
  91. }
  92. return resp, nil
  93. }
  94. var ErrNotFound = errors.New("filer: no entry is found in filer store")