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.

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