watcher_darwin.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. }
  20. func (d *darwinNotify) isTrackingPath(path string) bool {
  21. d.sm.Lock()
  22. defer d.sm.Unlock()
  23. for _, p := range d.stream.Paths {
  24. if p == path {
  25. return true
  26. }
  27. }
  28. return false
  29. }
  30. func (d *darwinNotify) loop() {
  31. ignoredSpuriousEvent := false
  32. for {
  33. select {
  34. case <-d.stop:
  35. return
  36. case events, ok := <-d.stream.Events:
  37. if !ok {
  38. return
  39. }
  40. for _, e := range events {
  41. e.Path = filepath.Join("/", e.Path)
  42. // ignore the first event that says the watched directory
  43. // has been created. these are fired spuriously on initiation.
  44. if e.Flags&fsevents.ItemCreated == fsevents.ItemCreated {
  45. if d.isTrackingPath(e.Path) && !ignoredSpuriousEvent {
  46. ignoredSpuriousEvent = true
  47. continue
  48. }
  49. }
  50. d.events <- FileEvent{
  51. Path: e.Path,
  52. }
  53. }
  54. }
  55. }
  56. }
  57. func (d *darwinNotify) Add(name string) error {
  58. d.sm.Lock()
  59. defer d.sm.Unlock()
  60. es := d.stream
  61. // Check if this is a subdirectory of any of the paths
  62. // we're already watching.
  63. for _, parent := range es.Paths {
  64. isChild := pathIsChildOf(name, parent)
  65. if isChild {
  66. return nil
  67. }
  68. }
  69. es.Paths = append(es.Paths, name)
  70. if len(es.Paths) == 1 {
  71. go d.loop()
  72. es.Start()
  73. } else {
  74. es.Restart()
  75. }
  76. return nil
  77. }
  78. func (d *darwinNotify) Close() error {
  79. d.sm.Lock()
  80. defer d.sm.Unlock()
  81. d.stream.Stop()
  82. close(d.errors)
  83. close(d.stop)
  84. return nil
  85. }
  86. func (d *darwinNotify) Events() chan FileEvent {
  87. return d.events
  88. }
  89. func (d *darwinNotify) Errors() chan error {
  90. return d.errors
  91. }
  92. func NewWatcher() (Notify, error) {
  93. dw := &darwinNotify{
  94. stream: &fsevents.EventStream{
  95. Latency: 1 * time.Millisecond,
  96. Flags: fsevents.FileEvents,
  97. },
  98. sm: &sync.Mutex{},
  99. events: make(chan FileEvent),
  100. errors: make(chan error),
  101. stop: make(chan struct{}),
  102. }
  103. return dw, nil
  104. }
  105. var _ Notify = &darwinNotify{}