[fpc-pascal] Truncate procedure unable to work under Linux, is this a bug?

Peter peter at pblackman.plus.com
Wed May 20 17:32:21 CEST 2015


On 18/05/15 08:30, Michael Van Canneyt wrote:
>
>
> On Sun, 17 May 2015, Géza Kovacs Géza wrote:
>
>> Truncate procedure unable to work under Linux, is this a bug?
>> This code is unable to work under linux: the Truncate drops an error
>> message if the file is larger than 2 or 3 GB.
>> I tested it with Free Pascal 2.6.4 under Debian Jessie 64 bit.
>> It produce the same error message under Ubuntu 12.04 32 bit with Free
>> Pascal 2.4.4
>> Sometimes I did not get any error message but the file (which was 2 or
>> 3 GB) will be only some hundred MB after truncate...
>> It operates correctly under Windows.
>>
>> It produce the following error message:
>> An unhandled exception occurred at $<address>:
>> EPrivilege : Privileged instruction
>> $<address>
>>
>> Program File_Trunc;
>> {$MODE OBJFPC} {$H+}
>> uses dos,sysutils;
>> var
>>     f : file of byte;
>>     fs : int64;
>> begin
>>     assign(f,paramstr(1));
>>     ReSet(f);
>>     fs := FileSize(f);
>>     WriteLn('Original file size: ',fs);
>>     fs := fs-3456789;
>>     WriteLn('File size after truncate: ',fs);
>>     Seek(f,fs);
>>     Truncate(f);
>>     Close(f);
>> end.
>
> Truncate is a leftover from the DOS days. It does not work with files
> >2 Gb, the file position is 32-bit.
>
> There is the Filetruncate function from sysutils:
>
> Function FileTruncate (Handle: THandle; Size: Int64) : boolean;
>
> but even that does not work with files >2Gb on unix, because the
> underlying system call does not support it.
> I don't know what happens on Windows.
>
>
> Michael.
>
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Hi,
It should work if Truncate64 is used instead of Truncate.
I suggest its a bug, albeit probably a wishlist one.

http://man7.org/linux/man-pages/man2/ftruncate.2.html

Regards,
Peter






More information about the fpc-pascal mailing list