watcher_linux.go 4.3 KB

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