inotify.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2015 HPE Software Inc. All rights reserved.
  2. // Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
  3. package watch
  4. import (
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "github.com/hpcloud/tail/util"
  9. "gopkg.in/fsnotify.v1"
  10. "gopkg.in/tomb.v1"
  11. )
  12. // InotifyFileWatcher uses inotify to monitor file changes.
  13. type InotifyFileWatcher struct {
  14. Filename string
  15. Size int64
  16. }
  17. func NewInotifyFileWatcher(filename string) *InotifyFileWatcher {
  18. fw := &InotifyFileWatcher{filepath.Clean(filename), 0}
  19. return fw
  20. }
  21. func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
  22. err := WatchCreate(fw.Filename)
  23. if err != nil {
  24. return err
  25. }
  26. defer RemoveWatchCreate(fw.Filename)
  27. // Do a real check now as the file might have been created before
  28. // calling `WatchFlags` above.
  29. if _, err = os.Stat(fw.Filename); !os.IsNotExist(err) {
  30. // file exists, or stat returned an error.
  31. return err
  32. }
  33. events := Events(fw.Filename)
  34. for {
  35. select {
  36. case evt, ok := <-events:
  37. if !ok {
  38. return fmt.Errorf("inotify watcher has been closed")
  39. }
  40. evtName, err := filepath.Abs(evt.Name)
  41. if err != nil {
  42. return err
  43. }
  44. fwFilename, err := filepath.Abs(fw.Filename)
  45. if err != nil {
  46. return err
  47. }
  48. if evtName == fwFilename {
  49. return nil
  50. }
  51. case <-t.Dying():
  52. return tomb.ErrDying
  53. }
  54. }
  55. panic("unreachable")
  56. }
  57. func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, pos int64) (*FileChanges, error) {
  58. err := Watch(fw.Filename)
  59. if err != nil {
  60. return nil, err
  61. }
  62. changes := NewFileChanges()
  63. fw.Size = pos
  64. go func() {
  65. defer RemoveWatch(fw.Filename)
  66. events := Events(fw.Filename)
  67. for {
  68. prevSize := fw.Size
  69. var evt fsnotify.Event
  70. var ok bool
  71. select {
  72. case evt, ok = <-events:
  73. if !ok {
  74. return
  75. }
  76. case <-t.Dying():
  77. return
  78. }
  79. switch {
  80. case evt.Op&fsnotify.Remove == fsnotify.Remove:
  81. fallthrough
  82. case evt.Op&fsnotify.Rename == fsnotify.Rename:
  83. changes.NotifyDeleted()
  84. return
  85. case evt.Op&fsnotify.Write == fsnotify.Write:
  86. fi, err := os.Stat(fw.Filename)
  87. if err != nil {
  88. if os.IsNotExist(err) {
  89. changes.NotifyDeleted()
  90. return
  91. }
  92. // XXX: report this error back to the user
  93. util.Fatal("Failed to stat file %v: %v", fw.Filename, err)
  94. }
  95. fw.Size = fi.Size()
  96. if prevSize > 0 && prevSize > fw.Size {
  97. changes.NotifyTruncated()
  98. } else {
  99. changes.NotifyModified()
  100. }
  101. prevSize = fw.Size
  102. }
  103. }
  104. }()
  105. return changes, nil
  106. }