mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:47:35 +00:00
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
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)
|
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());
|
auto buffer = TRY(file.read_until_eof());
|
||||||
out("{}", StringView { buffer });
|
out("{}", StringView { buffer });
|
||||||
return {};
|
return {};
|
||||||
|
@ -29,11 +29,9 @@ static ErrorOr<off_t> find_seek_pos(Core::File& file, int wanted_lines)
|
||||||
off_t end = pos;
|
off_t end = pos;
|
||||||
int lines = 0;
|
int lines = 0;
|
||||||
|
|
||||||
for (; pos >= 0; pos--) {
|
for (; pos >= 1; pos--) {
|
||||||
TRY(file.seek(pos, SeekMode::SetPosition));
|
TRY(file.seek(pos - 1, SeekMode::SetPosition));
|
||||||
|
|
||||||
if (file.is_eof())
|
|
||||||
break;
|
|
||||||
auto ch = TRY(file.read_value<u8>());
|
auto ch = TRY(file.read_value<u8>());
|
||||||
if (ch == '\n' && (end - pos) > 1) {
|
if (ch == '\n' && (end - pos) > 1) {
|
||||||
lines++;
|
lines++;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue