Rename and protect BufferedSink::write

The `write` name is ambiguous and could lead to some funny bugs like
https://github.com/NixOS/nix/pull/8173#issuecomment-1500009480. So
rename it to the more explicit `writeUnbuffered`.
Besides, this method shouldn't be (and isn't) used outside of the class
implementation, so mark it `protected`.

This makes it more symetrical to `BufferedSource` which uses a
`protected readUnbuffered` method.
This commit is contained in:
Théophane Hufschmitt 2023-04-07 09:16:40 +02:00
parent 9185639631
commit 214f1d6791
6 changed files with 14 additions and 12 deletions

View file

@ -23,7 +23,7 @@ struct ChunkedCompressionSink : CompressionSink
{ {
uint8_t outbuf[32 * 1024]; uint8_t outbuf[32 * 1024];
void write(std::string_view data) override void writeUnbuffered(std::string_view data) override
{ {
const size_t CHUNK_SIZE = sizeof(outbuf) << 2; const size_t CHUNK_SIZE = sizeof(outbuf) << 2;
while (!data.empty()) { while (!data.empty()) {
@ -103,7 +103,7 @@ struct ArchiveCompressionSink : CompressionSink
throw Error(reason, archive_error_string(this->archive)); throw Error(reason, archive_error_string(this->archive));
} }
void write(std::string_view data) override void writeUnbuffered(std::string_view data) override
{ {
ssize_t result = archive_write_data(archive, data.data(), data.length()); ssize_t result = archive_write_data(archive, data.data(), data.length());
if (result <= 0) check(result); if (result <= 0) check(result);
@ -136,7 +136,7 @@ struct NoneSink : CompressionSink
warn("requested compression level '%d' not supported by compression method 'none'", level); warn("requested compression level '%d' not supported by compression method 'none'", level);
} }
void finish() override { flush(); } void finish() override { flush(); }
void write(std::string_view data) override { nextSink(data); } void writeUnbuffered(std::string_view data) override { nextSink(data); }
}; };
struct BrotliDecompressionSink : ChunkedCompressionSink struct BrotliDecompressionSink : ChunkedCompressionSink

View file

@ -12,7 +12,7 @@ namespace nix {
struct CompressionSink : BufferedSink, FinishSink struct CompressionSink : BufferedSink, FinishSink
{ {
using BufferedSink::operator (); using BufferedSink::operator ();
using BufferedSink::write; using BufferedSink::writeUnbuffered;
using FinishSink::finish; using FinishSink::finish;
}; };

View file

@ -343,7 +343,7 @@ HashSink::~HashSink()
delete ctx; delete ctx;
} }
void HashSink::write(std::string_view data) void HashSink::writeUnbuffered(std::string_view data)
{ {
bytes += data.size(); bytes += data.size();
update(ht, *ctx, data); update(ht, *ctx, data);

View file

@ -197,7 +197,7 @@ public:
HashSink(HashType ht); HashSink(HashType ht);
HashSink(const HashSink & h); HashSink(const HashSink & h);
~HashSink(); ~HashSink();
void write(std::string_view data) override; void writeUnbuffered(std::string_view data) override;
HashResult finish() override; HashResult finish() override;
HashResult currentHash(); HashResult currentHash();
}; };

View file

@ -20,7 +20,7 @@ void BufferedSink::operator () (std::string_view data)
buffer size. */ buffer size. */
if (bufPos + data.size() >= bufSize) { if (bufPos + data.size() >= bufSize) {
flush(); flush();
write(data); writeUnbuffered(data);
break; break;
} }
/* Otherwise, copy the bytes to the buffer. Flush the buffer /* Otherwise, copy the bytes to the buffer. Flush the buffer
@ -38,7 +38,7 @@ void BufferedSink::flush()
if (bufPos == 0) return; if (bufPos == 0) return;
size_t n = bufPos; size_t n = bufPos;
bufPos = 0; // don't trigger the assert() in ~BufferedSink() bufPos = 0; // don't trigger the assert() in ~BufferedSink()
write({buffer.get(), n}); writeUnbuffered({buffer.get(), n});
} }
@ -48,7 +48,7 @@ FdSink::~FdSink()
} }
void FdSink::write(std::string_view data) void FdSink::writeUnbuffered(std::string_view data)
{ {
written += data.size(); written += data.size();
try { try {

View file

@ -53,7 +53,9 @@ struct BufferedSink : virtual Sink
void flush(); void flush();
virtual void write(std::string_view data) = 0; protected:
virtual void writeUnbuffered(std::string_view data) = 0;
}; };
@ -133,7 +135,7 @@ struct FdSink : BufferedSink
~FdSink(); ~FdSink();
void write(std::string_view data) override; void writeUnbuffered(std::string_view data) override;
bool good() override; bool good() override;
@ -520,7 +522,7 @@ struct FramedSink : nix::BufferedSink
} }
} }
void write(std::string_view data) override void writeUnbuffered(std::string_view data) override
{ {
/* Don't send more data if the remote has /* Don't send more data if the remote has
encountered an error. */ encountered an error. */