}
ssize_t write(const void *from, unsigned long long requested_size) APT_MUSTCHECK
{
- if (buffersize_max - size() < requested_size)
- requested_size = buffersize_max - size();
- memcpy(buffer + bufferend, from, requested_size);
+ if (free() < requested_size)
+ requested_size = free();
+ memcpy(getend(), from, requested_size);
bufferend += requested_size;
if (bufferstart == bufferend)
bufferstart = bufferend = 0;
{
buffer.reset();
unsigned long long actualread = 0;
- if (filefd->Read(buffer.get(), buffer.buffersize_max, &actualread) == false)
+ if (filefd->Read(buffer.getend(), buffer.free(), &actualread) == false)
return nullptr;
buffer.bufferend = actualread;
if (buffer.size() == 0)
return filefd->FileFdError("Seek on file %s because it couldn't be reopened", filefd->FileName.c_str());
buffer.reset();
+ set_seekpos(0);
if (To != 0)
return filefd->Skip(To);
if (written < 0 && errno == EINTR)
continue;
if (written < 0)
- return false;
+ return wrapped->InternalWriteError();
writebuffer.bufferstart += written;
}
}
virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) override
{
- size_t written = 0;
-
- while (written < Size) {
- auto buffered = writebuffer.write(static_cast<char const*>(From) + written, Size - written);
+ // Optimisation: If the buffer is empty and we have more to write than
+ // would fit in the buffer (or equal number of bytes), write directly.
+ if (writebuffer.empty() == true && Size >= writebuffer.free())
+ return wrapped->InternalWrite(From, Size);
- written += buffered;
+ // Write as much into the buffer as possible and then flush if needed
+ auto written = writebuffer.write(From, Size);
- if (writebuffer.full() && InternalFlush() == false)
- return -1;
- }
+ if (writebuffer.full() && InternalFlush() == false)
+ return -1;
return written;
}
}
virtual ssize_t InternalWrite(void const * const From, unsigned long long const Size) override
{
+ ssize_t Res;
lzma->stream.next_in = (uint8_t *)From;
lzma->stream.avail_in = Size;
lzma->stream.next_out = lzma->buffer;
size_t const n = sizeof(lzma->buffer)/sizeof(lzma->buffer[0]) - lzma->stream.avail_out;
size_t const m = (n == 0) ? 0 : fwrite(lzma->buffer, 1, n, lzma->file);
if (m != n)
- return -1;
+ {
+ Res = -1;
+ errno = 0;
+ }
else
- return Size - lzma->stream.avail_in;
+ {
+ Res = Size - lzma->stream.avail_in;
+ if (Res == 0)
+ {
+ // lzma run was okay, but produced no output…
+ Res = -1;
+ errno = EINTR;
+ }
+ }
+ return Res;
}
virtual bool InternalWriteError() override
{
while (Res > 0 && Size > 0)
{
Res = d->InternalWrite(From, Size);
- if (Res < 0 && errno == EINTR)
- continue;
+
if (Res < 0)
+ {
+ if (errno == EINTR)
+ {
+ // trick the while-loop into running again
+ Res = 1;
+ errno = 0;
+ continue;
+ }
return d->InternalWriteError();
+ }
From = (char const *)From + Res;
Size -= Res;