watcher_naive.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. var err error
  178. ignore := false
  179. // If path is directly in the notifyList, we should always watch it.
  180. if !d.notifyList[path] {
  181. ignore, err = d.ignore.Matches(path)
  182. if err != nil {
  183. return false, errors.Wrapf(err, "Error matching %s: %v", path, err)
  184. }
  185. }
  186. // The ignore filter is telling us to ignore this file,
  187. // but we may have to watch it anyway to catch files underneath it.
  188. if ignore {
  189. if !d.ignore.Exclusions() {
  190. return true, nil
  191. }
  192. // TODO(nick): Add more complex logic for interpreting exclusion patterns.
  193. }
  194. return false, nil
  195. }
  196. func (d *naiveNotify) add(path string) error {
  197. err := d.watcher.Add(path)
  198. if err != nil {
  199. return err
  200. }
  201. d.numWatches++
  202. numberOfWatches.Add(1)
  203. return nil
  204. }
  205. func newWatcher(paths []string, ignore PathMatcher, l logger.Logger) (*naiveNotify, error) {
  206. if ignore == nil {
  207. return nil, fmt.Errorf("newWatcher: ignore is nil")
  208. }
  209. fsw, err := fsnotify.NewWatcher()
  210. if err != nil {
  211. return nil, err
  212. }
  213. wrappedEvents := make(chan FileEvent)
  214. notifyList := make(map[string]bool, len(paths))
  215. for _, path := range paths {
  216. path, err := filepath.Abs(path)
  217. if err != nil {
  218. return nil, errors.Wrap(err, "newWatcher")
  219. }
  220. notifyList[path] = true
  221. }
  222. wmw := &naiveNotify{
  223. notifyList: notifyList,
  224. ignore: ignore,
  225. log: l,
  226. watcher: fsw,
  227. events: fsw.Events,
  228. wrappedEvents: wrappedEvents,
  229. errors: fsw.Errors,
  230. }
  231. return wmw, nil
  232. }
  233. func isDir(pth string) (bool, error) {
  234. fi, err := os.Lstat(pth)
  235. if os.IsNotExist(err) {
  236. return false, nil
  237. } else if err != nil {
  238. return false, err
  239. }
  240. return fi.IsDir(), nil
  241. }
  242. var _ Notify = &naiveNotify{}