tail.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package tail
  14. import (
  15. "bytes"
  16. "io"
  17. "io/ioutil"
  18. "os"
  19. )
  20. const (
  21. // blockSize is the block size used in tail.
  22. blockSize = 1024
  23. )
  24. var (
  25. // eol is the end-of-line sign in the log.
  26. eol = []byte{'\n'}
  27. )
  28. // ReadAtMost reads at most max bytes from the end of the file identified by path or
  29. // returns an error. It returns true if the file was longer than max. It will
  30. // allocate up to max bytes.
  31. func ReadAtMost(path string, max int64) ([]byte, bool, error) {
  32. f, err := os.Open(path)
  33. if err != nil {
  34. return nil, false, err
  35. }
  36. defer f.Close()
  37. fi, err := f.Stat()
  38. if err != nil {
  39. return nil, false, err
  40. }
  41. size := fi.Size()
  42. if size == 0 {
  43. return nil, false, nil
  44. }
  45. if size < max {
  46. max = size
  47. }
  48. offset, err := f.Seek(-max, io.SeekEnd)
  49. if err != nil {
  50. return nil, false, err
  51. }
  52. data, err := ioutil.ReadAll(f)
  53. return data, offset > 0, err
  54. }
  55. // FindTailLineStartIndex returns the start of last nth line.
  56. // * If n < 0, return the beginning of the file.
  57. // * If n >= 0, return the beginning of last nth line.
  58. // Notice that if the last line is incomplete (no end-of-line), it will not be counted
  59. // as one line.
  60. func FindTailLineStartIndex(f io.ReadSeeker, n int64) (int64, error) {
  61. if n < 0 {
  62. return 0, nil
  63. }
  64. size, err := f.Seek(0, io.SeekEnd)
  65. if err != nil {
  66. return 0, err
  67. }
  68. var left, cnt int64
  69. buf := make([]byte, blockSize)
  70. for right := size; right > 0 && cnt <= n; right -= blockSize {
  71. left = right - blockSize
  72. if left < 0 {
  73. left = 0
  74. buf = make([]byte, right)
  75. }
  76. if _, err := f.Seek(left, io.SeekStart); err != nil {
  77. return 0, err
  78. }
  79. if _, err := f.Read(buf); err != nil {
  80. return 0, err
  81. }
  82. cnt += int64(bytes.Count(buf, eol))
  83. }
  84. for ; cnt > n; cnt-- {
  85. idx := bytes.Index(buf, eol) + 1
  86. buf = buf[idx:]
  87. left += int64(idx)
  88. }
  89. return left, nil
  90. }