LibCore: Fix relative seeking in IODevice

The recently introduced read buffer in IODevice broke relative seeking.
The amount of data in the buffer wouldn't get taken into account.
This commit is contained in:
Arne Elster 2021-11-29 19:56:29 +01:00 committed by Andreas Kling
parent b818d4c7e3
commit cdaa179eeb
4 changed files with 35 additions and 1 deletions

1
Tests/LibCore/10kb.txt Normal file

File diff suppressed because one or more lines are too long

View file

@ -9,4 +9,4 @@ foreach(source IN LISTS TEST_SOURCES)
serenity_test("${source}" LibCore)
endforeach()
install(FILES long_lines.txt DESTINATION usr/Tests/LibCore)
install(FILES long_lines.txt 10kb.txt DESTINATION usr/Tests/LibCore)

View file

@ -44,3 +44,35 @@ TEST_CASE(file_readline)
EXPECT_EQ(inputData[i], outputData[i]);
}
}
TEST_CASE(file_get_read_position)
{
const String path = "10kb.txt";
auto file = Core::File::open(path, Core::OpenMode::ReadOnly).release_value();
const size_t step_size = 98;
for (size_t i = 0; i < 10240 - step_size; i += step_size) {
auto read_buffer = file->read(step_size);
EXPECT_EQ(read_buffer.size(), step_size);
for (size_t j = 0; j < read_buffer.size(); j++) {
EXPECT_EQ(static_cast<u32>(read_buffer[j] - '0'), (i + j) % 10);
}
off_t offset = 0;
VERIFY(file->seek(0, Core::SeekMode::FromCurrentPosition, &offset));
EXPECT_EQ(offset, static_cast<off_t>(i + step_size));
}
{
off_t offset = 0;
VERIFY(file->seek(0, Core::SeekMode::FromEndPosition, &offset));
EXPECT_EQ(offset, 10240);
}
{
off_t offset = 0;
VERIFY(file->seek(0, Core::SeekMode::SetPosition, &offset));
EXPECT_EQ(offset, 0);
}
}

View file

@ -245,6 +245,7 @@ bool IODevice::seek(i64 offset, SeekMode mode, off_t* pos)
break;
case SeekMode::FromCurrentPosition:
m = SEEK_CUR;
offset -= m_buffered_data.size();
break;
case SeekMode::FromEndPosition:
m = SEEK_END;