watcher_linux.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. isCreateOp := e.Op&fsnotify.Create == fsnotify.Create
  71. shouldWalk := false
  72. if isCreateOp {
  73. isDir, err := isDir(e.Name)
  74. if err != nil {
  75. log.Printf("Error stat-ing file %s: %s", e.Name, err)
  76. continue
  77. }
  78. shouldWalk = isDir
  79. }
  80. if shouldWalk {
  81. err := filepath.Walk(e.Name, func(path string, mode os.FileInfo, err error) error {
  82. if err != nil {
  83. return err
  84. }
  85. newE := fsnotify.Event{
  86. Op: fsnotify.Create,
  87. Name: path,
  88. }
  89. d.sendEventIfWatched(newE)
  90. // TODO(dmiller): symlinks 😭
  91. err = d.Add(path)
  92. if err != nil {
  93. log.Printf("Error watching path %s: %s", e.Name, err)
  94. }
  95. return nil
  96. })
  97. if err != nil {
  98. log.Printf("Error walking directory %s: %s", e.Name, err)
  99. }
  100. } else {
  101. d.sendEventIfWatched(e)
  102. }
  103. }
  104. }
  105. func (d *linuxNotify) sendEventIfWatched(e fsnotify.Event) {
  106. if _, ok := d.watchList[e.Name]; ok {
  107. d.wrappedEvents <- FileEvent{e.Name}
  108. } else {
  109. // TODO(dmiller): maybe use a prefix tree here?
  110. for path := range d.watchList {
  111. if pathIsChildOf(e.Name, path) {
  112. d.wrappedEvents <- FileEvent{e.Name}
  113. break
  114. }
  115. }
  116. }
  117. }
  118. func NewWatcher() (*linuxNotify, error) {
  119. fsw, err := fsnotify.NewWatcher()
  120. if err != nil {
  121. return nil, err
  122. }
  123. wrappedEvents := make(chan FileEvent)
  124. wmw := &linuxNotify{
  125. watcher: fsw,
  126. events: fsw.Events,
  127. wrappedEvents: wrappedEvents,
  128. errors: fsw.Errors,
  129. watchList: map[string]bool{},
  130. }
  131. go wmw.loop()
  132. return wmw, nil
  133. }
  134. func isDir(pth string) (bool, error) {
  135. fi, err := os.Lstat(pth)
  136. if os.IsNotExist(err) {
  137. return false, nil
  138. } else if err != nil {
  139. return false, err
  140. }
  141. return fi.IsDir(), nil
  142. }
  143. func checkInotifyLimits() error {
  144. if !LimitChecksEnabled() {
  145. return nil
  146. }
  147. data, err := ioutil.ReadFile("/proc/sys/fs/inotify/max_user_watches")
  148. if err != nil {
  149. return err
  150. }
  151. i, err := strconv.Atoi(strings.TrimSpace(string(data)))
  152. if err != nil {
  153. return err
  154. }
  155. if i < inotifyMin {
  156. return grpc.Errorf(
  157. codes.ResourceExhausted,
  158. "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",
  159. i,
  160. )
  161. }
  162. return nil
  163. }
  164. var _ Notify = &linuxNotify{}