inotify.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. events := Events(fw.Filename)
  66. for {
  67. prevSize := fw.Size
  68. var evt fsnotify.Event
  69. var ok bool
  70. select {
  71. case evt, ok = <-events:
  72. if !ok {
  73. RemoveWatch(fw.Filename)
  74. return
  75. }
  76. case <-t.Dying():
  77. RemoveWatch(fw.Filename)
  78. return
  79. }
  80. switch {
  81. case evt.Op&fsnotify.Remove == fsnotify.Remove:
  82. fallthrough
  83. case evt.Op&fsnotify.Rename == fsnotify.Rename:
  84. RemoveWatch(fw.Filename)
  85. changes.NotifyDeleted()
  86. return
  87. //With an open fd, unlink(fd) - inotify returns IN_ATTRIB (==fsnotify.Chmod)
  88. case evt.Op&fsnotify.Chmod == fsnotify.Chmod:
  89. fallthrough
  90. case evt.Op&fsnotify.Write == fsnotify.Write:
  91. fi, err := os.Stat(fw.Filename)
  92. if err != nil {
  93. if os.IsNotExist(err) {
  94. RemoveWatch(fw.Filename)
  95. changes.NotifyDeleted()
  96. return
  97. }
  98. // XXX: report this error back to the user
  99. util.Fatal("Failed to stat file %v: %v", fw.Filename, err)
  100. }
  101. fw.Size = fi.Size()
  102. if prevSize > 0 && prevSize > fw.Size {
  103. changes.NotifyTruncated()
  104. } else {
  105. changes.NotifyModified()
  106. }
  107. prevSize = fw.Size
  108. }
  109. }
  110. }()
  111. return changes, nil
  112. }