watcher_linux.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package watch
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "github.com/windmilleng/fsnotify"
  10. "google.golang.org/grpc"
  11. "google.golang.org/grpc/codes"
  12. )
  13. const enospc = "no space left on device"
  14. const inotifyErrMsg = "The user limit on the total number of inotify watches was reached; increase the fs.inotify.max_user_watches sysctl. See here for more information: https://facebook.github.io/watchman/docs/install.html#linux-inotify-limits"
  15. const inotifyMin = 8192
  16. type linuxNotify struct {
  17. watcher *fsnotify.Watcher
  18. events chan fsnotify.Event
  19. wrappedEvents chan FileEvent
  20. errors chan error
  21. watchList map[string]bool
  22. }
  23. func (d *linuxNotify) Add(name string) error {
  24. fi, err := os.Stat(name)
  25. if err != nil && !os.IsNotExist(err) {
  26. return err
  27. }
  28. // if it's a file that doesn't exist watch it's parent
  29. if os.IsNotExist(err) {
  30. parent := filepath.Join(name, "..")
  31. err = d.watcher.Add(parent)
  32. if err != nil {
  33. return err
  34. }
  35. d.watchList[parent] = true
  36. } else if fi.IsDir() {
  37. err = d.watchRecursively(name)
  38. if err != nil {
  39. return err
  40. }
  41. d.watchList[name] = true
  42. } else {
  43. err = d.watcher.Add(name)
  44. if err != nil {
  45. return err
  46. }
  47. d.watchList[name] = true
  48. }
  49. return nil
  50. }
  51. func (d *linuxNotify) watchRecursively(dir string) error {
  52. return filepath.Walk(dir, func(path string, mode os.FileInfo, err error) error {
  53. if err != nil {
  54. return err
  55. }
  56. return d.watcher.Add(path)
  57. })
  58. }
  59. func (d *linuxNotify) Close() error {
  60. return d.watcher.Close()
  61. }
  62. func (d *linuxNotify) Events() chan FileEvent {
  63. return d.wrappedEvents
  64. }
  65. func (d *linuxNotify) Errors() chan error {
  66. return d.errors
  67. }
  68. func (d *linuxNotify) loop() {
  69. for e := range d.events {
  70. if e.Op&fsnotify.Create == fsnotify.Create && isDir(e.Name) {
  71. err := filepath.Walk(e.Name, func(path string, mode os.FileInfo, err error) error {
  72. if err != nil {
  73. return err
  74. }
  75. newE := fsnotify.Event{
  76. Op: fsnotify.Create,
  77. Name: path,
  78. }
  79. d.sendEventIfWatched(newE)
  80. // TODO(dmiller): symlinks 😭
  81. err = d.Add(path)
  82. if err != nil {
  83. log.Printf("Error watching path %s: %s", e.Name, err)
  84. }
  85. return nil
  86. })
  87. if err != nil {
  88. log.Printf("Error walking directory %s: %s", e.Name, err)
  89. }
  90. } else {
  91. d.sendEventIfWatched(e)
  92. }
  93. }
  94. }
  95. func (d *linuxNotify) sendEventIfWatched(e fsnotify.Event) {
  96. if _, ok := d.watchList[e.Name]; ok {
  97. d.wrappedEvents <- FileEvent{e.Name}
  98. } else {
  99. // TODO(dmiller): maybe use a prefix tree here?
  100. for path := range d.watchList {
  101. if pathIsChildOf(e.Name, path) {
  102. d.wrappedEvents <- FileEvent{e.Name}
  103. break
  104. }
  105. }
  106. }
  107. }
  108. func NewWatcher() (*linuxNotify, error) {
  109. fsw, err := fsnotify.NewWatcher()
  110. if err != nil {
  111. return nil, err
  112. }
  113. wrappedEvents := make(chan FileEvent)
  114. wmw := &linuxNotify{
  115. watcher: fsw,
  116. events: fsw.Events,
  117. wrappedEvents: wrappedEvents,
  118. errors: fsw.Errors,
  119. watchList: map[string]bool{},
  120. }
  121. go wmw.loop()
  122. return wmw, nil
  123. }
  124. func isDir(pth string) bool {
  125. fi, _ := os.Stat(pth)
  126. return fi.IsDir()
  127. }
  128. func checkInotifyLimits() error {
  129. if !LimitChecksEnabled() {
  130. return nil
  131. }
  132. data, err := ioutil.ReadFile("/proc/sys/fs/inotify/max_user_watches")
  133. if err != nil {
  134. return err
  135. }
  136. i, err := strconv.Atoi(strings.TrimSpace(string(data)))
  137. if err != nil {
  138. return err
  139. }
  140. if i < inotifyMin {
  141. return grpc.Errorf(
  142. codes.ResourceExhausted,
  143. "The user limit on the total number of inotify watches is too low (%d); increase the fs.inotify.max_user_watches sysctl. See here for more information: https://facebook.github.io/watchman/docs/install.html#linux-inotify-limits",
  144. i,
  145. )
  146. }
  147. return nil
  148. }
  149. var _ Notify = &linuxNotify{}