diff --git a/weed/filer/reader_at.go b/weed/filer/reader_at.go index d7617b740..b87fa0411 100644 --- a/weed/filer/reader_at.go +++ b/weed/filer/reader_at.go @@ -173,15 +173,14 @@ func (c *ChunkReadAt) doReadAt(p []byte, offset int64) (n int, ts int64, err err // zero the remaining bytes if a gap exists at the end of the last chunk (or a fully sparse file) if err == nil && remaining > 0 { var delta int64 - if c.fileSize > startOffset { + if c.fileSize >= startOffset { delta = min(remaining, c.fileSize-startOffset) startOffset -= offset - } else { - delta = remaining - startOffset = max(startOffset-offset, startOffset-remaining-offset) } - glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+delta, c.fileSize) - n += zero(p, startOffset, delta) + if delta > 0 { + glog.V(4).Infof("zero2 [%d,%d) of file size %d bytes", startOffset, startOffset+delta, c.fileSize) + n += zero(p, startOffset, delta) + } } if err == nil && offset+int64(len(p)) >= c.fileSize { @@ -220,6 +219,9 @@ func (c *ChunkReadAt) readChunkSliceAt(buffer []byte, chunkView *ChunkView, next } func zero(buffer []byte, start, length int64) int { + if length <= 0 { + return 0 + } end := min(start+length, int64(len(buffer))) start = max(start, 0) diff --git a/weed/filer/reader_at_test.go b/weed/filer/reader_at_test.go index 0d95d1aad..6d985a397 100644 --- a/weed/filer/reader_at_test.go +++ b/weed/filer/reader_at_test.go @@ -31,7 +31,7 @@ func (m *mockChunkCache) ReadChunkAt(data []byte, fileId string, offset uint64) func (m *mockChunkCache) SetChunk(fileId string, data []byte) { } -func (m *mockChunkCache) GetMaxFilePartSizeInCache() (uint64) { +func (m *mockChunkCache) GetMaxFilePartSizeInCache() uint64 { return 0 } @@ -81,7 +81,7 @@ func TestReaderAt(t *testing.T) { } testReadAt(t, readerAt, 0, 10, 10, io.EOF, nil, nil) - testReadAt(t, readerAt, 0, 12, 12, io.EOF, nil, nil) + testReadAt(t, readerAt, 0, 12, 10, io.EOF, nil, nil) testReadAt(t, readerAt, 2, 8, 8, io.EOF, nil, nil) testReadAt(t, readerAt, 3, 6, 6, nil, nil, nil) @@ -131,8 +131,8 @@ func TestReaderAt0(t *testing.T) { testReadAt(t, readerAt, 3, 16, 7, io.EOF, nil, nil) testReadAt(t, readerAt, 3, 5, 5, nil, nil, nil) - testReadAt(t, readerAt, 11, 5, 5, io.EOF, nil, nil) - testReadAt(t, readerAt, 10, 5, 5, io.EOF, nil, nil) + testReadAt(t, readerAt, 11, 5, 0, io.EOF, nil, nil) + testReadAt(t, readerAt, 10, 5, 0, io.EOF, nil, nil) } diff --git a/weed/mount/filehandle_read.go b/weed/mount/filehandle_read.go index faf99952f..a609c97cc 100644 --- a/weed/mount/filehandle_read.go +++ b/weed/mount/filehandle_read.go @@ -47,6 +47,8 @@ func (fh *FileHandle) readFromChunks(buff []byte, offset int64) (int64, int64, e if fileSize == 0 { glog.V(1).Infof("empty fh %v", fileFullPath) return 0, 0, io.EOF + } else if offset == fileSize { + return 0, 0, io.EOF } else if offset >= fileSize { glog.V(1).Infof("invalid read, fileSize %d, offset %d for %s", fileSize, offset, fileFullPath) return 0, 0, io.EOF