cmd/tail: do not try to read more than file size

This commit is contained in:
Ashish Gandhi 2015-11-23 12:22:47 -08:00
Родитель 885c569282
Коммит 7df08bbd5e
1 изменённых файлов: 9 добавлений и 4 удалений

Просмотреть файл

@ -4,12 +4,13 @@ import (
"bufio"
"errors"
"fmt"
"github.com/colinmarc/hdfs"
"io"
"os"
"github.com/colinmarc/hdfs"
)
var tailSearchSize int64 = 16384
const tailSearchSize int64 = 16384
func cat(paths []string) {
expanded, client, err := getClientAndExpandedPaths(paths)
@ -119,10 +120,14 @@ func tailLines(file *hdfs.FileReader, numLines int64) {
if searchPoint < 0 {
searchPoint = 0
}
readSize := tailSearchSize
if readSize > fileSize {
readSize = fileSize
}
var printOffset int64
for searchPoint >= 0 {
section := bufio.NewReader(io.NewSectionReader(file, searchPoint, tailSearchSize))
section := bufio.NewReader(io.NewSectionReader(file, searchPoint, readSize))
off := searchPoint
newlines := make([]int64, 0, tailSearchSize/64)
@ -136,7 +141,7 @@ func tailLines(file *hdfs.FileReader, numLines int64) {
b, err = section.ReadByte()
}
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
if err != nil && err != io.EOF {
fatal(err)
}