meta_cache_init.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package meta_cache
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. func EnsureVisited(mc *MetaCache, client filer_pb.FilerClient, dirPath util.FullPath, entryChan chan *filer.Entry) error {
  11. currentPath := dirPath
  12. for {
  13. // the directory children are already cached
  14. // so no need for this and upper directories
  15. if mc.isCachedFn(currentPath) {
  16. return nil
  17. }
  18. if entryChan != nil && dirPath == currentPath {
  19. if err := doEnsureVisited(mc, client, currentPath, entryChan); err != nil {
  20. return err
  21. }
  22. } else {
  23. if err := doEnsureVisited(mc, client, currentPath, nil); err != nil {
  24. return err
  25. }
  26. }
  27. // continue to parent directory
  28. if currentPath != "/" {
  29. parent, _ := currentPath.DirAndName()
  30. currentPath = util.FullPath(parent)
  31. } else {
  32. break
  33. }
  34. }
  35. return nil
  36. }
  37. func doEnsureVisited(mc *MetaCache, client filer_pb.FilerClient, path util.FullPath, entryChan chan *filer.Entry) error {
  38. glog.V(4).Infof("ReadDirAllEntries %s ...", path)
  39. err := util.Retry("ReadDirAllEntries", func() error {
  40. return filer_pb.ReadDirAllEntries(client, path, "", func(pbEntry *filer_pb.Entry, isLast bool) error {
  41. entry := filer.FromPbEntry(string(path), pbEntry)
  42. if IsHiddenSystemEntry(string(path), entry.Name()) {
  43. return nil
  44. }
  45. if err := mc.doInsertEntry(context.Background(), entry); err != nil {
  46. glog.V(0).Infof("read %s: %v", entry.FullPath, err)
  47. return err
  48. }
  49. if entryChan != nil {
  50. entryChan <- entry
  51. }
  52. return nil
  53. })
  54. })
  55. if err != nil {
  56. err = fmt.Errorf("list %s: %v", path, err)
  57. }
  58. mc.markCachedFn(path)
  59. return err
  60. }
  61. func IsHiddenSystemEntry(dir, name string) bool {
  62. return dir == "/" && (name == "topics" || name == "etc")
  63. }