watcher_naive.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // +build !darwin
  2. package watch
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "github.com/pkg/errors"
  8. "github.com/windmilleng/fsnotify"
  9. "github.com/windmilleng/tilt/internal/logger"
  10. "github.com/windmilleng/tilt/internal/ospath"
  11. )
  12. // A naive file watcher that uses the plain fsnotify API.
  13. // Used on all non-Darwin systems (including Windows & Linux).
  14. //
  15. // All OS-specific codepaths are handled by fsnotify.
  16. type naiveNotify struct {
  17. // Paths that we're watching that should be passed up to the caller.
  18. // Note that we may have to watch ancestors of these paths
  19. // in order to fulfill the API promise.
  20. notifyList map[string]bool
  21. ignore PathMatcher
  22. log logger.Logger
  23. watcher *fsnotify.Watcher
  24. events chan fsnotify.Event
  25. wrappedEvents chan FileEvent
  26. errors chan error
  27. numWatches int64
  28. }
  29. func (d *naiveNotify) Start() error {
  30. if len(d.notifyList) == 0 {
  31. return nil
  32. }
  33. for name := range d.notifyList {
  34. fi, err := os.Stat(name)
  35. if err != nil && !os.IsNotExist(err) {
  36. return errors.Wrapf(err, "notify.Add(%q)", name)
  37. }
  38. // if it's a file that doesn't exist, watch its parent
  39. if os.IsNotExist(err) {
  40. err = d.watchAncestorOfMissingPath(name)
  41. if err != nil {
  42. return errors.Wrapf(err, "watchAncestorOfMissingPath(%q)", name)
  43. }
  44. } else if fi.IsDir() {
  45. err = d.watchRecursively(name)
  46. if err != nil {
  47. return errors.Wrapf(err, "notify.Add(%q)", name)
  48. }
  49. } else {
  50. err = d.add(filepath.Dir(name))
  51. if err != nil {
  52. return errors.Wrapf(err, "notify.Add(%q)", filepath.Dir(name))
  53. }
  54. }
  55. }
  56. go d.loop()
  57. return nil
  58. }
  59. func (d *naiveNotify) watchRecursively(dir string) error {
  60. return filepath.Walk(dir, func(path string, mode os.FileInfo, err error) error {
  61. if err != nil {
  62. return err
  63. }
  64. if !mode.IsDir() {
  65. return nil
  66. }
  67. shouldSkipDir, err := d.shouldSkipDir(path)
  68. if err != nil {
  69. return err
  70. }
  71. if shouldSkipDir {
  72. return filepath.SkipDir
  73. }
  74. err = d.add(path)
  75. if err != nil {
  76. if os.IsNotExist(err) {
  77. return nil
  78. }
  79. return errors.Wrapf(err, "watcher.Add(%q)", path)
  80. }
  81. return nil
  82. })
  83. }
  84. func (d *naiveNotify) watchAncestorOfMissingPath(path string) error {
  85. if path == string(filepath.Separator) {
  86. return fmt.Errorf("cannot watch root directory")
  87. }
  88. _, err := os.Stat(path)
  89. if err != nil && !os.IsNotExist(err) {
  90. return errors.Wrapf(err, "os.Stat(%q)", path)
  91. }
  92. if os.IsNotExist(err) {
  93. parent := filepath.Dir(path)
  94. return d.watchAncestorOfMissingPath(parent)
  95. }
  96. return d.add(path)
  97. }
  98. func (d *naiveNotify) Close() error {
  99. numberOfWatches.Add(-d.numWatches)
  100. d.numWatches = 0
  101. return d.watcher.Close()
  102. }
  103. func (d *naiveNotify) Events() chan FileEvent {
  104. return d.wrappedEvents
  105. }
  106. func (d *naiveNotify) Errors() chan error {
  107. return d.errors
  108. }
  109. func (d *naiveNotify) loop() {
  110. defer close(d.wrappedEvents)
  111. for e := range d.events {
  112. if e.Op&fsnotify.Create != fsnotify.Create {
  113. if d.shouldNotify(e.Name) {
  114. d.wrappedEvents <- FileEvent{e.Name}
  115. }
  116. continue
  117. }
  118. // TODO(dbentley): if there's a delete should we call d.watcher.Remove to prevent leaking?
  119. err := filepath.Walk(e.Name, func(path string, mode os.FileInfo, err error) error {
  120. if err != nil {
  121. return err
  122. }
  123. if d.shouldNotify(path) {
  124. d.wrappedEvents <- FileEvent{path}
  125. }
  126. // TODO(dmiller): symlinks 😭
  127. shouldWatch := false
  128. if mode.IsDir() {
  129. // watch directories unless we can skip them entirely
  130. shouldSkipDir, err := d.shouldSkipDir(path)
  131. if err != nil {
  132. return err
  133. }
  134. if shouldSkipDir {
  135. return filepath.SkipDir
  136. }
  137. shouldWatch = true
  138. } else {
  139. // watch files that are explicitly named, but don't watch others
  140. _, ok := d.notifyList[path]
  141. if ok {
  142. shouldWatch = true
  143. }
  144. }
  145. if shouldWatch {
  146. err := d.add(path)
  147. if err != nil && !os.IsNotExist(err) {
  148. d.log.Infof("Error watching path %s: %s", e.Name, err)
  149. }
  150. }
  151. return nil
  152. })
  153. if err != nil && !os.IsNotExist(err) {
  154. d.log.Infof("Error walking directory %s: %s", e.Name, err)
  155. }
  156. }
  157. }
  158. func (d *naiveNotify) shouldNotify(path string) bool {
  159. ignore, err := d.ignore.Matches(path)
  160. if err != nil {
  161. d.log.Infof("Error matching path %q: %v", path, err)
  162. } else if ignore {
  163. return false
  164. }
  165. if _, ok := d.notifyList[path]; ok {
  166. return true
  167. }
  168. // TODO(dmiller): maybe use a prefix tree here?
  169. for root := range d.notifyList {
  170. if ospath.IsChild(root, path) {
  171. return true
  172. }
  173. }
  174. return false
  175. }
  176. func (d *naiveNotify) shouldSkipDir(path string) (bool, error) {
  177. // If path is directly in the notifyList, we should always watch it.
  178. if d.notifyList[path] {
  179. return false, nil
  180. }
  181. skip, err := d.ignore.MatchesEntireDir(path)
  182. if err != nil {
  183. return false, errors.Wrap(err, "shouldSkipDir")
  184. }
  185. return skip, nil
  186. }
  187. func (d *naiveNotify) add(path string) error {
  188. err := d.watcher.Add(path)
  189. if err != nil {
  190. return err
  191. }
  192. d.numWatches++
  193. numberOfWatches.Add(1)
  194. return nil
  195. }
  196. func newWatcher(paths []string, ignore PathMatcher, l logger.Logger) (*naiveNotify, error) {
  197. if ignore == nil {
  198. return nil, fmt.Errorf("newWatcher: ignore is nil")
  199. }
  200. fsw, err := fsnotify.NewWatcher()
  201. if err != nil {
  202. return nil, err
  203. }
  204. wrappedEvents := make(chan FileEvent)
  205. notifyList := make(map[string]bool, len(paths))
  206. for _, path := range paths {
  207. path, err := filepath.Abs(path)
  208. if err != nil {
  209. return nil, errors.Wrap(err, "newWatcher")
  210. }
  211. notifyList[path] = true
  212. }
  213. wmw := &naiveNotify{
  214. notifyList: notifyList,
  215. ignore: ignore,
  216. log: l,
  217. watcher: fsw,
  218. events: fsw.Events,
  219. wrappedEvents: wrappedEvents,
  220. errors: fsw.Errors,
  221. }
  222. return wmw, nil
  223. }
  224. func isDir(pth string) (bool, error) {
  225. fi, err := os.Lstat(pth)
  226. if os.IsNotExist(err) {
  227. return false, nil
  228. } else if err != nil {
  229. return false, err
  230. }
  231. return fi.IsDir(), nil
  232. }
  233. var _ Notify = &naiveNotify{}