watcher_darwin.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // ignore the first event that says the watched directory
  20. // has been created. these are fired spuriously on initiation.
  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. d.ignoreCreatedEvents[e.Path] = false
  39. } else {
  40. // If we got a created event for something
  41. // that's not on the ignore list, we assume
  42. // we're done with the spurious events.
  43. d.ignoreCreatedEvents = nil
  44. }
  45. d.sm.Unlock()
  46. if shouldIgnore {
  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 d.ignoreCreatedEvents == nil {
  71. d.ignoreCreatedEvents = make(map[string]bool, 1)
  72. }
  73. d.ignoreCreatedEvents[name] = true
  74. if len(es.Paths) == 1 {
  75. go d.loop()
  76. es.Start()
  77. } else {
  78. es.Restart()
  79. }
  80. return nil
  81. }
  82. func (d *darwinNotify) Close() error {
  83. d.sm.Lock()
  84. defer d.sm.Unlock()
  85. d.stream.Stop()
  86. close(d.errors)
  87. close(d.stop)
  88. return nil
  89. }
  90. func (d *darwinNotify) Events() chan FileEvent {
  91. return d.events
  92. }
  93. func (d *darwinNotify) Errors() chan error {
  94. return d.errors
  95. }
  96. func NewWatcher() (Notify, error) {
  97. dw := &darwinNotify{
  98. stream: &fsevents.EventStream{
  99. Latency: 1 * time.Millisecond,
  100. Flags: fsevents.FileEvents,
  101. },
  102. sm: &sync.Mutex{},
  103. events: make(chan FileEvent),
  104. errors: make(chan error),
  105. stop: make(chan struct{}),
  106. }
  107. return dw, nil
  108. }
  109. var _ Notify = &darwinNotify{}