download_to_file no longer reports success for a file the disk did not keep.
It called ofs.write for each body chunk without looking at the stream, added
each chunk's size to bytesWritten from what the network delivered, and set
bytesWritten after ofs.close() without looking at that either. On a full disk
(ENOSPC) the transfer therefore succeeded: a 420,831,054 byte download left
220,979,200 bytes on disk while bytesWritten said 420,831,054 and ok() was
true, and the caller blamed the source (a checksum mismatch) for what was the
local disk.
- Each chunk is flushed and the stream checked. On the first failure the
transfer stops reading,errorbecomeswrite <path>: <reason>(the reason
iserrnoasstd::generic_categorywords it, for exampleNo space left on device), and the connection is dropped rather than returned to the pool with
the rest of the body still on it. Closing the file is checked the same way. DownloadToFileResult::writeFailedis new and is true when the fault is the
destination rather than the source: the write failed, the close failed, or the
file could not be opened (erroris stillCannot open file: <path>for the
last).DownloadToFileResult::bytesReceivedis new: the bytes of body the
connection delivered.DownloadToFileResult::bytesWrittenchanges meaning, and this is the one
behaviour change: it now counts bytes the file accepted, where it used to
count bytes the network delivered. The two are equal for every transfer that
did not fail to write. When a write fails, the chunk that failed is not
counted, though the file may hold part of it, so the value is a floor on the
file's size.