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.

122 lines
2.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package filer_pb
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. type FilerClient interface {
  11. WithFilerClient(fn func(SeaweedFilerClient) error) error
  12. AdjustedUrl(hostAndPort string) string
  13. }
  14. func GetEntry(filerClient FilerClient, fullFilePath util.FullPath) (entry *Entry, err error) {
  15. dir, name := fullFilePath.DirAndName()
  16. err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  17. request := &LookupDirectoryEntryRequest{
  18. Directory: dir,
  19. Name: name,
  20. }
  21. // glog.V(3).Infof("read %s request: %v", fullFilePath, request)
  22. resp, err := LookupEntry(client, request)
  23. if err != nil {
  24. if err == ErrNotFound {
  25. return nil
  26. }
  27. glog.V(3).Infof("read %s %v: %v", fullFilePath, resp, err)
  28. return err
  29. }
  30. if resp.Entry == nil {
  31. // glog.V(3).Infof("read %s entry: %v", fullFilePath, entry)
  32. return nil
  33. }
  34. entry = resp.Entry
  35. return nil
  36. })
  37. return
  38. }
  39. func ReadDirAllEntries(filerClient FilerClient, fullDirPath util.FullPath, prefix string, fn func(entry *Entry, isLast bool)) (err error) {
  40. err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  41. lastEntryName := ""
  42. request := &ListEntriesRequest{
  43. Directory: string(fullDirPath),
  44. Prefix: prefix,
  45. StartFromFileName: lastEntryName,
  46. Limit: math.MaxUint32,
  47. }
  48. glog.V(3).Infof("read directory: %v", request)
  49. stream, err := client.ListEntries(context.Background(), request)
  50. if err != nil {
  51. return fmt.Errorf("list %s: %v", fullDirPath, err)
  52. }
  53. var prevEntry *Entry
  54. for {
  55. resp, recvErr := stream.Recv()
  56. if recvErr != nil {
  57. if recvErr == io.EOF {
  58. if prevEntry != nil {
  59. fn(prevEntry, true)
  60. }
  61. break
  62. } else {
  63. return recvErr
  64. }
  65. }
  66. if prevEntry != nil {
  67. fn(prevEntry, false)
  68. }
  69. prevEntry = resp.Entry
  70. }
  71. return nil
  72. })
  73. return
  74. }
  75. func Exists(filerClient FilerClient, parentDirectoryPath string, entryName string, isDirectory bool) (exists bool, err error) {
  76. err = filerClient.WithFilerClient(func(client SeaweedFilerClient) error {
  77. request := &LookupDirectoryEntryRequest{
  78. Directory: parentDirectoryPath,
  79. Name: entryName,
  80. }
  81. glog.V(4).Infof("exists entry %v/%v: %v", parentDirectoryPath, entryName, request)
  82. resp, err := LookupEntry(client, request)
  83. if err != nil {
  84. if err == ErrNotFound {
  85. exists = false
  86. return nil
  87. }
  88. glog.V(0).Infof("exists entry %v: %v", request, err)
  89. return fmt.Errorf("exists entry %s/%s: %v", parentDirectoryPath, entryName, err)
  90. }
  91. exists = resp.Entry.IsDirectory == isDirectory
  92. return nil
  93. })
  94. return
  95. }