do not advance position of THandleStream if enough size after SetSize
Since fpc v3.2.2 (specifically, this commit: 74e65f77), we cannot set the size of a TFileStream
before writing to it in order to minimize file size changes.
Since this version, the position is unconditionally set to the new size even if it already fits in the new size. In my understanding, this change was introduced to fix the fact that position could be left outside the size in case the file is shrinked. It indeeds fixes that but then broke using SetSize
to extend the file.
This is an example of writing 4 bytes to a TFileStream
which is set to size 4 before writing:
program filestreamsize;
{$mode delphi}
uses Classes;
begin
with TFileStream.Create('test', fmCreate) do
try
Size := 4;
Write('test', 4);
finally
Free;
end;
end.
Using fpc v3.2.2, we end up with a file with 8 bytes: 0x00 0x00 0x00 0x00 0x74 0x65 0x73 0x74
Until fpc v3.2.0, the behavior was fine and we obtained a file with the four written bytes only.
This MR is handling both cases by setting position only if it is not contained in the new file size.