watcher_darwin.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package watch
  2. import (
  3. "path/filepath"
  4. "sync"
  5. "time"
  6. "github.com/windmilleng/fsevents"
  7. )
  8. type darwinNotify struct {
  9. stream *fsevents.EventStream
  10. events chan FileEvent
  11. errors chan error
  12. stop chan struct{}
  13. // TODO(nick): This mutex is needed for the case where we add paths after we
  14. // start watching. But because fsevents supports recursive watches, we don't
  15. // actually need this feature. We should change the api contract of wmNotify
  16. // so that, for recursive watches, we can guarantee that the path list doesn't
  17. // change.
  18. sm *sync.Mutex
  19. // When a watch is created for a directory, we've seen fsevents non-determistically
  20. // fire 0-3 CREATE events for that directory. We want to ignore these.
  21. ignoreCreatedEvents map[string]bool
  22. }
  23. func (d *darwinNotify) loop() {
  24. for {
  25. select {
  26. case <-d.stop:
  27. return
  28. case events, ok := <-d.stream.Events:
  29. if !ok {
  30. return
  31. }
  32. for _, e := range events {
  33. e.Path = filepath.Join("/", e.Path)
  34. if e.Flags&fsevents.ItemCreated == fsevents.ItemCreated {
  35. d.sm.Lock()
  36. shouldIgnore := d.ignoreCreatedEvents[e.Path]
  37. if !shouldIgnore {
  38. // If we got a created event for something
  39. // that's not on the ignore list, we assume
  40. // we're done with the spurious events.
  41. d.ignoreCreatedEvents = nil
  42. }
  43. d.sm.Unlock()
  44. if shouldIgnore {
  45. continue
  46. }
  47. }
  48. d.events <- FileEvent{
  49. Path: e.Path,
  50. }
  51. }
  52. }
  53. }
  54. }
  55. func (d *darwinNotify) Add(name string) error {
  56. d.sm.Lock()
  57. defer d.sm.Unlock()
  58. es := d.stream
  59. // Check if this is a subdirectory of any of the paths
  60. // we're already watching.
  61. for _, parent := range es.Paths {
  62. isChild := pathIsChildOf(name, parent)
  63. if isChild {
  64. return nil
  65. }
  66. }
  67. es.Paths = append(es.Paths, name)
  68. if d.ignoreCreatedEvents == nil {
  69. d.ignoreCreatedEvents = make(map[string]bool, 1)
  70. }
  71. d.ignoreCreatedEvents[name] = true
  72. if len(es.Paths) == 1 {
  73. es.Start()
  74. go d.loop()
  75. } else {
  76. es.Restart()
  77. }
  78. return nil
  79. }
  80. func (d *darwinNotify) Close() error {
  81. d.sm.Lock()
  82. defer d.sm.Unlock()
  83. d.stream.Stop()
  84. close(d.errors)
  85. close(d.stop)
  86. return nil
  87. }
  88. func (d *darwinNotify) Events() chan FileEvent {
  89. return d.events
  90. }
  91. func (d *darwinNotify) Errors() chan error {
  92. return d.errors
  93. }
  94. func NewWatcher() (Notify, error) {
  95. dw := &darwinNotify{
  96. stream: &fsevents.EventStream{
  97. Latency: 1 * time.Millisecond,
  98. Flags: fsevents.FileEvents,
  99. },
  100. sm: &sync.Mutex{},
  101. events: make(chan FileEvent),
  102. errors: make(chan error),
  103. stop: make(chan struct{}),
  104. }
  105. return dw, nil
  106. }
  107. var _ Notify = &darwinNotify{}