libutil: Fix writeFull to respect allowInterrupts

c0e849b696 broke interrupt handling since
writeFull started throwing Interrupted even when allowInterrupts was false.
This would lead to exceptions leaking out when they must not (for example during
progress bar shutdown).
This commit is contained in:
Sergei Zimmerman
2026-02-23 15:30:39 +03:00
parent b1ad42e6d5
commit 658c775f01
4 changed files with 8 additions and 6 deletions

View File

@@ -97,7 +97,7 @@ void writeFull(Descriptor fd, std::string_view s, bool allowInterrupts)
if (allowInterrupts)
checkInterrupt();
auto res = retryOnBlock(fd, PollDirection::Out, [&]() {
return write(fd, {reinterpret_cast<const std::byte *>(s.data()), s.size()});
return write(fd, {reinterpret_cast<const std::byte *>(s.data()), s.size()}, allowInterrupts);
});
if (res > 0)
s.remove_prefix(res);

View File

@@ -85,7 +85,7 @@ size_t read(Descriptor fd, std::span<std::byte> buffer);
* @return The number of bytes actually written
* @throws SystemError on failure
*/
size_t write(Descriptor fd, std::span<const std::byte> buffer);
size_t write(Descriptor fd, std::span<const std::byte> buffer, bool allowInterrupts);
/**
* Get the size of a file.

View File

@@ -42,11 +42,12 @@ size_t readOffset(Descriptor fd, off_t offset, std::span<std::byte> buffer)
return static_cast<size_t>(n);
}
size_t write(Descriptor fd, std::span<const std::byte> buffer)
size_t write(Descriptor fd, std::span<const std::byte> buffer, bool allowInterrupts)
{
ssize_t n;
do {
checkInterrupt();
if (allowInterrupts)
checkInterrupt();
n = ::write(fd, buffer.data(), buffer.size());
} while (n == -1 && errno == EINTR);
if (n == -1)

View File

@@ -60,9 +60,10 @@ size_t readOffset(Descriptor fd, off_t offset, std::span<std::byte> buffer)
return static_cast<size_t>(n);
}
size_t write(Descriptor fd, std::span<const std::byte> buffer)
size_t write(Descriptor fd, std::span<const std::byte> buffer, bool allowInterrupts)
{
checkInterrupt(); // For consistency with unix
if (allowInterrupts)
checkInterrupt(); // For consistency with unix
DWORD n;
if (!WriteFile(fd, buffer.data(), static_cast<DWORD>(buffer.size()), &n, NULL)) {
auto lastError = GetLastError();