Skip to content

Commit

Permalink
io/FileDescriptor: pass std::span to ReadAt()
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Aug 26, 2024
1 parent a92ac7b commit ca672aa
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion libcommon
2 changes: 1 addition & 1 deletion src/bp/EmulateModAuthEasy.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ParseBasicAuth(const char *authorization) noexcept
static char *
ReadFirstLine(FileDescriptor fd, char *buffer, size_t size) noexcept
{
ssize_t nbytes = fd.ReadAt(0, buffer, size - 1);
ssize_t nbytes = fd.ReadAt(0, std::as_writable_bytes(std::span{buffer, size - 1}));
if (nbytes <= 0)
return nullptr;

Expand Down
6 changes: 3 additions & 3 deletions src/io/Buffered.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ ReadToBufferAt(FileDescriptor fd, off_t offset,
if (w.empty())
return -2;

if (length > w.size())
length = w.size();
if (w.size() > length)
w = w.first(length);

ssize_t nbytes = fd.ReadAt(offset, w.data(), length);
ssize_t nbytes = fd.ReadAt(offset, w);
if (nbytes > 0)
buffer.Append((size_t)nbytes);

Expand Down
13 changes: 8 additions & 5 deletions src/istream/BufferedIstream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,19 @@ BufferedIstream::ReadToBuffer(FileDescriptor fd, off_t offset,
if (!buffer.IsDefined())
buffer = fb_pool_get().Alloc();

const auto w = buffer.Write();
auto w = buffer.Write();
if (w.empty())
/* buffer is full - the "ready" call is pending */
return IstreamDirectResult::BLOCKING;

const std::size_t n_try = std::min(w.size(), max_length);
const std::size_t buffer_space = w.size();

if (w.size() > max_length)
w = w.first(max_length);

ssize_t nbytes = HasOffset(offset)
? fd.ReadAt(offset, w.data(), n_try)
: fd.Read(w.data(), n_try);
? fd.ReadAt(offset, w)
: fd.Read(w);
if (nbytes <= 0)
return nbytes < 0
? IstreamDirectResult::ERRNO
Expand All @@ -171,7 +174,7 @@ BufferedIstream::ReadToBuffer(FileDescriptor fd, off_t offset,
input.ConsumeDirect(nbytes);
buffer.Append(nbytes);

if ((std::size_t)nbytes == w.size())
if ((std::size_t)nbytes == buffer_space)
/* buffer has become full - we can report to handler */
defer_ready.Schedule();

Expand Down
2 changes: 1 addition & 1 deletion src/istream/FileIstream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ FileIstream::TryData()
if (GetRemaining() < off_t(w.size()))
w = w.first(GetRemaining());

ssize_t nbytes = fd.ReadAt(offset, w.data(), w.size());
ssize_t nbytes = fd.ReadAt(offset, w);
if (nbytes == 0) {
throw FmtRuntimeError("premature end of file in '{}'", path);
} else if (nbytes == -1) {
Expand Down
9 changes: 4 additions & 5 deletions src/memory/SinkGrowingBuffer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "istream/Bucket.hxx"
#include "io/FileDescriptor.hxx"

#include <algorithm>

IstreamReadyResult
GrowingBufferSink::OnIstreamReady() noexcept
{
Expand Down Expand Up @@ -60,11 +58,12 @@ GrowingBufferSink::OnDirect(FdType, FileDescriptor fd, off_t offset,
[[maybe_unused]] bool then_eof) noexcept
{
auto w = buffer.BeginWrite();
const std::size_t n = std::min(w.size(), max_length);
if (w.size() > max_length)
w = w.first(max_length);

ssize_t nbytes = HasOffset(offset)
? fd.ReadAt(offset, w.data(), n)
: fd.Read(w.data(), n);
? fd.ReadAt(offset, w)
: fd.Read(w);
if (nbytes <= 0)
return nbytes < 0
? IstreamDirectResult::ERRNO
Expand Down
2 changes: 1 addition & 1 deletion src/memory/sink_rubber.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fd_read(FdType type, FileDescriptor fd, off_t offset,
return IsAnySocket(type)
? SocketDescriptor::FromFileDescriptor(fd).ReadNoWait(dest)
: (IstreamHandler::HasOffset(offset)
? fd.ReadAt(offset, dest.data(), dest.size())
? fd.ReadAt(offset, dest)
: fd.Read(dest));
}

Expand Down
11 changes: 6 additions & 5 deletions test/t_cgi.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ Context::OnDirect(FdType, FileDescriptor fd, off_t offset,
return IstreamDirectResult::BLOCKING;
}

char buffer[256];
if (max_length > sizeof(buffer))
max_length = sizeof(buffer);
std::byte buffer[256];
std::span<std::byte> w{buffer};
if (w.size() > max_length)
w = w.first(max_length);

ssize_t nbytes = HasOffset(offset)
? fd.ReadAt(offset, buffer, max_length)
: fd.Read(buffer, max_length);
? fd.ReadAt(offset, w)
: fd.Read(w);
if (nbytes <= 0)
return nbytes < 0
? IstreamDirectResult::ERRNO
Expand Down

0 comments on commit ca672aa

Please sign in to comment.