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:
parent
3c7b0192fa
commit
51b91af60b
Notes:
sideshowbarker
2024-07-16 23:59:28 +09:00
Author: https://github.com/tcl3 Commit: https://github.com/SerenityOS/serenity/commit/51b91af60b Pull-request: https://github.com/SerenityOS/serenity/pull/18932
1 changed files with 3 additions and 5 deletions
|
@ -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++;
|
||||
|
|
Loading…
Add table
Reference in a new issue