tail: Don't read past EOF when reading from a seekable stream

Previously, using tail to read from a file would fail, as we would
seek to the end of the file then try to read a byte from that
position.
This commit is contained in:
Tim Ledbetter 2023-05-20 06:51:04 +01:00 committed by Andreas Kling
parent 3c7b0192fa
commit 51b91af60b
Notes: sideshowbarker 2024-07-16 23:59:28 +09:00

View file

@ -14,7 +14,7 @@
static ErrorOr<void> tail_from_pos(Core::File& file, off_t startline)
{
TRY(file.seek(startline + 1, SeekMode::SetPosition));
TRY(file.seek(startline, SeekMode::SetPosition));
auto buffer = TRY(file.read_until_eof());
out("{}", StringView { buffer });
return {};
@ -29,11 +29,9 @@ static ErrorOr<off_t> find_seek_pos(Core::File& file, int wanted_lines)
off_t end = pos;
int lines = 0;
for (; pos >= 0; pos--) {
TRY(file.seek(pos, SeekMode::SetPosition));
for (; pos >= 1; pos--) {
TRY(file.seek(pos - 1, SeekMode::SetPosition));
if (file.is_eof())
break;
auto ch = TRY(file.read_value<u8>());
if (ch == '\n' && (end - pos) > 1) {
lines++;