forked from rhatdan/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement reverse reader for log reads
in cases where the log file exceeds the available memory of a system, we had a bug that triggered an oom because the entire logfile was being read when the tail parameter was given. this reads in chunks and is more or less memory safe. fixes: containers#5131 Signed-off-by: Brent Baude <[email protected]>
- Loading branch information
Showing
4 changed files
with
138 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package reversereader | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// ReverseReader structure for reading a file backwards | ||
type ReverseReader struct { | ||
reader *os.File | ||
offset int64 | ||
readSize int64 | ||
} | ||
|
||
// NewReverseReader returns a reader that reads from the end of a file | ||
// rather than the beginning. It sets the readsize to pagesize and determines | ||
// the first offset using using modulus. | ||
func NewReverseReader(reader *os.File) (*ReverseReader, error) { | ||
// pagesize should be safe for memory use and file reads should be on page | ||
// boundaries as well | ||
pageSize := int64(os.Getpagesize()) | ||
stat, err := reader.Stat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
// figure out the last page boundary | ||
remainder := stat.Size() % pageSize | ||
end, err := reader.Seek(0, 2) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// set offset (starting position) to the last page boundary or | ||
// zero if fits in one page | ||
startOffset := end - remainder | ||
if startOffset < 0 { | ||
startOffset = 0 | ||
} | ||
rr := ReverseReader{ | ||
reader: reader, | ||
offset: startOffset, | ||
readSize: pageSize, | ||
} | ||
return &rr, nil | ||
} | ||
|
||
// ReverseReader reads from a given offset to the previous offset and | ||
// then sets the newoff set one pagesize less than the previous read. | ||
func (r *ReverseReader) Read() (string, error) { | ||
if r.offset < 0 { | ||
return "", errors.Wrap(io.EOF, "at beginning of file") | ||
} | ||
// Read from given offset | ||
b := make([]byte, r.readSize) | ||
n, err := r.reader.ReadAt(b, r.offset) | ||
if err != nil && errors.Cause(err) != io.EOF { | ||
return "", err | ||
} | ||
if int64(n) < r.readSize { | ||
b = b[0:n] | ||
} | ||
// Set to the next page boundary | ||
r.offset = -r.readSize | ||
return string(b), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters