You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.5 KiB

  1. // Copyright (c) 2019 FOSS contributors of https://github.com/nxadm/tail
  2. // Copyright (c) 2015 HPE Software Inc. All rights reserved.
  3. // Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
  4. package watch
  5. import (
  6. "os"
  7. "runtime"
  8. "time"
  9. "github.com/nxadm/tail/util"
  10. "gopkg.in/tomb.v1"
  11. )
  12. // PollingFileWatcher polls the file for changes.
  13. type PollingFileWatcher struct {
  14. Filename string
  15. Size int64
  16. }
  17. func NewPollingFileWatcher(filename string) *PollingFileWatcher {
  18. fw := &PollingFileWatcher{filename, 0}
  19. return fw
  20. }
  21. var POLL_DURATION time.Duration
  22. func (fw *PollingFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
  23. for {
  24. if _, err := os.Stat(fw.Filename); err == nil {
  25. return nil
  26. } else if !os.IsNotExist(err) {
  27. return err
  28. }
  29. select {
  30. case <-time.After(POLL_DURATION):
  31. continue
  32. case <-t.Dying():
  33. return tomb.ErrDying
  34. }
  35. }
  36. panic("unreachable")
  37. }
  38. func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, pos int64) (*FileChanges, error) {
  39. origFi, err := os.Stat(fw.Filename)
  40. if err != nil {
  41. return nil, err
  42. }
  43. changes := NewFileChanges()
  44. var prevModTime time.Time
  45. // XXX: use tomb.Tomb to cleanly manage these goroutines. replace
  46. // the fatal (below) with tomb's Kill.
  47. fw.Size = pos
  48. go func() {
  49. prevSize := fw.Size
  50. for {
  51. select {
  52. case <-t.Dying():
  53. return
  54. default:
  55. }
  56. time.Sleep(POLL_DURATION)
  57. fi, err := os.Stat(fw.Filename)
  58. if err != nil {
  59. // Windows cannot delete a file if a handle is still open (tail keeps one open)
  60. // so it gives access denied to anything trying to read it until all handles are released.
  61. if os.IsNotExist(err) || (runtime.GOOS == "windows" && os.IsPermission(err)) {
  62. // File does not exist (has been deleted).
  63. changes.NotifyDeleted()
  64. return
  65. }
  66. // XXX: report this error back to the user
  67. util.Fatal("Failed to stat file %v: %v", fw.Filename, err)
  68. }
  69. // File got moved/renamed?
  70. if !os.SameFile(origFi, fi) {
  71. changes.NotifyDeleted()
  72. return
  73. }
  74. // File got truncated?
  75. fw.Size = fi.Size()
  76. if prevSize > 0 && prevSize > fw.Size {
  77. changes.NotifyTruncated()
  78. prevSize = fw.Size
  79. continue
  80. }
  81. // File got bigger?
  82. if prevSize > 0 && prevSize < fw.Size {
  83. changes.NotifyModified()
  84. prevSize = fw.Size
  85. continue
  86. }
  87. prevSize = fw.Size
  88. // File was appended to (changed)?
  89. modTime := fi.ModTime()
  90. if modTime != prevModTime {
  91. prevModTime = modTime
  92. changes.NotifyModified()
  93. }
  94. }
  95. }()
  96. return changes, nil
  97. }
  98. func init() {
  99. POLL_DURATION = 250 * time.Millisecond
  100. }