watcher_darwin.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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-2 CREATE events for that directory. We want to ignore these.
  21. ignoreCreatedEvents map[string]int
  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. ignoreCount := d.ignoreCreatedEvents[e.Path]
  37. shouldIgnore := ignoreCount > 0
  38. if shouldIgnore {
  39. d.ignoreCreatedEvents[e.Path]--
  40. } else {
  41. // If we got a created event for something
  42. // that's not on the ignore list, we assume
  43. // we're done with the spurious events.
  44. d.ignoreCreatedEvents = nil
  45. }
  46. d.sm.Unlock()
  47. if shouldIgnore {
  48. continue
  49. }
  50. }
  51. d.events <- FileEvent{
  52. Path: e.Path,
  53. }
  54. }
  55. }
  56. }
  57. }
  58. func (d *darwinNotify) Add(name string) error {
  59. d.sm.Lock()
  60. defer d.sm.Unlock()
  61. es := d.stream
  62. // Check if this is a subdirectory of any of the paths
  63. // we're already watching.
  64. for _, parent := range es.Paths {
  65. isChild := pathIsChildOf(name, parent)
  66. if isChild {
  67. return nil
  68. }
  69. }
  70. es.Paths = append(es.Paths, name)
  71. if d.ignoreCreatedEvents == nil {
  72. d.ignoreCreatedEvents = make(map[string]int, 1)
  73. }
  74. d.ignoreCreatedEvents[name] = 2
  75. if len(es.Paths) == 1 {
  76. es.Start()
  77. go d.loop()
  78. } else {
  79. es.Restart()
  80. }
  81. return nil
  82. }
  83. func (d *darwinNotify) Close() error {
  84. d.sm.Lock()
  85. defer d.sm.Unlock()
  86. d.stream.Stop()
  87. close(d.errors)
  88. close(d.stop)
  89. return nil
  90. }
  91. func (d *darwinNotify) Events() chan FileEvent {
  92. return d.events
  93. }
  94. func (d *darwinNotify) Errors() chan error {
  95. return d.errors
  96. }
  97. func NewWatcher() (Notify, error) {
  98. dw := &darwinNotify{
  99. stream: &fsevents.EventStream{
  100. Latency: 1 * time.Millisecond,
  101. Flags: fsevents.FileEvents,
  102. },
  103. sm: &sync.Mutex{},
  104. events: make(chan FileEvent),
  105. errors: make(chan error),
  106. stop: make(chan struct{}),
  107. }
  108. return dw, nil
  109. }
  110. var _ Notify = &darwinNotify{}