Skip to content

Commit

Permalink
file_stream: proper seek support.
Browse files Browse the repository at this point in the history
Our file_stream interface supports seek, but when we try to seek to arbitrary
locations that are smaller than an aio-boundary (say, for instance, f->seek(4)),
we will end up not being able to perform the read.

We need to guarantee the reads are aligned, and will then present to the caller
the buffer properly offset.

Signed-off-by: Glauber Costa <[email protected]>
  • Loading branch information
glommer authored and avikivity committed Feb 18, 2015
1 parent c4c5899 commit 861d262
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions core/fstream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ file_data_source_impl::get() {
auto q = static_cast<char*>(p);
temporary_buffer<char> buf(q, _buffer_size, make_free_deleter(p));
auto old_pos = _pos;
_pos += _buffer_size;
// dma_read needs to be aligned. It doesn't have to be page-aligned,
// though, and we could get away with something much smaller. However, if
// we start reading in things outside page boundaries, we will end up with
// various pages around, some of them with overlapping ranges. Those would
// be very challenging to cache.
old_pos &= ~4095;
auto front = _pos - old_pos;
_pos += _buffer_size - front;
return _file.dma_read(old_pos, q, _buffer_size).then(
[buf = std::move(buf)] (size_t size) mutable {
[buf = std::move(buf), front] (size_t size) mutable {
buf.trim(size);
buf.trim_front(front);
return make_ready_future<temporary_buffer<char>>(std::move(buf));
});
}
Expand Down

0 comments on commit 861d262

Please sign in to comment.