Blockwrite limited to 2Gb
Original Reporter info from Mantis: crlab @neurolabusc1
-
Reporter name: Chris Rorden
Original Reporter info from Mantis: crlab @neurolabusc1
- Reporter name: Chris Rorden
Description:
The overloaded function call
procedure BlockWrite( var f: file; const Buf; Count: Int64; var Result: Int64);
uses Int64, so it suggests one can write huge files. However, in practice this function fails when the count nears the maximum for a 32-bit integer. Ideally, this function should handle large files (e.g. I am working with images from MorphoSource that are 6 Gb before I crop and interpolate them). Alternatively, the documentation should be updated to describe the limits. I also know people can adjust the blocksize, but this is not flexible (consider large vector where the number of elements are a prime number).
Steps to reproduce:
The following program demonstrates the error. It notes that 64-bit pointers are used, but fails to save a file that should (barely) fit into 32 bits:
SizeInt:8
Failed to save all data: wrote 2147479552 of 2147483647 bytes
------------------------------------------------
program saveimg;
{$mode objfpc}
{$H+}
//uses System;
type
TUInt8s = array of uint8;
function SaveImg(fnm: string; img: TUInt8s): boolean;
var
f: file;
nImgBytes, nWritten: int64;
begin
result := false;
if fnm = '' then exit;
nImgBytes := length(img);
AssignFile(f, fnm);
FileMode := 2;
Rewrite(f, 1);
Blockwrite(f,img[0],nImgBytes, nWritten);
CloseFile(f);
if (nImgBytes <> nWritten) then begin
writeln('Failed to save all data: wrote ',nWritten,' of ', nImgBytes ,' bytes');
exit;
end;
result := true;
end;
procedure MakeImg;
const
kMaxInt32 = 2147483647;
var
img: TUInt8s;
begin
writeln('SizeInt:', sizeof(SizeInt));
setlength(img, kMaxInt32 );
SaveImg('big.img', img);
end;
begin
MakeImg();
end.
Mantis conversion info:
- Mantis ID: 37456
- OS: Ubuntu
- OS Build: 20.04
- Build: 3.2.0 [2020/07/07]
- Platform: Ryzen 3900X
- Version: 3.2.0