From 7df08bbd5e27b64237d4f67bbad88910151746ae Mon Sep 17 00:00:00 2001 From: Ashish Gandhi Date: Mon, 23 Nov 2015 12:22:47 -0800 Subject: [PATCH] cmd/tail: do not try to read more than file size --- cmd/hdfs/cat.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cmd/hdfs/cat.go b/cmd/hdfs/cat.go index 47f7b08..7285bf9 100644 --- a/cmd/hdfs/cat.go +++ b/cmd/hdfs/cat.go @@ -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) }